Files
magpie/internal/provider/import_lan_test.go
T
yetone 6aa04f6e6a provider: a keyless relay on the user's own network imports and saves, as one on 127.0.0.1 does
iSnow_999 on X: their company's internal Codex relay is at
http://<LAN IP>:8080; imported from CC Switch, Codex couldn't reach it
through magpie. A CC Switch Codex entry with no key (requires_openai_auth
= false, auth {}) at a LAN address was listed as "it has no API key"
and left out, and the same provider couldn't be saved by hand ("needs an
API key"), while the same server at 127.0.0.1 was taken keyless.

keyOptional now also takes a host on the user's own network, read from
the URL alone (ownNetwork, no lookup since Ready asks often): a private,
link-local, loopback or carrier-grade NAT (Tailscale) address, or a
.local name, as netproxy's onLAN sends around a proxy. A keyless address
on the internet still needs its key. Every importer (CC Switch, Alma,
Codex, OpenCode) goes through imported(), and the editor, CLI and TUI
through Save, so the one rule covers them.

Ruled out against the code and on mote, sandbox HOME, fake relay on
mote's LAN address 10.0.0.116 at a free port, `magpie serve --web` built
from this tree, import through POST /api/importapps:
- a CC Switch Codex entry at http://10.0.0.116:<port>/v1 with a key
  imports with port, path, key and Responses kept, and /v1/responses
  through the gateway reaches the relay (Bearer sk-corp); with
  HTTP_PROXY/HTTPS_PROXY/ALL_PROXY at a dead 127.0.0.1:9 too (onLAN);
- a request with Codex's User-Agent, originator and
  x-codex-turn-metadata reaches the relay with them (sub2api's
  codex_cli_only gate reads these; port 8080 is sub2api's default).
After the fix, the same run with a keyless entry: imported, served, the
relay sees no Authorization header.

Tests: TestKeylessRelayOnOwnNetwork (import of a keyless LAN entry,
Save + Ready at 192.168/10/172.16/100.64/.local/[fd00::] addresses, a
keyless internet address still refused) is red on the old provider.go
and green now.
Run on mote at ae0327945 plus this change: go test ./internal/provider ./internal/tui
./internal/netproxy ok (98s, 10s, 4s); the full ./... suite was not run.
gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./..., GOOS=linux go build -tags nogui and
GOOS=windows go build ok.
Not tried: the reporter's own relay and config (not given); a host
with no dot (http://relay:8080) is still refused by the import's URL
check, which this change leaves alone: filed as #4028.
2026-10-28 16:59:10 +08:00

53 lines
2.5 KiB
Go

package provider
import (
"path/filepath"
"testing"
)
// A company's internal Codex relay at a LAN address that takes no key
// (X, iSnow_999: http://<LAN IP>:8080, imported from CC Switch) is taken
// as a local server at 127.0.0.1 is: imported, and saved by hand, keyless.
// A keyless server out on the internet still needs its key.
func TestKeylessRelayOnOwnNetwork(t *testing.T) {
isolate(t)
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("APPDATA", filepath.Join(home, "AppData", "Roaming"))
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
codex := func(base string) string {
return `{"auth":{},"config":"model_provider = \"corp\"\nmodel = \"gpt-5.5\"\n\n[model_providers.corp]\nname = \"corp\"\nbase_url = \"` + base + `\"\nwire_api = \"responses\"\nrequires_openai_auth = false\n"}`
}
sqliteFixture(t, filepath.Join(home, ".cc-switch", "cc-switch.db"),
`CREATE TABLE providers (id TEXT, app_type TEXT, name TEXT, settings_config TEXT, website_url TEXT, category TEXT, created_at INTEGER, sort_index INTEGER, PRIMARY KEY (id, app_type))`,
`INSERT INTO providers VALUES ('lan','codex','公司中转','`+codex("http://192.168.1.10:8080/v1")+`','','custom',1,0)`,
`INSERT INTO providers VALUES ('far','codex','Far relay','`+codex("https://relay.example.com/v1")+`','','custom',2,0)`,
)
items := itemsOf(t, "cc-switch")
if it := items["codex/lan"]; it.Skip != "" || it.Provider.Responses != "http://192.168.1.10:8080/v1" || it.Provider.Key != "" {
t.Fatalf("keyless LAN relay: %+v", it)
}
if it := items["codex/far"]; it.Skip != "it has no API key" {
t.Fatalf("keyless internet relay offered: %+v", it)
}
if _, err := ImportFromApps([]AppPick{{Source: "cc-switch", Ref: "codex/lan"}}); err != nil {
t.Fatal(err)
}
for _, base := range []string{"http://192.168.1.10:8080/v1", "http://10.0.0.5:8080/v1", "http://172.16.3.4:8080", "http://100.101.102.103:8080/v1", "http://gpu.local:8080/v1", "http://[fd00::5]:8080/v1"} {
p := Provider{ID: "lan-relay", Name: "LAN relay", Responses: base}
if err := Save(p); err != nil {
t.Errorf("%s: %v", base, err)
} else if q, _ := Find("lan-relay"); !q.Ready() {
t.Errorf("%s: saved but not ready", base)
}
}
for _, base := range []string{"https://relay.example.com/v1", "http://8.8.8.8:8080/v1"} {
if err := Save(Provider{ID: "far-relay", Name: "Far relay", Responses: base}); err == nil {
t.Errorf("%s saved with no key", base)
}
}
}