// Package egress provides the attempt's private, restricted CONNECT endpoint. package egress import ( "crypto/sha256" "encoding/hex" "encoding/json" "errors" "fmt" "net/netip" "sort" "amadan.net/oficina/equipo/internal/runner/provider" "amadan.net/oficina/equipo/internal/websource" ) type Policy struct { Provider string // Harness is set only when it is not the provider's own CLI, so the // hash of a Claude or Codex policy is what it always was. Harness string `json:",omitempty"` Hosts []string } // Route is one (harness, provider) pair the relay may carry. type Route struct{ Harness, Provider string } // routes is the egress table (connectors spec ยง2.2): for each harness // and provider, the only hosts its namespace reaches. The provider's own // CLI is its own harness. A metered connection reaches its provider's // API host and nothing else; a later connector adds a row here. var routes = map[Route][]string{ {"claude", "claude"}: {"api.anthropic.com"}, {"codex", "codex"}: {"chatgpt.com"}, {"pi", "tensorx"}: {"api.tensorx.ai"}, // OpenCode on OpenCode Go (slice S5): the inference host only. {"opencode", "opencode-go"}: {"opencode.ai"}, } // Routes lists the egress table, sorted, for the checks that compare it // with the providers' records. func Routes() map[Route][]string { out := map[Route][]string{} for r, hosts := range routes { out[r] = append([]string(nil), hosts...) } return out } func route(harness, provider string) (Route, []string, bool) { if harness == "" { harness = provider } r := Route{Harness: harness, Provider: provider} hosts, ok := routes[r] return r, hosts, ok } var ErrConflict = errors.New("egress_certified_auth_conflict") var errPolicy = errors.New("egress_policy_rejected") // ProviderPolicy consumes an installed contract, never model or permission input. // V1 rejects auth hosts; expired logins need a new sign-in. func ProviderPolicy(c provider.NetworkContract, dedicated bool) (Policy, error) { if c.Conflict != "" { return Policy{}, fmt.Errorf("%w: %s", ErrConflict, c.Conflict) } r, hosts, ok := route(c.Harness, c.Provider) if !ok { return Policy{}, errPolicy } p := Policy{Provider: c.Provider, Hosts: append([]string(nil), hosts...)} if r.Harness != r.Provider { p.Harness = r.Harness } if c.AuthHost != "" { return Policy{}, errPolicy } return p, nil } // SignInPolicy is Claude's sign-in's: provider.SignInHosts, under the // same installed contract as Claude's launches. func SignInPolicy(c provider.NetworkContract) (Policy, error) { if c.Conflict != "" { return Policy{}, fmt.Errorf("%w: %s", ErrConflict, c.Conflict) } if c.Provider != "claude" || c.AuthHost != "" { return Policy{}, errPolicy } return Policy{Provider: "claude", Hosts: append([]string(nil), provider.SignInHosts...)}, nil } // hostname is the relay's host rule, shared with Research's URL contract. func hostname(s string) bool { return websource.Hostname(s) } // Hash binds the exact host set, provider and harness, for a route in // the table. The caller's slice is never retained. func (p Policy) Hash() (string, error) { if _, _, ok := route(p.Harness, p.Provider); !ok || len(p.Hosts) == 0 || p.Harness == p.Provider { return "", errPolicy } hosts := append([]string(nil), p.Hosts...) sort.Strings(hosts) for i, h := range hosts { if !hostname(h) || (i > 0 && h == hosts[i-1]) { return "", errPolicy } } b, _ := json.Marshal(Policy{Provider: p.Provider, Harness: p.Harness, Hosts: hosts}) hash := sha256.Sum256(b) return hex.EncodeToString(hash[:]), nil } // PublicAddress reports an address the relay, and Research's fetcher, may // dial: globally routed unicast, outside every private, shared, reserved, // documentation and transition range (websource.PublicAddress, the one // list the site inventory shares). The runner's own provider requests // (an API key's probe) use it too. func PublicAddress(a netip.Addr) bool { return publicAddress(a) } func publicAddress(a netip.Addr) bool { return websource.PublicAddress(a) }