fix: various fixes and additions

This commit is contained in:
fufsob committed 2026-09-15 13:46:02 +00:00
1 parent ea1922243a
commit e9b1542085
30 files changed
+462 -127

No files matched your search

+28 -5
View File
@@ -53,7 +53,9 @@ A group without a verb shows its status or list.
proxy [status|on|off] local proxy backend
proxy outbounds [list] outbounds, where each came from, and the pick
proxy outbounds add <tag> <url|json|-> add an outbound at runtime: a URL, or sing-box/XRay JSON (-: stdin)
proxy outbounds add <tag> <url|json|-> [--detour <tag>]
add an outbound at runtime: a URL, or sing-box/XRay JSON (-: stdin),
chained through another outbound with --detour
proxy outbounds rm <tag> remove a runtime outbound
proxy outbounds test [tag...] [--ping] [--delay] [--download]
TCP ping, real delay, download speed (default: ping, delay)
@@ -349,6 +351,7 @@ COMPLETE = {
"link": "its URL, QR code, JSON or client config",
}
},
"proxy outbounds add": {"flags": {"--detour": "chain it through another outbound"}},
"proxy outbounds link": {
"args": _outbound_choices,
"flags": {"--qr": "print a QR code", "--json": "backend JSON", "--config": "client config for this server"},
@@ -748,7 +751,15 @@ def cmd_outbounds(verb="list", *args):
if verb == "list":
_outbounds_list()
elif verb == "add":
_runtime_entry_add("outbound", *args)
args = list(args)
detour = ""
if "--detour" in args:
i = args.index("--detour")
detour = args[i + 1] if i + 1 < len(args) else ""
del args[i : i + 2]
if not detour:
usage("proxy outbounds add <tag> <url|json|-> [--detour <tag>]")
_runtime_entry_add("outbound", *args, detour=detour)
elif verb in ("rm", "remove", "del"):
_runtime_entry_rm("outbound", *args)
elif verb == "test":
@@ -963,6 +974,8 @@ def _outbounds_list():
inventory = _outbound_inventory()
pinned = _s(inventory.get("pinned") or "")
sources = inventory.get("sources") or {}
detours = inventory.get("detours") or {}
excluded = set(inventory.get("excluded") or [])
current = _outbound_current()
reputation = _reputation_by_tag()
@@ -980,7 +993,8 @@ def _outbounds_list():
mark = "*"
elif not pinned and tag == current:
mark = ">"
print(f" {mark}{tag:<34} {rep(tag)}{_s(sources.get(tag) or '-')}")
notes = ([f"via {_s(detours[tag])}"] if tag in detours else []) + (["never picked"] if tag in excluded else [])
print(f" {mark}{tag:<34} {rep(tag)}{', '.join([_s(sources.get(tag) or '-'), *notes])}")
# --- sharing ------------------------------------------------------------------
@@ -1160,11 +1174,14 @@ def _check_runtime_tag(kind, tag):
die(f"A subscription named '{tag}' is declared in the configuration.")
def _runtime_entry_add(kind, tag="", url="", *_):
what = "<url|json|->" if kind == "outbound" else "<url>"
def _runtime_entry_add(kind, tag="", url="", *_, detour=""):
what = "<url|json|-> [--detour <tag>]" if kind == "outbound" else "<url>"
if not tag or not url:
usage(f"proxy {_runtime_noun(kind)} add <tag> {what}")
_check_runtime_tag(kind, tag)
# The start script checks the chain again, and leaves the outbound out if it breaks later.
if detour and (detour == tag or detour not in _outbound_tags()):
die(f"Cannot chain through '{detour}': not an outbound. See: proxy-ctl proxy outbounds")
if kind == "outbound" and url == "-":
url = sys.stdin.read()
ext = ".url"
@@ -1177,6 +1194,10 @@ def _runtime_entry_add(kind, tag="", url="", *_):
path = os.path.join(_runtime_dir(kind), tag + ext)
old = os.umask(0o027)
try:
# The hop first: the entry is what the start script looks for.
if detour:
with open(os.path.join(_runtime_dir(kind), f"{tag}.detour"), "w", encoding="utf-8") as f:
f.write(f"{detour}\n")
with open(path, "w", encoding="utf-8") as f:
f.write(f"{url}\n")
except OSError:
@@ -1195,6 +1216,8 @@ def _runtime_entry_rm(kind, tag="", *_):
die(f"No runtime {kind} named '{tag}'. Ones declared in the NixOS configuration are removed there.")
try:
os.unlink(path)
if kind == "outbound" and os.path.exists(os.path.join(_runtime_dir(kind), f"{tag}.detour")):
os.unlink(os.path.join(_runtime_dir(kind), f"{tag}.detour"))
except FileNotFoundError:
pass
except OSError:
+6 -2
View File
@@ -24,6 +24,11 @@ def _die(message, status=1):
sys.exit(message)
# Clash API calls and systemctl spawns that several readers repeat within one load.
MEMOIZED = ("_outbound_current", "_outbound_inventory", "_autoproxy_state", "_autoproxy_next_run", "svc_state")
# proxy_ctl as the CLI has it, before the patching below: its own tests put these back.
CLI_FUNCTIONS = {name: getattr(ctl, name) for name in ("die", *MEMOIZED)}
ctl.die = _die
_memo = {} # (reader, args) -> result, cleared at the start of every load
@@ -39,8 +44,7 @@ def _per_load(fn):
return wrapped
# Clash API calls and systemctl spawns that several readers repeat within one load.
for _name in ("_outbound_current", "_outbound_inventory", "_autoproxy_state", "_autoproxy_next_run", "svc_state"):
for _name in MEMOIZED:
setattr(ctl, _name, _per_load(getattr(ctl, _name)))
+13
View File
@@ -5,7 +5,9 @@ import datetime
import io
import json
import os
import shutil
import socket
import sys
import tempfile
import unittest
from unittest import mock
@@ -13,6 +15,16 @@ from unittest import mock
import proxy_ctl as ctl
def setUpModule():
"""proxy_model, once imported into the same run, patches proxy_ctl for the front ends;
these tests want it the way the CLI runs."""
model = sys.modules.get("proxy_model")
for name, fn in (model.CLI_FUNCTIONS if model else {}).items():
patcher = mock.patch.object(ctl, name, fn)
patcher.start()
unittest.addModuleCleanup(patcher.stop)
def run(fn, *args):
"""(exit status, stdout, stderr) of a proxy-ctl function."""
out, err = io.StringIO(), io.StringIO()
@@ -642,6 +654,7 @@ class BadExitTest(EnvTest):
class WhereTest(EnvTest):
"""The route walk has to agree with sing-box and XRay, rule-sets included."""
@unittest.skipUnless(shutil.which(os.environ.get("SING_BOX", "sing-box")), "sing-box not found; set SING_BOX")
def test_sing_box_walk(self):
rs = self.write("rs.json", {"version": 1, "rules": [{"domain_suffix": ["youtube.com"]}]})
config = {