diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 55078cb50..009d14414 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -19550,7 +19550,9 @@ function renderProxy(s, keep) { // way magpie's requests leave. POST /api/proxy/check asks Cloudflare's // trace page through it, as a vendor request goes, and direct; the line // under the row's text says the address each left from. The same address -// both ways says the proxy isn't taking them. A result is kept until the +// both ways is a failure only when the request through the proxy didn't +// connect to it; when it did, a TUN or system VPN can give both one exit +// (#2369). A result is kept until the // proxy changes, so a redraw doesn't drop it. let proxyCheck = null; // { key, busy, out, err } function renderProxyCheck(s, row) { @@ -19600,7 +19602,13 @@ function proxyCheckText(c) { : [t("No proxy in force, and a direct request failed: {error}", { error: d.error || "" }), true]; } if (!via.ip) return [t("The proxy failed: {error}. Direct requests leave from {direct}", { error: via.error || "", direct }), true]; - if (d.ip && via.ip === d.ip) return [t("Through the proxy requests leave from {where}, the same address as direct ones: they leave as if there were no proxy", { where: where(via) }), true]; + // One address both ways: the connection tells whether the proxy took the + // request. A TUN or system VPN sends the direct one out its exit too (#2369). + if (d.ip && via.ip === d.ip) { + return via.proxied + ? [t("The proxy took the requests, but they leave from {where} as direct ones do: a TUN or system VPN may send every request out one exit, so the address can't tell the two paths apart", { where: where(via) }), false] + : [t("The request through the proxy didn't connect to it and leaves from {where}, the same address as direct ones: the proxy is passed by", { where: where(via) }), true]; + } return [t("Works: through the proxy requests leave from {where}, {ms} ms; direct ones from {direct}", { where: where(via), ms: via.ms || 0, direct }), false]; } diff --git a/internal/gui/assets/i18n.js b/internal/gui/assets/i18n.js index b5f973ba4..1021530a6 100644 --- a/internal/gui/assets/i18n.js +++ b/internal/gui/assets/i18n.js @@ -839,7 +839,8 @@ const I18N = { "No proxy in force: requests leave from {where}, {ms} ms": "未使用代理:请求从 {where} 发出,{ms} ms", "No proxy in force, and a direct request failed: {error}": "未使用代理,且直连请求失败:{error}", "The proxy failed: {error}. Direct requests leave from {direct}": "代理不可用:{error}。直连请求从 {direct} 发出", - "Through the proxy requests leave from {where}, the same address as direct ones: they leave as if there were no proxy": "经代理的请求从 {where} 发出,与直连相同:代理没有生效", + "The proxy took the requests, but they leave from {where} as direct ones do: a TUN or system VPN may send every request out one exit, so the address can't tell the two paths apart": "代理已接通,但请求与直连同样从 {where} 出去:可能开启了 TUN 或系统 VPN,所有请求共用一个出口,凭出口地址分不出两条路径", + "The request through the proxy didn't connect to it and leaves from {where}, the same address as direct ones: the proxy is passed by": "经代理的请求没有连到代理,与直连一样从 {where} 发出:代理被绕过了", "Works: through the proxy requests leave from {where}, {ms} ms; direct ones from {direct}": "已生效:经代理的请求从 {where} 发出,{ms} ms;直连从 {direct} 发出", "Off: requests to vendors without a proxy of their own, and update checks, go direct": "已关闭:未单独设置代理的模型厂商请求和检查更新都直连", "No system proxy found; requests to vendors without a proxy of their own, and update checks, go direct": "未发现系统代理,未单独设置代理的模型厂商请求和检查更新都直连", @@ -4468,7 +4469,8 @@ const I18N = { "No proxy in force: requests leave from {where}, {ms} ms": "未使用代理:請求從 {where} 發出,{ms} ms", "No proxy in force, and a direct request failed: {error}": "未使用代理,且直連請求失敗:{error}", "The proxy failed: {error}. Direct requests leave from {direct}": "代理無法使用:{error}。直連請求從 {direct} 發出", - "Through the proxy requests leave from {where}, the same address as direct ones: they leave as if there were no proxy": "經代理的請求從 {where} 發出,與直連相同:代理沒有生效", + "The proxy took the requests, but they leave from {where} as direct ones do: a TUN or system VPN may send every request out one exit, so the address can't tell the two paths apart": "代理已接通,但請求與直連同樣從 {where} 出去:可能開啟了 TUN 或系統 VPN,所有請求共用一個出口,憑出口位址分不出兩條路徑", + "The request through the proxy didn't connect to it and leaves from {where}, the same address as direct ones: the proxy is passed by": "經代理的請求沒有連到代理,與直連一樣從 {where} 發出:代理被繞過了", "Works: through the proxy requests leave from {where}, {ms} ms; direct ones from {direct}": "已生效:經代理的請求從 {where} 發出,{ms} ms;直連從 {direct} 發出", "Off: requests to vendors without a proxy of their own, and update checks, go direct": "已關閉:未單獨設定代理的模型廠商請求和檢查更新都直連", "No system proxy found; requests to vendors without a proxy of their own, and update checks, go direct": "未發現系統代理,未單獨設定代理的模型廠商請求和檢查更新都直連", @@ -8069,7 +8071,8 @@ const I18N = { "No proxy in force: requests leave from {where}, {ms} ms": "プロキシなし:リクエストは {where} から出ます、{ms} ms", "No proxy in force, and a direct request failed: {error}": "プロキシなし、直接のリクエストも失敗:{error}", "The proxy failed: {error}. Direct requests leave from {direct}": "プロキシが失敗:{error}。直接のリクエストは {direct} から出ます", - "Through the proxy requests leave from {where}, the same address as direct ones: they leave as if there were no proxy": "プロキシ経由のリクエストは {where} から出ており、直接と同じアドレスです:プロキシが効いていません", + "The proxy took the requests, but they leave from {where} as direct ones do: a TUN or system VPN may send every request out one exit, so the address can't tell the two paths apart": "プロキシにはつながりましたが、リクエストは直接と同じく {where} から出ています:TUN やシステム VPN がすべてを同じ出口に送っている可能性があり、アドレスでは 2 つの経路を区別できません", + "The request through the proxy didn't connect to it and leaves from {where}, the same address as direct ones: the proxy is passed by": "プロキシ経由のはずのリクエストはプロキシに接続せず、直接と同じく {where} から出ています:プロキシが迂回されています", "Works: through the proxy requests leave from {where}, {ms} ms; direct ones from {direct}": "有効:プロキシ経由のリクエストは {where} から出ます、{ms} ms;直接は {direct} から", "Off: requests to vendors without a proxy of their own, and update checks, go direct": "オフ:独自のプロキシを設定していないベンダーへのリクエストとアップデート確認は直接接続します", "No system proxy found; requests to vendors without a proxy of their own, and update checks, go direct": "システムプロキシが見つかりません。独自のプロキシを設定していないベンダーへのリクエストとアップデート確認は直接接続します", @@ -11684,7 +11687,8 @@ const I18N = { "No proxy in force: requests leave from {where}, {ms} ms": "Kein Proxy aktiv: Anfragen gehen von {where} aus, {ms} ms", "No proxy in force, and a direct request failed: {error}": "Kein Proxy aktiv, und eine direkte Anfrage scheiterte: {error}", "The proxy failed: {error}. Direct requests leave from {direct}": "Der Proxy scheiterte: {error}. Direkte Anfragen gehen von {direct} aus", - "Through the proxy requests leave from {where}, the same address as direct ones: they leave as if there were no proxy": "Über den Proxy gehen Anfragen von {where} aus, derselben Adresse wie direkt: Sie gehen aus, als gäbe es keinen Proxy", + "The proxy took the requests, but they leave from {where} as direct ones do: a TUN or system VPN may send every request out one exit, so the address can't tell the two paths apart": "Der Proxy hat die Anfragen angenommen, sie gehen aber wie direkte von {where} aus: Ein TUN oder System-VPN schickt womöglich alles über einen Ausgang, die Adresse unterscheidet die zwei Wege nicht", + "The request through the proxy didn't connect to it and leaves from {where}, the same address as direct ones: the proxy is passed by": "Die Anfrage über den Proxy hat sich nicht mit ihm verbunden und geht wie direkte von {where} aus: Der Proxy wird umgangen", "Works: through the proxy requests leave from {where}, {ms} ms; direct ones from {direct}": "Funktioniert: Über den Proxy gehen Anfragen von {where} aus, {ms} ms; direkt von {direct}", "Off: requests to vendors without a proxy of their own, and update checks, go direct": "Aus: Anfragen an Anbieter ohne eigenen Proxy und Update-Prüfungen laufen direkt", "No system proxy found; requests to vendors without a proxy of their own, and update checks, go direct": "Kein System-Proxy gefunden; Anfragen an Anbieter ohne eigenen Proxy und Update-Prüfungen laufen direkt", diff --git a/internal/gui/proxycheck.go b/internal/gui/proxycheck.go index e162973e4..0ba509fba 100644 --- a/internal/gui/proxycheck.go +++ b/internal/gui/proxycheck.go @@ -5,7 +5,10 @@ import ( "context" "fmt" "io" + "net" "net/http" + "net/http/httptrace" + "net/url" "strings" "sync" "time" @@ -17,8 +20,14 @@ import ( // way magpie's requests leave. One request to Cloudflare's trace page goes // as a vendor request would (the proxy Settings, *_PROXY or the system // names), and one goes direct; each says the address Cloudflare saw it -// from and the country. Different addresses say the proxy is taking the -// requests; the same address says they leave as if there were none. +// from and the country, and the request through the proxy says whether +// its connection went to the proxy. Different addresses say the proxy is +// taking the requests. The same address with the connection made to the +// proxy says it took them too, but both leave by one exit: a TUN or +// system VPN can send even the direct request through the same proxy +// (#2369, Emt-lin), so the address can't tell the two paths apart. The +// same address with the connection made elsewhere says the proxy was +// passed by. // proxyTraceURL is the page asked; a test points it at its own server. var proxyTraceURL = "https://www.cloudflare.com/cdn-cgi/trace" @@ -27,12 +36,16 @@ var proxyTraceURL = "https://www.cloudflare.com/cdn-cgi/trace" const proxyTraceTimeout = 10 * time.Second // proxyLeg is one of the two requests: the address and country Cloudflare -// saw it from, how long it took, or why it failed. +// saw it from, how long it took, or why it failed. Proxied, on the request +// through the proxy, is whether the connection that carried it went to +// the proxy in force, which an exit address shared with the direct +// request can't say. type proxyLeg struct { - IP string `json:"ip,omitempty"` - Loc string `json:"loc,omitempty"` - Ms int64 `json:"ms,omitempty"` - Error string `json:"error,omitempty"` + IP string `json:"ip,omitempty"` + Loc string `json:"loc,omitempty"` + Ms int64 `json:"ms,omitempty"` + Error string `json:"error,omitempty"` + Proxied bool `json:"proxied,omitempty"` } // proxyCheck is the answer: the proxy in force (redacted) and where it came @@ -55,7 +68,7 @@ func checkProxy(ctx context.Context, client *http.Client, via context.Context, t if through { out.Through = &proxyLeg{} wg.Add(1) - go func() { defer wg.Done(); *out.Through = traceLeg(via, client) }() + go func() { defer wg.Done(); *out.Through = throughLeg(via, client) }() } wg.Add(1) go func() { defer wg.Done(); out.Direct = traceLeg(netproxy.With(ctx, "direct"), client) }() @@ -63,6 +76,66 @@ func checkProxy(ctx context.Context, client *http.Client, via context.Context, t return out } +// throughLeg is traceLeg through the proxy ctx's requests take +// (netproxy.Func), with Proxied set when the answer came over a +// connection to that proxy. A proxy the trace page is passed by (a +// bypass list naming it) leaves Proxied false. +func throughLeg(ctx context.Context, client *http.Client) proxyLeg { + var remote net.Addr + trace := &httptrace.ClientTrace{GotConn: func(i httptrace.GotConnInfo) { + if i.Conn != nil { + remote = i.Conn.RemoteAddr() + } + }} + leg := traceLeg(httptrace.WithClientTrace(ctx, trace), client) + if leg.Error != "" || remote == nil { + return leg + } + req, err := http.NewRequestWithContext(ctx, http.MethodGet, proxyTraceURL, nil) + if err != nil { + return leg + } + if p, err := netproxy.Func(req); err == nil && p != nil { + leg.Proxied = connectedTo(ctx, remote, p) + } + return leg +} + +// connectedTo is whether remote, a connection's far end, is the proxy p: +// its port, and its address or one its name resolves to. +func connectedTo(ctx context.Context, remote net.Addr, p *url.URL) bool { + host, port, err := net.SplitHostPort(remote.String()) + if err != nil { + return false + } + want := p.Port() + if want == "" { + want = map[string]string{"http": "80", "https": "443", "socks5": "1080", "socks5h": "1080"}[p.Scheme] + } + if port != want { + return false + } + ip := net.ParseIP(host) + if ip == nil { + return false + } + if pip := net.ParseIP(p.Hostname()); pip != nil { + return pip.Equal(ip) + } + ctx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + addrs, err := net.DefaultResolver.LookupIPAddr(ctx, p.Hostname()) + if err != nil { + return false + } + for _, a := range addrs { + if a.IP.Equal(ip) { + return true + } + } + return false +} + // traceLeg makes one request to the trace page and reads its ip= and loc= // lines. func traceLeg(ctx context.Context, client *http.Client) proxyLeg { diff --git a/internal/gui/proxycheck_test.go b/internal/gui/proxycheck_test.go index 45f32e010..f3b0a338a 100644 --- a/internal/gui/proxycheck_test.go +++ b/internal/gui/proxycheck_test.go @@ -6,6 +6,7 @@ import ( "net" "net/http" "net/http/httptest" + "net/url" "strings" "testing" @@ -15,10 +16,14 @@ import ( // proxyCheckRig: a trace page at a name that resolves nowhere // (trace.example.invalid), answered as 198.51.100.7 in CN by a server the // test's dialer sends a direct request to, and a forward proxy that -// answers it as 203.0.113.9 in US. Nothing leaves the machine: the name -// is .invalid and both servers are loopback. -func proxyCheckRig(t *testing.T) (client *http.Client, proxyURL string, asked *int) { +// answers it as 203.0.113.9 in US (or as exit, when given). Nothing +// leaves the machine: the name is .invalid and both servers are loopback. +func proxyCheckRig(t *testing.T, exit ...string) (client *http.Client, proxyURL string, asked *int) { t.Helper() + answer := "fl=1\nip=203.0.113.9\nloc=US\n" + if len(exit) > 0 { + answer = exit[0] + } asked = new(int) direct := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { fmt.Fprint(rw, "fl=1\nh=trace.example.invalid\nip=198.51.100.7\nts=1\nloc=CN\nwarp=off\n") @@ -30,7 +35,7 @@ func proxyCheckRig(t *testing.T) (client *http.Client, proxyURL string, asked *i http.Error(rw, "not the trace page: "+r.URL.String(), http.StatusBadGateway) return } - fmt.Fprint(rw, "fl=1\nip=203.0.113.9\nloc=US\n") + fmt.Fprint(rw, answer) })) t.Cleanup(proxy.Close) old := proxyTraceURL @@ -62,6 +67,59 @@ func TestProxyCheckTellsTheProxyFromDirect(t *testing.T) { if *asked != 1 { t.Fatalf("the proxy was asked %d times, want 1 (the direct request went through it?)", *asked) } + if !got.Through.Proxied || got.Direct.Proxied { + t.Fatalf("proxied: through %v, direct %v; want true, false", got.Through.Proxied, got.Direct.Proxied) + } +} + +// #2369 (Emt-lin): under a TUN that takes every connection (FlClash with +// auto-route), the direct request leaves by the proxy's exit too, so both +// say one address. The request through the proxy still went to it, and +// the answer says so: Proxied, not "as if there were no proxy". +func TestProxyCheckSaysTheProxyTookItUnderOneExit(t *testing.T) { + client, proxyURL, asked := proxyCheckRig(t, "fl=1\nip=198.51.100.7\nloc=CN\n") + ctx := context.Background() + got := checkProxy(ctx, client, netproxy.With(ctx, proxyURL), true) + if got.Through == nil || got.Through.IP != got.Direct.IP || got.Through.Error != "" { + t.Fatalf("one exit both ways: through %+v, direct %+v", got.Through, got.Direct) + } + if !got.Through.Proxied || *asked != 1 { + t.Fatalf("the request through the proxy reached it (asked %d), yet proxied = %v", *asked, got.Through.Proxied) + } +} + +// A proxy the trace page is passed by (NO_PROXY names it): the request +// "through" it went direct, and Proxied says so. +func TestProxyCheckSaysAProxyPassedBy(t *testing.T) { + client, proxyURL, asked := proxyCheckRig(t) + t.Setenv("ALL_PROXY", proxyURL) + t.Setenv("NO_PROXY", "trace.example.invalid") + ctx := context.Background() + got := checkProxy(ctx, client, ctx, true) + if got.Through == nil || got.Through.IP != "198.51.100.7" || *asked != 0 { + t.Fatalf("passed by: %+v, the proxy asked %d times", got.Through, *asked) + } + if got.Through.Proxied { + t.Fatal("a request that went direct says it went through the proxy") + } +} + +// connectedTo matches a proxy named by host name to the address it +// resolves to, and tells another port apart. +func TestProxyCheckConnectedToByName(t *testing.T) { + ctx := context.Background() + at := &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 7890} + for raw, want := range map[string]bool{ + "http://localhost:7890": true, + "http://127.0.0.1:7890": true, + "http://127.0.0.1:7891": false, + "socks5://127.0.0.2:7890": false, + } { + u, _ := url.Parse(raw) + if got := connectedTo(ctx, at, u); got != want { + t.Errorf("%s: connectedTo = %v, want %v", raw, got, want) + } + } } // The proxy in force for every request (here ALL_PROXY; Settings' and the diff --git a/internal/gui/tests/proxy-test.test.cjs b/internal/gui/tests/proxy-test.test.cjs index 87f59a4e6..6b20ed02a 100644 --- a/internal/gui/tests/proxy-test.test.cjs +++ b/internal/gui/tests/proxy-test.test.cjs @@ -3,7 +3,8 @@ // works. Test asks Cloudflare's trace page through it and direct // (POST /api/proxy/check) and the line under the row's text says the // address each left from: working (two addresses), the same address both -// ways (it isn't taking the requests), the proxy failing, no proxy in +// ways with the proxy connected to (a TUN's one exit, #2369, not a +// failure) and without (the proxy passed by), the proxy failing, no proxy in // force, and the check itself failing. Testing… shows while it runs, the // result outlives a redraw of Settings and goes when the proxy changes. // Every language, Chromium and WebKit, at 1100 and 420 wide; no backend, @@ -59,10 +60,14 @@ function server(lang, st) { const US = { ip: "203.0.113.9", loc: "US", ms: 312 }, CN = { ip: "198.51.100.7", loc: "CN", ms: 41 }; // [name, proxy, answer, the line, a failure?] const CASES = (w) => [ - ["works", PROXY, { proxy: "http://127.0.0.1:7890", source: "settings", through: US, direct: CN }, + ["works", PROXY, { proxy: "http://127.0.0.1:7890", source: "settings", through: { ...US, proxied: true }, direct: CN }, w("Works: through the proxy requests leave from {where}, {ms} ms; direct ones from {direct}", { where: "203.0.113.9 (US)", ms: 312, direct: "198.51.100.7 (CN)" }), false], - ["same", PROXY, { proxy: "http://127.0.0.1:7890", source: "settings", through: CN, direct: CN }, - w("Through the proxy requests leave from {where}, the same address as direct ones: they leave as if there were no proxy", { where: "198.51.100.7 (CN)" }), true], + // #2369 (Emt-lin): a TUN gives both one exit, while the request through + // the proxy connected to it: not a failure + ["one-exit", PROXY, { proxy: "http://127.0.0.1:7890", source: "settings", through: { ...CN, proxied: true }, direct: CN }, + w("The proxy took the requests, but they leave from {where} as direct ones do: a TUN or system VPN may send every request out one exit, so the address can't tell the two paths apart", { where: "198.51.100.7 (CN)" }), false], + ["passed-by", PROXY, { proxy: "http://127.0.0.1:7890", source: "settings", through: CN, direct: CN }, + w("The request through the proxy didn't connect to it and leaves from {where}, the same address as direct ones: the proxy is passed by", { where: "198.51.100.7 (CN)" }), true], ["down", PROXY, { proxy: "http://127.0.0.1:7890", source: "settings", through: { error: "connect: connection refused" }, direct: CN }, w("The proxy failed: {error}. Direct requests leave from {direct}", { error: "connect: connection refused", direct: "198.51.100.7 (CN)" }), true], ["none", "", { source: "none", direct: CN },