feat: tor support

This commit is contained in:
FUFSoB committed 2026-09-17 20:12:17 +05:00
1 parent 97332c1774
commit 321e9ca468
42 files changed
+1853 -69

No files matched your search

+89 -9
View File
@@ -29,6 +29,7 @@ ALL_SERVICES = [
"proxy-suite-ssh-proxy",
"proxy-suite-warp",
"proxy-suite-warp-tunnel",
"proxy-suite-tor",
"proxy-suite-tg-ws-proxy",
"proxy-suite-zapret",
"proxy-suite-zapret-vm-exempt",
@@ -40,6 +41,7 @@ RESTART_SERVICES = [
"proxy-suite-inbounds",
"proxy-suite-ssh-proxy",
"proxy-suite-warp-tunnel",
"proxy-suite-tor",
"proxy-suite-tg-ws-proxy",
"proxy-suite-zapret",
]
@@ -101,14 +103,17 @@ Secrets and changes need root, or the userControl group.
ssh [status|on|off] SSH SOCKS5 tunnel
warp [status|on|off] WARP tunnel behind the warp outbound
tor [status|on|off] Tor, behind the tor outbound and the onion service
tor newnym new circuits for new connections
tg [status|on|off] Telegram WebSocket proxy
apps [list] per-app routing profiles
apps run <profile> -- <cmd> [args] run a command through a profile
inbounds [list] server inbounds
inbounds link <tag> [user] [--qr|--json]
client share link, or the client's outbound JSON
inbounds link <tag> [user] [--onion] [--qr|--json]
client share link, or the client's outbound JSON;
--onion: the one through the onion service
inbounds link <tag> [user] --config [--qr]
an AmneziaWG client's .conf, or its QR code
inbounds link <tag> --server-json the server's inbound JSON
@@ -313,6 +318,68 @@ def _warp_unit():
return "proxy-suite-warp-tunnel"
def _tor_control(*commands):
"""Replies to commands on Tor's control socket, one list of lines per command.
The socket authenticates by who can open it: root and the userControl group.
"""
path = env("TOR_CONTROL_SOCKET", f"{runtime_dir()}/proxy-suite-tor/control/socket")
replies = []
try:
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock:
sock.settimeout(5)
sock.connect(path)
stream = sock.makefile("rwb")
for command in ("AUTHENTICATE", *commands):
stream.write(command.encode("ascii") + b"\r\n")
stream.flush()
lines = []
while True:
line = stream.readline()
if not line:
die("Tor closed its control connection.")
line = line.decode("utf-8", "replace").rstrip("\r\n")
lines.append(line)
# "250-" and "250+" continue a reply; "250 " ends it.
if len(line) >= 4 and line[3] == " ":
break
if not lines[-1].startswith("250"):
die(f"Tor refused {command.split()[0]}: {lines[-1]}")
replies.append(lines)
except PermissionError:
denied(path, "open")
except (FileNotFoundError, ConnectionRefusedError):
die("Tor is not running: no control socket.")
except OSError as exc:
die(f"Cannot talk to Tor: {exc}")
return replies[1:]
def _tor_bootstrap():
"""Tor's bootstrap progress, as "100% (Done)"."""
line = _tor_control("GETINFO status/bootstrap-phase")[0][0]
progress = re.search(r"PROGRESS=(\d+)", line)
summary = re.search(r'SUMMARY="((?:[^"\\]|\\.)*)"', line)
if not progress:
return line
return f"{progress.group(1)}%" + (f" ({summary.group(1)})" if summary else "")
def cmd_tor(verb="status", *args):
unit = "proxy-suite-tor"
if verb == "newnym":
if not svc_exists(unit):
die("tor is not enabled in this configuration.")
_tor_control("SIGNAL NEWNYM")
print("New connections take new circuits (Tor allows this once every 10 seconds).")
return
if verb not in ("status", "on", "off"):
usage("tor [status|on|off|newnym]")
_toggle(unit, "tor", verb, *args)
if verb == "status" and svc_active(unit):
print(f"bootstrap {_tor_bootstrap()}")
def _emit(text, qr):
"""Prints text, or its QR code."""
if not qr:
@@ -379,6 +446,7 @@ COMPLETE = {
"awg": "AmneziaWG profiles",
"ssh": "SSH SOCKS5 tunnel",
"warp": "WARP tunnel behind the warp outbound",
"tor": "Tor, behind the tor outbound and the onion service",
"tg": "Telegram WebSocket proxy",
"apps": "per-app routing profiles",
"inbounds": "server inbounds",
@@ -497,6 +565,7 @@ COMPLETE = {
"awg restart": {"args": lambda: _names(_awg_profiles())},
"ssh": {"words": TOGGLE},
"warp": {"words": TOGGLE},
"tor": {"words": {**TOGGLE, "newnym": "new circuits for new connections"}},
"tg": {"words": TOGGLE},
"apps": {"words": {"list": "per-app routing profiles", "run": "run a command through a profile"}},
"apps run": {"args": lambda: {_s(p["name"]): _s(p.get("route") or "") for p in read_json(env("PER_APP_ROUTING_PROFILES_FILE"))}},
@@ -514,6 +583,7 @@ COMPLETE = {
"args": _inbound_link_choices,
"flags": {
"--qr": "print a QR code",
"--onion": "the link through the onion service",
"--json": "the client's outbound JSON",
"--config": "an AmneziaWG client's .conf",
"--server-json": "the server's inbound JSON",
@@ -2775,9 +2845,16 @@ def _inbound_server_json(tag):
print(_json_text(inbound))
def _inbound_link_for(tag, user="", *_, field="link"):
matches = [x for x in _inbound_links() if x.get("tag") == tag and (not user or x.get("user") == user)]
def _inbound_link_for(tag, user="", *_, field="link", variant=""):
"""variant "onion": the link to the listener through the onion service."""
matches = [
x
for x in _inbound_links()
if x.get("tag") == tag and (not user or x.get("user") == user) and (x.get("variant") or "") == variant
]
if not matches:
if variant == "onion":
die(f"No onion link for {tag}: is it in tor.onionService.listeners, and has Tor written its address?")
die(f"Unknown inbound, or no share link for it: {tag}")
if len(matches) > 1:
sys.stdout.flush()
@@ -2973,20 +3050,22 @@ def cmd_inbounds(verb="list", *args):
row = " {:<24} {:<16} {:<14} {:<8} {}"
print(row.format("TAG", "USER", "TYPE", "PORT", "STATE"))
for x in _inbound_links():
print(row.format(*(_s(x.get(k)) for k in ("tag", "user", "type", "port")), state))
kind = _s(x.get("type")) + (" (onion)" if x.get("variant") == "onion" else "")
print(row.format(_s(x.get("tag")), _s(x.get("user")), kind, _s(x.get("port")), state))
elif verb in ("link", "qr"):
rest = [a for a in args if not a.startswith("--")]
if not rest:
usage("inbounds link <tag> [user] [--qr|--json|--config|--server-json]")
usage("inbounds link <tag> [user] [--onion] [--qr|--json|--config|--server-json]")
qr = verb == "qr" or "--qr" in args
variant = "onion" if "--onion" in args else ""
if "--server-json" in args:
_inbound_server_json(rest[0])
elif "--json" in args:
print(_json_text(_inbound_link_for(*rest, field="outbound")))
print(_json_text(_inbound_link_for(*rest, field="outbound", variant=variant)))
elif "--config" in args:
_emit(_inbound_link_for(*rest, field="config").rstrip("\n"), qr)
_emit(_inbound_link_for(*rest, field="config", variant=variant).rstrip("\n"), qr)
else:
_emit(_inbound_link_for(*rest), qr)
_emit(_inbound_link_for(*rest, variant=variant), qr)
elif verb == "stats":
_inbound_stats(*args)
elif verb == "online":
@@ -3030,6 +3109,7 @@ COMMANDS = {
"awg": cmd_awg,
"ssh": lambda *args: _toggle("proxy-suite-ssh-proxy", "ssh", *args),
"warp": lambda *args: _toggle(_warp_unit(), "warp", *args),
"tor": cmd_tor,
"tg": lambda *args: _toggle("proxy-suite-tg-ws-proxy", "tg", *args),
"apps": cmd_apps,
"inbounds": cmd_inbounds,
+9 -2
View File
@@ -135,6 +135,7 @@ TOGGLES = {
# WARP as an AmneziaWG outbound: `proxy-ctl warp`, not `awg`, which only knows global profiles.
"proxy-suite-awg-warp": ["warp"],
"proxy-suite-tg-ws-proxy": ["tg"],
"proxy-suite-tor": ["tor"],
"proxy-suite-zapret": ["zapret"],
}
AWG_PREFIX = ctl._awg_service("")
@@ -303,8 +304,13 @@ def inbound_rows(_):
# No presence column: XRay keys the online map by user alone (no inbound dimension),
# so a per-listener row could only repeat the same verdict once per listener. The
# "who is online" action shows it once per user, which is the shape the data has.
# A listener behind the onion service has a second row, for its .onion link.
return [
{"key": f"{x.get('tag')}/{x.get('user')}", **{k: ctl._s(x.get(k) or "") for k in ("tag", "user", "type", "port")}}
{
"key": "/".join(ctl._s(x.get(k) or "") for k in ("tag", "user", "variant")),
**{k: ctl._s(x.get(k) or "") for k in ("tag", "user", "type", "port", "variant")},
**({"type": f"{ctl._s(x.get('type') or '')} (onion)"} if x.get("variant") == "onion" else {}),
}
for x in ctl._inbound_links()
]
@@ -360,7 +366,8 @@ def _natural(value):
def _link(row, *extra):
return ["inbounds", "link", row["tag"], *([row["user"]] if row["user"] else []), *extra]
onion = ["--onion"] if row.get("variant") == "onion" else []
return ["inbounds", "link", row["tag"], *([row["user"]] if row["user"] else []), *onion, *extra]
def _amneziawg(row):
+76
View File
@@ -9,6 +9,7 @@ import shutil
import socket
import sys
import tempfile
import threading
import unittest
from unittest import mock
@@ -873,6 +874,26 @@ class ShareTest(EnvTest):
self.assertNotEqual(status, 0)
self.assertIn("only AmneziaWG", err)
def test_inbound_onion_link(self):
self.write(
"inbounds/links.json",
[
{"tag": "in", "user": "u", "type": "vless", "link": "vless://x@h:443", "outbound": {"server": "h"}},
{"tag": "in", "user": "u", "type": "vless", "port": 443, "link": "vless://x@o.onion:443", "outbound": {"server": "o.onion"},
"variant": "onion"},
{"tag": "plain", "user": "u", "type": "vless", "link": "vless://x@h:80", "outbound": None},
],
)
# Same tag and user: the plain link unless --onion asks for the other.
self.assertEqual(ok(ctl.cmd_inbounds, "link", "in", "u"), "vless://x@h:443\n")
self.assertEqual(ok(ctl.cmd_inbounds, "link", "in", "--onion"), "vless://x@o.onion:443\n")
self.assertEqual(json.loads(ok(ctl.cmd_inbounds, "link", "in", "u", "--onion", "--json"))["server"], "o.onion")
status, _, err = run(ctl.cmd_inbounds, "link", "plain", "--onion")
self.assertNotEqual(status, 0)
self.assertIn("onionService.listeners", err)
self.patch("svc_state", lambda unit: "active")
self.assertRegex(ok(ctl.cmd_inbounds, "list"), r"(?m)^ in +u +vless \(onion\) +443 +active$")
@unittest.skipIf(os.geteuid() == 0, "root reads anything")
def test_unreadable(self):
os.chmod(self.share, 0)
@@ -881,6 +902,61 @@ class ShareTest(EnvTest):
self.assertIn("re-run with sudo", err)
class TorTest(EnvTest):
"""proxy-ctl tor against a fake control socket that answers like Tor 0.4.9."""
def setUp(self):
super().setUp()
# A relative path: an absolute one under TMPDIR can outgrow sun_path.
cwd = os.getcwd()
os.chdir(self.dir)
self.addCleanup(os.chdir, cwd)
os.environ["TOR_CONTROL_SOCKET"] = "control"
self.received = []
self.server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.server.bind("control")
self.server.listen(1)
self.addCleanup(self.server.close)
self.thread = threading.Thread(target=self.serve, daemon=True)
self.thread.start()
self.patch("systemctl", lambda *a, **kw: (0, "active\n") if kw.get("capture") else (0, None))
def serve(self):
replies = {
"AUTHENTICATE": "250 OK",
"GETINFO status/bootstrap-phase": '250-status/bootstrap-phase=NOTICE BOOTSTRAP PROGRESS=75 TAG=enough_dirinfo '
'SUMMARY="Loaded enough directory info to build circuits"\r\n250 OK',
"SIGNAL NEWNYM": "250 OK",
}
try:
conn, _ = self.server.accept()
except OSError:
return
with conn, conn.makefile("rwb") as stream:
for line in stream:
command = line.decode().rstrip("\r\n")
self.received.append(command)
stream.write((replies.get(command, '510 Unrecognized command "x"') + "\r\n").encode())
stream.flush()
def test_status_shows_bootstrap(self):
out = ok(ctl.cmd_tor)
self.assertIn("bootstrap 75% (Loaded enough directory info to build circuits)", out)
self.assertEqual(self.received, ["AUTHENTICATE", "GETINFO status/bootstrap-phase"])
def test_newnym(self):
ok(ctl.cmd_tor, "newnym")
self.thread.join(5)
self.assertEqual(self.received, ["AUTHENTICATE", "SIGNAL NEWNYM"])
def test_not_running(self):
os.environ["TOR_CONTROL_SOCKET"] = "missing"
status, _, err = run(ctl.cmd_tor, "newnym")
self.assertNotEqual(status, 0)
self.assertIn("not running", err)
self.assertNotEqual(run(ctl.cmd_tor, "bogus")[0], 0)
class BadExitTest(EnvTest):
def test_shown_from_state(self):
os.environ.update(
+14
View File
@@ -107,6 +107,20 @@ class ModelTest(unittest.TestCase):
self.assertEqual(called, [])
self.assertNotIn("online", [c for c, _ in next(t for t in model.TABS if t.id == "inbounds").columns])
def test_onion_rows_link_through_the_onion(self):
links = [
{"tag": "ws", "user": "alice", "type": "vless", "port": 443},
{"tag": "ws", "user": "alice", "type": "vless", "port": 443, "variant": "onion"},
]
with mock.patch.object(ctl, "_inbound_links", lambda: links):
plain, onion = model.inbound_rows({})
self.assertNotEqual(plain["key"], onion["key"])
self.assertEqual((plain["type"], onion["type"]), ("vless", "vless (onion)"))
self.assertEqual(model._link(plain, "--qr"), ["inbounds", "link", "ws", "alice", "--qr"])
self.assertEqual(model._link(onion, "--qr"), ["inbounds", "link", "ws", "alice", "--onion", "--qr"])
self.assertFalse(model._amneziawg(onion))
self.assertEqual(model.TOGGLES["proxy-suite-tor"], ["tor"])
def test_tray_menu(self):
states = {
"proxy-suite-socks": "active",