AP manageur V1.1 (Bêta)
This commit is contained in:
1 parent
ef6961d756
commit
25934624ea
1 file changed
+47
-15
@@ -3961,8 +3961,8 @@ def ensure_ap_nat_active(iface=None):
|
|||||||
ipt = find_bin('iptables')
|
ipt = find_bin('iptables')
|
||||||
if not ipt:
|
if not ipt:
|
||||||
return
|
return
|
||||||
r_fwd = subprocess.run([ipt, '-w', '2', '-C', 'FORWARD', '-j', 'AP_NAT_FORWARD'], capture_output=True, timeout=2)
|
r_fwd = subprocess.run([ipt, '-w', '1', '-C', 'FORWARD', '-j', 'AP_NAT_FORWARD'], capture_output=True, timeout=3)
|
||||||
r_nat = subprocess.run([ipt, '-w', '2', '-t', 'nat', '-C', 'POSTROUTING', '-j', 'AP_NAT_POST'], capture_output=True, timeout=2)
|
r_nat = subprocess.run([ipt, '-w', '1', '-t', 'nat', '-C', 'POSTROUTING', '-j', 'AP_NAT_POST'], capture_output=True, timeout=3)
|
||||||
if r_fwd.returncode != 0 or r_nat.returncode != 0:
|
if r_fwd.returncode != 0 or r_nat.returncode != 0:
|
||||||
setup_ap_nat(iface)
|
setup_ap_nat(iface)
|
||||||
except Exception:
|
except Exception:
|
||||||
@@ -4386,33 +4386,46 @@ def setup_bridge(bridge_name='br0', bridge_eth=None):
|
|||||||
# Si le pont n'existe pas encore au niveau noyau, le créer directement via iproute2 (sans profil NetworkManager)
|
# Si le pont n'existe pas encore au niveau noyau, le créer directement via iproute2 (sans profil NetworkManager)
|
||||||
if not bridge_already_existed:
|
if not bridge_already_existed:
|
||||||
print(f"[ap-bridge] Création sécurisée du pont noyau {bridge_name}...", flush=True)
|
print(f"[ap-bridge] Création sécurisée du pont noyau {bridge_name}...", flush=True)
|
||||||
|
try:
|
||||||
subprocess.run(['ip', 'link', 'add', 'name', bridge_name, 'type', 'bridge', 'forward_delay', '0', 'stp_state', '0'], capture_output=True, timeout=2)
|
subprocess.run(['ip', 'link', 'add', 'name', bridge_name, 'type', 'bridge', 'forward_delay', '0', 'stp_state', '0'], capture_output=True, timeout=2)
|
||||||
|
except Exception: pass
|
||||||
try: open(CREATED_BRIDGE_FILE, 'w').write(f"{bridge_name}:{bridge_eth or ''}")
|
try: open(CREATED_BRIDGE_FILE, 'w').write(f"{bridge_name}:{bridge_eth or ''}")
|
||||||
except OSError: pass
|
except OSError: pass
|
||||||
else:
|
else:
|
||||||
# S'assurer que le pont transmet immédiatement les paquets sans délai STP (forward_delay 0)
|
# S'assurer que le pont transmet immédiatement les paquets sans délai STP (forward_delay 0)
|
||||||
|
try:
|
||||||
subprocess.run(['ip', 'link', 'set', 'dev', bridge_name, 'type', 'bridge', 'forward_delay', '0', 'stp_state', '0'], capture_output=True, timeout=2)
|
subprocess.run(['ip', 'link', 'set', 'dev', bridge_name, 'type', 'bridge', 'forward_delay', '0', 'stp_state', '0'], capture_output=True, timeout=2)
|
||||||
|
except Exception: pass
|
||||||
|
|
||||||
# Asservir l'Ethernet physique au pont pour relier le Wi-Fi au réseau local et au DHCP de la box
|
# Asservir l'Ethernet physique au pont pour relier le Wi-Fi au réseau local et au DHCP de la box
|
||||||
migrated_gw = None
|
migrated_gw = None
|
||||||
has_bridge_ip = bool(get_iface_ip_info(bridge_name)[0])
|
has_bridge_ip = bool(get_iface_ip_info(bridge_name)[0])
|
||||||
if bridge_eth and os.path.exists(f'/sys/class/net/{bridge_eth}'):
|
if bridge_eth and os.path.exists(f'/sys/class/net/{bridge_eth}'):
|
||||||
|
try:
|
||||||
subprocess.run(['ip', 'link', 'set', 'dev', bridge_eth, 'up'], capture_output=True, timeout=2)
|
subprocess.run(['ip', 'link', 'set', 'dev', bridge_eth, 'up'], capture_output=True, timeout=2)
|
||||||
|
except Exception: pass
|
||||||
if eth_master != bridge_name:
|
if eth_master != bridge_name:
|
||||||
ip_cidr, gw, mac = get_iface_ip_info(bridge_eth)
|
ip_cidr, gw, mac = get_iface_ip_info(bridge_eth)
|
||||||
migrated_gw = gw
|
migrated_gw = gw
|
||||||
if mac and not has_bridge_ip:
|
if mac and not has_bridge_ip:
|
||||||
subprocess.run(['ip', 'link', 'set', 'dev', bridge_name, 'address', mac], capture_output=True, timeout=2)
|
try: subprocess.run(['ip', 'link', 'set', 'dev', bridge_name, 'address', mac], capture_output=True, timeout=2)
|
||||||
subprocess.run(['ip', 'link', 'set', 'dev', bridge_name, 'up'], capture_output=True, timeout=2)
|
except Exception: pass
|
||||||
subprocess.run(['ip', 'link', 'set', 'dev', bridge_eth, 'master', bridge_name], capture_output=True, timeout=2)
|
try: subprocess.run(['ip', 'link', 'set', 'dev', bridge_name, 'up'], capture_output=True, timeout=2)
|
||||||
|
except Exception: pass
|
||||||
|
try: subprocess.run(['ip', 'link', 'set', 'dev', bridge_eth, 'master', bridge_name], capture_output=True, timeout=2)
|
||||||
|
except Exception: pass
|
||||||
if ip_cidr and not has_bridge_ip:
|
if ip_cidr and not has_bridge_ip:
|
||||||
# Ajouter l'IP au pont AVANT de flusher bridge_eth pour ne jamais couper les connexions TCP
|
# Ajouter l'IP au pont AVANT de flusher bridge_eth pour ne jamais couper les connexions TCP
|
||||||
subprocess.run(['ip', 'addr', 'add', ip_cidr, 'dev', bridge_name], capture_output=True, timeout=2)
|
try: subprocess.run(['ip', 'addr', 'add', ip_cidr, 'dev', bridge_name], capture_output=True, timeout=2)
|
||||||
|
except Exception: pass
|
||||||
if gw:
|
if gw:
|
||||||
subprocess.run(['ip', 'route', 'replace', 'default', 'via', gw, 'dev', bridge_name], capture_output=True, timeout=2)
|
try: subprocess.run(['ip', 'route', 'replace', 'default', 'via', gw, 'dev', bridge_name], capture_output=True, timeout=2)
|
||||||
|
except Exception: pass
|
||||||
clean_ip = ip_cidr.split('/')[0]
|
clean_ip = ip_cidr.split('/')[0]
|
||||||
subprocess.run(['arping', '-c', '1', '-A', '-I', bridge_name, clean_ip], capture_output=True, timeout=2)
|
try: subprocess.run(['arping', '-c', '1', '-A', '-I', bridge_name, clean_ip], capture_output=True, timeout=2)
|
||||||
subprocess.run(['ip', 'addr', 'flush', 'dev', bridge_eth], capture_output=True, timeout=2)
|
except Exception: pass
|
||||||
|
try: subprocess.run(['ip', 'addr', 'flush', 'dev', bridge_eth], capture_output=True, timeout=2)
|
||||||
|
except Exception: pass
|
||||||
print(f"[ap-bridge] Ethernet {bridge_eth} asservi à {bridge_name}. IP {ip_cidr} et passerelle {gw} migrées sur {bridge_name} (Ping préservé).", flush=True)
|
print(f"[ap-bridge] Ethernet {bridge_eth} asservi à {bridge_name}. IP {ip_cidr} et passerelle {gw} migrées sur {bridge_name} (Ping préservé).", flush=True)
|
||||||
else:
|
else:
|
||||||
print(f"[ap-bridge] Ethernet {bridge_eth} asservi à {bridge_name}.", flush=True)
|
print(f"[ap-bridge] Ethernet {bridge_eth} asservi à {bridge_name}.", flush=True)
|
||||||
@@ -4422,24 +4435,43 @@ def setup_bridge(bridge_name='br0', bridge_eth=None):
|
|||||||
cur_mac = open(f'{bridge_path}/address').read().strip()
|
cur_mac = open(f'{bridge_path}/address').read().strip()
|
||||||
assign_type = open(f'{bridge_path}/addr_assign_type').read().strip()
|
assign_type = open(f'{bridge_path}/addr_assign_type').read().strip()
|
||||||
if cur_mac and assign_type != '3':
|
if cur_mac and assign_type != '3':
|
||||||
subprocess.run(['ip', 'link', 'set', 'dev', bridge_name, 'address', cur_mac], capture_output=True, timeout=2)
|
try: subprocess.run(['ip', 'link', 'set', 'dev', bridge_name, 'address', cur_mac], capture_output=True, timeout=2)
|
||||||
|
except Exception: pass
|
||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if not is_iface_up(bridge_name):
|
if not is_iface_up(bridge_name):
|
||||||
subprocess.run(['ip', 'link', 'set', 'dev', bridge_name, 'up'], capture_output=True, timeout=2)
|
try: subprocess.run(['ip', 'link', 'set', 'dev', bridge_name, 'up'], capture_output=True, timeout=2)
|
||||||
|
except Exception: pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
subprocess.run(['sysctl', '-w', 'net.ipv4.ip_forward=1'], capture_output=True, timeout=2)
|
subprocess.run(['sysctl', '-w', 'net.ipv4.ip_forward=1'], capture_output=True, timeout=2)
|
||||||
except Exception: pass
|
except Exception: pass
|
||||||
|
try:
|
||||||
clean_stale_router_leases()
|
clean_stale_router_leases()
|
||||||
|
except Exception: pass
|
||||||
|
|
||||||
|
try:
|
||||||
for ipt in _ap_iptables_bins():
|
for ipt in _ap_iptables_bins():
|
||||||
if subprocess.run([ipt, '-w', '2', '-C', 'FORWARD', '-i', bridge_name, '-j', 'ACCEPT'], capture_output=True, timeout=2).returncode != 0:
|
try:
|
||||||
subprocess.run([ipt, '-w', '2', '-I', 'FORWARD', '1', '-i', bridge_name, '-j', 'ACCEPT'], capture_output=True, timeout=2)
|
r1 = subprocess.run([ipt, '-w', '1', '-C', 'FORWARD', '-i', bridge_name, '-j', 'ACCEPT'], capture_output=True, timeout=3)
|
||||||
if subprocess.run([ipt, '-w', '2', '-C', 'FORWARD', '-o', bridge_name, '-j', 'ACCEPT'], capture_output=True, timeout=2).returncode != 0:
|
if r1.returncode != 0:
|
||||||
subprocess.run([ipt, '-w', '2', '-I', 'FORWARD', '1', '-o', bridge_name, '-j', 'ACCEPT'], capture_output=True, timeout=2)
|
subprocess.run([ipt, '-w', '1', '-I', 'FORWARD', '1', '-i', bridge_name, '-j', 'ACCEPT'], capture_output=True, timeout=3)
|
||||||
|
except Exception: pass
|
||||||
|
try:
|
||||||
|
r2 = subprocess.run([ipt, '-w', '1', '-C', 'FORWARD', '-o', bridge_name, '-j', 'ACCEPT'], capture_output=True, timeout=3)
|
||||||
|
if r2.returncode != 0:
|
||||||
|
subprocess.run([ipt, '-w', '1', '-I', 'FORWARD', '1', '-o', bridge_name, '-j', 'ACCEPT'], capture_output=True, timeout=3)
|
||||||
|
except Exception: pass
|
||||||
|
except Exception: pass
|
||||||
|
|
||||||
|
try:
|
||||||
setup_bridge_dns_redirection(bridge_name, INTERFACE)
|
setup_bridge_dns_redirection(bridge_name, INTERFACE)
|
||||||
|
except Exception: pass
|
||||||
|
|
||||||
|
try:
|
||||||
sync_host_resolved_bridge_dns(bridge_name, migrated_gw)
|
sync_host_resolved_bridge_dns(bridge_name, migrated_gw)
|
||||||
|
except Exception: pass
|
||||||
|
|
||||||
def teardown_bridge(bridge_name='br0', bridge_eth=None):
|
def teardown_bridge(bridge_name='br0', bridge_eth=None):
|
||||||
stop_hostapd(INTERFACE)
|
stop_hostapd(INTERFACE)
|
||||||
|
|||||||
Reference in new issue
Block a user