fix: zapret2 & naming fixes
This commit is contained in:
32 files changed
+580
-261
No files matched your search
@@ -2,6 +2,7 @@
|
||||
"""Headless proxy-tui: arrows, the action menu and shortcuts turn into the right proxy-ctl argv."""
|
||||
|
||||
import asyncio
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
@@ -11,29 +12,32 @@ import proxy_tui as tui
|
||||
|
||||
class TuiTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.units = {"proxy-suite-socks": "active", "proxy-suite-tun": "inactive"}
|
||||
self.units = {"proxy-suite-tun": "inactive"}
|
||||
self.zapret = {
|
||||
"zapret-hosts-auto.txt": ["learned.example"],
|
||||
"zapret-hosts-user.txt": [],
|
||||
"zapret-hosts-user-exclude.txt": ["kept.example"],
|
||||
}
|
||||
env = {"ZAPRET_AUTO_ENABLED": "1"}
|
||||
self.capture, self.awg, self.current = "", [], mock.Mock(return_value="")
|
||||
self.env = env = {"ZAPRET_AUTO_ENABLED": "1"}
|
||||
for target, name, value in [
|
||||
(tui, "unit_states", lambda units: {u: s for u, s in self.units.items() if u in units}),
|
||||
(tui, "_capture", lambda argv: ""),
|
||||
(tui, "_capture", lambda argv: self.capture),
|
||||
(tui, "_lines", lambda name: self.zapret[name]),
|
||||
(ctl, "env", lambda name, default="": env.get(name, default)),
|
||||
(ctl, "_awg_profiles", lambda: []),
|
||||
(ctl, "_awg_profiles", lambda: self.awg),
|
||||
(ctl, "_route_mode_current", lambda: "default"),
|
||||
(ctl, "_route_mode_effective", lambda: "whitelist"),
|
||||
(ctl, "_outbound_inventory", lambda: {"tags": ["a", "b"], "pinned": "b", "sources": {"a": "config"}}),
|
||||
(ctl, "_outbound_current", lambda: ""),
|
||||
(ctl, "_outbound_current", tui._per_load(self.current)),
|
||||
(ctl, "_reputation_by_tag", lambda: {}),
|
||||
(ctl, "_runtime_tags", lambda kind: []),
|
||||
(ctl, "_sub_tags", lambda: []),
|
||||
(ctl, "_status_outbound", lambda: "b (pinned)"),
|
||||
(ctl, "_status_autoproxy", lambda: ""),
|
||||
(ctl, "_status_zapret", lambda: ""),
|
||||
(ctl, "svc_state", lambda unit: ""),
|
||||
(ctl, "_inbound_links", lambda: [{"tag": "vless", "user": "alice", "type": "vless", "port": 443}]),
|
||||
]:
|
||||
patcher = mock.patch.object(target, name, value)
|
||||
patcher.start()
|
||||
@@ -43,6 +47,15 @@ class TuiTest(unittest.TestCase):
|
||||
patcher.start()
|
||||
self.addCleanup(patcher.stop)
|
||||
|
||||
def test_subscription_age(self):
|
||||
with tempfile.NamedTemporaryFile() as cache, mock.patch.multiple(
|
||||
ctl,
|
||||
_sub_tags=lambda: ["s"],
|
||||
_subscription_cache=lambda tag: cache.name,
|
||||
_subscription_proxy_count_text=lambda path: "3",
|
||||
):
|
||||
self.assertEqual(tui.subscription_rows({})[0]["updated"], "0m ago")
|
||||
|
||||
def test_ui(self):
|
||||
# Not IsolatedAsyncioTestCase: its asyncio debug mode makes Textual crawl.
|
||||
asyncio.run(self.ui())
|
||||
@@ -61,9 +74,23 @@ class TuiTest(unittest.TestCase):
|
||||
app = tui.ProxyTui()
|
||||
async with app.run_test(size=(100, 30)) as pilot:
|
||||
await self.settle(app, pilot)
|
||||
self.assertEqual(list(app.tabs), ["services", "routing", "outbounds", "subs", "zapret"])
|
||||
self.assertEqual(app.shown, ["services", "zapret"])
|
||||
self.assertEqual(app.focused.id, "services-table")
|
||||
|
||||
# Tabs follow what runs: socks starting brings its tabs in.
|
||||
self.units = {"proxy-suite-socks": "active", **self.units}
|
||||
app.action_reload()
|
||||
await self.settle(app, pilot)
|
||||
self.assertEqual(app.shown, ["services", "routing", "outbounds", "subs", "zapret"])
|
||||
|
||||
# Stopping socks asks first: it takes tun and tproxy down too.
|
||||
await pilot.press("up", "space")
|
||||
await pilot.pause()
|
||||
self.assertIsInstance(app.screen, tui.Confirm)
|
||||
await pilot.press("n")
|
||||
await pilot.pause()
|
||||
self.assertEqual(self.ran, [])
|
||||
|
||||
# Services: space toggles the selected unit.
|
||||
await pilot.press("down", "space")
|
||||
self.assertEqual(self.ran.pop(), ["proxy", "tun", "on"])
|
||||
@@ -75,6 +102,18 @@ class TuiTest(unittest.TestCase):
|
||||
self.assertEqual(app.selected_key("services"), "proxy-suite-tun")
|
||||
self.assertEqual(str(app.table().get_cell("proxy-suite-tun", "state")), "● active")
|
||||
|
||||
# An active AmneziaWG profile restarts on its own; a failed unit shows in the status bar.
|
||||
self.awg = ["p"]
|
||||
self.units[tui.AWG_PREFIX + "p"] = "active"
|
||||
self.units["proxy-suite-ssh-proxy"] = "failed"
|
||||
app.action_reload()
|
||||
await self.settle(app, pilot)
|
||||
self.assertIn("1 failed", tui.status_text(app.states))
|
||||
app.table().move_cursor(row=list(app.rows["services"]).index(tui.AWG_PREFIX + "p"))
|
||||
await pilot.press("ctrl+r")
|
||||
self.assertEqual(self.ran.pop(), ["awg", "restart", "p"])
|
||||
app.table().move_cursor(row=list(app.rows["services"]).index("proxy-suite-tun"))
|
||||
|
||||
# Right arrow moves to Routing and its table has focus.
|
||||
await pilot.press("right")
|
||||
await self.settle(app, pilot)
|
||||
@@ -92,11 +131,16 @@ class TuiTest(unittest.TestCase):
|
||||
# Outbounds: letter shortcuts; the pinned row offers no pin, and nothing removes a declared one.
|
||||
await pilot.press("right")
|
||||
await self.settle(app, pilot)
|
||||
await pilot.press("p", "t", "a")
|
||||
self.assertEqual(self.ran, [["proxy", "select", "a"], ["proxy", "outbounds", "test", "a"], ["proxy", "select", "auto"]])
|
||||
# One load asks the backend what it dials once, however many readers need it.
|
||||
self.current.reset_mock()
|
||||
app.action_reload()
|
||||
await self.settle(app, pilot)
|
||||
self.assertEqual(self.current.call_count, 1)
|
||||
await pilot.press("p", "t", "u")
|
||||
self.assertEqual(self.ran, [["proxy", "pin", "a"], ["proxy", "outbounds", "test", "a"]])
|
||||
await pilot.press("down", "p", "d", "u")
|
||||
self.assertEqual(self.ran.pop(), ["proxy", "unpin"])
|
||||
self.ran.clear()
|
||||
await pilot.press("down", "p", "d")
|
||||
self.assertEqual(self.ran, [])
|
||||
await pilot.press("enter")
|
||||
await pilot.pause()
|
||||
self.assertNotIn("pin it", self.menu_labels(app))
|
||||
@@ -107,10 +151,20 @@ class TuiTest(unittest.TestCase):
|
||||
await pilot.press("n", *"x vless://h", "enter")
|
||||
await pilot.pause()
|
||||
self.assertEqual(self.ran.pop(), ["proxy", "outbounds", "add", "x", "vless://h"])
|
||||
# A prompt remembers what was typed into it last.
|
||||
await pilot.press("n")
|
||||
await pilot.pause()
|
||||
self.assertEqual(app.screen.query_one(tui.Input).value, "x vless://h")
|
||||
await pilot.press("escape")
|
||||
|
||||
# zapret: an excluded host can be included again, not forgotten.
|
||||
# Brackets in a summary are text, not markup.
|
||||
self.env["ZAPRET_CUTOFF_ENABLED"] = "1"
|
||||
self.capture = "Probed: [b] from [/x]\nCutoff: none on this line\n"
|
||||
await pilot.press("5")
|
||||
await self.settle(app, pilot)
|
||||
self.assertIn("[/x]", str(app.main.query_one("#zapret-summary").content))
|
||||
await self.settle(app, pilot)
|
||||
await pilot.press("down", "enter")
|
||||
await pilot.pause()
|
||||
labels = self.menu_labels(app)
|
||||
@@ -127,6 +181,43 @@ class TuiTest(unittest.TestCase):
|
||||
await pilot.pause()
|
||||
self.assertEqual(self.ran.pop(), ["zapret", "auto", "clear"])
|
||||
|
||||
# / filters the rows on any column; esc shows them all again.
|
||||
await pilot.press("slash", *"learned", "enter")
|
||||
await self.settle(app, pilot)
|
||||
self.assertEqual(list(app.rows["zapret"]), ["learned:learned.example"])
|
||||
await pilot.press("escape")
|
||||
await self.settle(app, pilot)
|
||||
self.assertEqual(len(app.rows["zapret"]), 2)
|
||||
|
||||
# Inbounds appears once enabled; c copies the share link.
|
||||
self.env["INBOUNDS_ENABLED"] = "1"
|
||||
app.action_reload()
|
||||
await self.settle(app, pilot)
|
||||
await pilot.press("6")
|
||||
await self.settle(app, pilot)
|
||||
await pilot.press("c")
|
||||
self.assertEqual(self.ran.pop(), ["inbounds", "link", "vless", "alice"])
|
||||
|
||||
# A reader that dies says why in place of the rows.
|
||||
with mock.patch.object(ctl, "_inbound_links", lambda: ctl.die("Share links are not readable")):
|
||||
app.action_reload()
|
||||
await self.settle(app, pilot)
|
||||
self.assertEqual(app.rows["inbounds"], {})
|
||||
self.assertIn("Share links are not readable", str(app.main.query_one("#inbounds-summary").content))
|
||||
|
||||
# Closing a dialog stops the command still streaming into it; c copies what it showed.
|
||||
dialog = tui.Output("proxy-ctl proxy outbounds test")
|
||||
dialog.proc = mock.Mock(**{"poll.return_value": None})
|
||||
app.push_screen(dialog)
|
||||
await pilot.pause()
|
||||
dialog.write("\x1b[32mok\x1b[0m a")
|
||||
with mock.patch.object(app, "copy_to_clipboard") as copy:
|
||||
await pilot.press("c")
|
||||
copy.assert_called_once_with("ok a")
|
||||
await pilot.press("escape")
|
||||
await pilot.pause()
|
||||
dialog.proc.terminate.assert_called_once()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user