diff --git a/ap.py b/ap.py index dda5ac2..1ed1331 100644 --- a/ap.py +++ b/ap.py @@ -7527,15 +7527,18 @@ def format_vpn_bytes(b): if b < 1099511627776: return f"{b / 1073741824:.2f} Go" return f"{b / 1099511627776:.2f} To" -def prepare_wireguard_config_text(raw_config, table_id): +def prepare_wireguard_config_text(raw_config, table_id=None): content = raw_config.replace('\r\n', '\n').replace('\r', '\n').strip() content = re.sub(r"^\s*DNS\s*=.*$", r"# \g<0> (DNS géré localement)", content, flags=re.MULTILINE|re.IGNORECASE) # Neutraliser ListenPort pour attribuer un port dynamique par tunnel et éviter les conflits d'adresses content = re.sub(r"^\s*ListenPort\s*=.*$", r"# \g<0> (Port dynamique)", content, flags=re.MULTILINE|re.IGNORECASE) + # Désactiver la gestion automatique des routes par wg-quick (Table = off) + # Cela évite que wg-quick échoue avec 'RTNETLINK answers: File exists' lors d'un 'route add' + # AP Manageur applique lui-même les routes de façon sécurisée et idempotente avec 'ip route replace' if re.search(r"^\s*Table\s*=", content, re.MULTILINE|re.IGNORECASE): - content = re.sub(r"^\s*Table\s*=.*$", f"Table = {table_id}", content, flags=re.MULTILINE|re.IGNORECASE) + content = re.sub(r"^\s*Table\s*=.*$", "Table = off", content, flags=re.MULTILINE|re.IGNORECASE) else: - content = re.sub(r"(\[Interface\])", f"\\1\nTable = {table_id}", content, flags=re.IGNORECASE, count=1) + content = re.sub(r"(\[Interface\])", "\\1\nTable = off", content, flags=re.IGNORECASE, count=1) return content def prepare_openvpn_config_text(raw_config, script_path, auth_path=None): @@ -7700,12 +7703,16 @@ def connect_vpn_profile(pid, caller_ip=''): if not os.path.exists('/sys/module/wireguard'): subprocess.run(['modprobe', 'wireguard'], capture_output=True) - # 2. Nettoyage préventif de l'interface et de la table PBR + # 2. Nettoyage préventif de l'interface et de la table PBR (IPv4 et IPv6) subprocess.run(['wg-quick', 'down', iface], capture_output=True) subprocess.run(['ip', 'link', 'delete', 'dev', iface], capture_output=True) - subprocess.run(['ip', 'route', 'flush', 'table', str(table_id)], capture_output=True) + subprocess.run(['ip', '-4', 'route', 'flush', 'table', str(table_id)], capture_output=True) + subprocess.run(['ip', '-6', 'route', 'flush', 'table', str(table_id)], capture_output=True) for _ in range(5): - r_del = subprocess.run(['ip', 'rule', 'del', 'table', str(table_id)], capture_output=True) + r_del = subprocess.run(['ip', '-4', 'rule', 'del', 'table', str(table_id)], capture_output=True) + if r_del.returncode != 0: break + for _ in range(5): + r_del = subprocess.run(['ip', '-6', 'rule', 'del', 'table', str(table_id)], capture_output=True) if r_del.returncode != 0: break # 3. Pré-résolution DNS de l'Endpoint WireGuard si c'est un nom de domaine @@ -7735,8 +7742,21 @@ def connect_vpn_profile(pid, caller_ip=''): c.execute('UPDATE vpn_profiles SET status="error", last_error=? WHERE id=?', (err_msg, pid)) c.commit(); c.close() return False, f"Échec de connexion WireGuard : {err_msg}" - # Route par défaut garantie dans la table PBR - subprocess.run(['ip', 'route', 'replace', 'default', 'dev', iface, 'table', str(table_id)], capture_output=True) + # Routes garanties dans la table PBR (IPv4 et IPv6 avec 'replace' pour éviter tout File exists) + allowed_lines = re.findall(r"^\s*AllowedIPs\s*=\s*(.+)$", prof['config_content'], flags=re.MULTILINE|re.IGNORECASE) + has_custom_routes = False + for a_line in allowed_lines: + for cidr in a_line.split(','): + cidr = cidr.strip() + if not cidr or cidr.startswith('#'): continue + has_custom_routes = True + if ':' in cidr: + subprocess.run(['ip', '-6', 'route', 'replace', cidr, 'dev', iface, 'table', str(table_id)], capture_output=True) + else: + subprocess.run(['ip', '-4', 'route', 'replace', cidr, 'dev', iface, 'table', str(table_id)], capture_output=True) + if not has_custom_routes: + subprocess.run(['ip', '-4', 'route', 'replace', 'default', 'dev', iface, 'table', str(table_id)], capture_output=True) + subprocess.run(['ip', '-6', 'route', 'replace', 'default', 'dev', iface, 'table', str(table_id)], capture_output=True) elif vtype == 'openvpn': pid_file = f"{VPN_RUN_DIR}/{iface}.pid" if os.path.exists(pid_file): @@ -7901,16 +7921,25 @@ def disconnect_vpn_profile(pid, caller_ip=''): subprocess.run(['iptables', '-D', 'FORWARD', '-o', iface, '-j', 'ACCEPT'], capture_output=True) subprocess.run(['iptables', '-D', 'FORWARD', '-i', iface, '-m', 'state', '--state', 'RELATED,ESTABLISHED', '-j', 'ACCEPT'], capture_output=True) subprocess.run(['iptables', '-t', 'mangle', '-D', 'FORWARD', '-p', 'tcp', '--tcp-flags', 'SYN,RST', 'SYN', '-o', iface, '-j', 'TCPMSS', '--clamp-mss-to-pmtu'], capture_output=True) - subprocess.run(['ip', 'route', 'flush', 'table', str(table_id)], capture_output=True) + subprocess.run(['ip', '-4', 'route', 'flush', 'table', str(table_id)], capture_output=True) + subprocess.run(['ip', '-6', 'route', 'flush', 'table', str(table_id)], capture_output=True) # Nettoyage règles locales et cache IP vpn_public_ip_cache.pop(iface, None) - subprocess.run(['ip', 'rule', 'del', 'oif', iface, 'lookup', str(table_id)], capture_output=True) + subprocess.run(['ip', '-4', 'rule', 'del', 'oif', iface, 'lookup', str(table_id)], capture_output=True) + subprocess.run(['ip', '-6', 'rule', 'del', 'oif', iface, 'lookup', str(table_id)], capture_output=True) try: r_ip = subprocess.run(['ip', '-4', '-o', 'addr', 'show', 'dev', iface], capture_output=True, text=True) if r_ip.returncode == 0: m_ip = re.search(r'inet\s+(\d+\.\d+\.\d+\.\d+)', r_ip.stdout) if m_ip: - subprocess.run(['ip', 'rule', 'del', 'from', m_ip.group(1), 'lookup', str(table_id)], capture_output=True) + subprocess.run(['ip', '-4', 'rule', 'del', 'from', m_ip.group(1), 'lookup', str(table_id)], capture_output=True) + except Exception: pass + try: + r_ip6 = subprocess.run(['ip', '-6', '-o', 'addr', 'show', 'dev', iface], capture_output=True, text=True) + if r_ip6.returncode == 0: + m_ip6 = re.search(r'inet6\s+([0-9a-fA-F:]+)', r_ip6.stdout) + if m_ip6: + subprocess.run(['ip', '-6', 'rule', 'del', 'from', m_ip6.group(1), 'lookup', str(table_id)], capture_output=True) except Exception: pass c = db() c.execute('UPDATE vpn_profiles SET status="disconnected", last_error="" WHERE id=?', (pid,)) @@ -8561,7 +8590,8 @@ def sync_vpn_client_rules(): # 9. S'assurer que chaque interface VPN connectée a sa route default dans sa table et masquerade dans iptables for pid, prof in profiles.items(): if prof['status'] == 'connected' and is_iface_up(prof['iface']): - subprocess.run(['ip', 'route', 'replace', 'default', 'dev', prof['iface'], 'table', str(prof['table_id'])], capture_output=True) + subprocess.run(['ip', '-4', 'route', 'replace', 'default', 'dev', prof['iface'], 'table', str(prof['table_id'])], capture_output=True) + subprocess.run(['ip', '-6', 'route', 'replace', 'default', 'dev', prof['iface'], 'table', str(prof['table_id'])], capture_output=True) subprocess.run(['iptables', '-t', 'nat', '-I', 'POSTROUTING', '1', '-o', prof['iface'], '-j', 'MASQUERADE'], capture_output=True) subprocess.run(['iptables', '-I', 'FORWARD', '1', '-o', prof['iface'], '-j', 'ACCEPT'], capture_output=True) subprocess.run(['iptables', '-I', 'FORWARD', '1', '-i', prof['iface'], '-j', 'ACCEPT'], capture_output=True) diff --git a/index.html b/index.html index 3485dda..432c304 100644 --- a/index.html +++ b/index.html @@ -2376,8 +2376,7 @@ Administration de l’AP Wi‑Fi
AP Manageur V1.2 - (Bêta) + style="font-weight:700;background:var(--panel2);padding:2px 8px;border-radius:6px;border:1px solid var(--line);color:var(--blue);font-size:11px;margin-left:4px">V1.2 (Bêta)
@@ -2572,8 +2571,7 @@

Administration de l’AP Wi‑Fi

AP Manageur V1.2 - (Bêta) + style="font-weight:700;background:var(--panel2);padding:2px 8px;border-radius:6px;border:1px solid var(--line);color:var(--blue);font-size:12px;margin-left:4px">V1.2 (Bêta)
diff --git a/version.json b/version.json index fce6071..4ceed8d 100644 --- a/version.json +++ b/version.json @@ -1,5 +1,5 @@ { "version": "V1.2 (Bêta)", - "commit": "e2eaea4b46072d2b82848fa222538bbd0ad35447", - "build_date": "2026-11-22T23:21:55Z" + "commit": "165a06be2256a6fbcf35e8ff6e1f04f085586459", + "build_date": "2026-11-23T00:24:08Z" } \ No newline at end of file