env/egress
import "github.com/barevoidco/zelk/core/env/egress"Package egress provides a domain/port HTTP and CONNECT broker for confined subprocesses. It is enforcement only when a launcher denies direct network access and exposes only this broker. Hosts own listeners and HTTP servers; Proxy owns its outgoing transport and tracked CONNECT tunnels.
Constants
Section titled “Constants”const RefusalHeader = "Zelk-Egress"RefusalHeader marks the proxy’s own refusal of a plain-HTTP request, with the value “refused”, so a browser driver can tell it from a page: Chrome shows the proxy’s 403 as the page, with no error (browser design, S1.11). Upstream responses never carry it; the proxy removes it. A refused CONNECT can’t be marked: Chrome reports any failed tunnel alike.
Variables
Section titled “Variables”var ErrDenied = errors.New("network destination not allowed")ErrDenied reports a destination refused by the host/port or address policy.
Option
Section titled “Option”type Option func(*Proxy)Option configures a broker’s injected resolver or dialer.
WithDialer
Section titled “WithDialer”func WithDialer(dial func(context.Context, string, string) (net.Conn, error)) OptionWithDialer sets the connector, which receives only checked numeric addresses. It must connect exactly to that address to preserve the enforcement guarantee.
WithResolver
Section titled “WithResolver”func WithResolver(lookup func(context.Context, string, string) ([]netip.Addr, error)) OptionWithResolver sets the DNS resolver. Every returned address must be public.
type Proxy struct { // contains filtered or unexported fields}Proxy forwards HTTP requests and CONNECT tunnels to allowed public addresses. It is safe for concurrent use. Close cancels tunnels and releases idle outgoing connections; the host must also close its HTTP server to cancel HTTP requests. Rules name exact domains, or *.domain for subdomains only, optionally with a port. Without a port a rule permits 80 and 443. IP literals are also accepted, but private, loopback, metadata and other nonpublic addresses remain forbidden. CONNECT authorizes its destination authority, not encrypted application content.
func New(rules []string, opts ...Option) (*Proxy, error)New validates domain/port rules and constructs a broker. It opens no listener. Its default resolver and dialer are independent of HTTP proxy environment variables.
Example
proxy, err := egress.New([]string{"example.com:443"})if err != nil { panic(err)}defer proxy.Close()request := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "http://blocked.example/", nil)response := httptest.NewRecorder()proxy.ServeHTTP(response, request)fmt.Println(response.Code)// Output: 403Output:
403Proxy.Allowed
Section titled “Proxy.Allowed”func (p *Proxy) Allowed(host, port string) boolAllowed reports whether the rules admit host on port. The host’s own web tools ask it too, so a network allowlist means one thing everywhere.
Proxy.Close
Section titled “Proxy.Close”func (p *Proxy) Close() errorClose refuses new requests, closes tracked CONNECT tunnels, waits for their pumps, and releases idle transport connections. It is idempotent.
Proxy.ServeHTTP
Section titled “Proxy.ServeHTTP”func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request)ServeHTTP serves one HTTP request or CONNECT tunnel. The server must be reachable only through the confinement boundary when enforcing subprocess policy.