fix: zapret2 improvements
This commit is contained in:
30 files changed
+872
-49
No files matched your search
@@ -5292,7 +5292,7 @@ def _follow_in_pager(argv, pager):
|
||||
signal.signal(signal.SIGINT, old)
|
||||
die(f"proxy-ctl: {argv[0]}: {e.strerror}", 127)
|
||||
try:
|
||||
return subprocess.call([pager, "-R", "-S", "-M", "+F"], stdin=source.stdout)
|
||||
return subprocess.call([pager, "-R", "-M", "+F"], stdin=source.stdout)
|
||||
finally:
|
||||
source.stdout.close()
|
||||
source.terminate()
|
||||
|
||||
@@ -1106,6 +1106,10 @@ class ProxySuiteGui(Adw.Application):
|
||||
self.pending = False
|
||||
self.generation = 0
|
||||
self.failed = None # units in failed state at the last load; None before the first
|
||||
# model.FAILURE_EVENTS failures (unit -> mark): on screen in the window now, and seen there
|
||||
# before. A seen one stops marking the tray and the status line once the window is left.
|
||||
self.viewing = {}
|
||||
self.seen = {}
|
||||
self.root_read_refused = False # pkexec for a tab read was dismissed: not asked again until Ctrl+E flips
|
||||
self.timer = None
|
||||
|
||||
@@ -1177,6 +1181,8 @@ class ProxySuiteGui(Adw.Application):
|
||||
def present(self, tab=None):
|
||||
if self.window is None:
|
||||
self.window = Window(self)
|
||||
# Hidden to the tray or switched away from: what it showed has been seen.
|
||||
self.window.connect("notify::is-active", lambda w, _: w.is_active() or self.leave_window())
|
||||
self.window.show_tabs(model.available_tabs(self.states) if self.states else ["services"])
|
||||
if tab and tab in self.window.pages:
|
||||
self.window.stack.set_visible_child_name(tab)
|
||||
@@ -1191,6 +1197,12 @@ class ProxySuiteGui(Adw.Application):
|
||||
else:
|
||||
self.present()
|
||||
|
||||
def leave_window(self):
|
||||
if self.viewing:
|
||||
self.seen.update(self.viewing)
|
||||
self.viewing.clear()
|
||||
self.reload()
|
||||
|
||||
def on_tray_available(self, available):
|
||||
if not available and self.window is None:
|
||||
# Started hidden, and nothing shows the tray: no way back to the window but this.
|
||||
@@ -1212,15 +1224,17 @@ class ProxySuiteGui(Adw.Application):
|
||||
if window:
|
||||
window.set_busy(True)
|
||||
tab = window.pages[window.stack.get_visible_child_name()].tab if window and window.stack.get_visible_child_name() else None
|
||||
threading.Thread(target=self.load, args=(self.generation, tab, self.elevated()), daemon=True).start()
|
||||
threading.Thread(target=self.load, args=(self.generation, tab, self.elevated(), dict(self.seen)), daemon=True).start()
|
||||
|
||||
def load(self, generation, tab, elevated):
|
||||
def load(self, generation, tab, elevated, seen):
|
||||
model.new_load()
|
||||
states = model._read_states()
|
||||
marks = model._safe(model.failure_marks, states, fallback={})
|
||||
states = model.unseen(states, marks, seen)
|
||||
snap = model.snapshot(states) if states else None
|
||||
outbound = model._safe(ctl._status_outbound, fallback="") if snap and snap["proxy"]["active"] else ""
|
||||
tray = (model.tray_menu(snap, model.tray_outbounds(snap)), ctl._overall_state(snap), outbound)
|
||||
result = {"states": states, "snap": snap, "tray": tray}
|
||||
result = {"states": states, "snap": snap, "tray": tray, "marks": marks}
|
||||
if tab is not None:
|
||||
result["visible"] = model.available_tabs(states)
|
||||
result["status"] = model._safe(model.status_items, states, fallback=[("", "Status unavailable", "bad")])
|
||||
@@ -1252,6 +1266,12 @@ class ProxySuiteGui(Adw.Application):
|
||||
stale = not result["states"] and bool(self.states)
|
||||
if not stale:
|
||||
self.states = result["states"]
|
||||
marks = result["marks"]
|
||||
# A failure that is over, or failed again since, is no longer the one seen.
|
||||
self.seen = {u: m for u, m in self.seen.items() if marks.get(u) == m}
|
||||
self.viewing = {u: m for u, m in self.viewing.items() if marks.get(u) == m}
|
||||
if "tab" in result and self.window and self.window.is_active():
|
||||
self.viewing.update((u, m) for u, m in marks.items() if u not in self.seen)
|
||||
snap = result["snap"]
|
||||
tree, overall, outbound = result["tray"]
|
||||
if self.tray:
|
||||
|
||||
@@ -213,6 +213,30 @@ def _controllable(row):
|
||||
return row["unit"] in TOGGLES or bool(_awg_profile(row)) or bool(_wl_entry(row))
|
||||
|
||||
|
||||
# Units whose failure is one run's, not something still down: once seen, it stops marking the
|
||||
# suite failed until a later run fails again.
|
||||
FAILURE_EVENTS = (ctl.SUBSCRIPTION_UPDATE,)
|
||||
|
||||
|
||||
def failure_marks(states):
|
||||
"""unit -> when it last failed, for the failed FAILURE_EVENTS: a later failure is a new mark.
|
||||
Empty where the manager does not say (the supervisor): a run that does not fail ends the mark there."""
|
||||
units = [u for u in FAILURE_EVENTS if states.get(u) == "failed"]
|
||||
if not units:
|
||||
return {}
|
||||
_, out = ctl.systemctl("show", "--property=Id,InactiveEnterTimestampMonotonic", "--", *units, capture=True, quiet=True)
|
||||
marks = {}
|
||||
for unit, block in zip(units, out.strip().split("\n\n"), strict=False):
|
||||
props = dict(line.split("=", 1) for line in block.splitlines() if "=" in line)
|
||||
marks[unit] = props.get("InactiveEnterTimestampMonotonic", "")
|
||||
return {u: marks.get(u, "") for u in units}
|
||||
|
||||
|
||||
def unseen(states, marks, seen):
|
||||
"""states with the failures already seen (unit -> mark) read as inactive: nothing counts them as failed."""
|
||||
return {u: "inactive" if u in marks and seen.get(u) == marks[u] else s for u, s in states.items()}
|
||||
|
||||
|
||||
def service_rows(states):
|
||||
return [
|
||||
{"key": u, "unit": u, "name": u.removeprefix("proxy-suite-"), "state": s}
|
||||
@@ -582,8 +606,8 @@ def _enabled(name):
|
||||
return lambda _: ctl.env(name) == "1"
|
||||
|
||||
|
||||
def _sub_url(row):
|
||||
return bool(row["user"]) and bool(ctl.env("INBOUNDS_SUB_BASE_URL"))
|
||||
def _sub_url(row, user="user"):
|
||||
return bool(row[user]) and bool(ctl.env("INBOUNDS_SUB_BASE_URL"))
|
||||
|
||||
|
||||
def _add_args(text):
|
||||
@@ -937,6 +961,10 @@ TABS = [
|
||||
Action("e", "set its order…", lambda r, t, _: ["inbounds", "users", "order", r["name"], t.strip()], when=_runtime_row, prompt="<order>", offered=_inbounds_runtime),
|
||||
Action("b", "bind it to a listener…", lambda r, t, _: ["inbounds", "bind", r["name"], t.strip()], when=ROW, prompt="<listener>", offered=_inbounds_runtime),
|
||||
Action("x", "unbind it from a listener…", lambda r, t, _: ["inbounds", "unbind", r["name"], t.strip()], when=ROW, prompt="<listener>", offered=_inbounds_runtime),
|
||||
# The user's one subscription, as the inbounds tab has it on each of the user's rows.
|
||||
Action("s", "subscription URL", lambda r, *_: ["inbounds", "sub", r["name"]], when=ROW, mode="dialog"),
|
||||
Action("c", "copy subscription URL", lambda r, *_: ["inbounds", "sub", r["name"]], when=lambda r: _sub_url(r, "name"), mode="copy"),
|
||||
Action("Q", "subscription URL as QR", lambda r, *_: ["inbounds", "sub", r["name"], "--qr"], when=lambda r: _sub_url(r, "name"), mode="dialog"),
|
||||
Action("i", "traffic per user", lambda r, *_: ["inbounds", "stats"], mode="dialog"),
|
||||
],
|
||||
),
|
||||
|
||||
@@ -72,6 +72,21 @@ class ModelTest(unittest.TestCase):
|
||||
rows = model.service_rows({"proxy-suite-socks": "active", ctl.SUBSCRIPTION_UPDATE: "inactive"})
|
||||
self.assertEqual([r["unit"] for r in rows], ["proxy-suite-socks"])
|
||||
|
||||
def test_a_seen_failure_event_reads_inactive(self):
|
||||
sub = ctl.SUBSCRIPTION_UPDATE
|
||||
states = {"proxy-suite-socks": "failed", sub: "failed"}
|
||||
with mock.patch.object(ctl, "systemctl", return_value=(0, f"Id={sub}\nInactiveEnterTimestampMonotonic=42\n")) as systemctl:
|
||||
marks = model.failure_marks(states)
|
||||
self.assertEqual(marks, {sub: "42"})
|
||||
self.assertIn(sub, systemctl.call_args.args)
|
||||
# A long-running unit that failed is still down: only the event is let go.
|
||||
self.assertEqual(model.unseen(states, marks, {sub: "42"}), {"proxy-suite-socks": "failed", sub: "inactive"})
|
||||
# Failed again since: a new mark, counted again.
|
||||
self.assertEqual(model.unseen(states, {sub: "99"}, {sub: "42"}), states)
|
||||
with mock.patch.object(ctl, "systemctl") as systemctl:
|
||||
self.assertEqual(model.failure_marks({sub: "inactive"}), {})
|
||||
systemctl.assert_not_called()
|
||||
|
||||
def test_warp_over_amneziawg_toggles_with_warp(self):
|
||||
tab = model.TABS[0]
|
||||
row = model.service_rows({"proxy-suite-awg-warp": "inactive"})[0]
|
||||
@@ -109,7 +124,7 @@ class ModelTest(unittest.TestCase):
|
||||
with mock.patch.dict(os.environ, {"INBOUNDS_ENABLED": "1", "INBOUNDS_RUNTIME_ENABLED": "0"}):
|
||||
# Listed from the configuration; nothing to change them with.
|
||||
self.assertTrue(users.available({}))
|
||||
self.assertEqual([a.key for a in users.actions if model.offered(a)], ["i"])
|
||||
self.assertEqual([a.key for a in users.actions if model.offered(a)], ["s", "c", "Q", "i"])
|
||||
self.assertNotIn("n", [a.key for a in inbounds.actions if model.offered(a)])
|
||||
with mock.patch.dict(os.environ, {"INBOUNDS_ENABLED": "0"}):
|
||||
self.assertFalse(users.available({}))
|
||||
@@ -124,7 +139,12 @@ class ModelTest(unittest.TestCase):
|
||||
self.assertEqual(actions["n"].argv(None, "carol 7 in ws", {}),
|
||||
["inbounds", "users", "add", "carol", "--order", "7", "--listener", "in", "--listener", "ws"])
|
||||
self.assertEqual(actions["n"].argv(None, "carol", {}), ["inbounds", "users", "add", "carol"])
|
||||
self.assertEqual([a.key for a in model.applicable(users, bob)], ["n", "b", "x", "i"])
|
||||
self.assertEqual([a.key for a in model.applicable(users, bob)], ["n", "b", "x", "s", "i"])
|
||||
self.assertEqual(actions["s"].argv(bob), ["inbounds", "sub", "bob"])
|
||||
# A URL to copy or encode only with subscriptions.baseUrl: else it is a file path.
|
||||
with mock.patch.dict(os.environ, {"INBOUNDS_SUB_BASE_URL": "https://sub.example"}):
|
||||
self.assertEqual([a.key for a in model.applicable(users, bob)], ["n", "b", "x", "s", "c", "Q", "i"])
|
||||
self.assertEqual(actions["Q"].argv(bob), ["inbounds", "sub", "bob", "--qr"])
|
||||
self.assertEqual(actions["e"].argv(alice, " 9 ", {}), ["inbounds", "users", "order", "alice", "9"])
|
||||
self.assertEqual(actions["b"].argv(alice, "in", {}), ["inbounds", "bind", "alice", "in"])
|
||||
actions = {a.key: a for a in inbounds.actions}
|
||||
|
||||
Reference in new issue
Block a user