Documentation
¶
Index ¶
Constants ¶
const ( HostAnthropic = "api.anthropic.com" HostOpenAI = "api.openai.com" HostCopilot = "api.individual.githubcopilot.com" )
Known AI provider hosts.
const ( RequestTypeMITM = "mitm" RequestTypeTunneled = "tunneled" )
const ( // ProxyAuthRealm is the realm used in Proxy-Authenticate challenges. // The realm helps clients identify which credentials to use. ProxyAuthRealm = `"Coder AI Gateway Proxy"` )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CertCache ¶
type CertCache struct {
// contains filtered or unexported fields
}
CertCache implements goproxy.CertStorage to cache generated leaf certificates in memory. Certificate generation is expensive (RSA key generation + signing), so caching avoids repeated generation for the same hostname during MITM.
func NewCertCache ¶
func NewCertCache() *CertCache
NewCertCache creates a new certificate cache that maps hostnames to their generated TLS certificates.
func (*CertCache) Fetch ¶
func (c *CertCache) Fetch(hostname string, genFunc func() (*tls.Certificate, error)) (*tls.Certificate, error)
Fetch retrieves a cached certificate for the given hostname, or generates and caches a new one using the provided generator function.
Uses singleflight to ensure concurrent requests for the same hostname share a single in-flight generation rather than waiting on a mutex. This means only one goroutine generates the certificate while others wait on the result directly.
type Metrics ¶
type Metrics struct {
// ConnectSessionsTotal counts CONNECT sessions established.
// Labels: type (mitm/tunneled)
ConnectSessionsTotal *prometheus.CounterVec
// MITMRequestsTotal counts MITM requests handled by the proxy.
// Labels: provider
MITMRequestsTotal *prometheus.CounterVec
// InflightMITMRequests tracks the number of MITM requests currently being processed.
// Labels: provider
InflightMITMRequests *prometheus.GaugeVec
// MITMResponsesTotal counts MITM responses by HTTP status code.
// Labels: code (HTTP status code), provider
// Cardinality is bounded: ~100 used status codes x few providers.
MITMResponsesTotal *prometheus.CounterVec
// ProviderInfo is one series per configured provider; value is
// always 1 and the status label carries the alertable signal.
// Labels: provider_name, provider_type, status.
ProviderInfo *prometheus.GaugeVec
// ProvidersLastReloadTimestampSeconds is the unix timestamp of the
// last reload attempt, success or failure.
ProvidersLastReloadTimestampSeconds prometheus.Gauge
// ProvidersLastReloadSuccessTimestampSeconds is the unix timestamp
// of the last reload that successfully refreshed the router. A gap
// against ProvidersLastReloadTimestampSeconds means the loop is
// firing but the refresh function is failing.
ProvidersLastReloadSuccessTimestampSeconds prometheus.Gauge
// contains filtered or unexported fields
}
Metrics holds all prometheus metrics for aibridgeproxyd.
func NewMetrics ¶
func NewMetrics(reg prometheus.Registerer) *Metrics
NewMetrics creates and registers all metrics for aibridgeproxyd.
func (*Metrics) Unregister ¶
func (m *Metrics) Unregister()
Unregister removes all metrics from the registerer.
type Options ¶
type Options struct {
// ListenAddr is the address the proxy server will listen on.
ListenAddr string
// TLSCertFile is the path to the TLS certificate file for the proxy listener.
TLSCertFile string
// TLSKeyFile is the path to the TLS private key file for the proxy listener.
TLSKeyFile string
// GatewayURL is the base URL that receives intercepted AI provider
// requests. It may include a path prefix, such as /api/v2/ai-gateway
// when forwarding through coderd.
GatewayURL string
// MITMCertFile is the path to the CA certificate file used for MITM.
MITMCertFile string
// MITMKeyFile is the path to the CA private key file used for MITM.
MITMKeyFile string
// AllowedPorts is the list of ports allowed for CONNECT requests.
// Defaults to ["80", "443"] if empty.
AllowedPorts []string
// CertStore is an optional certificate cache for MITM. If nil, a default
// cache is created. Exposed for testing.
CertStore goproxy.CertStorage
// UpstreamProxy is the URL of an upstream HTTP proxy to chain tunneled
// (non-provider-host) requests through. If empty, tunneled requests connect
// directly to their destinations.
// Format: http://[user:pass@]host:port or https://[user:pass@]host:port
UpstreamProxy string
// UpstreamProxyCA is the path to a PEM-encoded CA certificate file to trust
// for the upstream proxy's TLS connection. Only needed for HTTPS upstream
// proxies with certificates not trusted by the system. If empty, the system
// certificate pool is used.
UpstreamProxyCA string
// AllowedPrivateCIDRs is a list of CIDR ranges that are permitted even
// though they fall within blocked private/reserved IP ranges. This allows
// access to specific internal networks while keeping all other private
// ranges blocked. If empty, all private ranges are blocked.
AllowedPrivateCIDRs []string
// NewDumper, when non-nil, is called for each MITM request to create
// a RoundTripDumper that writes .req.txt and .resp.txt files. The
// caller is responsible for constructing the dumper with the correct
// base path.
NewDumper func(provider, requestID string) RoundTripDumper
// Metrics is the prometheus metrics instance for recording proxy metrics.
// If nil, metrics will not be recorded.
Metrics *Metrics
// RefreshProviders, when set, is invoked by Server.Reload to fetch
// the live provider snapshot used to derive the MITM host set and
// host -> provider-name routing. Nil disables hot-reload.
RefreshProviders RefreshProvidersFunc
}
Options configures the AI Bridge Proxy server.
type ProviderReload ¶ added in v2.34.0
type ProviderReload struct {
Providers []ReloadedProvider
}
ProviderReload is the result of a single refresh pass: every configured provider with its classification.
type RefreshProvidersFunc ¶ added in v2.34.0
type RefreshProvidersFunc func(ctx context.Context) (ProviderReload, error)
RefreshProvidersFunc returns the live provider classification used by Reload to rebuild the proxy's routing snapshot.
type ReloadedProvider ¶ added in v2.34.0
type ReloadedProvider struct {
aibridged.ProviderOutcome
Host string
}
ReloadedProvider is the classification of one ai_providers row. Host is the routable hostname; it's populated only when the embedded outcome's Status == aibridged.ProviderStatusEnabled.
type RoundTripDumper ¶ added in v2.34.0
type RoundTripDumper interface {
DumpRequest(*http.Request) error
DumpResponse(*http.Response) error
DumpError(error) error
}
RoundTripDumper captures an HTTP request/response pair to disk.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is the AI MITM (Man-in-the-Middle) proxy server. It is responsible for:
- intercepting HTTPS requests to AI providers
- decrypting requests using the configured MITM CA certificate
- forwarding requests to AI Gateway for processing
func (*Server) Addr ¶
Addr returns the address the server is listening on. This is useful when the server was started with port 0.
func (*Server) GatewayURL ¶ added in v2.36.0
GatewayURL returns the parsed AI Gateway URL.
func (*Server) Handler ¶
Handler returns an HTTP handler for the AI Bridge Proxy's HTTP endpoints. This is separate from the proxy server itself and is used by coderd to serve endpoints like the CA certificate.
func (*Server) IsTLSListener ¶ added in v2.32.0
IsTLSListener reports whether the proxy listener is serving TLS.