Files
Shadowbroker/backend/services/wormhole_settings.py
BigBodyCobainandClaude Opus 5.5 8c7aca8810 Keep Tor as the backbone; add opt-in maximum privacy and a Tor-down choice
With Reticulum on, DMs and gate posts used to wait until both Tor and
Reticulum were ready. Now:

- Normal mode needs Tor only. DMs keep going through the Tor relay, gate
  posts publish over Tor first, and Reticulum joins in when it is up.
- Maximum privacy (off by default, labelled slow) sends DMs and gate posts
  only over Reticulum inside Tor and waits while either is down. It never
  falls back to the relay, and this node's own gate posts are not served
  over the Tor peer push or pull, so no peer can tell which node wrote
  them.
- When Tor is down, queued DMs and gate posts ask right away whether to
  send over a direct Reticulum link, saying what that gives up (the hub
  sees your IP address; watchers see you use Shadowbroker; content stays
  end-to-end encrypted and the recipient still cannot see your IP).
  "Send over Reticulum" covers everything already waiting. "Don't ask me
  again" asks "Are you sure?" first, then makes the direct link the
  automatic backup. The direct link opens only after that consent and
  closes as soon as Tor is back.
- Settings has the maximum-privacy switch and a way to be asked again.
- A hub node also offers its hub port on its onion address.
- Only the primary backend runs Reticulum, and Tor-tier checks read its
  status in-process, so one node does not announce two identities.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 23:34:51 -07:00

116 lines
4.2 KiB
Python

from __future__ import annotations
import json
import time
from pathlib import Path
DATA_DIR = Path(__file__).parent.parent / "data"
WORMHOLE_FILE = DATA_DIR / "wormhole.json"
_cache: dict | None = None
_cache_ts: float = 0.0
_CACHE_TTL = 5.0 # seconds
_DEFAULTS = {
"enabled": False,
"transport": "direct",
"socks_proxy": "",
"socks_dns": True,
"privacy_profile": "default",
"anonymous_mode": False,
# Maximum privacy: DMs and gate posts go only over Reticulum inside Tor
# and wait while either is down. Slower; off by default.
"max_privacy": False,
# When Tor is down: "ask" before sending over a direct Reticulum link,
# or "always" after the user said not to ask again (and confirmed).
"rns_direct_fallback": "ask",
}
_RNS_DIRECT_FALLBACK_VALUES = {"ask", "always"}
def _fallback_value(value) -> str:
text = str(value or "").strip().lower()
return text if text in _RNS_DIRECT_FALLBACK_VALUES else _DEFAULTS["rns_direct_fallback"]
def _safe_int(val, default=0) -> int:
try:
return int(val)
except (TypeError, ValueError):
return default
def read_wormhole_settings() -> dict:
global _cache, _cache_ts
now = time.monotonic()
if _cache is not None and (now - _cache_ts) < _CACHE_TTL:
return _cache
if not WORMHOLE_FILE.exists():
result = {**_DEFAULTS, "updated_at": 0}
else:
try:
data = json.loads(WORMHOLE_FILE.read_text(encoding="utf-8"))
except Exception:
result = {**_DEFAULTS, "updated_at": 0}
else:
result = {
"enabled": bool(data.get("enabled", _DEFAULTS["enabled"])),
"transport": str(data.get("transport", _DEFAULTS["transport"]) or _DEFAULTS["transport"]),
"socks_proxy": str(data.get("socks_proxy", _DEFAULTS["socks_proxy"]) or ""),
"socks_dns": bool(data.get("socks_dns", _DEFAULTS["socks_dns"])),
"privacy_profile": str(
data.get("privacy_profile", _DEFAULTS["privacy_profile"]) or _DEFAULTS["privacy_profile"]
),
"anonymous_mode": bool(data.get("anonymous_mode", _DEFAULTS["anonymous_mode"])),
"max_privacy": bool(data.get("max_privacy", _DEFAULTS["max_privacy"])),
"rns_direct_fallback": _fallback_value(data.get("rns_direct_fallback")),
"updated_at": _safe_int(data.get("updated_at", 0) or 0),
}
_cache = result
_cache_ts = now
return result
def write_wormhole_settings(
*,
enabled: bool | None = None,
transport: str | None = None,
socks_proxy: str | None = None,
socks_dns: bool | None = None,
privacy_profile: str | None = None,
anonymous_mode: bool | None = None,
max_privacy: bool | None = None,
rns_direct_fallback: str | None = None,
) -> dict:
DATA_DIR.mkdir(parents=True, exist_ok=True)
existing = read_wormhole_settings()
payload = {
"enabled": bool(existing.get("enabled")) if enabled is None else bool(enabled),
"transport": existing.get("transport", _DEFAULTS["transport"])
if transport is None
else str(transport),
"socks_proxy": existing.get("socks_proxy", "")
if socks_proxy is None
else str(socks_proxy),
"socks_dns": bool(existing.get("socks_dns", _DEFAULTS["socks_dns"]))
if socks_dns is None
else bool(socks_dns),
"privacy_profile": existing.get("privacy_profile", _DEFAULTS["privacy_profile"])
if privacy_profile is None
else str(privacy_profile),
"anonymous_mode": bool(existing.get("anonymous_mode", _DEFAULTS["anonymous_mode"]))
if anonymous_mode is None
else bool(anonymous_mode),
"max_privacy": bool(existing.get("max_privacy", _DEFAULTS["max_privacy"]))
if max_privacy is None
else bool(max_privacy),
"rns_direct_fallback": _fallback_value(
existing.get("rns_direct_fallback") if rns_direct_fallback is None else rns_direct_fallback
),
"updated_at": int(time.time()),
}
WORMHOLE_FILE.write_text(json.dumps(payload, indent=2), encoding="utf-8")
global _cache, _cache_ts
_cache = payload
_cache_ts = time.monotonic()
return payload