wslProxy.test.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. buildWslHostGatewayCommand,
  4. buildWslProxyPortProbeCommand,
  5. formatWslProxyForLog,
  6. isLoopbackProxyHostname,
  7. normalizeWslProxyUrl,
  8. parseWslHostGatewayOutput,
  9. parseWslProxyPortProbeOutput,
  10. replaceProxyHostname,
  11. } from "../src/remote/wslProxy.js";
  12. describe("WSL proxy resolution helpers", () => {
  13. it("识别 loopback 主机并保留 IPv6 替换格式", () => {
  14. expect(isLoopbackProxyHostname("localhost")).toBe(true);
  15. expect(isLoopbackProxyHostname("127.0.0.1")).toBe(true);
  16. expect(isLoopbackProxyHostname("[::1]")).toBe(true);
  17. expect(isLoopbackProxyHostname("192.168.1.10")).toBe(false);
  18. expect(replaceProxyHostname("http://user:pass@localhost:7890/path", "2001:db8::1")).toBe(
  19. "http://user:pass@[2001:db8::1]:7890/path",
  20. );
  21. });
  22. it("规范化裸代理地址并解析探测结果", () => {
  23. expect(normalizeWslProxyUrl("127.0.0.1:7890")).toBe("http://127.0.0.1:7890/");
  24. expect(parseWslProxyPortProbeOutput("reachable\n")).toBe(true);
  25. expect(parseWslProxyPortProbeOutput("unreachable")).toBe(false);
  26. expect(parseWslProxyPortProbeOutput("unexpected")).toBeUndefined();
  27. expect(buildWslProxyPortProbeCommand("http://127.0.0.1:7890")).toContain(
  28. "/dev/tcp/127.0.0.1/7890",
  29. );
  30. });
  31. it("只从网关输出中接受合法 IP,并在日志中隐藏凭据和路径", () => {
  32. expect(parseWslHostGatewayOutput("nameserver 172.21.240.1\n")).toBe("172.21.240.1");
  33. expect(parseWslHostGatewayOutput("route=127.0.0.53")).toBeNull();
  34. expect(parseWslHostGatewayOutput("8.8.8.8")).toBeNull();
  35. expect(parseWslHostGatewayOutput("route=127.0.0.53 resolv=172.21.240.1")).toBe("172.21.240.1");
  36. expect(parseWslHostGatewayOutput("resolv=127.0.0.53 resolv=8.8.8.8 resolv=172.21.240.1")).toBe(
  37. "172.21.240.1",
  38. );
  39. const command = buildWslHostGatewayCommand();
  40. expect(command.indexOf("ip route show default")).toBeLessThan(
  41. command.indexOf("/etc/resolv.conf"),
  42. );
  43. expect(command).toContain('$1=="default" && $2=="via"');
  44. expect(parseWslHostGatewayOutput("gateway unavailable")).toBeNull();
  45. expect(formatWslProxyForLog("http://user:secret@proxy.example.com:7890/private")).toBe(
  46. "http://proxy.example.com:7890",
  47. );
  48. });
  49. });