-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.py
132 lines (111 loc) · 3.97 KB
/
block.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from multiprocessing import Manager
from ipaddress import ip_address
from datetime import datetime, timedelta
import random
import sys
class UnknownBlockType(Exception):
pass
class ConfigError(Exception):
pass
class BlockGenerator:
def __init__(self, config):
l = []
for pair in config['Blocks']:
tar_block = config[config['Blocks'][pair]]
type = tar_block['Assign'].lower()
if type == 'random':
block = RandomBlock()
elif type == 'normal':
block = NormalBlock()
else:
raise UnknownBlockType(f'Uknown block type: {type}')
block.name = config['Blocks'][pair]
if 'Netmask' in tar_block:
block.netmask = tar_block['Netmask']
if 'Router' in tar_block:
block.router = tar_block['Router']
if 'DNS' in tar_block:
block.dns = tar_block['DNS']
if 'Lease' in tar_block:
block.lease = int(tar_block['Lease'])
if 'Range' in tar_block:
if '-' in tar_block['Range']:
block.start_addr = ip_address(tar_block['Range'].split('-')[0])
block.end_addr = ip_address(tar_block['Range'].split('-')[1])
if not block.start_addr < block.end_addr:
raise ConfigError('End address is less than start address')
block.initPool()
l.append(block)
self.blocks = l
class Block:
def __init__(self):
self.router = None
self.start_addr = None
self.end_addr = None
self.netmask = None
self.dns = None
self.name = None
m = Manager()
self.pool = m.list()
def filter(self, hostname, mac):
return True
def getIP(self):
ip = None
try:
ip = self.pool.pop(0)
except Exception as e:
print('ip pool empty')
return ip
def addToPool(self, item):
self.pool.append(item)
class RandomBlock(Block):
def __init__(self):
super().__init__()
def initPool(self):
addr = self.start_addr
while addr <= self.end_addr:
self.pool.append(addr)
addr += 1
random.shuffle(self.pool)
def addToPool(self, ip):
self.pool.insert(random.randint(0,len(self.pool)), ip)
class NormalBlock(Block):
def __init__(self):
super().__init__()
def initPool(self):
addr = self.start_addr
while addr <= self.end_addr:
self.pool.append(addr)
addr += 1
class LeaseManager:
def __init__(self):
m = Manager()
self.d = m.dict()
# list should contain tuples of (ip, mac, original lease, lease time, state, blocl_name)
def getLease(self, mac):
try:
lease = self.d[mac]
except KeyError:
return None
try:
if lease[2] < datetime.now():
return (lease[0], lease[1], 0, lease[3], lease[4], lease[5])
else:
return (lease[0], lease[1], (lease[2]-datetime.now()).seconds, lease[3], lease[4], lease[5])
except KeyError:
return None
def resetLease(self, mac):
try:
lease = self.d[mac]
except KeyError:
return None
self.d[mac] = (lease[0], lease[1], datetime.now()+timedelta(seconds=lease[1]), lease[3], lease[4], lease[5])
def setID(self, mac, transaction_id):
try:
lease = self.d[mac]
except KeyError:
return None
self.d[mac] = (lease[0], lease[1], datetime.now()+timedelta(seconds=lease[1]), lease[3], lease[4], transaction_id)
def addLease(self, mac, ip, original_lease, lease, state, block_name, transaction_id): # states: "ASSOC", "ESTB", "EXPR"
lease = datetime.now()+timedelta(seconds=lease)
self.d[mac] = (ip, original_lease, lease, state, block_name, transaction_id)