APmanageur Wi-Fi V1.0
This commit is contained in:
1 parent
fffffcf301
commit
849a3e2034
2 files changed
+146
-1
No files matched your search
@@ -2817,7 +2817,121 @@ def teardown_bridge_dns_redirection(bridge_name='br0', iface=None):
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _ap_iptables_bins():
|
||||
"""Retourne les binaires iptables actifs à synchroniser (iptables par défaut + iptables-legacy si sa politique FORWARD est DROP)."""
|
||||
bins = []
|
||||
default_bin = find_bin('iptables')
|
||||
if default_bin:
|
||||
bins.append(default_bin)
|
||||
legacy_bin = find_bin('iptables-legacy')
|
||||
if legacy_bin and os.path.realpath(legacy_bin) != os.path.realpath(default_bin or ''):
|
||||
try:
|
||||
r = subprocess.run([legacy_bin, '-S', 'FORWARD'], capture_output=True, text=True, timeout=2)
|
||||
if r.returncode == 0 and ('-P FORWARD DROP' in r.stdout or 'DOCKER' in r.stdout):
|
||||
bins.append(legacy_bin)
|
||||
except Exception:
|
||||
pass
|
||||
return bins
|
||||
|
||||
def _get_ap_subnet(iface):
|
||||
"""Détecte le sous-réseau IPv4 actif de l'interface AP (ex. 10.42.0.0/24)."""
|
||||
try:
|
||||
import ipaddress
|
||||
r = subprocess.run(['ip', '-4', '-o', 'addr', 'show', 'dev', iface], capture_output=True, text=True, timeout=2)
|
||||
if r.returncode == 0:
|
||||
m = re.search(r'inet\s+(\d+\.\d+\.\d+\.\d+/\d+)', r.stdout)
|
||||
if m:
|
||||
net = ipaddress.ip_network(m.group(1), strict=False)
|
||||
return str(net)
|
||||
except Exception:
|
||||
pass
|
||||
return '10.42.0.0/24'
|
||||
|
||||
def setup_ap_nat(iface=None):
|
||||
"""Garantit le transfert (FORWARD) et le NAT (MASQUERADE) en mode AP, même sur un hôte Docker (politique FORWARD DROP)."""
|
||||
iface = iface or INTERFACE
|
||||
if not iface:
|
||||
return
|
||||
try:
|
||||
subprocess.run(['sysctl', '-w', 'net.ipv4.ip_forward=1'], capture_output=True)
|
||||
subprocess.run(['sysctl', '-w', 'net.ipv4.conf.all.forwarding=1'], capture_output=True)
|
||||
subprocess.run(['sysctl', '-w', f'net.ipv4.conf.{iface}.forwarding=1'], capture_output=True)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
subnet = _get_ap_subnet(iface)
|
||||
for ipt in _ap_iptables_bins():
|
||||
try:
|
||||
# 1. Chaîne filter FORWARD dédiée au mode AP (autorise le trafic des clients Wi-Fi vers Internet et le retour)
|
||||
subprocess.run([ipt, '-w', '5', '-N', 'AP_NAT_FORWARD'], capture_output=True)
|
||||
subprocess.run([ipt, '-w', '5', '-F', 'AP_NAT_FORWARD'], capture_output=True)
|
||||
subprocess.run([ipt, '-w', '5', '-A', 'AP_NAT_FORWARD', '-i', iface, '-s', subnet, '-j', 'ACCEPT'], capture_output=True)
|
||||
subprocess.run([ipt, '-w', '5', '-A', 'AP_NAT_FORWARD', '-o', iface, '-d', subnet, '-m', 'conntrack', '--ctstate', 'ESTABLISHED,RELATED', '-j', 'ACCEPT'], capture_output=True)
|
||||
subprocess.run([ipt, '-w', '5', '-A', 'AP_NAT_FORWARD', '-i', iface, '-o', iface, '-j', 'ACCEPT'], capture_output=True)
|
||||
|
||||
while subprocess.run([ipt, '-w', '5', '-D', 'FORWARD', '-j', 'AP_NAT_FORWARD'], capture_output=True).returncode == 0:
|
||||
pass
|
||||
subprocess.run([ipt, '-w', '5', '-I', 'FORWARD', '1', '-j', 'AP_NAT_FORWARD'], capture_output=True)
|
||||
|
||||
# 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', '5', '-C', 'FORWARD', '-j', prio_chain], capture_output=True).returncode == 0:
|
||||
while subprocess.run([ipt, '-w', '5', '-D', 'FORWARD', '-j', prio_chain], capture_output=True).returncode == 0:
|
||||
pass
|
||||
subprocess.run([ipt, '-w', '5', '-I', 'FORWARD', '1', '-j', prio_chain], capture_output=True)
|
||||
|
||||
# 2. Chaîne nat POSTROUTING dédiée au mode AP (MASQUERADE vers la passerelle Internet)
|
||||
subprocess.run([ipt, '-w', '5', '-t', 'nat', '-N', 'AP_NAT_POST'], capture_output=True)
|
||||
subprocess.run([ipt, '-w', '5', '-t', 'nat', '-F', 'AP_NAT_POST'], capture_output=True)
|
||||
subprocess.run([ipt, '-w', '5', '-t', 'nat', '-A', 'AP_NAT_POST', '-s', subnet, '!', '-o', iface, '-j', 'MASQUERADE'], capture_output=True)
|
||||
|
||||
while subprocess.run([ipt, '-w', '5', '-t', 'nat', '-D', 'POSTROUTING', '-j', 'AP_NAT_POST'], capture_output=True).returncode == 0:
|
||||
pass
|
||||
subprocess.run([ipt, '-w', '5', '-t', 'nat', '-A', 'POSTROUTING', '-j', 'AP_NAT_POST'], capture_output=True)
|
||||
except Exception as e:
|
||||
print(f"[ap-nat] Erreur configuration NAT ({ipt}) : {e}", flush=True)
|
||||
print(f"[ap-nat] NAT/FORWARD actif pour le mode AP ({iface}, {subnet})", flush=True)
|
||||
|
||||
def ensure_ap_nat_active(iface=None):
|
||||
"""Vérifie que les règles NAT/FORWARD du mode AP sont toujours présentes (utile après autoconnect ou rechargement Docker)."""
|
||||
iface = iface or INTERFACE
|
||||
if not iface or is_bridge_mode() or not is_iface_up(iface):
|
||||
return
|
||||
try:
|
||||
ipt = find_bin('iptables')
|
||||
if not ipt:
|
||||
return
|
||||
r_fwd = subprocess.run([ipt, '-w', '2', '-C', 'FORWARD', '-j', 'AP_NAT_FORWARD'], capture_output=True, timeout=2)
|
||||
r_nat = subprocess.run([ipt, '-w', '2', '-t', 'nat', '-C', 'POSTROUTING', '-j', 'AP_NAT_POST'], capture_output=True, timeout=2)
|
||||
if r_fwd.returncode != 0 or r_nat.returncode != 0:
|
||||
setup_ap_nat(iface)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def teardown_ap_nat():
|
||||
"""Supprime proprement les règles NAT/FORWARD dédiées au mode AP."""
|
||||
seen = set()
|
||||
for candidate in (find_bin('iptables'), find_bin('iptables-legacy')):
|
||||
if not candidate:
|
||||
continue
|
||||
rp = os.path.realpath(candidate)
|
||||
if rp in seen:
|
||||
continue
|
||||
seen.add(rp)
|
||||
try:
|
||||
while subprocess.run([candidate, '-w', '5', '-D', 'FORWARD', '-j', 'AP_NAT_FORWARD'], capture_output=True).returncode == 0:
|
||||
pass
|
||||
subprocess.run([candidate, '-w', '5', '-F', 'AP_NAT_FORWARD'], capture_output=True)
|
||||
subprocess.run([candidate, '-w', '5', '-X', 'AP_NAT_FORWARD'], capture_output=True)
|
||||
while subprocess.run([candidate, '-w', '5', '-t', 'nat', '-D', 'POSTROUTING', '-j', 'AP_NAT_POST'], capture_output=True).returncode == 0:
|
||||
pass
|
||||
subprocess.run([candidate, '-w', '5', '-t', 'nat', '-F', 'AP_NAT_POST'], capture_output=True)
|
||||
subprocess.run([candidate, '-w', '5', '-t', 'nat', '-X', 'AP_NAT_POST'], capture_output=True)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def setup_bridge(bridge_name='br0', bridge_eth=None):
|
||||
teardown_ap_nat()
|
||||
bridge_name = bridge_name or 'br0'
|
||||
bridge_path = f'/sys/class/net/{bridge_name}'
|
||||
|
||||
@@ -4461,6 +4575,10 @@ def configure_ap(data, initial=False, caller_ip=None):
|
||||
r = subprocess.run(['nmcli', 'connection', 'up', name], text=True, capture_output=True)
|
||||
if r.returncode:
|
||||
raise RuntimeError(r.stderr.strip() or 'Impossible de démarrer le point d’accès')
|
||||
if mode == 'bridge':
|
||||
teardown_ap_nat()
|
||||
else:
|
||||
setup_ap_nat(INTERFACE)
|
||||
mark_ap_started()
|
||||
invalidate_status_cache()
|
||||
log('création' if initial else 'enregistrement_modifie', f"Réseau Wi-Fi {ssid} ({'Mode Pont' if mode == 'bridge' else 'Mode AP'})", ip=caller_ip or 'Non attribuée', mac='AP')
|
||||
@@ -7919,8 +8037,14 @@ class Handler(BaseHTTPRequestHandler):
|
||||
r=subprocess.run(cmd,capture_output=True,text=True)
|
||||
if action=='restart': r=subprocess.run(['nmcli','connection','up',name],capture_output=True,text=True)
|
||||
if r.returncode: raise RuntimeError(r.stderr.strip())
|
||||
if action in ('start','restart'): mark_ap_started()
|
||||
if action in ('start','restart'):
|
||||
if not is_bridge_mode():
|
||||
setup_ap_nat(INTERFACE)
|
||||
else:
|
||||
teardown_ap_nat()
|
||||
mark_ap_started()
|
||||
elif action == 'stop':
|
||||
teardown_ap_nat()
|
||||
try: os.remove(UPTIME_FILE)
|
||||
except OSError: pass
|
||||
invalidate_status_cache()
|
||||
@@ -8165,6 +8289,7 @@ def monitor():
|
||||
if sync_vpn_counter >= 5:
|
||||
sync_vpn_counter = 0
|
||||
try:
|
||||
ensure_ap_nat_active()
|
||||
sync_vpn_client_rules()
|
||||
except Exception:
|
||||
pass
|
||||
@@ -8263,6 +8388,8 @@ if __name__ == '__main__':
|
||||
else:
|
||||
# En mode AP (par défaut) ou tant que le setup n'est pas terminé, s'assurer que l'Ethernet hôte est totalement libre et le pont démonté
|
||||
teardown_bridge(b_name, b_eth)
|
||||
if not is_bridge_mode() and auth_state().get('setup_completed') and is_iface_up(INTERFACE):
|
||||
setup_ap_nat(INTERFACE)
|
||||
apply_radio_defaults()
|
||||
try: sync_client_bandwidth_and_quotas(force=True)
|
||||
except Exception: pass
|
||||
|
||||
@@ -107,6 +107,15 @@ iptables -X AP_VPN_INPUT 2>/dev/null || true
|
||||
while iptables -D FORWARD -j AP_VPN_ISOLATION 2>/dev/null; do :; done
|
||||
iptables -F AP_VPN_ISOLATION 2>/dev/null || true
|
||||
iptables -X AP_VPN_ISOLATION 2>/dev/null || true
|
||||
for _ipt in iptables iptables-legacy; do
|
||||
command -v "$_ipt" >/dev/null 2>&1 || continue
|
||||
while "$_ipt" -D FORWARD -j AP_NAT_FORWARD 2>/dev/null; do :; done
|
||||
"$_ipt" -F AP_NAT_FORWARD 2>/dev/null || true
|
||||
"$_ipt" -X AP_NAT_FORWARD 2>/dev/null || true
|
||||
while "$_ipt" -t nat -D POSTROUTING -j AP_NAT_POST 2>/dev/null; do :; done
|
||||
"$_ipt" -t nat -F AP_NAT_POST 2>/dev/null || true
|
||||
"$_ipt" -t nat -X AP_NAT_POST 2>/dev/null || true
|
||||
done
|
||||
|
||||
# Suppression des anciens profils NetworkManager résiduels créés par APmanageur
|
||||
rm -f /etc/NetworkManager/system-connections/ap-bridge-*.nmconnection 2>/dev/null || true
|
||||
@@ -357,6 +366,15 @@ cleanup() {
|
||||
while iptables -D FORWARD -j AP_VPN_ISOLATION 2>/dev/null; do :; done
|
||||
iptables -F AP_VPN_ISOLATION 2>/dev/null || true
|
||||
iptables -X AP_VPN_ISOLATION 2>/dev/null || true
|
||||
for _ipt in iptables iptables-legacy; do
|
||||
command -v "$_ipt" >/dev/null 2>&1 || continue
|
||||
while "$_ipt" -D FORWARD -j AP_NAT_FORWARD 2>/dev/null; do :; done
|
||||
"$_ipt" -F AP_NAT_FORWARD 2>/dev/null || true
|
||||
"$_ipt" -X AP_NAT_FORWARD 2>/dev/null || true
|
||||
while "$_ipt" -t nat -D POSTROUTING -j AP_NAT_POST 2>/dev/null; do :; done
|
||||
"$_ipt" -t nat -F AP_NAT_POST 2>/dev/null || true
|
||||
"$_ipt" -t nat -X AP_NAT_POST 2>/dev/null || true
|
||||
done
|
||||
kill "$WEB_PID" "$API_PID" "$NM_PID" 2>/dev/null || true
|
||||
exit 0
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user