oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1// Package egress provides the attempt's private, restricted CONNECT endpoint.
2package egress
3
4import (
5 "crypto/sha256"
6 "encoding/hex"
7 "encoding/json"
8 "errors"
9 "fmt"
10 "net/netip"
11 "sort"
12
13 "amadan.net/oficina/equipo/internal/runner/provider"
14 "amadan.net/oficina/equipo/internal/websource"
15)
16
17type Policy struct {
18 Provider string
19 // Harness is set only when it is not the provider's own CLI, so the
20 // hash of a Claude or Codex policy is what it always was.
21 Harness string `json:",omitempty"`
22 Hosts []string
23}
24
25// Route is one (harness, provider) pair the relay may carry.
26type Route struct{ Harness, Provider string }
27
28// routes is the egress table (connectors spec §2.2): for each harness
29// and provider, the only hosts its namespace reaches. The provider's own
30// CLI is its own harness. A metered connection reaches its provider's
31// API host and nothing else; a later connector adds a row here.
32var routes = map[Route][]string{
33 {"claude", "claude"}: {"api.anthropic.com"},
34 {"codex", "codex"}: {"chatgpt.com"},
35 {"pi", "tensorx"}: {"api.tensorx.ai"},
36 // OpenCode on OpenCode Go (slice S5): the inference host only.
37 {"opencode", "opencode-go"}: {"opencode.ai"},
38}
39
40// Routes lists the egress table, sorted, for the checks that compare it
41// with the providers' records.
42func Routes() map[Route][]string {
43 out := map[Route][]string{}
44 for r, hosts := range routes {
45 out[r] = append([]string(nil), hosts...)
46 }
47 return out
48}
49
50func route(harness, provider string) (Route, []string, bool) {
51 if harness == "" {
52 harness = provider
53 }
54 r := Route{Harness: harness, Provider: provider}
55 hosts, ok := routes[r]
56 return r, hosts, ok
57}
58
59var ErrConflict = errors.New("egress_certified_auth_conflict")
60
61var errPolicy = errors.New("egress_policy_rejected")
62
63// ProviderPolicy consumes an installed contract, never model or permission input.
64// V1 rejects auth hosts; expired logins need a new sign-in.
65func ProviderPolicy(c provider.NetworkContract, dedicated bool) (Policy, error) {
66 if c.Conflict != "" {
67 return Policy{}, fmt.Errorf("%w: %s", ErrConflict, c.Conflict)
68 }
69 r, hosts, ok := route(c.Harness, c.Provider)
70 if !ok {
71 return Policy{}, errPolicy
72 }
73 p := Policy{Provider: c.Provider, Hosts: append([]string(nil), hosts...)}
74 if r.Harness != r.Provider {
75 p.Harness = r.Harness
76 }
77 if c.AuthHost != "" {
78 return Policy{}, errPolicy
79 }
80
81 return p, nil
82}
83
84// SignInPolicy is Claude's sign-in's: provider.SignInHosts, under the
85// same installed contract as Claude's launches.
86func SignInPolicy(c provider.NetworkContract) (Policy, error) {
87 if c.Conflict != "" {
88 return Policy{}, fmt.Errorf("%w: %s", ErrConflict, c.Conflict)
89 }
90 if c.Provider != "claude" || c.AuthHost != "" {
91 return Policy{}, errPolicy
92 }
93 return Policy{Provider: "claude", Hosts: append([]string(nil), provider.SignInHosts...)}, nil
94}
95
96// hostname is the relay's host rule, shared with Research's URL contract.
97func hostname(s string) bool { return websource.Hostname(s) }
98
99// Hash binds the exact host set, provider and harness, for a route in
100// the table. The caller's slice is never retained.
101func (p Policy) Hash() (string, error) {
102 if _, _, ok := route(p.Harness, p.Provider); !ok || len(p.Hosts) == 0 || p.Harness == p.Provider {
103 return "", errPolicy
104 }
105 hosts := append([]string(nil), p.Hosts...)
106 sort.Strings(hosts)
107 for i, h := range hosts {
108 if !hostname(h) || (i > 0 && h == hosts[i-1]) {
109 return "", errPolicy
110 }
111 }
112 b, _ := json.Marshal(Policy{Provider: p.Provider, Harness: p.Harness, Hosts: hosts})
113 hash := sha256.Sum256(b)
114 return hex.EncodeToString(hash[:]), nil
115}
116
117// PublicAddress reports an address the relay, and Research's fetcher, may
118// dial: globally routed unicast, outside every private, shared, reserved,
119// documentation and transition range (websource.PublicAddress, the one
120// list the site inventory shares). The runner's own provider requests
121// (an API key's probe) use it too.
122func PublicAddress(a netip.Addr) bool { return publicAddress(a) }
123
124func publicAddress(a netip.Addr) bool { return websource.PublicAddress(a) }
125