AP manageur V1.1 (Bêta)
This commit is contained in:
1 parent
bfc0475665
commit
f1b3179a16
5 files changed
+96
-73
No files matched your search
@@ -3990,9 +3990,10 @@ def setup_ap_nat(iface=None):
|
||||
|
||||
# Conserver les chaînes prioritaires (blocage quota et isolation VPN) toujours AU-DESSUS de AP_NAT_FORWARD
|
||||
for prio_chain in ('AP_VPN_ISOLATION', 'AP_QUOTA_BLOCK'):
|
||||
if subprocess.run([ipt, '-w', '1', '-C', 'FORWARD', '-j', prio_chain], capture_output=True, timeout=1).returncode == 0:
|
||||
_ipt_del_jump(ipt, 'filter', 'FORWARD', prio_chain)
|
||||
subprocess.run([ipt, '-w', '1', '-I', 'FORWARD', '1', '-j', prio_chain], capture_output=True, timeout=1)
|
||||
if _ipt_has_chain(ipt, 'filter', prio_chain):
|
||||
if subprocess.run([ipt, '-w', '1', '-C', 'FORWARD', '-j', prio_chain], capture_output=True, timeout=1).returncode == 0:
|
||||
_ipt_del_jump(ipt, 'filter', 'FORWARD', prio_chain)
|
||||
subprocess.run([ipt, '-w', '1', '-I', 'FORWARD', '1', '-j', prio_chain], capture_output=True, timeout=1)
|
||||
|
||||
# 2. Chaîne nat POSTROUTING dédiée au mode AP (MASQUERADE vers la passerelle Internet)
|
||||
subprocess.run([ipt, '-w', '1', '-t', 'nat', '-N', 'AP_NAT_POST'], capture_output=True, timeout=1)
|
||||
@@ -5698,68 +5699,81 @@ def sync_client_bandwidth_and_quotas(force=False):
|
||||
ebtables_bin = find_bin('ebtables')
|
||||
if ebtables_bin and (all_blocked_macs or os.path.exists('/sys/module/ebtable_filter')):
|
||||
try:
|
||||
subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-N', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=5)
|
||||
subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-F', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=5)
|
||||
subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-D', 'FORWARD', '-j', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=5)
|
||||
subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-D', 'INPUT', '-j', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=5)
|
||||
if all_blocked_macs:
|
||||
subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-I', 'FORWARD', '1', '-j', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=5)
|
||||
subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-I', 'INPUT', '1', '-j', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=5)
|
||||
subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-A', 'AP_QUOTA_BLOCK', '-p', 'ARP', '-j', 'ACCEPT'], capture_output=True, timeout=5)
|
||||
subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-A', 'AP_QUOTA_BLOCK', '-p', 'IPv4', '--ip-proto', 'udp', '--ip-dport', '67:68', '-j', 'ACCEPT'], capture_output=True, timeout=5)
|
||||
subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-A', 'AP_QUOTA_BLOCK', '-p', 'IPv4', '--ip-proto', 'udp', '--ip-sport', '67:68', '-j', 'ACCEPT'], capture_output=True, timeout=5)
|
||||
if not all_blocked_macs:
|
||||
# Si aucun équipement n'est bloqué, nettoyer seulement si la chaîne existe
|
||||
r_eb = subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-L', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=1.5)
|
||||
if r_eb.returncode == 0:
|
||||
subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-D', 'FORWARD', '-j', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=1.5)
|
||||
subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-D', 'INPUT', '-j', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=1.5)
|
||||
subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-F', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=1.5)
|
||||
subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-X', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=1.5)
|
||||
else:
|
||||
subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-N', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=1.5)
|
||||
subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-F', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=1.5)
|
||||
check_fwd = subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-C', 'FORWARD', '-j', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=1.5)
|
||||
if check_fwd.returncode != 0:
|
||||
subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-I', 'FORWARD', '1', '-j', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=1.5)
|
||||
check_inp = subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-C', 'INPUT', '-j', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=1.5)
|
||||
if check_inp.returncode != 0:
|
||||
subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-I', 'INPUT', '1', '-j', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=1.5)
|
||||
subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-A', 'AP_QUOTA_BLOCK', '-p', 'ARP', '-j', 'ACCEPT'], capture_output=True, timeout=1.5)
|
||||
subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-A', 'AP_QUOTA_BLOCK', '-p', 'IPv4', '--ip-proto', 'udp', '--ip-dport', '67:68', '-j', 'ACCEPT'], capture_output=True, timeout=1.5)
|
||||
subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-A', 'AP_QUOTA_BLOCK', '-p', 'IPv4', '--ip-proto', 'udp', '--ip-sport', '67:68', '-j', 'ACCEPT'], capture_output=True, timeout=1.5)
|
||||
for b_mac in all_blocked_macs:
|
||||
cm = b_mac.strip().lower()
|
||||
if cm:
|
||||
subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-A', 'AP_QUOTA_BLOCK', '-s', cm, '-j', 'DROP'], capture_output=True, timeout=5)
|
||||
subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-A', 'AP_QUOTA_BLOCK', '-d', cm, '-j', 'DROP'], capture_output=True, timeout=5)
|
||||
subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-A', 'AP_QUOTA_BLOCK', '-s', cm, '-j', 'DROP'], capture_output=True, timeout=1.5)
|
||||
subprocess.run([ebtables_bin, '--concurrent', '-t', 'filter', '-A', 'AP_QUOTA_BLOCK', '-d', cm, '-j', 'DROP'], capture_output=True, timeout=1.5)
|
||||
except Exception as e:
|
||||
print(f"[ap-limits] Erreur ebtables : {e}", flush=True)
|
||||
|
||||
# 4. Couche 3 (IPv4 & IPv6) : iptables et ip6tables (filter & mangle avec verrou -w 5 et test -C)
|
||||
# 4. Couche 3 (IPv4 & IPv6) : iptables et ip6tables (optimisé avec _ipt_has_chain et verrou léger)
|
||||
iptables_bin = find_bin('iptables')
|
||||
ip6tables_bin = find_bin('ip6tables')
|
||||
for bin_cmd, is_v6 in [(iptables_bin, False), (ip6tables_bin, True)]:
|
||||
if not bin_cmd: continue
|
||||
try:
|
||||
for tbl, chain in [('filter', 'FORWARD'), ('filter', 'INPUT'), ('mangle', 'PREROUTING')]:
|
||||
# Créer et vider la chaîne
|
||||
subprocess.run([bin_cmd, '-w', '5', '-t', tbl, '-N', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=5)
|
||||
subprocess.run([bin_cmd, '-w', '5', '-t', tbl, '-F', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=5)
|
||||
target_chains = [('filter', 'FORWARD'), ('filter', 'INPUT'), ('mangle', 'PREROUTING')]
|
||||
if not all_blocked_macs:
|
||||
# Si aucun équipement n'est bloqué : ne toucher aux tables que si AP_QUOTA_BLOCK existe
|
||||
for tbl in ('filter', 'mangle'):
|
||||
if _ipt_has_chain(bin_cmd, tbl, 'AP_QUOTA_BLOCK'):
|
||||
for t_tbl, chain in target_chains:
|
||||
if t_tbl == tbl:
|
||||
_ipt_del_jump(bin_cmd, tbl, chain, 'AP_QUOTA_BLOCK')
|
||||
subprocess.run([bin_cmd, '-w', '1', '-t', tbl, '-F', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=1.5)
|
||||
subprocess.run([bin_cmd, '-w', '1', '-t', tbl, '-X', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=1.5)
|
||||
else:
|
||||
for tbl, chain in target_chains:
|
||||
if not _ipt_has_chain(bin_cmd, tbl, 'AP_QUOTA_BLOCK'):
|
||||
subprocess.run([bin_cmd, '-w', '1', '-t', tbl, '-N', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=1.5)
|
||||
subprocess.run([bin_cmd, '-w', '1', '-t', tbl, '-F', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=1.5)
|
||||
|
||||
# Vérifier si la règle de saut existe
|
||||
check_r = subprocess.run([bin_cmd, '-w', '5', '-t', tbl, '-C', chain, '-j', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=5)
|
||||
has_jump = (check_r.returncode == 0)
|
||||
check_r = subprocess.run([bin_cmd, '-w', '1', '-t', tbl, '-C', chain, '-j', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=1.5)
|
||||
if check_r.returncode != 0:
|
||||
subprocess.run([bin_cmd, '-w', '1', '-t', tbl, '-I', chain, '1', '-j', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=1.5)
|
||||
|
||||
if all_blocked_macs:
|
||||
if not has_jump:
|
||||
subprocess.run([bin_cmd, '-w', '5', '-t', tbl, '-I', chain, '1', '-j', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=5)
|
||||
else:
|
||||
if has_jump:
|
||||
subprocess.run([bin_cmd, '-w', '5', '-t', tbl, '-D', chain, '-j', 'AP_QUOTA_BLOCK'], capture_output=True, timeout=5)
|
||||
|
||||
if all_blocked_macs:
|
||||
if not is_v6:
|
||||
for tbl in ['filter', 'mangle']:
|
||||
subprocess.run([bin_cmd, '-w', '5', '-t', tbl, '-A', 'AP_QUOTA_BLOCK', '-p', 'udp', '--dport', '67:68', '-j', 'RETURN'], capture_output=True, timeout=5)
|
||||
subprocess.run([bin_cmd, '-w', '5', '-t', tbl, '-A', 'AP_QUOTA_BLOCK', '-p', 'udp', '--sport', '67:68', '-j', 'RETURN'], capture_output=True, timeout=5)
|
||||
subprocess.run([bin_cmd, '-w', '1', '-t', tbl, '-A', 'AP_QUOTA_BLOCK', '-p', 'udp', '--dport', '67:68', '-j', 'RETURN'], capture_output=True, timeout=1.5)
|
||||
subprocess.run([bin_cmd, '-w', '1', '-t', tbl, '-A', 'AP_QUOTA_BLOCK', '-p', 'udp', '--sport', '67:68', '-j', 'RETURN'], capture_output=True, timeout=1.5)
|
||||
else:
|
||||
for tbl in ['filter', 'mangle']:
|
||||
subprocess.run([bin_cmd, '-w', '5', '-t', tbl, '-A', 'AP_QUOTA_BLOCK', '-p', 'udp', '--dport', '546:547', '-j', 'RETURN'], capture_output=True, timeout=5)
|
||||
subprocess.run([bin_cmd, '-w', '5', '-t', tbl, '-A', 'AP_QUOTA_BLOCK', '-p', 'udp', '--sport', '546:547', '-j', 'RETURN'], capture_output=True, timeout=5)
|
||||
subprocess.run([bin_cmd, '-w', '5', '-t', tbl, '-A', 'AP_QUOTA_BLOCK', '-p', 'ipv6-icmp', '-j', 'RETURN'], capture_output=True, timeout=5)
|
||||
subprocess.run([bin_cmd, '-w', '1', '-t', tbl, '-A', 'AP_QUOTA_BLOCK', '-p', 'udp', '--dport', '546:547', '-j', 'RETURN'], capture_output=True, timeout=1.5)
|
||||
subprocess.run([bin_cmd, '-w', '1', '-t', tbl, '-A', 'AP_QUOTA_BLOCK', '-p', 'udp', '--sport', '546:547', '-j', 'RETURN'], capture_output=True, timeout=1.5)
|
||||
subprocess.run([bin_cmd, '-w', '1', '-t', tbl, '-A', 'AP_QUOTA_BLOCK', '-p', 'ipv6-icmp', '-j', 'RETURN'], capture_output=True, timeout=1.5)
|
||||
|
||||
for b_mac in all_blocked_macs:
|
||||
cm = b_mac.strip().lower()
|
||||
if cm:
|
||||
for tbl in ['filter', 'mangle']:
|
||||
subprocess.run([bin_cmd, '-w', '5', '-t', tbl, '-A', 'AP_QUOTA_BLOCK', '-m', 'mac', '--mac-source', cm, '-j', 'DROP'], capture_output=True, timeout=5)
|
||||
subprocess.run([bin_cmd, '-w', '1', '-t', tbl, '-A', 'AP_QUOTA_BLOCK', '-m', 'mac', '--mac-source', cm, '-j', 'DROP'], capture_output=True, timeout=1.5)
|
||||
if not is_v6:
|
||||
ip = mac_to_ip.get(b_mac)
|
||||
if ip and ip not in ('Non attribuée', '—'):
|
||||
for tbl in ['filter', 'mangle']:
|
||||
subprocess.run([bin_cmd, '-w', '5', '-t', tbl, '-A', 'AP_QUOTA_BLOCK', '-s', ip, '-j', 'DROP'], capture_output=True, timeout=5)
|
||||
subprocess.run([bin_cmd, '-w', '5', '-t', tbl, '-A', 'AP_QUOTA_BLOCK', '-d', ip, '-j', 'DROP'], capture_output=True, timeout=5)
|
||||
subprocess.run([bin_cmd, '-w', '1', '-t', tbl, '-A', 'AP_QUOTA_BLOCK', '-s', ip, '-j', 'DROP'], capture_output=True, timeout=1.5)
|
||||
subprocess.run([bin_cmd, '-w', '1', '-t', tbl, '-A', 'AP_QUOTA_BLOCK', '-d', ip, '-j', 'DROP'], capture_output=True, timeout=1.5)
|
||||
except Exception as e:
|
||||
print(f"[ap-limits] Erreur iptables/ip6tables ({bin_cmd}) : {e}", flush=True)
|
||||
|
||||
@@ -11828,6 +11842,9 @@ class Handler(BaseHTTPRequestHandler):
|
||||
body=open(os.path.join(os.path.dirname(__file__),'index.html'),'rb').read()
|
||||
self.send_response(200)
|
||||
self.send_header('Content-Type','text/html; charset=utf-8')
|
||||
self.send_header('Cache-Control','no-cache, no-store, must-revalidate')
|
||||
self.send_header('Pragma','no-cache')
|
||||
self.send_header('Expires','0')
|
||||
self.send_header('Content-Length',str(len(body)))
|
||||
self.send_header('X-Content-Type-Options','nosniff')
|
||||
self.send_header('X-Frame-Options','SAMEORIGIN')
|
||||
@@ -11841,6 +11858,9 @@ class Handler(BaseHTTPRequestHandler):
|
||||
body = open(os.path.join(os.path.dirname(__file__), 'setup.html'), 'rb').read()
|
||||
self.send_response(200)
|
||||
self.send_header('Content-Type', 'text/html; charset=utf-8')
|
||||
self.send_header('Cache-Control','no-cache, no-store, must-revalidate')
|
||||
self.send_header('Pragma','no-cache')
|
||||
self.send_header('Expires','0')
|
||||
self.send_header('Content-Length', str(len(body)))
|
||||
self.send_header('X-Content-Type-Options','nosniff')
|
||||
self.send_header('X-Frame-Options','SAMEORIGIN')
|
||||
@@ -11859,7 +11879,9 @@ class Handler(BaseHTTPRequestHandler):
|
||||
self.send_response(200)
|
||||
self.send_header('Content-Type', 'application/javascript; charset=utf-8')
|
||||
self.send_header('Content-Length', str(len(body)))
|
||||
self.send_header('Cache-Control', 'public, max-age=3600')
|
||||
self.send_header('Cache-Control', 'no-cache, no-store, must-revalidate')
|
||||
self.send_header('Pragma', 'no-cache')
|
||||
self.send_header('Expires', '0')
|
||||
self.send_header('X-Content-Type-Options', 'nosniff')
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
Reference in new issue
Block a user