package containment import ( "context" "net" "os" "path/filepath" "strconv" "sync" "amadan.net/oficina/equipo/internal/runner/egress" "amadan.net/oficina/equipo/internal/runner/provider" ) func (m *Manager) openEgress(ctx context.Context, l provider.Launch, dir string, p *mounts) (string, error) { // Check jobs have no provider and retain a completely disconnected namespace. if l.Selection.Provider == "" { return "", nil } if l.Binding.DedicatedHome && !l.Binding.VerifiedDedicatedHome() { return "", errMount } contract := provider.NetworkContract{Provider: l.Selection.Provider, Harness: l.Policy.Harness()} for _, c := range m.cfg.ProviderNetworks { if c.Provider == contract.Provider && c.Harness == contract.Harness { contract = c } } policy, e := egress.ProviderPolicy(contract, l.Binding.VerifiedDedicatedHome()) if l.Policy.IsSignIn() { // Claude's sign-in reaches its sign-in and API hosts, nothing // else (provider.SignInHosts). policy, e = egress.SignInPolicy(contract) } if e != nil { return "", e } if e = l.Policy.ValidateNetwork(policy.Provider, policy.Hosts); e != nil { return "", e } hash, e := policy.Hash() if e != nil { return "", e } private := filepath.Join(dir, "egress") if e = os.Mkdir(private, 0700); e != nil { return "", e } proxy, e := egress.Open(ctx, private, policy) if e != nil { return "", e } ok := false defer func() { if !ok { proxy.Close() } }() f, e := pin(proxy.SocketPath()) if e != nil { return "", e } info, e := f.Stat() if e != nil || info.Mode()&os.ModeSocket == 0 || info.Mode().Perm() != 0600 { f.Close() return "", errMount } // This is the sole socket import. The parent directory and other host sockets // remain absent; the retained O_PATH descriptor pins this exact endpoint. p.args = append(p.args, "--ro-bind-fd", strconv.Itoa(6+len(p.files)), egress.NamespaceSocket) p.files = append(p.files, f) for _, key := range []string{"HTTPS_PROXY", "HTTP_PROXY", "https_proxy", "http_proxy"} { p.args = append(p.args, "--setenv", key, egress.ProxyURL) } p.proxy = proxy ok = true return hash, nil } // The relay runs in PID 1's Go process. It cannot select a host socket or remote // address, and starts before the harness. No subprocess or external binary is used. type namespaceRelay struct { listener net.Listener failed chan struct{} once sync.Once mu sync.Mutex closed bool connections map[net.Conn]struct{} wg sync.WaitGroup } func startRelay() (*namespaceRelay, error) { ln, e := net.Listen("tcp4", egress.RelayAddress) if e != nil { return nil, e } r := &namespaceRelay{listener: ln, failed: make(chan struct{}), connections: map[net.Conn]struct{}{}} r.wg.Add(1) go r.serve() return r, nil } func (r *namespaceRelay) shutdown() { r.once.Do(func() { r.mu.Lock() r.closed = true r.listener.Close() for c := range r.connections { c.Close() } r.mu.Unlock() close(r.failed) }) } func (r *namespaceRelay) close() { r.shutdown(); r.wg.Wait() } func (r *namespaceRelay) serve() { defer r.wg.Done() defer r.shutdown() slots := make(chan struct{}, egress.MaxTunnels) for { c, e := r.listener.Accept() if e != nil { return } select { case slots <- struct{}{}: default: c.Close() continue } if !r.add(c) { c.Close() <-slots return } r.wg.Add(1) go func() { defer r.wg.Done() defer func() { <-slots; r.remove(c) }() upstream, e := net.DialTimeout("unix", egress.NamespaceSocket, egress.ConnectTimeout) if e != nil { r.shutdown() return } if !r.add(upstream) { upstream.Close() return } defer r.remove(upstream) egress.Tunnel(c, upstream, egress.IdleTimeout) }() } } func (r *namespaceRelay) add(c net.Conn) bool { r.mu.Lock() defer r.mu.Unlock() if r.closed { return false } r.connections[c] = struct{}{} return true } func (r *namespaceRelay) remove(c net.Conn) { c.Close() r.mu.Lock() delete(r.connections, c) r.mu.Unlock() }