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
3.9 KiB
Go
130 lines
3.9 KiB
Go
package gui
|
||
|
||
import (
|
||
"bufio"
|
||
"context"
|
||
"fmt"
|
||
"io"
|
||
"net/http"
|
||
"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. Different addresses say the proxy is taking the
|
||
// requests; the same address says they leave as if there were none.
|
||
|
||
// 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.
|
||
type proxyLeg struct {
|
||
IP string `json:"ip,omitempty"`
|
||
Loc string `json:"loc,omitempty"`
|
||
Ms int64 `json:"ms,omitempty"`
|
||
Error string `json:"error,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 = traceLeg(via, client) }()
|
||
}
|
||
wg.Add(1)
|
||
go func() { defer wg.Done(); out.Direct = traceLeg(netproxy.With(ctx, "direct"), client) }()
|
||
wg.Wait()
|
||
return out
|
||
}
|
||
|
||
// 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)
|
||
}
|