The proxy test compared the exit IP through the proxy with the direct one,
and called the same address "they leave as if there were no proxy". Under
a TUN or system VPN every request leaves from one exit, so a working proxy
read as not in effect.
The leg through the proxy now records what it can see: the connection that
carried it (httptrace GotConn's RemoteAddr) and the proxy netproxy chose for
the URL. It is Proxied when that connection is the proxy's (same port, the
default by scheme; the host as an IP or resolved). With the same exit IP:
- Proxied: "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" (not a failure);
- not Proxied (bypassed by NO_PROXY or the system bypass list): "...didn't
connect to it ...: the proxy is passed by" (a failure).
The verdict is made only in app.js's proxyCheckText; no CLI, TUI or plugin
path makes it.
Run:
- go test -race -count=3 -run TestProxyCheck ./internal/gui/: ok.
New: TestProxyCheckSaysTheProxyTookItUnderOneExit (the proxy answers with
the direct leg's IP), TestProxyCheckSaysAProxyPassedBy (NO_PROXY),
TestProxyCheckConnectedToByName; TellsTheProxyFromDirect also checks
Proxied.
- Broken by hand: no Proxied assignment turns TellsTheProxyFromDirect and
SaysTheProxyTookItUnderOneExit red; Proxied always true turns
SaysAProxyPassedBy red; origin/main's app.js turns proxy-test's
"one-exit" case red.
- GUI: proxy-test.test.cjs 22/22 in Chromium and WebKit (1100 and 420, en,
zh, zh-TW, ja, de); gui-de, gui-ja, gui-zh-tw, i18n-keys 12/12.
- Real use: a local CONNECT proxy named by localhost, to the real
Cloudflare trace: through Proxied true, direct Proxied false, one
CONNECT, the same exit both ways (the reporter's case). A real TUN was
not tried.
- gofmt -l clean; go vet ./... (darwin), GOOS=linux and GOOS=windows
go vet -tags nogui ./..., and GOOS=windows go vet ./...: ok.
- Not fixed here: at 420px the proxy row's mode control overlaps a long
check line; origin/main's "down" message does the same.
Suite run here: HOME=<sandbox> go test -tags nogui -timeout 45m ./...: 62 ok,
internal/gateway FAIL only on TestOTelFastRequestTranscriptDiscovery/true
("request must still have a cached discovery miss"), open as #1811 (red
under load; load average ~170 here); it and its sibling pass -count=3
alone.
Full suite: SUITE_LINE
203 lines
6.2 KiB
Go
203 lines
6.2 KiB
Go
package gui
|
||
|
||
import (
|
||
"bufio"
|
||
"context"
|
||
"fmt"
|
||
"io"
|
||
"net"
|
||
"net/http"
|
||
"net/http/httptrace"
|
||
"net/url"
|
||
"strings"
|
||
"sync"
|
||
"time"
|
||
|
||
"github.com/yetone/magpie/internal/netproxy"
|
||
)
|
||
|
||
// Settings › Proxy's Test (#2076, c4ex): whether the proxy in force is the
|
||
// 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, 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"
|
||
|
||
// proxyTraceTimeout is how long each request may take.
|
||
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. 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"`
|
||
Proxied bool `json:"proxied,omitempty"`
|
||
}
|
||
|
||
// proxyCheck is the answer: the proxy in force (redacted) and where it came
|
||
// from, as Settings says it (netproxy.Describe), the request through it and
|
||
// the one without. Through is absent when no proxy is in force, since the
|
||
// request "through" it is the direct one.
|
||
type proxyCheck struct {
|
||
Proxy string `json:"proxy,omitempty"`
|
||
Source string `json:"source"`
|
||
Through *proxyLeg `json:"through,omitempty"`
|
||
Direct proxyLeg `json:"direct"`
|
||
}
|
||
|
||
// checkProxy asks the trace page through via (a context as a vendor
|
||
// request carries) and direct, at once. through is false when no proxy is
|
||
// in force: only the direct request is made.
|
||
func checkProxy(ctx context.Context, client *http.Client, via context.Context, through bool) proxyCheck {
|
||
var out proxyCheck
|
||
var wg sync.WaitGroup
|
||
if through {
|
||
out.Through = &proxyLeg{}
|
||
wg.Add(1)
|
||
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) }()
|
||
wg.Wait()
|
||
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 {
|
||
ctx, cancel := context.WithTimeout(ctx, proxyTraceTimeout)
|
||
defer cancel()
|
||
start := time.Now()
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, proxyTraceURL, nil)
|
||
if err != nil {
|
||
return proxyLeg{Error: err.Error()}
|
||
}
|
||
resp, err := client.Do(req)
|
||
if err != nil {
|
||
// a proxy's password can be in the error's URL
|
||
return proxyLeg{Error: redactProxyErr(err.Error())}
|
||
}
|
||
defer resp.Body.Close()
|
||
var leg proxyLeg
|
||
leg.Ms = time.Since(start).Milliseconds()
|
||
if resp.StatusCode != http.StatusOK {
|
||
leg.Error = fmt.Sprintf("HTTP %d", resp.StatusCode)
|
||
return leg
|
||
}
|
||
sc := bufio.NewScanner(io.LimitReader(resp.Body, 16<<10))
|
||
for sc.Scan() {
|
||
k, v, ok := strings.Cut(sc.Text(), "=")
|
||
if !ok {
|
||
continue
|
||
}
|
||
switch k {
|
||
case "ip":
|
||
leg.IP = strings.TrimSpace(v)
|
||
case "loc":
|
||
leg.Loc = strings.TrimSpace(v)
|
||
}
|
||
}
|
||
if leg.IP == "" {
|
||
leg.Error = "no address in the answer"
|
||
}
|
||
return leg
|
||
}
|
||
|
||
// redactProxyErr takes a user:password@ out of an error's text.
|
||
func redactProxyErr(s string) string {
|
||
words := strings.Fields(s)
|
||
for i, w := range words {
|
||
if strings.Contains(w, "://") && strings.Contains(w, "@") {
|
||
q := strings.Trim(w, `"':,`)
|
||
if r := netproxy.Redact(q); r != "" {
|
||
words[i] = strings.Replace(w, q, r, 1)
|
||
} else {
|
||
words[i] = "[proxy]"
|
||
}
|
||
}
|
||
}
|
||
return strings.Join(words, " ")
|
||
}
|
||
|
||
func handleProxyCheck(rw http.ResponseWriter, r *http.Request) {
|
||
proxy, source := netproxy.Describe()
|
||
through := source == "settings" || source == "environment" || source == "system"
|
||
out := checkProxy(r.Context(), http.DefaultClient, r.Context(), through)
|
||
out.Proxy, out.Source = proxy, source
|
||
writeJSON(rw, out)
|
||
}
|