-
Notifications
You must be signed in to change notification settings - Fork 1
/
allow_rev3.py
executable file
·197 lines (156 loc) · 5.86 KB
/
allow_rev3.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env python2
import re
import os
import os.path
import time
from datetime import datetime as dt
import argparse
from collections import namedtuple
# from model import DHCPLease
# from model import UserGroup
from pymongo import MongoClient
import xxhash
in_iface = 'eth1'
out_iface = 'eth0'
MacCache = namedtuple('MacCache', ['val', 'time'])
class DHCP:
def __init__(self, user):
self.CACHE_TIME = 7
self.user = user
self.mac_cache = dict()
self.dhcp_cache = []
self.dhcp_cache_len = 0
self.dhcp_hash = ""
def load_dhcp(self, dhcp_lease):
with open(dhcp_lease, "r") as fd:
return fd.read().split("\n")
def mac_status(self, mac, cur_time=None):
if cur_time is None:
cur_time = dt.utcnow()
life_time = (cur_time - self.mac_cache[mac].time).total_seconds() \
if mac in self.mac_cache else 2 * self.CACHE_TIME
if life_time < self.CACHE_TIME:
return self.mac_cache[mac].val
else:
doc = self.user.find_one({"devices": {"mac": mac}}, {"group": True})
if doc is None:
res = ('guest', 'ACCEPT')
elif doc['group'] == 'blocked':
res = ('blocked', 'DROP')
else:
res = (doc['group'], 'ACCEPT')
self.mac_cache[mac] = MacCache(res, cur_time)
return res
def cached_parse_dhcp(self, lines, cur_time=None):
if cur_time is None:
cur_time = dt.utcnow()
m = xxhash.xxh64()
m.update("".join(lines[:self.dhcp_cache_len]).encode("utf8"))
new_hash = m.digest()
# new_len = len(lines)
if new_hash != self.dhcp_hash:
self.dhcp_cache_len = 0
self.dhcp_cache = []
m = xxhash.xxh64()
lines = lines[self.dhcp_cache_len:]
self.dhcp_cache.extend(self.from_dhcp(lines, cur_time))
m.update("".join(lines).encode("utf8"))
self.dhcp_hash = m.digest()
self.dhcp_cache_len += len(lines)
return self.dhcp_cache
def from_dhcp(self, st, cur_time=None):
if cur_time is None:
cur_time = dt.utcnow()
rules = []
for line in st:
line = line.strip()
if line.startswith("lease"):
m = re.search(r'lease\s*([0-9\.]+)\s*\{', line)
ip = m.groups()[0]
elif line.startswith("hardware ethernet"):
mac = line[18:35]
elif line.startswith("starts"):
starts = dt.strptime(line[7:28], "%w %Y/%m/%d %H:%M:%S")
elif line.startswith("ends"):
ends = dt.strptime(line[5:26], "%w %Y/%m/%d %H:%M:%S")
elif line.startswith("}") and ip:
if(ends > cur_time):
group, access = self.mac_status(mac, cur_time)
rules.append((ip, mac, group, access))
# rules.append((ip, mac, starts, ends))
ip = None
rules = list(set(rules))
return rules
def make_script(self, dhcp):
# print("make_script")
# templ = [
# ('filter', "-A INPUT --src {0} -m mac --mac-source {1} -j {2}"),
# ('filter', "-A FORWARD --src {0} -m mac --mac-source {1} -j {2}"),
# ('mangle', "-A FORWARD --src {0} -j {2}"),
# ('mangle', "-A FORWARD --dst {0} -j {2}")
# ]
iptb = {"mangle": [], "filter": []}
filter_tmpl = "-A allow-inet --src {0} -m mac --mac-source {1} -j {2}"
for (ip, mac, group, access) in dhcp:
iptb['mangle'].append(
"-A FORWARD --src {0} -j {2}".format(ip, mac, group))
iptb['mangle'].append(
"-A FORWARD --dst {0} -j {2}".format(ip, mac, group))
iptb['filter'].append(filter_tmpl.format(ip, mac, access))
script = ''
script += '*mangle\n'
script += '\n'.join(iptb['mangle'])
script += '\nCOMMIT\n'
script += '*filter\n'
script += ':allow-inet - [0:0]\n'
script += '\n'.join(iptb['filter']) + '\n'
script += 'COMMIT\n'
return script
def apply_script(self, script):
# print("apply_script")
fd = os.popen("sudo iptables-restore --noflush", "w")
# print(script)
fd.write(script)
fd.close()
def save_dhcp(self, dhcp, last):
# print("save_dhcp")
last = dt.utcfromtimestamp(last - 3)
# print("Last: ", last)
for lease in dhcp:
# print(lease.starts, last)
if(lease.starts >= last):
# print("save")
lease.save()
def main():
parser = argparse.ArgumentParser(description="")
parser.add_argument('lease_filename', type=str)
parser.add_argument('--debug', const=True, default=False,
action='store_const', help='enable debug mode')
parser.add_argument('--profile', const=True, default=False,
action='store_const', help='enable profiler mode')
args = parser.parse_args()
client = MongoClient('localhost', 27017)
db = client['iasa-wifi']
user = db.user
dhcp = DHCP(user)
lastmtime = 0
while 1:
mtime = os.stat(args.lease_filename)[-2]
if lastmtime == mtime:
time.sleep(1)
continue
start = time.time()
lines = dhcp.load_dhcp(args.lease_filename)
leases = dhcp.cached_parse_dhcp(lines, dt.fromtimestamp(lastmtime))
script = dhcp.make_script(leases)
end = time.time()
if(args.debug):
print("Loaded", len(leases), "leases")
print("Time spend", end - start)
print ("last modified: %s" % mtime)
else:
dhcp.apply_script(script)
dhcp.save_dhcp(leases, lastmtime)
lastmtime = mtime
if __name__ == "__main__":
main()