Settings > Network has a new "App updates" row under the plugin proxy row:
Same as above (default, today's behaviour), Direct, Custom or a saved proxy.
It is Settings.UpdateProxy / UpdateProxyCustom, kept per computer
(KeepOwn), left out of auto-backups with the other proxies, and listed in
the saved proxies' "in use" check, so a saved proxy used by updates can't be
deleted. update.proxied() wraps its transport: a request that names no
proxy of its own (magpie update --proxy does) takes the setting; unset, it
follows the global proxy as before. A value a user already set is never
changed.
Per-model proxy inside one provider is not added: a proxy belongs to a
provider, an account or a key, so a provider can't split by model without a
second provider on the same URL (noted in providers-accounts.md).
app.js stays within TestCoreScriptBudget by cutting five long comments, no
raise.
Run (Linux via wsl-run, rebased on origin/main 961427f12): go test of
internal/update, settings, settingsapi, backup and internal/gui (-tags
nogui) incl. TestCoreScriptBudget and the settings-keep guards; new
settings-update-proxy.test.cjs in chromium and webkit, 5 languages, 1100
and 420px; gofmt; go vet for darwin, windows and linux (nogui) on the
touched packages.
Broken by hand: removing the updateProxy wrapper turns
TestUpdateTakesItsOwnProxy red.
GUI reds named: settings-proxy-auth "optional proxy credentials" webkit
(expected 'name', actual ''; red on origin/main too, #5562, seen again);
saved-proxies "webkit de 420px" stopped passing with the third row because
the add form's Save button sat 1px below a 760px window and WebKit's
scrollTop stuck at 214 (#6130); that test's window is now 900px high.
Not tried: a real update behind a real proxy, the Windows/macOS apps,
WebKitGTK.
251 lines
6.5 KiB
Go
251 lines
6.5 KiB
Go
package gui
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/netproxy"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// Named proxies (the Network page's list): kept once, chosen by reference
|
|
// wherever a proxy can be chosen. The page is sent the address without its
|
|
// login, the username and whether a password is set; the password is
|
|
// written and never read back, as the GitHub token is.
|
|
|
|
// savedProxyJSON is a saved proxy as the page sees it.
|
|
type savedProxyJSON struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
URL string `json:"url"`
|
|
User string `json:"user,omitempty"`
|
|
HasPass bool `json:"hasPass,omitempty"`
|
|
// Uses is where it is chosen: Settings' own two, then providers,
|
|
// accounts and keys
|
|
Uses []proxyUseJSON `json:"uses,omitempty"`
|
|
Check *proxyTestJSON `json:"check,omitempty"`
|
|
}
|
|
|
|
type proxyUseJSON struct {
|
|
Where string `json:"where,omitempty"` // "global", "plugins" or "updates"; else a provider
|
|
Provider string `json:"provider,omitempty"`
|
|
Account string `json:"account,omitempty"`
|
|
}
|
|
|
|
// proxyTestJSON is one connectivity check: whether a request got through
|
|
// the proxy, how long it took, and where it left from.
|
|
type proxyTestJSON struct {
|
|
OK bool `json:"ok"`
|
|
Ms int64 `json:"ms,omitempty"`
|
|
IP string `json:"ip,omitempty"`
|
|
Loc string `json:"loc,omitempty"`
|
|
Error string `json:"error,omitempty"`
|
|
At int64 `json:"at"`
|
|
}
|
|
|
|
// proxyTestClient makes the check's request; a test swaps its own.
|
|
var proxyTestClient = http.DefaultClient
|
|
|
|
var (
|
|
proxyTestsMu sync.Mutex
|
|
proxyTests = map[string]proxyTestJSON{}
|
|
)
|
|
|
|
func savedProxies() []savedProxyJSON {
|
|
cur := settings.Load()
|
|
var out []savedProxyJSON
|
|
proxyTestsMu.Lock()
|
|
defer proxyTestsMu.Unlock()
|
|
for _, p := range cur.Proxies {
|
|
ref := settings.ProxyRef(p.ID)
|
|
row := savedProxyJSON{ID: p.ID, Name: p.Name, URL: p.URL, User: p.User, HasPass: p.Pass != ""}
|
|
if strings.TrimSpace(cur.Proxy) == ref {
|
|
row.Uses = append(row.Uses, proxyUseJSON{Where: "global"})
|
|
}
|
|
if strings.TrimSpace(cur.PluginProxy) == ref {
|
|
row.Uses = append(row.Uses, proxyUseJSON{Where: "plugins"})
|
|
}
|
|
if strings.TrimSpace(cur.UpdateProxy) == ref {
|
|
row.Uses = append(row.Uses, proxyUseJSON{Where: "updates"})
|
|
}
|
|
for _, u := range provider.ProxyUses(p.ID) {
|
|
row.Uses = append(row.Uses, proxyUseJSON{Provider: u.Provider, Account: u.Account})
|
|
}
|
|
if c, ok := proxyTests[p.ID]; ok {
|
|
c := c
|
|
row.Check = &c
|
|
}
|
|
out = append(out, row)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// proxyIn is what the page sends to save or test a proxy. A nil Pass keeps
|
|
// the saved password (an edit that left it blank).
|
|
type proxyIn struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
URL string `json:"url"`
|
|
User string `json:"user"`
|
|
Pass *string `json:"pass"`
|
|
}
|
|
|
|
func (in proxyIn) named(cur []settings.NamedProxy) settings.NamedProxy {
|
|
p := settings.NamedProxy{ID: strings.TrimSpace(in.ID), Name: in.Name, URL: in.URL, User: strings.TrimSpace(in.User)}
|
|
if in.Pass != nil {
|
|
p.Pass = *in.Pass
|
|
} else {
|
|
for _, c := range cur {
|
|
if c.ID == p.ID {
|
|
p.Pass = c.Pass
|
|
}
|
|
}
|
|
}
|
|
return p
|
|
}
|
|
|
|
func handleProxySave(rw http.ResponseWriter, r *http.Request) {
|
|
var in proxyIn
|
|
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
s := settings.Load()
|
|
p := in.named(s.Proxies)
|
|
found := false
|
|
if p.ID == "" {
|
|
p.ID = settings.NewProxyID()
|
|
if in.Pass == nil {
|
|
p.Pass = ""
|
|
}
|
|
}
|
|
for i := range s.Proxies {
|
|
if s.Proxies[i].ID == p.ID {
|
|
s.Proxies[i], found = p, true
|
|
}
|
|
}
|
|
if !found {
|
|
s.Proxies = append(s.Proxies, p)
|
|
}
|
|
if err := settings.Save(s); err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
provider.ProxyEdited() // Claude Code's env holds an address, not the name
|
|
// what it was checked as no longer says anything about it
|
|
proxyTestsMu.Lock()
|
|
delete(proxyTests, p.ID)
|
|
proxyTestsMu.Unlock()
|
|
writeJSON(rw, settingsState())
|
|
}
|
|
|
|
func handleProxyDelete(rw http.ResponseWriter, r *http.Request) {
|
|
var in struct {
|
|
ID string `json:"id"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
s := settings.Load()
|
|
var keep []settings.NamedProxy
|
|
for _, p := range s.Proxies {
|
|
if p.ID != in.ID {
|
|
keep = append(keep, p)
|
|
}
|
|
}
|
|
if len(keep) == len(s.Proxies) {
|
|
writeJSON(rw, settingsState())
|
|
return
|
|
}
|
|
for _, row := range savedProxies() {
|
|
if row.ID == in.ID && len(row.Uses) > 0 {
|
|
rw.Header().Set("Content-Type", "application/json")
|
|
rw.WriteHeader(http.StatusConflict)
|
|
_ = json.NewEncoder(rw).Encode(map[string]any{"error": "the proxy is in use", "uses": row.Uses})
|
|
return
|
|
}
|
|
}
|
|
s.Proxies = keep
|
|
if err := settings.Save(s); err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
proxyTestsMu.Lock()
|
|
delete(proxyTests, in.ID)
|
|
proxyTestsMu.Unlock()
|
|
writeJSON(rw, settingsState())
|
|
}
|
|
|
|
// handleProxyTest asks the trace page through one saved proxy (by id), or
|
|
// through one being typed (its address and login, unsaved).
|
|
func handleProxyTest(rw http.ResponseWriter, r *http.Request) {
|
|
var in proxyIn
|
|
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
cur := settings.Load().Proxies
|
|
var p settings.NamedProxy
|
|
if strings.TrimSpace(in.ID) != "" {
|
|
for _, c := range cur {
|
|
if c.ID == strings.TrimSpace(in.ID) {
|
|
p = c
|
|
}
|
|
}
|
|
if p.ID == "" {
|
|
fail(rw, fmt.Errorf("the saved proxy isn't there"))
|
|
return
|
|
}
|
|
if strings.TrimSpace(in.URL) != "" { // an edit not yet saved
|
|
p = in.named(cur)
|
|
}
|
|
} else {
|
|
p = in.named(cur)
|
|
p.Name = "test"
|
|
}
|
|
addr, err := p.Address()
|
|
if err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
// a typed address is checked as saving would
|
|
if err := settings.CheckProxy(addr); err != nil {
|
|
fail(rw, err)
|
|
return
|
|
}
|
|
out := testProxy(r.Context(), addr)
|
|
if in.ID != "" && strings.TrimSpace(in.URL) == "" {
|
|
proxyTestsMu.Lock()
|
|
proxyTests[p.ID] = out
|
|
proxyTestsMu.Unlock()
|
|
}
|
|
writeJSON(rw, out)
|
|
}
|
|
|
|
func testProxy(ctx context.Context, addr string) proxyTestJSON {
|
|
leg := throughLeg(netproxy.With(ctx, addr), proxyTestClient)
|
|
out := proxyTestJSON{Ms: leg.Ms, IP: leg.IP, Loc: leg.Loc, At: time.Now().Unix()}
|
|
switch {
|
|
case leg.Error != "":
|
|
out.Error = redactProxyErr(leg.Error)
|
|
case !leg.Proxied:
|
|
out.Error = "the request didn't go to the proxy"
|
|
default:
|
|
out.OK = true
|
|
}
|
|
return out
|
|
}
|
|
|
|
func proxyRoutes(mux *http.ServeMux) {
|
|
mux.HandleFunc("POST /api/proxies/save", handleProxySave)
|
|
mux.HandleFunc("POST /api/proxies/delete", handleProxyDelete)
|
|
mux.HandleFunc("POST /api/proxies/test", handleProxyTest)
|
|
}
|