Commit 1fd5d463 authored by Ad Schellevis's avatar Ad Schellevis

(captiveportal, new) work in progress scriptbase, idletimeout, hardtimeout, etc

parent 3677ab28
...@@ -62,10 +62,7 @@ def main(): ...@@ -62,10 +62,7 @@ def main():
expected_clients = db.list_clients(zoneid) expected_clients = db.list_clients(zoneid)
# handle connected clients, timeouts, address changes, etc. # handle connected clients, timeouts, address changes, etc.
for db_client in expected_clients: for db_client in expected_clients:
# convert ip address to net, tables are registered as nets # fetch ip address (or network) from database
if db_client['ipAddress'].strip().find('/') == -1:
cpnet = '%s/32' % db_client['ipAddress'].strip()
else:
cpnet = db_client['ipAddress'].strip() cpnet = db_client['ipAddress'].strip()
# there are different reasons why a session should be removed, check for all reasons and # there are different reasons why a session should be removed, check for all reasons and
...@@ -75,10 +72,17 @@ def main(): ...@@ -75,10 +72,17 @@ def main():
# todo, static ip and addresses shouldn't be affected by the timeout rules below. # todo, static ip and addresses shouldn't be affected by the timeout rules below.
# check if hardtimeout is set and overrun for this session # check if hardtimeout is set and overrun for this session
if 'hardtimeout' in cpzones[zoneid] and str(cpzones[zoneid]['hardtimeout']).isdigit(): if 'hardtimeout' in cpzones[zoneid] and str(cpzones[zoneid]['hardtimeout']).isdigit():
if int(cpzones[zoneid]['hardtimeout']) > 0: # hardtimeout should be set and we should have collected some session data from the client
if time.time() - float(db_client['startTime']) / 60 > int(cpzones[zoneid]['hardtimeout']): if int(cpzones[zoneid]['hardtimeout']) > 0 and float(db_client['startTime']) > 0:
if (time.time() - float(db_client['startTime'])) / 60 > int(cpzones[zoneid]['hardtimeout']):
drop_session = True
# check if idletimeout is set and overrun for this session
if 'idletimeout' in cpzones[zoneid] and str(cpzones[zoneid]['idletimeout']).isdigit():
# idletimeout should be set and we should have collected some session data from the client
if int(cpzones[zoneid]['idletimeout']) > 0 and float(db_client['last_accessed']) > 0:
if (time.time() - float(db_client['last_accessed'])) / 60 > int(cpzones[zoneid]['idletimeout']):
drop_session = True drop_session = True
drop_session = False
# check session, if it should be active, validate its properties # check session, if it should be active, validate its properties
if not drop_session: if not drop_session:
......
...@@ -78,6 +78,7 @@ class DB(object): ...@@ -78,6 +78,7 @@ class DB(object):
response['ipAddress'] = ip_address response['ipAddress'] = ip_address
response['macAddress'] = mac_address response['macAddress'] = mac_address
response['startTime'] = time.time() # record creation = sign-in time response['startTime'] = time.time() # record creation = sign-in time
response['last_accessed'] = time.time() # last accessed_time = sign-in time
response['sessionId'] = base64.b64encode(os.urandom(16)) # generate a new random session id response['sessionId'] = base64.b64encode(os.urandom(16)) # generate a new random session id
cur = self._connection.cursor() cur = self._connection.cursor()
...@@ -89,8 +90,8 @@ class DB(object): ...@@ -89,8 +90,8 @@ class DB(object):
""", response) """, response)
# add new session # add new session
cur.execute("""INSERT INTO cp_clients(zoneid, authenticated_via, sessionid, username, ip_address, mac_address, created) cur.execute("""INSERT INTO cp_clients(zoneid, authenticated_via, sessionid, username, ip_address, mac_address, created, last_accessed)
VALUES (:zoneid, :authenticated_via, :sessionId, :userName, :ipAddress, :macAddress, :startTime) VALUES (:zoneid, :authenticated_via, :sessionId, :userName, :ipAddress, :macAddress, :startTime, :last_accessed)
""", response) """, response)
self._connection.commit() self._connection.commit()
......
...@@ -48,6 +48,13 @@ class IPFW(object): ...@@ -48,6 +48,13 @@ class IPFW(object):
output_stream.seek(0) output_stream.seek(0)
for line in output_stream.read().split('\n'): for line in output_stream.read().split('\n'):
if line.split(' ')[0].strip() != "": if line.split(' ')[0].strip() != "":
# process / 32 nets as single addresses to align better with the rule syntax
# and local administration.
if line.split(' ')[0].split('/')[-1] == '32':
# single IPv4 address
result.append(line.split(' ')[0].split('/')[0])
else:
# network
result.append(line.split(' ')[0]) result.append(line.split(' ')[0])
return result return result
...@@ -58,11 +65,7 @@ class IPFW(object): ...@@ -58,11 +65,7 @@ class IPFW(object):
:return: boolean :return: boolean
""" """
ipfw_tbl = self.list_table(table_number) ipfw_tbl = self.list_table(table_number)
if address.find('.') > -1 and address.find('/') == -1: if address.strip() in ipfw_tbl:
# address given, search for /32 net in ipfw rules
if '%s/32' % address.strip() in ipfw_tbl:
return True
elif address.strip() in ipfw_tbl:
return True return True
return False return False
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment