feat: better inbounds user management
This commit is contained in:
47 files changed
+6767
-78
No files matched your search
+381
-7
@@ -204,6 +204,29 @@ Changes and secrets need root or the userControl group.
|
||||
inbounds rm <tag> remove a runtime listener
|
||||
inbounds show <tag> a runtime listener's JSON
|
||||
inbounds online who is online, and when others were last seen
|
||||
|
||||
With inbounds.manager (users' policies; the API proxy-ctl and bots use):
|
||||
inbounds users [list] [--json] users, whether they may connect, traffic, expiry
|
||||
inbounds users show <name> [--json] one user: policy, traffic, online, subscription
|
||||
inbounds users add <name> [...] [policy flags]
|
||||
add a runtime user with a policy
|
||||
inbounds users set <name> [--expires 2026-12-31|+30d] [--on-hold 30d] [--traffic 50G]
|
||||
[--period monthly] [--ips 2] [--speed 20M/5M] [--enable|--disable] [--meta key=value]...
|
||||
change a user's policy (declared users too);
|
||||
--on-hold: a term that starts on the first connection
|
||||
inbounds users unset <name> <expires|hold|traffic|period|ips|speed|meta|enabled>...
|
||||
back to the config's value, or none
|
||||
inbounds users enable|disable <name> switch a user on or off
|
||||
inbounds users reset <name> start their traffic over
|
||||
inbounds users extend <name> 30d [--reset]
|
||||
move the expiry on (from now, or from it if later;
|
||||
on hold: a longer term); --reset: traffic anew too
|
||||
inbounds users extend <name> --until <time> [--reset]
|
||||
inbounds users rotate-sub <name> a new subscription URL, the old one stops answering
|
||||
inbounds bans [rm <address>] addresses banned past a user's ipLimit
|
||||
inbounds export [file] [--no-stats] users, policies, keys and traffic, to move them
|
||||
inbounds import <file|-> [--replace] take them in on this server
|
||||
inbounds events [N] what the manager did and told its webhooks
|
||||
"""
|
||||
|
||||
ROUTE_MODES = ["whitelist", "blacklist", "all-proxy", "all-bypass"]
|
||||
@@ -1016,19 +1039,46 @@ COMPLETE = {
|
||||
"add": "add a listener at runtime",
|
||||
"rm": "remove a runtime listener",
|
||||
"show": "a runtime listener's JSON",
|
||||
"bans": "addresses banned past a user's ipLimit",
|
||||
"export": "users, policies, keys and traffic as JSON",
|
||||
"import": "take an export in",
|
||||
"events": "what the inbound manager did",
|
||||
}
|
||||
},
|
||||
"inbounds import": {"flags": {"--replace": "the export wins over what is here"}},
|
||||
"inbounds export": {"flags": {"--no-stats": "leave the traffic counts out"}},
|
||||
"inbounds stats": {"flags": {"--by": "user, inbound or outbound"}},
|
||||
"inbounds users": {
|
||||
"words": {
|
||||
"list": "users, their order, address and listeners",
|
||||
"show": "one user's policy, traffic and subscription",
|
||||
"add": "add a user at runtime",
|
||||
"rm": "remove a runtime user",
|
||||
"order": "set a runtime user's order",
|
||||
"listeners": "put a user on exactly these listeners",
|
||||
"on": "put exactly these users on a listener",
|
||||
"set": "change a user's policy",
|
||||
"unset": "drop policy fields set at runtime",
|
||||
"enable": "switch a user on",
|
||||
"disable": "switch a user off",
|
||||
"reset": "start a user's traffic over",
|
||||
"extend": "move a user's expiry on",
|
||||
"rotate-sub": "give a user a new subscription URL",
|
||||
}
|
||||
},
|
||||
"inbounds users show": {"args": lambda: _inbound_runtime_names("users")},
|
||||
"inbounds users set": {
|
||||
"args": lambda: _inbound_runtime_names("users"),
|
||||
"flags": {"--expires": "a date, +30d, never", "--on-hold": "30d: from the first connection", "--traffic": "50G, none", "--period": "daily, weekly, monthly, none",
|
||||
"--ips": "addresses at once", "--speed": "20M or down/up", "--enable": "switch on",
|
||||
"--disable": "switch off", "--meta": "key=value"},
|
||||
},
|
||||
"inbounds users unset": {"args": lambda: {**_inbound_runtime_names("users"), **{k: "" for k in POLICY_NAMES}}, "repeat": True},
|
||||
"inbounds users enable": {"args": lambda: _inbound_runtime_names("users")},
|
||||
"inbounds users disable": {"args": lambda: _inbound_runtime_names("users")},
|
||||
"inbounds users reset": {"args": lambda: _inbound_runtime_names("users")},
|
||||
"inbounds users extend": {"args": lambda: _inbound_runtime_names("users")},
|
||||
"inbounds users rotate-sub": {"args": lambda: _inbound_runtime_names("users")},
|
||||
"inbounds users listeners": {"args": lambda: {**_inbound_runtime_names("users"), **_inbound_runtime_names("listeners")}, "repeat": True},
|
||||
"inbounds users on": {"args": lambda: {**_inbound_runtime_names("listeners"), **_inbound_runtime_names("users")}, "repeat": True},
|
||||
"inbounds users add": {"flags": {"--order": "its serverSource number", "--listener": "a listener to put it on"}},
|
||||
@@ -6088,11 +6138,17 @@ def _inbound_subscriptions(*args):
|
||||
user = arg
|
||||
path = env("INBOUNDS_SUBS_FILE", f"{runtime_dir()}/proxy-suite-inbounds/subscriptions.json")
|
||||
base = env("INBOUNDS_SUB_BASE_URL")
|
||||
if not os.path.isfile(path):
|
||||
if not os.path.isfile(path) and _manager_enabled():
|
||||
# Served by the manager only (no static files): its API knows the tokens.
|
||||
rows = [r for r in _manager("GET", "/users") if r.get("subscription")]
|
||||
subs = [{"user": r["name"], "token": r["subscription"]["token"]} for r in rows]
|
||||
path = ""
|
||||
elif not os.path.isfile(path):
|
||||
die("No subscriptions available. Is proxy-suite-inbounds running, and is inbounds.subscriptions enabled?")
|
||||
if not readable(path):
|
||||
elif not readable(path):
|
||||
denied(path)
|
||||
subs = read_json(path)
|
||||
else:
|
||||
subs = read_json(path)
|
||||
if not user:
|
||||
if qr:
|
||||
usage("inbounds sub <user> --qr")
|
||||
@@ -6109,7 +6165,8 @@ def _inbound_subscriptions(*args):
|
||||
else:
|
||||
if qr:
|
||||
die("A QR code needs inbounds.subscriptions.baseUrl.")
|
||||
print(f"{path.removesuffix('.json')}/{_s(token)}")
|
||||
# The manager's: the token, which its socket serves at any path ending in it.
|
||||
print(f"{path.removesuffix('.json')}/{_s(token)}" if path else _s(token))
|
||||
sys.stdout.flush()
|
||||
print("Set inbounds.subscriptions.baseUrl to get a URL instead of a path.", file=sys.stderr)
|
||||
|
||||
@@ -6204,7 +6261,311 @@ def _inbound_runtime_apply():
|
||||
print(f"warning: {line}", file=sys.stderr)
|
||||
|
||||
|
||||
# --- the inbound manager --------------------------------------------------------
|
||||
#
|
||||
# inbounds.manager: a daemon with an HTTP+JSON API on a Unix socket (scripts/inbound_manager.py),
|
||||
# which bots use too. `inbounds users show|set|unset|enable|disable|reset|extend`, bans, export
|
||||
# and import go to it; with it on, adding and removing users do as well, so a user's policy
|
||||
# comes and goes with them.
|
||||
|
||||
MANAGER_TIMEOUT = 120
|
||||
# The CLI's names for the policy's fields, as `users set` takes them and `users unset` names them.
|
||||
POLICY_FLAGS = {
|
||||
"--expires": "expiresAt",
|
||||
"--on-hold": "expiresAfter",
|
||||
"--traffic": "trafficLimit",
|
||||
"--period": "trafficPeriod",
|
||||
"--ips": "ipLimit",
|
||||
"--speed": "speedLimit",
|
||||
}
|
||||
POLICY_NAMES = {
|
||||
"expires": "expiresAt",
|
||||
"hold": "expiresAfter",
|
||||
"traffic": "trafficLimit",
|
||||
"period": "trafficPeriod",
|
||||
"ips": "ipLimit",
|
||||
"speed": "speedLimit",
|
||||
"meta": "meta",
|
||||
"enabled": "enabled",
|
||||
}
|
||||
|
||||
|
||||
def _manager_enabled():
|
||||
return bool(env("INBOUNDS_MANAGER_SOCKET"))
|
||||
|
||||
|
||||
def _manager(method, path, body=None):
|
||||
"""One call to the manager's API: its JSON, or its complaint as ours."""
|
||||
socket_path = env("INBOUNDS_MANAGER_SOCKET")
|
||||
if not socket_path:
|
||||
die("The inbound manager is not enabled in this configuration (inbounds.manager.enable).")
|
||||
conn = _UnixHTTPConnection(socket_path, MANAGER_TIMEOUT)
|
||||
try:
|
||||
conn.request(method, "/api/v1" + path, body=None if body is None else json.dumps(body),
|
||||
headers={"Content-Type": "application/json"})
|
||||
response = conn.getresponse()
|
||||
status, raw = response.status, response.read()
|
||||
except PermissionError:
|
||||
die(f"Cannot use the inbound manager ({socket_path}): permission denied - {ask_group()}.", 77)
|
||||
except (FileNotFoundError, ConnectionRefusedError):
|
||||
die("The inbound manager is not running - see: proxy-ctl logs proxy-suite-inbounds-manager")
|
||||
except (OSError, http.client.HTTPException) as e:
|
||||
die(f"Cannot reach the inbound manager: {e}")
|
||||
finally:
|
||||
conn.close()
|
||||
try:
|
||||
value = json.loads(raw) if raw else None
|
||||
except ValueError:
|
||||
value = None
|
||||
if status >= 400:
|
||||
error = value.get("error") if isinstance(value, dict) else None
|
||||
die(error or f"The inbound manager answered {status}.")
|
||||
return value
|
||||
|
||||
|
||||
def _when(ts):
|
||||
return datetime.datetime.fromtimestamp(ts).strftime("%Y-%m-%d %H:%M") if ts else "-"
|
||||
|
||||
|
||||
def _rate(bits):
|
||||
if not bits:
|
||||
return "-"
|
||||
for unit, size in (("Gbit/s", 1e9), ("Mbit/s", 1e6), ("kbit/s", 1e3)):
|
||||
if bits >= size:
|
||||
return f"{bits / size:g} {unit}"
|
||||
return f"{bits} bit/s"
|
||||
|
||||
|
||||
def _user_status(row):
|
||||
if not row.get("active"):
|
||||
return "off: " + ",".join(row.get("reasons") or [])
|
||||
return "on hold" if row.get("onHold") else "active"
|
||||
|
||||
|
||||
def _user_expiry(row):
|
||||
if row.get("onHold"):
|
||||
return f"+{_span((row.get('policy') or {}).get('expiresAfter'))} on 1st use"
|
||||
return _when(row.get("expiresAt"))
|
||||
|
||||
|
||||
def _span(seconds):
|
||||
for unit, size in (("w", 604800), ("d", 86400), ("h", 3600), ("m", 60)):
|
||||
if seconds and seconds % size == 0:
|
||||
return f"{seconds // size}{unit}"
|
||||
return f"{seconds}s"
|
||||
|
||||
|
||||
def _user_traffic(row):
|
||||
traffic = row.get("traffic") or {}
|
||||
used = _human_bytes(traffic.get("used") or 0)
|
||||
return f"{used} / {_human_bytes(traffic['limit'])}" if traffic.get("limit") else used
|
||||
|
||||
|
||||
def _manager_users_table(rows):
|
||||
line = " {:<20} {:<18} {:<24} {:<17} {}"
|
||||
print(line.format("USER", "STATUS", "TRAFFIC", "EXPIRES", "LISTENERS").rstrip())
|
||||
for row in rows:
|
||||
print(line.format(_s(row["name"]), _user_status(row), _user_traffic(row), _user_expiry(row),
|
||||
",".join(row.get("listeners") or []) or "-").rstrip())
|
||||
for problem in row.get("problems") or []:
|
||||
print(f" ! {problem}")
|
||||
|
||||
|
||||
def _manager_user_show(row):
|
||||
policy = row.get("policy") or {}
|
||||
traffic = row.get("traffic") or {}
|
||||
speed = policy.get("speedLimit") or {}
|
||||
out = [
|
||||
f"{row['name']} ({row.get('source')}): {_user_status(row)}",
|
||||
f" listeners {', '.join(row.get('listeners') or []) or '-'}",
|
||||
f" expires {_user_expiry(row)}",
|
||||
f" traffic {_user_traffic(row)} (down {_human_bytes(traffic.get('down') or 0)}, up {_human_bytes(traffic.get('up') or 0)})"
|
||||
+ (f", renews {policy['trafficPeriod']} at {_when(traffic.get('periodEnd'))}" if traffic.get("periodEnd") else ""),
|
||||
f" since {_when(traffic.get('periodStart'))}",
|
||||
f" addresses {policy.get('ipLimit') or 'any number'} at once; online: {', '.join(row['online']['ips']) or '-'}",
|
||||
f" speed down {_rate(speed.get('down'))}, up {_rate(speed.get('up'))}",
|
||||
]
|
||||
if row.get("subscription") and row["subscription"].get("url"):
|
||||
out.append(f" sub {row['subscription']['url']}")
|
||||
for key, value in sorted((policy.get("meta") or {}).items()):
|
||||
out.append(f" meta.{key} = {value}")
|
||||
overridden = sorted(set(row.get("stored") or {}) & set(row.get("declared") or {}))
|
||||
if overridden:
|
||||
out.append(f" (set at runtime over the config: {', '.join(overridden)})")
|
||||
for problem in row.get("problems") or []:
|
||||
out.append(f" ! {problem}")
|
||||
print("\n".join(out))
|
||||
|
||||
|
||||
def _policy_args(args):
|
||||
"""Policy fields from `users set`'s flags; the arguments that are none of them."""
|
||||
fields, rest, i = {}, [], 0
|
||||
args = list(args)
|
||||
while i < len(args):
|
||||
arg = args[i]
|
||||
flag, has_value, value = arg.partition("=")
|
||||
if flag in POLICY_FLAGS or flag == "--meta":
|
||||
if not has_value:
|
||||
if i + 1 >= len(args):
|
||||
usage(f"inbounds users set <name> {flag} <value>")
|
||||
value = args[i + 1]
|
||||
i += 1
|
||||
if flag == "--meta":
|
||||
key, sep, item = value.partition("=")
|
||||
if not sep:
|
||||
usage("inbounds users set <name> --meta <key>=<value>")
|
||||
fields.setdefault("meta", {})[key] = item
|
||||
else:
|
||||
fields[POLICY_FLAGS[flag]] = value
|
||||
elif arg in ("--enable", "--disable"):
|
||||
fields["enabled"] = arg == "--enable"
|
||||
else:
|
||||
rest.append(arg)
|
||||
i += 1
|
||||
return fields, rest
|
||||
|
||||
|
||||
def _manager_users(verb, args):
|
||||
"""`inbounds users ...` with the manager on. False: not one of its verbs."""
|
||||
as_json = "--json" in args
|
||||
args = [a for a in args if a != "--json"]
|
||||
|
||||
def show(row):
|
||||
print(_json_text(row)) if as_json else _manager_user_show(row)
|
||||
|
||||
if verb in ("list", "--json"):
|
||||
rows = _manager("GET", "/users")
|
||||
print(_json_text(rows)) if as_json or verb == "--json" else _manager_users_table(rows)
|
||||
elif verb == "show" and len(args) == 1:
|
||||
show(_manager("GET", f"/users/{urllib.parse.quote(args[0], safe='')}"))
|
||||
elif verb == "add" and args:
|
||||
fields, rest = _policy_args(args[1:])
|
||||
listeners, order, i = [], None, 0
|
||||
while i < len(rest):
|
||||
if rest[i] in ("--listener", "--order") and i + 1 < len(rest):
|
||||
if rest[i] == "--listener":
|
||||
listeners.append(rest[i + 1])
|
||||
else:
|
||||
order = int(rest[i + 1]) if rest[i + 1].isdigit() else die(f"Not an order: {rest[i + 1]}")
|
||||
i += 2
|
||||
else:
|
||||
usage("inbounds users add <name> [--order N] [--listener <tag>]... [policy flags: see users set]")
|
||||
body = {"name": args[0], "listeners": listeners, "policy": fields}
|
||||
if order is not None:
|
||||
body["order"] = order
|
||||
show(_manager("POST", "/users", body))
|
||||
elif verb == "rm" and len(args) == 1:
|
||||
print(_manager("DELETE", f"/users/{urllib.parse.quote(args[0], safe='')}").get("message", ""))
|
||||
elif verb == "set" and args:
|
||||
fields, rest = _policy_args(args[1:])
|
||||
if rest or not fields:
|
||||
usage("inbounds users set <name> [--expires T] [--traffic SIZE] [--period daily|weekly|monthly|none]"
|
||||
" [--ips N] [--speed RATE[/RATE]] [--enable|--disable] [--meta k=v]...")
|
||||
show(_manager("PATCH", f"/users/{urllib.parse.quote(args[0], safe='')}", fields))
|
||||
elif verb == "unset" and len(args) >= 2:
|
||||
unknown = [a for a in args[1:] if a not in POLICY_NAMES]
|
||||
if unknown:
|
||||
usage(f"inbounds users unset <name> <{'|'.join(POLICY_NAMES)}>...")
|
||||
show(_manager("PATCH", f"/users/{urllib.parse.quote(args[0], safe='')}", {"unset": [POLICY_NAMES[a] for a in args[1:]]}))
|
||||
elif verb in ("enable", "disable") and len(args) == 1:
|
||||
show(_manager("POST", f"/users/{urllib.parse.quote(args[0], safe='')}/{verb}"))
|
||||
elif verb == "reset" and len(args) == 1:
|
||||
show(_manager("POST", f"/users/{urllib.parse.quote(args[0], safe='')}/reset-traffic"))
|
||||
elif verb == "extend" and len([a for a in args if a != "--reset"]) in (2, 3):
|
||||
reset = "--reset" in args
|
||||
rest = [a for a in args if a != "--reset"]
|
||||
if len(rest) == 2:
|
||||
body = {"by": rest[1]}
|
||||
elif rest[1] == "--until":
|
||||
body = {"until": rest[2]}
|
||||
else:
|
||||
usage("inbounds users extend <name> <30d | --until <time>> [--reset]")
|
||||
if reset:
|
||||
body["resetTraffic"] = True
|
||||
show(_manager("POST", f"/users/{urllib.parse.quote(rest[0], safe='')}/extend", body))
|
||||
elif verb == "rotate-sub" and len(args) == 1:
|
||||
show(_manager("POST", f"/users/{urllib.parse.quote(args[0], safe='')}/rotate-subscription"))
|
||||
else:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def _inbound_bans(*args):
|
||||
as_json = "--json" in args
|
||||
args = [a for a in args if a != "--json"]
|
||||
if args[:1] == ["rm"] and len(args) == 2:
|
||||
result = _manager("DELETE", f"/bans/{urllib.parse.quote(args[1], safe='')}")
|
||||
print(f"{result['ip']}: {result['lifted']} ban(s) lifted")
|
||||
return
|
||||
if args:
|
||||
usage("inbounds bans [--json] | bans rm <address>")
|
||||
bans = _manager("GET", "/bans")
|
||||
if as_json:
|
||||
print(_json_text(bans))
|
||||
return
|
||||
if not bans:
|
||||
print("No address is banned.")
|
||||
return
|
||||
print(" {:<40} {:<6} {}".format("ADDRESS", "PORT", "LEFT"))
|
||||
for ban in bans:
|
||||
print(" {:<40} {:<6} {}".format(ban["ip"], ban["port"], f"{ban['expires']}s" if ban.get("expires") is not None else "-"))
|
||||
|
||||
|
||||
def _inbound_export(*args):
|
||||
"""The users, their policies, keys and traffic, as JSON: to a file (0600) or stdout."""
|
||||
rest = [a for a in args if a != "--no-stats"]
|
||||
if len(rest) > 1:
|
||||
usage("inbounds export [file] [--no-stats]")
|
||||
bundle = _manager("GET", "/export" + ("?stats=0" if "--no-stats" in args else ""))
|
||||
text = json.dumps(bundle, indent=2) + "\n"
|
||||
if not rest or rest[0] == "-":
|
||||
sys.stdout.write(text)
|
||||
return
|
||||
fd = os.open(rest[0], os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
|
||||
with os.fdopen(fd, "w", encoding="utf-8") as f:
|
||||
# A file that was there keeps its mode through O_CREAT: this one holds every secret.
|
||||
os.fchmod(f.fileno(), 0o600)
|
||||
f.write(text)
|
||||
users = len(((bundle.get("runtime") or {}).get("users") or {}))
|
||||
print(f"Exported {users} runtime user(s), {len(bundle.get('policies') or {})} policies to {rest[0]} - it holds their secrets.")
|
||||
|
||||
|
||||
def _inbound_import(*args):
|
||||
rest = [a for a in args if a != "--replace"]
|
||||
if len(rest) != 1:
|
||||
usage("inbounds import <file|-> [--replace]")
|
||||
try:
|
||||
text = sys.stdin.read() if rest[0] == "-" else open(rest[0], encoding="utf-8").read()
|
||||
bundle = json.loads(text)
|
||||
except (OSError, ValueError) as e:
|
||||
die(f"Cannot read {rest[0]}: {e}")
|
||||
report = _manager("POST", "/import", {"bundle": bundle, "replace": "--replace" in args})
|
||||
for key in ("users", "listeners", "policies", "amneziaWg", "salamander"):
|
||||
if report.get(key):
|
||||
print(f"{key}: {', '.join(report[key])}")
|
||||
if report.get("subscriptionKey"):
|
||||
print("subscriptionKey: taken; the subscription URLs are the exported server's")
|
||||
for line in report.get("skipped") or []:
|
||||
print(f"skipped: {line}")
|
||||
for line in report.get("applied") or []:
|
||||
print(f"applied: {line}")
|
||||
|
||||
|
||||
def _inbound_events(*args):
|
||||
limit = args[0] if args and args[0].isdigit() else "50"
|
||||
events = _manager("GET", f"/events?limit={limit}")
|
||||
if "--json" in args:
|
||||
print(_json_text(events))
|
||||
return
|
||||
for event in events:
|
||||
print(f"{_when(event['at'])} {event['event']:<15} {event.get('user') or '-':<20} {json.dumps(event.get('data') or {}, sort_keys=True)}")
|
||||
|
||||
|
||||
def _inbound_users(verb="list", *args):
|
||||
if _manager_enabled() and _manager_users(verb, list(args)):
|
||||
return
|
||||
if verb in ("show", "set", "unset", "enable", "disable", "reset", "extend", "rotate-sub"):
|
||||
die("Policies need the inbound manager (inbounds.manager.enable).")
|
||||
if verb in ("list", "--json"):
|
||||
sys.stdout.write(_inbound_runtime("users", *(["--json"] if "--json" in (verb, *args) else []), listing=True)[1])
|
||||
elif verb == "add" and args:
|
||||
@@ -6219,8 +6580,10 @@ def _inbound_users(verb="list", *args):
|
||||
_inbound_bindings_set(tag=args[0], wanted=args[1:])
|
||||
else:
|
||||
usage(
|
||||
"inbounds users [list] | add <name> [--order N] [--listener <tag>]... | rm <name> | order <name> <N>"
|
||||
" | listeners <name> [tag...] | on <tag> [user...]"
|
||||
"inbounds users [list] [--json] | show <name> | add <name> [--order N] [--listener <tag>]... | rm <name>"
|
||||
" | order <name> <N> | listeners <name> [tag...] | on <tag> [user...]"
|
||||
" | set <name> [policy flags] | unset <name> <field>... | enable|disable|reset <name>"
|
||||
" | extend <name> <30d>|--until <time> | rotate-sub <name>"
|
||||
)
|
||||
|
||||
|
||||
@@ -6285,8 +6648,19 @@ def cmd_inbounds(verb="list", *args):
|
||||
_inbound_online()
|
||||
elif verb == "sub":
|
||||
_inbound_subscriptions(*args)
|
||||
elif verb == "bans":
|
||||
_inbound_bans(*args)
|
||||
elif verb == "export":
|
||||
_inbound_export(*args)
|
||||
elif verb == "import":
|
||||
_inbound_import(*args)
|
||||
elif verb == "events":
|
||||
_inbound_events(*args)
|
||||
else:
|
||||
usage("inbounds [list] | link <tag> [user] [--qr] | sub [user] [--qr] | stats [days] [--by user|inbound|outbound] | online")
|
||||
usage(
|
||||
"inbounds [list] | link <tag> [user] [--qr] | sub [user] [--qr] | stats [days] [--by user|inbound|outbound]"
|
||||
" | online | users ... | bans | export [file] | import <file> | events"
|
||||
)
|
||||
|
||||
|
||||
# --- main ---------------------------------------------------------------------
|
||||
|
||||
@@ -1515,13 +1515,26 @@ class ProxySuiteGui(Adw.Application):
|
||||
if self.timer is None:
|
||||
self.timer = GLib.timeout_add_seconds(self.refresh, self.tick)
|
||||
self.reload()
|
||||
if not options.get("hidden"):
|
||||
# proxy-suite:// links, from xdg-open (the .desktop file's %u): shown even to --hidden.
|
||||
links = [a for a in command_line.get_arguments()[1:] if a.lower().startswith(model.DEEP_LINK_SCHEME + ":")]
|
||||
if not options.get("hidden") or links:
|
||||
# A launcher's token comes along when the running instance is asked to show itself.
|
||||
platform = command_line.get_platform_data()
|
||||
token = platform.lookup_value("activation-token", None) if platform else None
|
||||
self.present(options.get("tab"), token=token.get_string() if token else None)
|
||||
for link in links:
|
||||
self.open_link(link)
|
||||
return 0
|
||||
|
||||
def open_link(self, link):
|
||||
"""A proxy-suite:// link: what it adds, run once confirmed."""
|
||||
argv, why, stdin = model.deep_link_argv(link)
|
||||
if argv is None:
|
||||
self.window.toast(why)
|
||||
return
|
||||
# Asked first, as a paste is: any web page can open such a link.
|
||||
self.confirm(argv, lambda: self.run_argv("run", argv, self.window, stdin=stdin), self.window, stdin=stdin)
|
||||
|
||||
def do_activate(self):
|
||||
self.present()
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ import signal
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import urllib.parse
|
||||
import zlib
|
||||
from dataclasses import dataclass, field, replace
|
||||
from typing import Callable
|
||||
@@ -1360,6 +1361,28 @@ def paste_argv(tab_id, text):
|
||||
return None, "Not a share link, a subscription URL or a host.", None
|
||||
|
||||
|
||||
DEEP_LINK_SCHEME = "proxy-suite"
|
||||
|
||||
|
||||
def deep_link_argv(link):
|
||||
"""What a proxy-suite://install-config?url=<subscription>&name=... link runs, as paste_argv.
|
||||
|
||||
The subscription pages of inbounds.manager offer it on Linux, as clash:// for Clash. Any web
|
||||
page can open one, so the GUI asks first. Only a subscription: the URL on stdin, and
|
||||
proxy-ctl refuses plain http. name is a title, so it is made a tag as proxy-ctl makes one
|
||||
(-2, -3... when taken); without one left, proxy-ctl names it after its host.
|
||||
"""
|
||||
parts = urllib.parse.urlsplit((link or "").strip())
|
||||
if parts.scheme.lower() != DEEP_LINK_SCHEME or parts.netloc.lower() not in ("install-config", "installconfig"):
|
||||
return None, f"Not a {DEEP_LINK_SCHEME}://install-config link.", None
|
||||
query = urllib.parse.parse_qs(parts.query)
|
||||
url = (query.get("url") or [""])[0].strip()
|
||||
if url.split("://", 1)[0].lower() not in ("http", "https") or len(url.splitlines()) > 1:
|
||||
return None, "The link carries no subscription URL.", None
|
||||
tag = ctl._unique_runtime_tag("subscription", (query.get("name") or [""])[0], "")
|
||||
return ["proxy", "subs", "add", *([tag] if tag else []), "-"], "", url
|
||||
|
||||
|
||||
def _awg_paste(tab_id, text):
|
||||
"""An AmneziaWG config, on stdin: an outbound where outbounds are, else a global profile."""
|
||||
outbound = ctl.env("AWG_RUNTIME_OUTBOUNDS") == "1"
|
||||
|
||||
@@ -2175,6 +2175,141 @@ class OutboundTestTest(EnvTest):
|
||||
self.assertIn("proxy-suite group, or re-run with sudo", err)
|
||||
|
||||
|
||||
class InboundManagerTest(EnvTest):
|
||||
"""inbounds.manager through proxy-ctl: requests to its socket, its answers shown."""
|
||||
|
||||
ROW = {
|
||||
"name": "alice", "source": "runtime", "active": False, "reasons": ["traffic"], "listeners": ["vless-in"],
|
||||
"problems": [], "policy": {"trafficLimit": 1073741824, "trafficPeriod": "monthly", "expiresAt": None, "ipLimit": 2,
|
||||
"speedLimit": {"up": 5000000, "down": 20000000}, "meta": {"tg": "42"}, "enabled": True},
|
||||
"stored": {"ipLimit": 2}, "declared": {},
|
||||
"traffic": {"up": 1, "down": 1073741823, "used": 1073741824, "limit": 1073741824, "periodStart": 1700000000,
|
||||
"periodEnd": 1702592000},
|
||||
"expiresAt": None, "online": {"ips": ["198.51.100.7"], "lastSeen": None},
|
||||
"subscription": {"token": "a" * 32, "url": "https://vpn.example.com/sub/" + "a" * 32},
|
||||
}
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
import http.server
|
||||
import socketserver
|
||||
|
||||
self.requests = []
|
||||
self.answers = {}
|
||||
test = self
|
||||
|
||||
class Handler(http.server.BaseHTTPRequestHandler):
|
||||
def log_message(self, *_):
|
||||
pass
|
||||
|
||||
def address_string(self):
|
||||
return "unix"
|
||||
|
||||
def handle_one(self):
|
||||
length = int(self.headers.get("Content-Length") or 0)
|
||||
body = json.loads(self.rfile.read(length)) if length else None
|
||||
test.requests.append((self.command, self.path, body))
|
||||
status, value = test.answers.get((self.command, self.path.split("?")[0]), (200, test.ROW))
|
||||
raw = json.dumps(value).encode()
|
||||
self.send_response(status)
|
||||
self.send_header("Content-Length", str(len(raw)))
|
||||
self.end_headers()
|
||||
self.wfile.write(raw)
|
||||
|
||||
do_GET = do_POST = do_PATCH = do_DELETE = handle_one
|
||||
|
||||
class Server(socketserver.ThreadingMixIn, socketserver.UnixStreamServer):
|
||||
daemon_threads = True
|
||||
|
||||
def get_request(self):
|
||||
request, _ = super().get_request()
|
||||
return request, ("unix", 0)
|
||||
|
||||
sock = self.path("api.sock")
|
||||
server = Server(sock, Handler)
|
||||
threading.Thread(target=server.serve_forever, daemon=True).start()
|
||||
self.addCleanup(server.server_close)
|
||||
self.addCleanup(server.shutdown)
|
||||
os.environ.update(INBOUNDS_ENABLED="1", INBOUNDS_MANAGER_SOCKET=sock)
|
||||
|
||||
def test_set_unset_and_the_rest(self):
|
||||
out = ok(ctl.cmd_inbounds, "users", "set", "alice", "--traffic", "1G", "--speed=20M/5M", "--meta", "tg=42", "--disable",
|
||||
"--expires", "+30d")
|
||||
self.assertEqual(self.requests[-1], ("PATCH", "/api/v1/users/alice", {
|
||||
"trafficLimit": "1G", "speedLimit": "20M/5M", "meta": {"tg": "42"}, "enabled": False, "expiresAt": "+30d"}))
|
||||
self.assertIn("alice (runtime): off: traffic", out)
|
||||
self.assertIn("down 20 Mbit/s, up 5 Mbit/s", out)
|
||||
self.assertIn("https://vpn.example.com/sub/", out)
|
||||
ok(ctl.cmd_inbounds, "users", "unset", "alice", "ips", "speed")
|
||||
self.assertEqual(self.requests[-1][2], {"unset": ["ipLimit", "speedLimit"]})
|
||||
ok(ctl.cmd_inbounds, "users", "extend", "alice", "30d")
|
||||
self.assertEqual(self.requests[-1], ("POST", "/api/v1/users/alice/extend", {"by": "30d"}))
|
||||
ok(ctl.cmd_inbounds, "users", "reset", "alice")
|
||||
self.assertEqual(self.requests[-1][:2], ("POST", "/api/v1/users/alice/reset-traffic"))
|
||||
ok(ctl.cmd_inbounds, "users", "add", "bob", "--listener", "vless-in", "--ips", "3", "--order", "7")
|
||||
self.assertEqual(self.requests[-1], ("POST", "/api/v1/users", {
|
||||
"name": "bob", "listeners": ["vless-in"], "policy": {"ipLimit": "3"}, "order": 7}))
|
||||
rows = json.loads(ok(ctl.cmd_inbounds, "users", "show", "alice", "--json"))
|
||||
self.assertEqual(rows["name"], "alice")
|
||||
|
||||
def test_on_hold_and_renewals(self):
|
||||
ok(ctl.cmd_inbounds, "users", "add", "bob", "--on-hold", "30d")
|
||||
self.assertEqual(self.requests[-1][2]["policy"], {"expiresAfter": "30d"})
|
||||
ok(ctl.cmd_inbounds, "users", "unset", "bob", "hold")
|
||||
self.assertEqual(self.requests[-1][2], {"unset": ["expiresAfter"]})
|
||||
ok(ctl.cmd_inbounds, "users", "extend", "alice", "30d", "--reset")
|
||||
self.assertEqual(self.requests[-1], ("POST", "/api/v1/users/alice/extend", {"by": "30d", "resetTraffic": True}))
|
||||
ok(ctl.cmd_inbounds, "users", "extend", "alice", "--until", "2027-01-31")
|
||||
self.assertEqual(self.requests[-1][2], {"until": "2027-01-31"})
|
||||
held = {**self.ROW, "active": True, "reasons": [], "onHold": True,
|
||||
"policy": {**self.ROW["policy"], "expiresAfter": 30 * 86400}}
|
||||
self.answers[("GET", "/api/v1/users")] = (200, [held])
|
||||
out = ok(ctl.cmd_inbounds, "users")
|
||||
self.assertIn("on hold", out)
|
||||
self.assertIn("+30d on 1st use", out)
|
||||
|
||||
def test_list_table(self):
|
||||
self.answers[("GET", "/api/v1/users")] = (200, [self.ROW])
|
||||
out = ok(ctl.cmd_inbounds, "users")
|
||||
self.assertIn("alice", out)
|
||||
self.assertIn("1.0 GiB / 1.0 GiB", out)
|
||||
self.assertIn("off: traffic", out)
|
||||
|
||||
def test_errors_and_absence(self):
|
||||
self.answers[("PATCH", "/api/v1/users/zed")] = (404, {"error": "no user 'zed'"})
|
||||
status, _, err = run(ctl.cmd_inbounds, "users", "set", "zed", "--ips", "1")
|
||||
self.assertEqual(status, 1)
|
||||
self.assertIn("no user 'zed'", err)
|
||||
os.environ["INBOUNDS_MANAGER_SOCKET"] = ""
|
||||
status, _, err = run(ctl.cmd_inbounds, "users", "set", "zed", "--ips", "1")
|
||||
self.assertIn("need the inbound manager", err)
|
||||
os.environ["INBOUNDS_MANAGER_SOCKET"] = self.path("nothing.sock")
|
||||
status, _, err = run(ctl.cmd_inbounds, "bans")
|
||||
self.assertIn("not running", err)
|
||||
|
||||
def test_subscription_urls_from_the_manager(self):
|
||||
os.environ.update(INBOUNDS_SUBS_FILE=self.path("absent.json"), INBOUNDS_SUB_BASE_URL="https://vpn.example.com/sub")
|
||||
self.answers[("GET", "/api/v1/users")] = (200, [self.ROW])
|
||||
self.assertEqual(ok(ctl.cmd_inbounds, "sub", "alice"), "https://vpn.example.com/sub/" + "a" * 32 + "\n")
|
||||
self.assertIn("alice", ok(ctl.cmd_inbounds, "sub"))
|
||||
|
||||
def test_bans_export_import(self):
|
||||
self.answers[("GET", "/api/v1/bans")] = (200, [{"ip": "203.0.113.9", "port": 443, "expires": 120}])
|
||||
self.assertIn("203.0.113.9", ok(ctl.cmd_inbounds, "bans"))
|
||||
self.answers[("DELETE", "/api/v1/bans/203.0.113.9")] = (200, {"ip": "203.0.113.9", "lifted": 1})
|
||||
self.assertIn("1 ban(s) lifted", ok(ctl.cmd_inbounds, "bans", "rm", "203.0.113.9"))
|
||||
bundle = {"format": "proxy-suite-inbounds", "version": 1, "runtime": {"users": {"a": {}}}, "policies": {}}
|
||||
self.answers[("GET", "/api/v1/export")] = (200, bundle)
|
||||
target = self.path("export.json")
|
||||
ok(ctl.cmd_inbounds, "export", target, "--no-stats")
|
||||
self.assertEqual(self.requests[-1][1], "/api/v1/export?stats=0")
|
||||
self.assertEqual(stat.S_IMODE(os.stat(target).st_mode), 0o600)
|
||||
self.answers[("POST", "/api/v1/import")] = (200, {"users": ["a"], "skipped": [], "applied": ["vless-in: +a"]})
|
||||
out = ok(ctl.cmd_inbounds, "import", target, "--replace")
|
||||
self.assertEqual(self.requests[-1][2], {"bundle": bundle, "replace": True})
|
||||
self.assertIn("users: a", out)
|
||||
|
||||
|
||||
class InboundRuntimeTest(EnvTest):
|
||||
"""inbounds.runtime through proxy-ctl: the tool does the work, proxy-ctl reloads after."""
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import subprocess
|
||||
import tempfile
|
||||
import time
|
||||
import unittest
|
||||
import urllib.parse
|
||||
import zlib
|
||||
from unittest import mock
|
||||
|
||||
@@ -406,6 +407,35 @@ class ModelTest(unittest.TestCase):
|
||||
self.assertEqual([i.id for i in model.tray_menu(None)], ["status", "open", "sep-0", "quit"])
|
||||
self.assertEqual(model.icon_name(ctl._overall_state(None)), "proxy-suite-disabled-unknown")
|
||||
|
||||
def test_deep_link_argv(self):
|
||||
link = model.deep_link_argv
|
||||
sub = "https://vpn.test/sub/" + "a" * 32 + "?format=links&x=1"
|
||||
quoted = urllib.parse.quote(sub, safe="")
|
||||
# The URL on stdin, as pasted; the name made a tag, one not taken yet.
|
||||
with mock.patch.object(ctl, "_runtime_tags", lambda kind: ["work"]), \
|
||||
mock.patch.object(ctl, "_sub_tags", lambda: ["home"]):
|
||||
self.assertEqual(link(f"proxy-suite://install-config?url={quoted}&name=VPN%20alice"),
|
||||
(["proxy", "subs", "add", "VPN-alice", "-"], "", sub))
|
||||
for name, tag in [("work", "work-2"), ("home", "home-2"), ("tor", "tor-2"), ("..%2F..%2Fx", "x"),
|
||||
("%F0%9F%87%A9%F0%9F%87%AA%20" + "y" * 40, "y" * 32)]:
|
||||
self.assertEqual(link(f"proxy-suite://install-config?url={quoted}&name={name}")[0][3], tag, name)
|
||||
# No name, or none left of it: proxy-ctl names it after its host.
|
||||
for name in ["", "&name=", "&name=%F0%9F%87%A9%F0%9F%87%AA"]:
|
||||
self.assertEqual(link(f"proxy-suite://install-config?url={quoted}{name}")[0], ["proxy", "subs", "add", "-"], name)
|
||||
self.assertEqual(link(f"PROXY-SUITE://installconfig?url={quoted}")[2], sub)
|
||||
for text in [
|
||||
"",
|
||||
f"clash://install-config?url={quoted}",
|
||||
f"proxy-suite://import?url={quoted}",
|
||||
"proxy-suite://install-config?name=x",
|
||||
"proxy-suite://install-config?url=vless%3A%2F%2Fu%40a.test%3A443",
|
||||
"proxy-suite://install-config?url=https%3A%2F%2Fa.test%2F%0Ahttps%3A%2F%2Fb.test%2F",
|
||||
]:
|
||||
argv, why, stdin = link(text)
|
||||
self.assertIsNone(argv, text)
|
||||
self.assertIsNone(stdin)
|
||||
self.assertTrue(why)
|
||||
|
||||
def test_paste_argv(self):
|
||||
paste = model.paste_argv
|
||||
# Links and JSON carry credentials: on stdin, never in argv (ps, pkexec's log).
|
||||
|
||||
Reference in new issue
Block a user