diff --git a/internal/gateway/codex_http1.go b/internal/gateway/codex_http1.go new file mode 100644 index 000000000..5ac2e863c --- /dev/null +++ b/internal/gateway/codex_http1.go @@ -0,0 +1,53 @@ +package gateway + +import ( + "crypto/tls" + "net/http" + "net/url" + "time" + + "github.com/yetone/magpie/internal/netproxy" + "github.com/yetone/magpie/internal/provider" +) + +// upstreamTLS is what the gateway's transports dial TLS with: nil, the +// system's roots, but for a test trusting its own server. +var upstreamTLS *tls.Config + +// upstreamTransport is the gateway's transport to the vendors: a provider +// with a proxy of its own is sent through a transport kept for that proxy +// (#237). protocols nil speaks HTTP/2 wherever the server offers it. +func upstreamTransport(protocols *http.Protocols) http.RoundTripper { + return netproxy.Dispatch(&http.Transport{ + Proxy: netproxy.Func, + ResponseHeaderTimeout: 10 * time.Minute, + MaxIdleConnsPerHost: 8, + IdleConnTimeout: 90 * time.Second, + ForceAttemptHTTP2: protocols == nil, + Protocols: protocols, + TLSClientConfig: upstreamTLS, + }) +} + +// codexHTTP1 sends what goes to the ChatGPT backend (provider.CodexBase's +// host, chatgpt.com) over HTTP/1.1, and the rest over next. Codex itself +// offers no ALPN at all and so speaks HTTP/1.1 to it, and Pi's own Codex +// provider a WebSocket or Node's fetch, HTTP/1.1 both; the gateway's +// HTTP/2 put every Codex stream, every account's, on one connection, +// where Cloudflare in front reset one now and then mid-reply ("stream +// error: stream ID 21; INTERNAL_ERROR; received from peer", jasonz3g on +// X: Codex in Pi through magpie, never Codex or Pi alone). +type codexHTTP1 struct{ next, h1 http.RoundTripper } + +func newCodexHTTP1(next http.RoundTripper) codexHTTP1 { + var h1 http.Protocols + h1.SetHTTP1(true) + return codexHTTP1{next: next, h1: upstreamTransport(&h1)} +} + +func (t codexHTTP1) RoundTrip(r *http.Request) (*http.Response, error) { + if u, err := url.Parse(provider.CodexBase); err == nil && r.URL.Host == u.Host { + return t.h1.RoundTrip(r) + } + return t.next.RoundTrip(r) +} diff --git a/internal/gateway/codex_http1_test.go b/internal/gateway/codex_http1_test.go new file mode 100644 index 000000000..7f8171144 --- /dev/null +++ b/internal/gateway/codex_http1_test.go @@ -0,0 +1,53 @@ +package gateway + +import ( + "crypto/tls" + "crypto/x509" + "io" + "net/http" + "net/http/httptest" + "testing" + + "github.com/yetone/magpie/internal/provider" +) + +// The ChatGPT backend is spoken to over HTTP/1.1, as Codex and Pi speak +// to it, while every other vendor offering HTTP/2 still gets it: on one +// HTTP/2 connection every Codex stream was reset now and then mid-reply +// (jasonz3g on X, "stream ID 21; INTERNAL_ERROR; received from peer"). +func TestCodexBackendOverHTTP1(t *testing.T) { + fresh(t) + proto := func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, r.Proto) } + chatgpt := httptest.NewUnstartedServer(http.HandlerFunc(proto)) + chatgpt.EnableHTTP2 = true + chatgpt.StartTLS() + t.Cleanup(chatgpt.Close) + other := httptest.NewUnstartedServer(http.HandlerFunc(proto)) + other.EnableHTTP2 = true + other.StartTLS() + t.Cleanup(other.Close) + pool := x509.NewCertPool() + pool.AddCert(chatgpt.Certificate()) + pool.AddCert(other.Certificate()) + was, wasTLS := provider.CodexBase, upstreamTLS + provider.CodexBase = chatgpt.URL + "/backend-api/codex" + upstreamTLS = &tls.Config{RootCAs: pool} + t.Cleanup(func() { provider.CodexBase, upstreamTLS = was, wasTLS }) + s := New() + got := func(u string) string { + t.Helper() + res, err := s.client.Post(u, "application/json", nil) + if err != nil { + t.Fatal(err) + } + defer res.Body.Close() + b, _ := io.ReadAll(res.Body) + return string(b) + } + if p := got(provider.CodexBase + "/responses"); p != "HTTP/1.1" { + t.Errorf("the ChatGPT backend was asked over %s, not HTTP/1.1", p) + } + if p := got(other.URL + "/v1/responses"); p != "HTTP/2.0" { + t.Errorf("another vendor was asked over %s, not HTTP/2.0", p) + } +} diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.go index 90d474b3b..17f8d527f 100644 --- a/internal/gateway/gateway.go +++ b/internal/gateway/gateway.go @@ -30,7 +30,6 @@ import ( "github.com/yetone/magpie/internal/awake" - "github.com/yetone/magpie/internal/netproxy" "github.com/yetone/magpie/internal/plugin" "github.com/yetone/magpie/internal/provider" "github.com/yetone/magpie/internal/redact" @@ -341,16 +340,10 @@ type Server struct { func New() *Server { redact.SetKeyPath(filepath.Join(settings.Dir(), "redact.key")) return &Server{ - // a provider with a proxy of its own is sent through a transport - // kept for that proxy (#237); a request counted against a MaxRPM - // waits for room in its minute first (rpm.go) - client: &http.Client{Transport: rpmTransport{netproxy.Dispatch(&http.Transport{ - Proxy: netproxy.Func, - ResponseHeaderTimeout: 10 * time.Minute, - MaxIdleConnsPerHost: 8, - IdleConnTimeout: 90 * time.Second, - ForceAttemptHTTP2: true, - })}}, + // a request counted against a MaxRPM waits for room in its minute + // first (rpm.go); the ChatGPT backend is spoken to over HTTP/1.1 + // (codex_http1.go) + client: &http.Client{Transport: rpmTransport{newCodexHTTP1(upstreamTransport(nil))}}, unfit: make(map[string]bool), subscription: newSubscriptionBridge(), debug: os.Getenv("MAGPIE_DEBUG") != "",