-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqos2nat.py
executable file
·1319 lines (1119 loc) · 52.4 KB
/
qos2nat.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
# vim:set shiftwidth=4 softtabstop=4 expandtab:
import sys
import re
import shutil
import string
import argparse
import subprocess
import tempfile
import glob
from ipaddress import ip_address, ip_network
from collections import defaultdict
import time
from datetime import datetime, date
# for debugging/development
config_prefix=""
config_qos_conf = "/etc/qos.conf"
config_nat_conf = "/etc/nat.conf"
config_nat_global = "/etc/nat_global.conf"
config_nat_up = "/etc/nat.up"
config_logfile = "/var/log/qos2nat.log"
config_nat_backup = "/etc/nat_backup/nat_conf_"
config_portmap = "/var/www/portmap.txt"
config_mangle_up = "/etc/mangle.up"
config_tc_up = "/etc/tc.up"
config_html_preview = "/var/www/today.html"
config_html_day = "/var/www/yesterday.html"
config_logdir = "/var/www/logs/"
logfile = None
errors_not_fatal = False
errors_log = None
verbose_prints = False
# log to file and optionally append error to errors_log
def log(msg, err=False):
global logfile, errors_log
if logfile:
logfile.write(f"{msg}\n")
if err and errors_log is not None:
errors_log += f"{msg}\n"
# log to file without newline
def logc(msg):
global logfile
if logfile:
logfile.write(msg)
# log to file and print
def logp(msg, quiet=False):
global verbose_prints
if not quiet or verbose_prints:
print(msg)
log(msg)
# log to file and print and append to errors_log
def logpe(msg):
print(msg)
log(msg, err=True)
# log to file and print without newline
def logpc(msg, quiet=False):
global verbose_prints
if not quiet or verbose_prints:
print(msg, end='')
logc(msg)
class ConfError(RuntimeError):
"""Unexpected content in conf file"""
def td(content):
return f"<td>{content}</td>"
def tdr(content):
return f"<td align=\"right\">{content}</td>"
def tr(tds):
content = ''.join(tds)
return f"<tr>{content}</tr>\n"
def human(val):
if val < 1024:
return str(val)
val //= 1024
if val < 1024:
return f"{val} KB"
val //= 1024
if val < 1024:
return f"{val} MB"
return f"{val/1024:.2f} GB"
def humanmb(val):
if val < 1024:
return f"{val} MB"
val //= 1024
if val < 1024:
return f"{val} GB"
return f"{val/1024:.2f} TB"
def humankbps(val):
if val < 1000:
return f"{val} kbps"
val //= 1000
return f"{val} Mbps"
class Hosts:
def init_nat_conf(self):
# from nat.conf
self.pubip2user = dict()
self.user2pubip = dict()
self.ipuser2pubip = dict()
self.ip2pubip = dict()
self.ip2portfwd = defaultdict(set) # ip -> set((pubip, src, dst, user, comment))
self.pubip_port2ip_port = dict() # (pubip, src) -> (ip, dst)
self.pubip_port_nowarn = set() # (pubip, src)
# what nat.conf updates needed
self.nat_conf_ips_to_delete = set()
self.nat_conf_user2pubip_to_add = dict() # user -> pubip
self.nat_conf_pubip2ip_to_add = defaultdict(set) # pubip -> ip
self.nat_conf_pubip_already_added = set()
self.nat_conf_ips_to_change = dict() # ip -> new_ip
self.nat_conf_user_renames = dict() # ip -> (olduser, oldpubip, newuser)
self.nat_conf_pubip_changes = dict() # ip -> (oldpubip, newpubip)
# ips of users with private - should be deleted
self.nat_conf_ips_no_shaping = set()
# to clean up after bug duplicating nat.conf * * entries
self.nat_conf_ip_already_written = set()
def __init__(self):
# from qos.conf
self.ip2host = dict()
self.host2ip = dict()
self.ip2user = dict()
self.user2ip = dict()
self.user2shaping = dict()
self.users_with_subclasses = set()
self.ip2shaping = dict()
self.users = set()
# from qos.conf config
self.conf_uplink_mbit = None
self.local_network = None
self.all_public_ips = set()
self.dev_lan = None
self.dev_wan = None
self.dns_private_domain = None
self.dns_db = None
self.dns_rev_db = None
self.shaping_classes = dict()
self.shaping_class2user = dict()
self.post_message = None
# from nftables stats
self.ip2download = dict()
self.ip2upload = dict()
self.ip2download_packets = dict()
self.ip2upload_packets = dict()
self.ip2traffic = defaultdict(int)
# from logs
self.host2traffic_stats = dict() # host -> (total, down, up, speed)
self.last_classid = 2089
self.user2classid = dict()
self.user2superclassid = dict()
self.ip2classid = dict()
self.init_nat_conf()
def get_classid(self):
self.last_classid += 1
return self.last_classid
def get_shaping_ceilh(self, shaping):
(_type, details) = shaping
if _type == "legacy":
(_, ceil) = details
ceil = humankbps(ceil)
elif _type == "class":
cls = details
ceil = self.shaping_classes[cls]
elif _type == "speed":
ceil = details
else:
raise RuntimeError(f"Unknown shaping {shaping}")
return ceil
def add_qos(self, ip, host, user, shaping):
if ip in self.ip2host:
host_other = self.ip2host[ip]
logpe(f"Warning: Duplicate IP in qos.conf: {ip} is hosts {host_other} and {host}")
ip_other = self.host2ip[host_other]
user_other = self.ip2user[ip_other]
if user != user_other:
raise ConfError(f"Duplicate IP in qos.conf: {ip} belongs to users {user_other} and {user}")
else:
self.ip2host[ip] = host
if host in self.host2ip:
ip_other = self.host2ip[host]
user_other = self.ip2user[ip_other]
logpe(f"Warning: Duplicate hostname in qos.conf: {host} is IP {ip_other} (user {user_other}) "
f"and {ip} (user {user})")
else:
self.host2ip[host] = ip
self.ip2user[ip] = user
if host == user:
self.users.add(user)
if user in self.user2shaping:
raise ConfError(f"Multiple shaping non-sharing definitions for user {user}: "
f"{self.user2shaping[user]} and {shaping} for ip {ip}")
self.user2shaping[user] = shaping
self.user2ip[user] = ip
if shaping is not None:
self.user2classid[user] = self.get_classid()
elif shaping is not None:
self.ip2shaping[ip] = shaping
self.ip2classid[ip] = self.get_classid()
self.users_with_subclasses.add(user)
def read_qos_conf(self, qosconf, section = None):
valid_sections = set(["hosts", "config", "classes"])
line_num = 0
for line in qosconf:
line_num += 1
# remove leading/trailing whitespace
line = line.strip()
# empty
if line == "":
continue
# commented out
m = re.match("#.*", line)
if m:
continue
try:
m = re.match(r"include \"([\S]+)\"", line)
if m:
subconf_path = f"{config_prefix}{m.group(1)}";
with open(subconf_path, 'r') as subconf:
self.read_qos_conf(subconf, section);
continue
m = re.match(r"\[([\S]+)\]", line)
if m:
section = m.group(1)
if section not in valid_sections:
raise ConfError(f"invalid qos.conf section: {line}")
if section == "hosts":
if self.conf_uplink_mbit is None:
raise ConfError(f"missing uplink_mbit=$num in [config]")
if not self.local_network:
raise ConfError(f"missing lan_range=\"$range\" in [config]")
if len(self.all_public_ips) == 0:
raise ConfError(f"missing wan_ranges=\"$range1,$range2...\" in [config]")
if not self.dev_lan:
raise ConfError(f"missing lan_dev=\"$dev\" in [config]")
if not self.dev_wan:
raise ConfError(f"missing wan_dev=\"$dev\" in [config]")
if not self.dns_private_domain:
raise ConfError(f"missing dns_private_domain=\"example.tld\" in [config]")
if not self.dns_db:
raise ConfError(f"missing dns_db=\"/path/to/$domain.db\" in [config]")
if not self.dns_rev_db:
raise ConfError(f"missing dns_rev_db=\"/path/to/$ip.db\" in [config]")
continue
if section is None:
raise ConfError(f"no qos.conf [section] specified")
if section == "config":
m = re.match(r"([\S]+)=\"([^\"]+)\"", line)
if not m:
m = re.match(r"([\S]+)=(.+)", line)
if not m:
raise ConfError(f"did not match key=value expected in [config]: {line}")
key = m.group(1)
val = m.group(2)
if key == "uplink_mbit":
try:
mbits = int(val)
self.conf_uplink_mbit = mbits
except ValueError as e:
raise ConfError(f"could not parse uplink_mbit value: {e}")
elif key == "lan_range":
try:
self.local_network = ip_network(val)
except ValueError as e:
raise ConfError(f"could not parse lan_range value: {e}")
elif key == "wan_ranges":
try:
for net_str in val.split(","):
net = ip_network(net_str)
self.all_public_ips.update(net.hosts())
except ValueError as e:
raise ConfError(f"could not parse wan_ranges value: {e}")
elif key == "lan_dev":
self.dev_lan = val
elif key == "wan_dev":
self.dev_wan = val
elif key == "dns_private_domain":
self.dns_private_domain = val
elif key == "dns_db":
self.dns_db = val
elif key == "dns_rev_db":
self.dns_rev_db = val
elif key == "post_message":
self.post_message = val
else:
raise ConfError(f"unknown key=value in [config]: {line}")
continue
elif section == "classes":
if m := re.match(r"([\S]+)=\"([\S]+)\"", line):
pass
elif m := re.match(r"([\S]+)=([\S]+)", line):
pass
else:
raise ConfError(f"did not match key=value expected in [classes]: {line}")
cls = m.group(1)
user = None
total_speed = None
for prop in m.group(2).split(","):
if m := re.match(r"[0-9]+[kKmMgG]?bit", prop):
speed = prop
self.shaping_classes[cls] = speed
elif m := re.match(r"user=([\S]+)", prop):
user = m.group(1)
self.users.add(user)
self.shaping_class2user[cls] = user
elif m := re.match(r"total=([0-9]+[kKmMgG]?bit)", prop):
total_speed = m.group(1)
else:
raise ConfError(f"unknown configuration '{prop}' of class {cls}")
if user is not None and total_speed is not None:
self.user2shaping[user] = ("speed", total_speed)
continue
m = re.match(r"([0-9.]+)[ \t]+([\S]+)[ \t]+#([\S]+).*", line)
if not m:
raise ConfError(f"regex failed: {line}")
(ip, host, shaping) = m.groups()
try:
ip = ip_address(ip)
except ValueError as e:
raise ConfError(f"IP parsing error: {e}")
if host == 'loopback':
continue
if ip not in self.local_network:
raise ConfError(f"IP {ip} not in local network {self.local_network}")
if shaping == 'private':
user = host
shaping = None
elif m := re.match(r"sharing-([\S]+),([\S]+)", shaping):
user = m.group(1)
shaping = m.group(2)
elif m := re.match(r"sharing-([\S]+)", shaping):
user = m.group(1)
shaping = None
else:
user = host
if shaping is not None:
if m:= re.match(r"speed-([0-9]+[kKmMgG]?bit)", shaping):
shaping = ("speed", m.group(1))
elif m := re.match(r"via-prometheus-([0-9]+)-([0-9]+)", shaping):
speeds = (int(m.group(1)), int(m.group(2)))
shaping = ("legacy", speeds)
elif m := re.match(r"class-([\S]+)", shaping):
cls = m.group(1)
if cls not in self.shaping_classes:
raise ConfError(f"unknown shaping class: {shaping}")
shaping = ("class", cls)
if cls in self.shaping_class2user:
user = self.shaping_class2user[cls]
else:
raise ConfError(f"unknown shaping: {shaping}")
self.add_qos(ip, host, user, shaping)
except ConfError as e:
err = f"Error processing qos.conf line {line_num}: {e}"
if not errors_not_fatal:
raise ConfError(err)
logpe(err)
self.free_public_ips = set(self.all_public_ips)
for (ip, user) in self.ip2user.items():
if user not in self.users:
raise ConfError(f"qos.conf error: ip {ip} is sharing-{user} but user {user} has no primary entry")
for user in self.users:
if user not in self.user2shaping:
logpe(f"Warning: No shaping in qos.conf defined for user {user}")
def add_nat_conf(self, pubip, ip, port_src, port_dst, user, comment):
if port_src != "*" or port_dst != "*":
if port_src != "all" or port_dst != "all":
try:
port_src = int(port_src)
if port_src < 1 or port_src > 65535:
raise ValueError()
except ValueError:
raise ConfError(f"invalid external port number {port_src}")
try:
port_dst = int(port_dst)
if port_dst < 1 or port_dst > 65535:
raise ValueError()
except ValueError:
raise ConfError(f"invalid internal port number {port_dst}")
self.ip2portfwd[ip].add((pubip, port_src, port_dst, user, comment))
pubport = (pubip, port_src)
locport = (ip, port_dst)
if pubport in self.pubip_port2ip_port:
other_locport = self.pubip_port2ip_port[pubport]
if locport == other_locport:
logp(f"Warning: In nat.conf multiple lines with same port forward "
f"from public {pubip}:{port_src} to local {ip}:{port_dst}")
else:
(other_ip, other_port_dst) = other_locport
raise ConfError(f"conflicting port forward from public {pubip}:{port_src} "
f"to local {ip}:{port_dst} with previously seen local "
f"{other_ip}:{other_port_dst}")
else:
self.pubip_port2ip_port[pubport] = locport
self.free_public_ips.discard(pubip)
if comment.find("nowarn") != -1:
self.pubip_port_nowarn.add(pubport)
return
if ip in self.ip2pubip:
pubip_other = self.ip2pubip[ip]
if pubip != pubip_other:
raise ConfError(f"local IP {ip} is translated to public IP {pubip} but also {pubip_other}")
else:
self.ip2pubip[ip] = pubip
if ip in self.ip2user and user != self.ip2user[ip]:
self.nat_conf_user_renames[ip] = (user, pubip, self.ip2user[ip])
return
if ip in self.ip2user:
ipuser = self.ip2user[ip]
if ipuser in self.ipuser2pubip:
pubip_other = self.ipuser2pubip[ipuser]
if pubip != pubip_other:
logp(f"Warning: In nat.conf {user} has public IP {pubip} but also {pubip_other}")
else:
self.ipuser2pubip[ipuser] = pubip
if ipuser in self.user2shaping and self.user2shaping[ipuser] is None:
self.nat_conf_ips_no_shaping.add(ip)
if user in self.user2pubip:
pubip_other = self.user2pubip[user]
if pubip != pubip_other:
logp(f"Warning: In nat.conf {user} has public IP {pubip} but also {pubip_other}")
else:
self.user2pubip[user] = pubip
if pubip in self.pubip2user:
user_other = self.pubip2user[pubip]
if user != user_other:
logp(f"Warning: In nat.conf public IP {pubip} assigned to user {user} but also {user_other}")
else:
self.pubip2user[pubip] = user
self.free_public_ips.discard(pubip)
def write_nat_conf_line(self, pubip, ip, port_src, port_dst, user, comment, natconf_new):
if ip in self.nat_conf_ips_to_delete:
return
if ip in self.nat_conf_ips_to_change:
ip = self.nat_conf_ips_to_change[ip]
else:
if ip in self.nat_conf_user_renames:
(olduser, _, newuser) = self.nat_conf_user_renames[ip]
if olduser == user:
user = newuser
if ip in self.nat_conf_pubip_changes:
(oldpubip, newpubip) = self.nat_conf_pubip_changes[ip]
if oldpubip == pubip:
pubip = newpubip
if pubip in self.nat_conf_pubip2ip_to_add and pubip not in self.nat_conf_pubip_already_added:
for new_ip in self.nat_conf_pubip2ip_to_add[pubip]:
# we are adding new local IP to existing public IP, so write the line
# next to existing line
new_user = self.ip2user[new_ip]
natconf_new.write(f"{pubip}\t{new_ip}\t*\t*\t# {new_user} added by script\n")
self.nat_conf_pubip_already_added.add(pubip)
is_auto_entry = (port_src == "*" and port_dst == "*")
if not is_auto_entry or ip not in self.nat_conf_ip_already_written:
natconf_new.write(f"{pubip}\t{ip}\t{port_src}\t{port_dst}\t# {user}{comment}\n")
if is_auto_entry:
self.nat_conf_ip_already_written.add(ip)
def read_nat_conf(self, natconf, natconf_new=None):
line_num = 0
for line in natconf:
line_num += 1
# remove leading/trailing whitespace
#line = line.strip()
m = re.match(r"([0-9.]+)[ \t]+([0-9.]+)[ \t]+([\S]+)[ \t]([\S]+)[ \t]# ([\S]+)(.*)", line)
if not m:
raise ConfError(f"Error parsing nat.conf line {line_num}: {line}")
(pubip, ip, port_src, port_dst, user, comment) = m.groups()
try:
pubip = ip_address(pubip)
ip = ip_address(ip)
comment = comment.rstrip()
except ValueError as e:
raise ConfError(f"Error parsing nat.conf line {line_num}: {e}")
if ip not in self.local_network:
raise ConfError(f"Error parsing nat.conf line {line_num}: local IP {ip} not in local network {self.local_network}")
if pubip not in self.all_public_ips:
raise ConfError(f"Error parsing nat.conf line {line_num}: public IP {pubip} not in defined wan_ranges")
if natconf_new:
self.write_nat_conf_line(pubip, ip, port_src, port_dst, user, comment, natconf_new)
else:
try:
self.add_nat_conf(pubip, ip, port_src, port_dst, user, comment)
except ConfError as e:
raise ConfError(f"Error parsing nat.conf line {line_num}: {e}")
# check for port forwards from public IPs that are not fully assigned to a user's private IP
for (pubip, port_src) in self.pubip_port2ip_port:
if pubip not in self.pubip2user:
(ip, port_dst) = self.pubip_port2ip_port[(pubip, port_src)]
# if the public ip is changing, don't warn
if ip in self.nat_conf_pubip_changes:
continue
if (pubip, port_src) in self.pubip_port_nowarn:
continue
user = self.ip2user[ip]
logp(f"Warning: port forward for unassigned public IP {pubip}:{port_src} to {ip}:{port_dst} (user {user})")
def update_nat_conf(self, natconf_old, natconf_new):
self.read_nat_conf(natconf_old, natconf_new)
for (pubip, list_ip) in self.nat_conf_pubip2ip_to_add.items():
if pubip in self.nat_conf_pubip_already_added:
continue
for ip in list_ip:
user = self.ip2user[ip]
natconf_new.write(f"{pubip}\t{ip}\t*\t*\t# {user} added by script\n")
def get_new_public_ip(self, user):
if len(self.free_public_ips) == 0:
raise ConfError(f"Need new public IP for user {user}, but none left.")
pubip = self.free_public_ips.pop()
return pubip
def find_differences(self):
found = 0
for ip in self.ip2pubip:
if ip not in self.ip2host or ip in self.nat_conf_ips_no_shaping:
if ip not in self.ip2host:
reason = "not found in qos.conf"
else:
reason = "marked private"
found += 1
fwds = ""
if ip in self.ip2portfwd:
fwds = f", including {len(self.ip2portfwd[ip])} defined port forwards"
pubip = self.ip2pubip[ip]
user = self.pubip2user[pubip]
if ip not in self.ip2host and user in self.user2ip:
newIp = self.user2ip[user]
if newIp not in self.ip2pubip:
logp(f"User {user} (public IP {pubip}): changing primary local IP from {ip} to {newIp}{fwds}")
self.nat_conf_ips_to_change[ip] = newIp
continue
logp(f"Removing nat.conf entry from {pubip} to {ip} for user {user} (because {reason}){fwds}")
self.nat_conf_ips_to_delete.add(ip)
elif ip in self.nat_conf_user_renames:
found += 1
(olduser, oldpubip, newuser) = self.nat_conf_user_renames[ip]
ipchange = ""
# qos.conf line changed from user that still exists, or to user that already exists, so change public IP
if olduser in self.users or newuser in self.user2pubip:
fresh = ""
if newuser in self.user2pubip:
newpubip = self.user2pubip[newuser]
elif newuser in self.nat_conf_user2pubip_to_add:
newpubip = self.nat_conf_user2pubip_to_add[user]
else:
newpubip = self.get_new_public_ip(newuser)
self.nat_conf_user2pubip_to_add[newuser] = newpubip
fresh = "newly assigned "
ipchange = f" and changing public IP {oldpubip} to {fresh}{newpubip}"
self.nat_conf_pubip_changes[ip] = (oldpubip, newpubip)
fwds = 0
if ip in self.ip2portfwd:
for (fwdpubip, _, _, _, _,) in self.ip2portfwd[ip]:
if fwdpubip == oldpubip:
fwds += 1
if fwds == 0:
fwdstr = ""
else:
fwdstr = f", including {fwds} defined port forwards"
logp(f"Renaming user {olduser} to {newuser} for IP {ip}{ipchange}{fwdstr}")
for ip in self.ip2host:
if ip not in self.ip2pubip:
found += 1
if ip in self.nat_conf_ips_to_change.values():
continue
host = self.ip2host[ip]
user = self.ip2user[ip]
if self.user2shaping[user] is None:
found -= 1
continue
if user in self.user2ip and self.user2ip[user] in self.ip2pubip:
pubip = self.ip2pubip[self.user2ip[user]]
info = f"with existing user's public IP {pubip}"
elif user in self.user2pubip:
pubip = self.user2pubip[user]
info = f"with existing user's public IP {pubip}"
elif user in self.nat_conf_user2pubip_to_add:
pubip = self.nat_conf_user2pubip_to_add[user]
info = f"with pending new user's public IP {pubip}"
else:
pubip = self.get_new_public_ip(user)
self.nat_conf_user2pubip_to_add[user] = pubip
info = f"with new user's public IP {pubip}"
logp(f"User {user}: adding local IP {ip} (host {host}) {info}")
self.nat_conf_pubip2ip_to_add[pubip].add(ip)
return found
def write_nat_up_nft(self, nat_up):
localnet = self.local_network
nat_up.write("add map ip nat snat_map { type ipv4_addr : ipv4_addr; }\n")
for (ip, pubip) in self.ip2pubip.items():
nat_up.write(f"add element ip nat snat_map {{ {ip} : {pubip} }}\n")
if not ip in self.ip2portfwd:
continue
for (pubip, pub_port, loc_port, _, _) in self.ip2portfwd[ip]:
if pub_port == "all" and loc_port == "all":
nat_up.write(f"add rule ip nat PREROUTING ip daddr {pubip} "
f"dnat to {ip}\n")
else:
for proto in ("tcp", "udp"):
nat_up.write(f"add rule ip nat PREROUTING ip daddr {pubip} {proto} dport {pub_port} "
f"dnat to {ip}:{loc_port}\n")
nat_up.write(f"add rule ip nat POSTROUTING ip daddr != {localnet} snat ip saddr map @snat_map\n")
def write_nat_up(self, nat_up):
localnet = self.local_network.with_netmask
for (ip, pubip) in self.ip2pubip.items():
nat_up.write(f"-A POSTROUTING -s {ip} ! -d {localnet} -j SNAT --to-source {pubip} \n")
if not ip in self.ip2portfwd:
continue
for (pubip, pub_port, loc_port, _, _) in self.ip2portfwd[ip]:
if pub_port == "all" and loc_port == "all":
nat_up.write(f"-A PREROUTING -d {pubip} "
f"-j DNAT --to-destination {ip}")
else:
for proto in ("tcp", "udp"):
nat_up.write(f"-A PREROUTING -d {pubip} -p {proto} -m {proto} --dport {pub_port} "
f"-j DNAT --to-destination {ip}:{loc_port}\n")
nat_up.write("COMMIT\n")
def write_portmap(self, portmap):
for (ip, pubip) in self.ip2pubip.items():
if not ip in self.ip2portfwd:
continue
for (pubip, pub_port, loc_port, user, comment) in self.ip2portfwd[ip]:
portmap.write(f"{pubip}\t{ip}\t{pub_port}\t{loc_port}\t# {user}{comment}\n")
def write_dns_hosts(self, db):
for (ip, host) in sorted(self.ip2host.items(), key = lambda item: item[1]):
host = host.replace("_", "-")
db.write(f"{host:<25} IN A {ip}\n")
def write_dns_reverse(self, db):
current_net = None
for (ip, host) in sorted(self.ip2host.items(), key = lambda item: item[0]):
if not current_net or ip not in current_net:
current_net = ip_network(ip).supernet(new_prefix=24)
ipp = current_net.network_address.packed
db.write(";##################################################\n")
db.write(f"$ORIGIN {ipp[2]}.{ipp[1]}.{ipp[0]}.in-addr.arpa.\n")
host = host.replace("_", "-")
db.write(f"{ip.packed[3]:<14} IN PTR {host}.{self.dns_private_domain}.\n")
def write_nft_mangle(self, out, reset_stats):
localnet = self.local_network
out.write("add table ip mangle\n")
out.write("delete table ip mangle\n")
out.write("add table ip mangle\n")
out.write("add map ip mangle forw_map { type ipv4_addr : verdict; }\n")
out.write("add map ip mangle post_map { type ipv4_addr : verdict; }\n")
for (ip, user) in self.ip2user.items():
if user not in self.user2shaping:
log(f"skip ip {ip} of user {user} due to no defined shaping")
continue
if self.user2shaping[user] is None:
continue
if ip in self.ip2classid:
classid = self.ip2classid[ip]
else:
classid = self.user2classid[user]
ipstr = str(ip).replace(".", "_")
for prefix in ("post", "forw"):
_bytes = 0
packets = 0
if not reset_stats:
if prefix == "post" and ip in self.ip2download:
_bytes = self.ip2download[ip]
packets = self.ip2download_packets[ip]
elif ip in self.ip2upload:
_bytes = self.ip2upload[ip]
packets = self.ip2upload_packets[ip]
out.write(f"add chain ip mangle {prefix}_{ipstr}\n")
out.write(f"add rule ip mangle {prefix}_{ipstr} counter packets {packets} bytes {_bytes} meta priority set 1:{classid} accept\n")
out.write(f"add element ip mangle {prefix}_map {{ {ip} : goto {prefix}_{ipstr} }}\n")
out.write("add chain ip mangle forw_common\n")
out.write("add rule ip mangle forw_common counter packets 0 bytes 0 meta priority set 1:3 accept\n")
out.write("add chain ip mangle post_common\n")
out.write("add rule ip mangle post_common counter packets 0 bytes 0 meta priority set 1:3 accept\n")
out.write("add chain ip mangle forward { type filter hook forward priority -150; policy accept; }\n")
out.write(f"add rule ip mangle forward oifname \"{self.dev_wan}\" ip daddr {localnet} counter packets 0 bytes 0 accept\n")
out.write(f"add rule ip mangle forward oifname \"{self.dev_wan}\" ip saddr vmap @forw_map\n")
out.write(f"add rule ip mangle forward oifname \"{self.dev_wan}\" counter packets 0 bytes 0 jump forw_common\n")
out.write("add chain ip mangle postrouting { type filter hook postrouting priority -150; policy accept; }\n")
out.write(f"add rule ip mangle postrouting oifname \"{self.dev_lan}\" ip saddr {localnet} counter packets 0 bytes 0 accept\n")
out.write(f"add rule ip mangle postrouting oifname \"{self.dev_lan}\" ip daddr vmap @post_map\n")
out.write(f"add rule ip mangle postrouting oifname \"{self.dev_lan}\" counter packets 0 bytes 0 jump post_common\n")
def __write_tc_shaping(self, out, classid, parent, shaping, qdisc = True):
if shaping is None:
return
(_type, details) = shaping
if _type == "legacy":
(rate, ceil) = details
rate = f"{rate}kbit"
ceil = f"{ceil}kbit"
elif _type == "class":
cls = details
#TODO: scale with ceil?
rate = "200kbit"
ceil = self.shaping_classes[cls]
elif _type == "speed":
rate = "200kbit"
ceil = details
else:
raise RuntimeError(f"Unknown shaping {shaping}")
for dev in (self.dev_lan, self.dev_wan):
out.write(f"class add dev {dev} parent 1:{parent} classid 1:{classid} htb rate {rate} ceil {ceil} burst 256k cburst 256k prio 1 quantum 1500\n")
if qdisc:
out.write(f"qdisc add dev {dev} parent 1:{classid} handle {classid} fq_codel\n")
def write_tc_up(self, out):
top_mbit = self.conf_uplink_mbit
sub_mbit = int(top_mbit * 0.975)
for dev in (self.dev_lan, self.dev_wan):
out.write(f"qdisc add dev {dev} root handle 1: htb r2q 5 default 1\n")
out.write(f"class add dev {dev} parent 1: classid 1:2 htb rate {top_mbit}Mbit ceil {top_mbit}Mbit burst 14300k cburst 14300k prio 0 quantum 20000\n")
out.write(f"class add dev {dev} parent 1:2 classid 1:1 htb rate {sub_mbit}Mbit ceil {sub_mbit}Mbit burst 10300k cburst 10300k prio 0 quantum 20000\n")
out.write(f"class add dev {dev} parent 1:1 classid 1:1025 htb rate {sub_mbit}bit ceil {sub_mbit}Mbit burst 10300k cburst 10300k prio 1 quantum 20000\n")
for (user, shaping) in self.user2shaping.items():
parent = 1025
if user in self.users_with_subclasses:
super_classid = self.get_classid()
self.__write_tc_shaping(out, super_classid, parent, shaping, False)
parent = super_classid
self.user2superclassid[user] = super_classid
if user in self.user2classid:
classid = self.user2classid[user]
self.__write_tc_shaping(out, classid, parent, shaping)
for (ip, shaping) in self.ip2shaping.items():
user = self.ip2user[ip]
if user in self.user2superclassid:
parent = self.user2superclassid[user]
else:
parent = 1025
classid = self.ip2classid[ip]
self.__write_tc_shaping(out, classid, parent, shaping)
for dev in (self.dev_lan, self.dev_wan):
out.write(f"class add dev {dev} parent 1:1025 classid 1:3 htb rate 64kbit ceil 128kbit burst 256k cburst 256k prio 7 quantum 1500\n")
out.write(f"qdisc add dev {dev} parent 1:3 handle 3 fq_codel\n")
out.write(f"filter add dev {dev} parent 1:0 protocol ip handle 3 fw flowid 1:3\n")
def read_nft_stats(self, stats):
table_based = None
down = None
table_in_chain = False
for line in stats:
line = line.strip()
if table_based is None:
m = re.match(r"map forw_map {", line)
if m:
table_based = True
continue
m = re.match(r"chain PREROUTING {", line)
if m:
table_based = False
continue
continue
if not table_based:
m = re.match(r"oifname \"([^\"]+)\" ip ([ds]addr) ([0-9.]+) counter packets ([0-9]+) bytes ([0-9]+) meta priority set 1:([0-9]+)", line)
if not m:
continue
(dev, sdaddr, ip, packets, _bytes, classid) = m.groups()
if dev == self.dev_lan and sdaddr == "daddr":
down = True
elif dev == self.dev_wan and sdaddr == "saddr":
down = False
else:
continue
else:
if table_in_chain:
m = re.match(f"counter packets ([0-9]+) bytes ([0-9]+) meta priority set 1:([0-9]+)", line)
if not m:
raise ConfError(f"Unexpected content of chain for {ip} down {down}: {line}")
(packets, _bytes, classid) = m.groups()
table_in_chain = False
else:
m = re.match(r"chain (post|forw)_([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+) {", line)
if not m:
continue
(_type, ip1, ip2, ip3, ip4) = m.groups()
if _type == "post":
down = True
else:
down = False
ip = f"{ip1}.{ip2}.{ip3}.{ip4}"
table_in_chain = True
continue
ip = ip_address(ip)
if ip not in self.local_network:
continue
_bytes = int(_bytes)
self.ip2traffic[ip] = self.ip2traffic[ip] + _bytes
packets = int(packets)
if down:
self.ip2download[ip] = _bytes
self.ip2download_packets[ip] = packets
else:
self.ip2upload[ip] = _bytes
self.ip2upload_packets[ip] = packets
if table_based is None:
raise ConfError("Content of nft list table mangle not recognized")
def write_day_html(self, html):
if errors_log:
html.write(f"<pre>{errors_log}</pre>")
timestamp = datetime.now().strftime("%a %b %d %H:%M:%S %Y")
html.write("<table border>\n")
html.write(f"<tr><th colspan=7>Top Traffic Hosts ({timestamp})</th></tr>\n")
html.write(tr((tdr("#"), td("hostname (user)"), td("ip"), tdr("total"), tdr("down"), tdr("up"), tdr("speed"))))
num = 0
for (ip, traffic) in sorted(self.ip2traffic.items(), key = lambda item: item[1], reverse=True):
num += 1
try:
host = self.ip2host[ip]
except KeyError:
host = "(unknown)"
try:
user = self.ip2user[ip]
except KeyError:
user = "(unknown)"
if host == user:
hostuser = f"<a href=\"logs/{host}.log\"><b>{host}</b></a>"
else:
hostuser = f"<a href=\"logs/{host}.log\"><b>{host}</b></a> (<a href=\"logs/{user}.log\">{user}</a>)"
try:
shaping = self.user2shaping[user]
ceil = self.get_shaping_ceilh(shaping)
except KeyError:
ceil = 0
down = self.ip2download[ip]
up = self.ip2upload[ip]
html.write(tr((tdr(f"<a name=\"{host}\">{num}</a>"), td(hostuser), td(f"{ip}"), tdr(f"<b>{human(traffic)}</b>"),\
tdr(human(down)), tdr(human(up)), tdr(ceil))))
html.write("</table>\n")
def write_monthyear_html(self, html, header):
html.write("<table border>\n")
html.write(f"<tr><th colspan=6>Top Traffic Hosts ({header})</th></tr>\n")
html.write(tr((tdr("#"), td("hostname"), tdr("total"), tdr("down"), tdr("up"), tdr("speed"))))
num = 0
for (host, stats) in sorted(self.host2traffic_stats.items(), key = lambda item: max(item[1][0], item[1][1]), reverse=True):
num += 1
(total, down, up, speed) = stats
html.write(tr((tdr(f"<a name=\"{host}\">{num}</a>"), td(f"<b>{host}</b>"), tdr(f"<b>{humanmb(total)}</b>"),\
tdr(humanmb(down)), tdr(humanmb(up)), tdr(f"{humankbps(speed)}"))))
html.write("</table>\n")
def write_host_logs(self):
now = int(time.time())
dt = datetime.fromtimestamp(now)
timestamp = dt.strftime("%a %b %d %H:%M:%S %Y")
for (ip, traffic) in self.ip2traffic.items():
if ip not in self.ip2host:
continue
host = self.ip2host[ip]
user = self.ip2user[ip]
down = self.ip2download[ip] // (1024*1024)
up = self.ip2upload[ip] // (1024*1024)
traffic = traffic // (1024*1024)
shaping = self.user2shaping[user]
ceil = self.get_shaping_ceilh(shaping)
with open(f"{config_prefix}{config_logdir}/{host}.log", 'a') as log:
log.write(f"{now}\t{host}\t{traffic}\t{down}\t0\t{up}\trate\t{ceil}\t{ceil}\t{timestamp}\n")
def read_host_log(self, log, ts_start, ts_end):
stat_host = ""
stat_traffic = 0
stat_down = 0
stat_up = 0
stat_ceil = 0
for line in log:
line = line.strip()
m = re.match(r"([0-9]+)[ \t]+([\S]+)[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+[0-9]+[ \t]+([0-9]+)[ \t]+[0-9]+[ \t]+[0-9]+[ \t]+([0-9]+)[ \t]+", line)
if not m: