mirror of
https://codeberg.org/prof_x_pvt_ltd/captive.whump.shanti-portal
synced 2024-08-14 22:46:42 +00:00
added "expires" field to clients
This commit is contained in:
parent
2ebdc37eae
commit
44625db697
4 changed files with 38 additions and 7 deletions
|
@ -16,6 +16,7 @@ create table if not exists client (
|
||||||
enabled boolean NOT NULL,
|
enabled boolean NOT NULL,
|
||||||
last_packets bigint default 0,
|
last_packets bigint default 0,
|
||||||
last_activity timestamp,
|
last_activity timestamp,
|
||||||
|
expires timestamp DEFAULT NULL,
|
||||||
primary key (client_id, ip_address, protocol)
|
primary key (client_id, ip_address, protocol)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -4,11 +4,11 @@ Handles "clients" in IPtables for captive portal.
|
||||||
|
|
||||||
import ipaddress
|
import ipaddress
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
from datetime import datetime
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
import iptc
|
import iptc
|
||||||
|
|
||||||
import errors
|
from errors import StorageNotFound
|
||||||
|
|
||||||
|
|
||||||
class Client(object):
|
class Client(object):
|
||||||
|
@ -46,6 +46,7 @@ class Client(object):
|
||||||
self.enabled = False
|
self.enabled = False
|
||||||
self.last_packets = 0
|
self.last_packets = 0
|
||||||
self.last_activity = None
|
self.last_activity = None
|
||||||
|
self.expires = datetime.now() + timedelta(days=1)
|
||||||
|
|
||||||
# Init iptables
|
# Init iptables
|
||||||
self.table = iptc.Table(iptc.Table.MANGLE)
|
self.table = iptc.Table(iptc.Table.MANGLE)
|
||||||
|
|
|
@ -6,17 +6,45 @@ from sys import exit
|
||||||
from argparse import ArgumentParser, FileType
|
from argparse import ArgumentParser, FileType
|
||||||
from pprint import pprint as pp
|
from pprint import pprint as pp
|
||||||
from configparser import RawConfigParser
|
from configparser import RawConfigParser
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
import errors
|
import errors
|
||||||
from storage import StoragePostgres
|
from storage import StoragePostgres
|
||||||
from client import Client
|
from client import Client
|
||||||
|
|
||||||
|
|
||||||
|
# Custom defined argparse types for dates
|
||||||
|
def valid_date_type(arg_date_str):
|
||||||
|
"""custom argparse *date* type for user dates values given from the command line"""
|
||||||
|
try:
|
||||||
|
return datetime.datetime.strptime(arg_date_str, "%Y-%m-%d")
|
||||||
|
except ValueError:
|
||||||
|
msg = "Given Date ({0}) not valid! Expected format, YYYY-MM-DD!".format(arg_date_str)
|
||||||
|
raise argparse.ArgumentTypeError(msg)
|
||||||
|
|
||||||
|
|
||||||
|
def valid_datetime_type(arg_datetime_str):
|
||||||
|
"""custom argparse type for user datetime values given from the command line"""
|
||||||
|
try:
|
||||||
|
return datetime.datetime.strptime(arg_datetime_str, "%Y-%m-%d %H:%M")
|
||||||
|
except ValueError:
|
||||||
|
msg = "Given Datetime ({0}) not valid! Expected format, 'YYYY-MM-DD HH:mm'!".format(arg_datetime_str)
|
||||||
|
raise argparse.ArgumentTypeError(msg)
|
||||||
|
|
||||||
|
|
||||||
parser = ArgumentParser((
|
parser = ArgumentParser((
|
||||||
'Handle clients in the captive portal. Default mode of operation is to'
|
'Handle clients in the captive portal. Default mode of operation is to'
|
||||||
' create new clients and enable them. Other mode is to --disable the '
|
' create new clients and enable them. Other mode is to --disable the '
|
||||||
'client. And last mode is to --delete the client completely.'
|
'client. And last mode is to --delete the client completely.'
|
||||||
))
|
))
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
'--expires',
|
||||||
|
type=valid_datetime_type,
|
||||||
|
default=datetime.now() + timedelta(days=1),
|
||||||
|
help='Expiry date in format "YYYY-MM-DD HH:mm"'
|
||||||
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--disable',
|
'--disable',
|
||||||
default=False,
|
default=False,
|
||||||
|
|
|
@ -56,10 +56,10 @@ class StoragePostgres(object):
|
||||||
def write_client(self, client):
|
def write_client(self, client):
|
||||||
query = (
|
query = (
|
||||||
'insert into client (client_id, created, ip_address, protocol, '
|
'insert into client (client_id, created, ip_address, protocol, '
|
||||||
'enabled, last_packets, last_activity) values (%s, %s, %s, %s, '
|
'enabled, last_packets, last_activity, expires) values '
|
||||||
'%s, %s, %s) on conflict (client_id, ip_address, protocol) do '
|
'(%s, %s, %s, %s, %s, %s, %s, %s) on conflict (client_id, '
|
||||||
'update set (enabled, last_packets, last_activity) = '
|
'ip_address, protocol) do update set (enabled, last_packets, '
|
||||||
'(EXCLUDED.enabled, EXCLUDED.last_packets, '
|
'last_activity) = (EXCLUDED.enabled, EXCLUDED.last_packets, '
|
||||||
'EXCLUDED.last_activity)'
|
'EXCLUDED.last_activity)'
|
||||||
)
|
)
|
||||||
self.cur.execute(
|
self.cur.execute(
|
||||||
|
@ -71,7 +71,8 @@ class StoragePostgres(object):
|
||||||
client.protocol,
|
client.protocol,
|
||||||
client.enabled,
|
client.enabled,
|
||||||
client.last_packets,
|
client.last_packets,
|
||||||
client.last_activity
|
client.last_activity,
|
||||||
|
client.expires
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
|
|
Loading…
Reference in a new issue