Files
yetone bc141ad834 update: install.sh and magpie update take --proxy and --mirror, and magpie update mirror keeps one for the app's updates too (akic404 on Discord: 安装和update如果没有梯子也是麻烦事,能否在install.sh和magpie update后加子选项?--proxy等可自定义或默认ghproxy) — install.sh had no options and fetched with curl alone, and magpie update took none; without a way out to GitHub both failed. Now both take --proxy <url> (http, https, socks5, socks5h; install.sh also MAGPIE_PROXY=) for the feed and the download, and --mirror <prefix> (or MAGPIE_MIRROR=), a GitHub download mirror the release file is fetched from as <prefix>https://github.com/…; only github.com / githubusercontent.com URLs are put through it. No mirror is used unless one is given: magpie names none and has none by default. The feed with each file's SHA-256 is always fetched from usemagpie.ai, never through the mirror, so a file the mirror changed is refused and deleted ("… from the mirror X does not match its checksum from https://usemagpie.ai; not installed"); macOS still checks the app's signing team. magpie update mirror <prefix>|off saves Settings' updateMirror (http(s) only), which the app's own background update follows too; magpie update --mirror off skips it once; the Settings page's save keeps it. The app's update already followed Settings → Proxy (#294); magpie update --proxy overrides it for one run. The environment's ALL_PROXY / all_proxy is now honoured when HTTPS_PROXY / HTTP_PROXY are unset (Go's ProxyFromEnvironment ignores it), with NO_PROXY as curl reads it. magpie update takes --flag value or --flag=value, and an unknown word is an error instead of an update. The help, README and the docs' FAQ (en/zh/ja) say how. install.test.mjs's proxy, mirror and tampered-mirror tests, TestAllProxyFromTheEnvironment, TestUpdateThroughAMirror, TestUpdateThroughAProxyGiven and TestUpdateFlags fail on the old code and pass; TestSettingsSaveKeepsWhatItDoesNotSend covers updateMirror. A dry run of install.sh as Linux amd64 into a temp HOME installed the real 0.1.773 with its SHA-256 checked, refused a local mirror serving a wrong file, and with --proxy to a closed port failed reaching the site through it. go vet, builds (darwin, GOOS=linux, GOOS=windows), sh -n, node --test site/install.test.mjs and go test -tags nogui -count=1 ./... under a temp HOME pass but TestClaudeIdentityServedWhileAsked, a timing test that passes alone. Not tried through a real proxy server or a real public mirror; no install.ps1 exists for Windows, and the app has no field for the mirror.
2026-10-04 00:19:47 +08:00

60 lines
2.0 KiB
Go

package netproxy
import (
"net/url"
"testing"
)
// ALL_PROXY, which curl reads and Go doesn't, is magpie's too when neither
// HTTPS_PROXY nor HTTP_PROXY is set: a shell with only
// ALL_PROXY=socks5://… gets magpie update through it (akic404 on Discord),
// and NO_PROXY still keeps hosts off it.
func TestAllProxyFromTheEnvironment(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
for _, k := range []string{"HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", "ALL_PROXY", "all_proxy", "NO_PROXY", "no_proxy"} {
t.Setenv(k, "")
}
sysCache.at, sysCache.p = farFuture, Proxy{}
t.Cleanup(func() { sysCache.at = zero })
t.Setenv("all_proxy", "socks5h://127.0.0.1:1080")
t.Setenv("NO_PROXY", "internal.example, .corp.example:443")
gh, _ := url.Parse("https://github.com/yetone/magpie-releases/releases/download/v1/magpie")
if p, err := For(gh); err != nil || p == nil || p.String() != "socks5h://127.0.0.1:1080" {
t.Fatalf("ALL_PROXY: %v %v", p, err)
}
if proxy, source := Describe(); proxy != "socks5h://127.0.0.1:1080" || source != "environment" {
t.Fatalf("described as %q (%s)", proxy, source)
}
for _, raw := range []string{"https://internal.example/x", "https://api.internal.example/x", "https://git.corp.example/x", "http://127.0.0.1:3/x"} {
u, _ := url.Parse(raw)
if p, _ := For(u); p != nil {
t.Errorf("%s went through %v despite NO_PROXY", raw, p)
}
}
t.Setenv("NO_PROXY", "*")
if p, _ := For(gh); p != nil {
t.Fatalf("NO_PROXY=* still proxied: %v", p)
}
}
func TestNoProxyList(t *testing.T) {
for _, c := range []struct {
host, list string
want bool
}{
{"github.com", "", false},
{"github.com", "github.com", true},
{"objects.github.com", "github.com", true},
{"notgithub.com", "github.com", false},
{"api.example.com", "*.example.com", true},
{"example.com", ".example.com", true},
{"example.com", "example.com:443", true},
{"localhost", "", true},
} {
if got := noProxy(c.host, c.list); got != c.want {
t.Errorf("noProxy(%q, %q) = %v, want %v", c.host, c.list, got, c.want)
}
}
}