Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(tunnel): 命名隧道把全局选项放到 run 前面

longmeidao05a9b71bb429
diff --git a/src/peach/tunnel.py b/src/peach/tunnel.py
index 41cbdcbc8..c9048946d 100644
--- a/src/peach/tunnel.py
+++ b/src/peach/tunnel.py
@@ -255,10 +255,15 @@ def build_command(binary: Path, origin: TunnelOrigin) -> tuple[str, ...]:
def build_named_command(binary: Path, token: str) -> tuple[str, ...]:
- """命名隧道的入向配置在 Cloudflare 后台,这里不传 `--url` 也不传 origin 选项。"""
+ """命名隧道的入向配置在 Cloudflare 后台,这里不传 `--url` 也不传 origin 选项。
+
+ `--no-autoupdate`、`--loglevel`、`--metrics` 和管理器补的 `--pidfile` 都是 `tunnel`
+ 这一级的选项,必须写在 `run` 前面;`run` 只认它自己的 `--token`。
+ """
return (
- str(binary), "tunnel", "run", "--token", token,
+ str(binary), "tunnel",
"--no-autoupdate", "--loglevel", "info", "--metrics", "127.0.0.1:0",
+ "run", "--token", token,
)
@@ -837,7 +842,8 @@ class TunnelManager:
# writable, no untracked cloudflared process may be left behind.
handle = self.log_file.open("a", encoding="utf-8")
process = self._popen(
- [*plan.command, "--pidfile", str(self.pid_file)],
+ # `--pidfile` 紧跟 `tunnel` 子命令:Quick 与 Named 两种命令里它都属于这一级。
+ [*plan.command[:2], "--pidfile", str(self.pid_file), *plan.command[2:]],
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
text=True, encoding="utf-8", errors="replace", bufsize=1,
diff --git a/tests/test_tunnel.py b/tests/test_tunnel.py
index 28444e456..726fb0366 100644
--- a/tests/test_tunnel.py
+++ b/tests/test_tunnel.py
@@ -248,10 +248,12 @@ class TunnelPlanTests(unittest.TestCase):
self.assertEqual(plan.mode, "named")
self.assertEqual(plan.url, "https://peach.example.com")
self.assertEqual(plan.origin.url, "https://peach.example.com")
- self.assertEqual(plan.command[1:5], ("tunnel", "run", "--token", FAKE_TOKEN))
+ self.assertEqual(plan.command[1], "tunnel")
+ self.assertEqual(plan.command[-3:], ("run", "--token", FAKE_TOKEN))
self.assertNotIn("--url", plan.command)
- for option in ("--no-autoupdate", "--metrics"):
- self.assertIn(option, plan.command)
+ run_index = plan.command.index("run")
+ for option in ("--no-autoupdate", "--loglevel", "--metrics"):
+ self.assertLess(plan.command.index(option), run_index)
self.assertEqual(plan.command[plan.command.index("--metrics") + 1], "127.0.0.1:0")
def test_a_standalone_package_refuses_the_named_mode(self):
@@ -325,6 +327,8 @@ class TunnelManagerTests(unittest.TestCase):
self.temp.cleanup()
def popen(self, command, **kwargs):
+ # `--pidfile` 是 `tunnel` 这一级的选项,得紧跟在子命令后面,落到 `run` 后面会被拒。
+ self.assertEqual(command[1:3], ["tunnel", "--pidfile"])
pid_index = command.index("--pidfile") + 1
Path(command[pid_index]).write_text("4123\n", encoding="ascii")
process = _Process(_Stream("INF | Your quick Tunnel has been created! https://unit-7.trycloudflare.com\n"))