secubox-deb/packages/secubox-proxypac/proxypac/catalog.py
CyberMind ff8ba36ff0
Some checks are pending
License Headers / check (push) Waiting to run
feat(proxypac): WPAD/PAC auto-config routing to active mesh services (Closes #784) (#788)
* docs(spec): secubox-proxypac WPAD/PAC mesh-service routing design (ref #784)

* docs(plan): secubox-proxypac 11-task TDD implementation plan (ref #784)

* feat(annuaire): optional byte-stable pac descriptor on ServiceOffer (ref #784)

* feat(proxypac): PAC template + fail-open directive builder (ref #784)

* feat(proxypac): rules.d parsing + precedence compose (ref #784)

* feat(proxypac): p2p /services catalog reader → routing rules (ref #784)

* feat(proxypac): generator + atomic fail-safe write + CLI (ref #784)

* fix(proxypac): shadow file is <pac>.shadow not <base>.shadow (ref #784)

* feat(proxypac): WebUI API override CRUD + candidates + audit (ref #784)

* fix(proxypac): regen honors RULES_DIR + reject whitespace in override host (ref #784)

* feat(proxypac): nginx PAC/WPAD serving (LAN-only) + seed rule (ref #784)

* fix(proxypac): gate /proxy.pac to LAN/mesh (never public) (ref #784)

* feat(proxypac): regen path+timer units + DHCP 252 WPAD advertise (ref #784)

* feat(proxypac): WebUI override panel + menu entry (ref #784)

* fix(proxypac): correct menu schema (name/category) + escape panel output (ref #784)

* feat(proxypac): debian packaging (ref #784)

* fix(proxypac): drop duplicate debian/compat + correct secubox user creation (ref #784)

* docs(proxypac): README + deploy notes (ref #784)

* fix(proxypac): apply final-review follow-ups (ref #784)

- compose: overrides now last-file-wins per host, so an operator's later
  50-webui.rules override beats a shipped 00-onion.rules seed for the same glob
  (explicit policy > defaults); cross-source override>service>toolbox unchanged.
  New test test_compose_override_last_file_wins.
- packaging: declare Depends: secubox-hub — the /proxy.pac allow/deny gate matches
  $remote_addr, which behind HAProxy is 127.0.0.1 unless secubox-hub's real_ip
  (X-Forwarded-For) rewrite is loaded; without it the PAC would be world-readable.
  nginx/proxypac.conf comments the dependency; test asserts it in control.
- README: document last-file-wins precedence + the real_ip/secubox-hub dependency.

---------

Co-authored-by: CyberMind-FR <gandalf@Gk2.net>
2026-07-03 11:57:26 +02:00

57 lines
1.9 KiB
Python

# SPDX-License-Identifier: LicenseRef-CMSD-1.0
# Copyright (c) 2026 CyberMind — Gérald Kerma <devel@cybermind.fr>
# Source-Disclosed License — All rights reserved except as expressly granted.
# See LICENCE-CMSD-1.0.md for terms.
"""SecuBox-Deb :: proxypac.catalog — read p2p /services, map to service Rules."""
import json
import socket
from urllib.parse import urlparse
from .pac_template import directive
from .rules import Rule
def _endpoint_host(endpoint):
return urlparse(endpoint or "").hostname or ""
def service_rules(services):
"""Map active services carrying a `pac` descriptor to routing Rules."""
out = []
for s in services:
if not s.get("enabled"):
continue
pac = s.get("pac")
if not pac:
continue
proxy = pac.get("proxy", "direct")
host = _endpoint_host(s.get("endpoint"))
if proxy == "socks5":
port = (s.get("macro") or {}).get("params", {}).get("socks_port", 9050)
addr = f"{host}:{port}"
else: # http | gateway use the endpoint host as-is
addr = host
d = directive(proxy, addr)
for m in pac.get("match", []):
out.append(Rule(m, d, f"service:{s['service_id']}"))
return out
def read_services(sock="/run/secubox/p2p.sock"):
"""GET /services over the p2p unix socket. Returns the services list (fail-open: [])."""
try:
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as c:
c.settimeout(5)
c.connect(sock)
c.sendall(b"GET /services HTTP/1.0\r\nHost: x\r\n\r\n")
buf = b""
while True:
chunk = c.recv(65536)
if not chunk:
break
buf += chunk
body = buf.split(b"\r\n\r\n", 1)[1]
return json.loads(body).get("services", [])
except Exception:
return []