added "expires" field to clients

This commit is contained in:
Stefan Midjich 2017-09-29 17:44:40 +02:00
parent 2ebdc37eae
commit 44625db697
4 changed files with 38 additions and 7 deletions

View file

@ -16,6 +16,7 @@ create table if not exists client (
enabled boolean NOT NULL,
last_packets bigint default 0,
last_activity timestamp,
expires timestamp DEFAULT NULL,
primary key (client_id, ip_address, protocol)
);

View file

@ -4,11 +4,11 @@ Handles "clients" in IPtables for captive portal.
import ipaddress
from uuid import uuid4
from datetime import datetime
from datetime import datetime, timedelta
import iptc
import errors
from errors import StorageNotFound
class Client(object):
@ -46,6 +46,7 @@ class Client(object):
self.enabled = False
self.last_packets = 0
self.last_activity = None
self.expires = datetime.now() + timedelta(days=1)
# Init iptables
self.table = iptc.Table(iptc.Table.MANGLE)

View file

@ -6,17 +6,45 @@ from sys import exit
from argparse import ArgumentParser, FileType
from pprint import pprint as pp
from configparser import RawConfigParser
from datetime import datetime
import errors
from storage import StoragePostgres
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((
'Handle clients in the captive portal. Default mode of operation is to'
' create new clients and enable them. Other mode is to --disable the '
'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(
'--disable',
default=False,

View file

@ -56,10 +56,10 @@ class StoragePostgres(object):
def write_client(self, client):
query = (
'insert into client (client_id, created, ip_address, protocol, '
'enabled, last_packets, last_activity) values (%s, %s, %s, %s, '
'%s, %s, %s) on conflict (client_id, ip_address, protocol) do '
'update set (enabled, last_packets, last_activity) = '
'(EXCLUDED.enabled, EXCLUDED.last_packets, '
'enabled, last_packets, last_activity, expires) values '
'(%s, %s, %s, %s, %s, %s, %s, %s) on conflict (client_id, '
'ip_address, protocol) do update set (enabled, last_packets, '
'last_activity) = (EXCLUDED.enabled, EXCLUDED.last_packets, '
'EXCLUDED.last_activity)'
)
self.cur.execute(
@ -71,7 +71,8 @@ class StoragePostgres(object):
client.protocol,
client.enabled,
client.last_packets,
client.last_activity
client.last_activity,
client.expires
)
)
self.conn.commit()