c4ex: the proxy setting gave no way to see whether it takes effect, and
suggested asking a Cloudflare address.
What changed:
- POST /api/proxy/check (proxycheck.go) asks Cloudflare's trace page
(www.cloudflare.com/cdn-cgi/trace) twice at once, 10s each: once as a
vendor request without a proxy of its own goes (http.DefaultClient,
netproxy's Settings / *_PROXY / system proxy), once direct
(netproxy.With "direct"). Each answer is the ip= and loc= Cloudflare saw,
and the ms. With no proxy in force (source off/none) only the direct one
is made. The proxy is reported redacted (Describe); a failure's text has
user:password taken out.
- The Proxy row gets a Test button; the result goes on a line under the
row's text: works (two addresses), the same address both ways (shown as
a failure: the proxy isn't taking the requests), the proxy failing (with
the direct address), no proxy in force, the check failing. Testing… while
it runs; the result outlives a redraw and goes when the proxy changes.
Strings in zh, zh-TW, ja, de.
Decision (delegated): compare against a direct request rather than only
show the proxied address, since an address alone can't say whether the
proxy was used; Cloudflare's trace page as the reporter suggested (plain
text, no account, served worldwide).
Run:
- go test ./internal/gui -run TestProxyCheck: 5 tests (proxy vs direct;
no proxy asks only direct; a refusing proxy, password not in the error;
a page with no ip= fails; ALL_PROXY global proxy is gone around for the
direct leg). All loopback/.invalid, nothing leaves the machine. Broke
the direct leg's With("direct"): the global-proxy test goes red ("the
proxy asked 2 times").
- go test ./internal/gui -count=1: all pass except TestMainWindowCorners,
the load red filed as #1936 (same on origin/main).
- New proxy-test.test.cjs (5 languages x 1100/420, 6 cases each): chromium
11/11, webkit 11/11; with the same-address branch broken 0/11.
- proxy-test, settings-proxy-auth, proxy-own-wording, i18n-keys and six
settings-* tests: chromium 63/63; webkit 62/63, the red being
settings-proxy-auth zh once under the 2-wide run; it passed alone 5/5
and three more 2-wide runs with proxy-test 3x0 fails (noted on #2142).
- gofmt -l clean; go vet darwin, linux -tags nogui, windows clean.
Tried: checkProxy against the real www.cloudflare.com trace page, direct
leg only, from a temporary test (not committed): ip and loc read, 1.2s.
Not tried: a real proxy (Settings, *_PROXY or system) through to it.
130 lines
4.6 KiB
Go
130 lines
4.6 KiB
Go
package gui
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/netproxy"
|
|
)
|
|
|
|
// 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) {
|
|
t.Helper()
|
|
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")
|
|
}))
|
|
t.Cleanup(direct.Close)
|
|
proxy := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
*asked++
|
|
if r.URL.Host != "trace.example.invalid" || r.URL.Path != "/cdn-cgi/trace" {
|
|
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")
|
|
}))
|
|
t.Cleanup(proxy.Close)
|
|
old := proxyTraceURL
|
|
proxyTraceURL = "http://trace.example.invalid/cdn-cgi/trace"
|
|
t.Cleanup(func() { proxyTraceURL = old })
|
|
base := &http.Transport{Proxy: netproxy.Func}
|
|
dial := (&net.Dialer{}).DialContext
|
|
base.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
if addr == "trace.example.invalid:80" {
|
|
addr = direct.Listener.Addr().String()
|
|
}
|
|
return dial(ctx, network, addr)
|
|
}
|
|
tr := netproxy.Dispatch(base)
|
|
t.Cleanup(base.CloseIdleConnections)
|
|
return &http.Client{Transport: tr}, proxy.URL, asked
|
|
}
|
|
|
|
func TestProxyCheckTellsTheProxyFromDirect(t *testing.T) {
|
|
client, proxyURL, asked := proxyCheckRig(t)
|
|
ctx := context.Background()
|
|
got := checkProxy(ctx, client, netproxy.With(ctx, proxyURL), true)
|
|
if got.Through == nil || got.Through.IP != "203.0.113.9" || got.Through.Loc != "US" || got.Through.Error != "" {
|
|
t.Fatalf("through the proxy: %+v", got.Through)
|
|
}
|
|
if got.Direct.IP != "198.51.100.7" || got.Direct.Loc != "CN" || got.Direct.Error != "" {
|
|
t.Fatalf("direct: %+v", got.Direct)
|
|
}
|
|
if *asked != 1 {
|
|
t.Fatalf("the proxy was asked %d times, want 1 (the direct request went through it?)", *asked)
|
|
}
|
|
}
|
|
|
|
// The proxy in force for every request (here ALL_PROXY; Settings' and the
|
|
// system's are read the same way, through netproxy.For): the request
|
|
// through it goes as a vendor request with no proxy of its own would, and
|
|
// the direct one must go around it, or the two would always agree.
|
|
func TestProxyCheckGoesAroundTheGlobalProxyForDirect(t *testing.T) {
|
|
client, proxyURL, asked := proxyCheckRig(t)
|
|
t.Setenv("ALL_PROXY", proxyURL)
|
|
t.Setenv("NO_PROXY", "")
|
|
ctx := context.Background()
|
|
got := checkProxy(ctx, client, ctx, true)
|
|
if got.Through == nil || got.Through.IP != "203.0.113.9" {
|
|
t.Fatalf("through the global proxy: %+v", got.Through)
|
|
}
|
|
if got.Direct.IP != "198.51.100.7" || *asked != 1 {
|
|
t.Fatalf("direct: %+v, the proxy asked %d times (want 1)", got.Direct, *asked)
|
|
}
|
|
}
|
|
|
|
func TestProxyCheckWithNoProxyAsksOnlyDirect(t *testing.T) {
|
|
client, _, asked := proxyCheckRig(t)
|
|
ctx := context.Background()
|
|
got := checkProxy(ctx, client, ctx, false)
|
|
if got.Through != nil {
|
|
t.Fatalf("no proxy in force, yet a request through one: %+v", got.Through)
|
|
}
|
|
if got.Direct.IP != "198.51.100.7" || *asked != 0 {
|
|
t.Fatalf("direct: %+v, proxy asked %d", got.Direct, *asked)
|
|
}
|
|
}
|
|
|
|
func TestProxyCheckSaysAProxyDownWithoutItsPassword(t *testing.T) {
|
|
client, _, _ := proxyCheckRig(t)
|
|
l, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dead := l.Addr().String()
|
|
l.Close() // refused from here on
|
|
ctx := context.Background()
|
|
got := checkProxy(ctx, client, netproxy.With(ctx, "http://me:s3cret-pw@"+dead), true)
|
|
if got.Through == nil || got.Through.Error == "" || got.Through.IP != "" {
|
|
t.Fatalf("a proxy that refuses: %+v", got.Through)
|
|
}
|
|
if strings.Contains(got.Through.Error, "s3cret-pw") {
|
|
t.Fatalf("the error shows the proxy's password: %q", got.Through.Error)
|
|
}
|
|
if got.Direct.IP != "198.51.100.7" {
|
|
t.Fatalf("direct still answers: %+v", got.Direct)
|
|
}
|
|
}
|
|
|
|
func TestProxyCheckReadsNoAddressAsAFailure(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprint(rw, "<html>captive portal</html>")
|
|
}))
|
|
defer srv.Close()
|
|
old := proxyTraceURL
|
|
proxyTraceURL = srv.URL
|
|
defer func() { proxyTraceURL = old }()
|
|
if leg := traceLeg(context.Background(), srv.Client()); leg.Error == "" || leg.IP != "" {
|
|
t.Fatalf("a page with no ip= line: %+v", leg)
|
|
}
|
|
}
|