networkfilter

package
v1.440.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 3, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ProxyPort is the port the forward proxy listens on.
	// It serves both HTTP forward proxy requests and HTTP CONNECT tunnels.
	// Transparent iptables-redirected connections (port 80/443) are also accepted here.
	ProxyPort = 3128

	// ControlPort is the port the control server listens on (localhost only).
	// Used to enable the policy after startup via POST /enable-policy.
	ControlPort = 3129
)
View Source
const SidecarUID = 0

SidecarUID is the UID the network-filter sidecar container runs as. filter OUTPUT rules accept all traffic from this UID unconditionally so the sidecar can reach real upstreams without being self-blocked.

Variables

This section is empty.

Functions

func PeekSNI

func PeekSNI(r io.Reader) (string, []byte, error)

PeekSNI reads a TLS ClientHello from r and returns the SNI hostname. It does not consume bytes beyond what it reads; use a bufio.Reader or bytes.Buffer so the caller can replay the bytes to the upstream connection. Returns an error if the record is not a valid TLS ClientHello or has no SNI.

func SetupIPTables

func SetupIPTables() error

SetupIPTables configures iptables rules to enforce network isolation for the session Pod, following the same strategy as NemoClaw / NVIDIA OpenShell:

  • All outbound TCP is REJECT-ed by default.
  • Traffic from the network-filter sidecar (UID 0) is exempted so it can reach real upstreams without looping back through itself.
  • The proxy port (127.0.0.1:3128) is explicitly allowed for the main container to reach the sidecar.
  • Established / related packets (TCP replies, ICMP unreachables) are let through so the proxy's upstream connections work normally.
  • UDP is left unrestricted to avoid breaking DNS and other infrastructure; add explicit UDP rules here if stricter UDP isolation is required.

Primary mechanism: HTTP_PROXY / HTTPS_PROXY environment variables (injected by the session manager) make proxy-aware clients (curl, Go http.Client, npm, pip, etc.) use CONNECT tunnels for ALL ports, not just 80/443. The REJECT rules below ensure non-proxy-aware direct TCP connections are also blocked.

Must be called with CAP_NET_ADMIN — from the network-filter-setup init container.

Types

type ControlServer added in v1.420.0

type ControlServer struct {
	// contains filtered or unexported fields
}

ControlServer exposes a minimal HTTP API for managing the proxy at runtime. It listens on ControlPort (localhost only) and accepts:

  • POST /enable-policy — activate the configured filter (idempotent)

func NewControlServer added in v1.420.0

func NewControlServer(proxy *Proxy) *ControlServer

NewControlServer creates a ControlServer that operates on the given proxy.

func (*ControlServer) Run added in v1.420.0

func (c *ControlServer) Run(lis net.Listener) error

Run starts the HTTP control server on lis and blocks until lis is closed.

type DomainLog added in v1.434.0

type DomainLog struct {
	// contains filtered or unexported fields
}

DomainLog tracks which domains were accessed (allowed) or rejected (denied) by the proxy.

func (*DomainLog) Snapshot added in v1.434.0

func (d *DomainLog) Snapshot() (allowed, denied []string)

Snapshot returns a copy of the current allowed and denied domain sets.

type DomainsResponse added in v1.434.0

type DomainsResponse struct {
	Allowed []string `json:"allowed"`
	Denied  []string `json:"denied"`
}

DomainsResponse is the JSON body returned by GET /domains.

type Filter

type Filter struct {
	// contains filtered or unexported fields
}

Filter decides whether a given host should be blocked. In allowlist mode (created via NewAllowlistFilter), only listed domains pass; an empty allowlist blocks everything. In denylist mode (created via NewFilter), listed domains are blocked; an empty denylist allows everything. bypassDomains are always allowed regardless of mode.

func NewAllowlistFilter added in v1.414.0

func NewAllowlistFilter(allowedDomains []string) *Filter

NewAllowlistFilter creates an allowlist filter: only listed domains are permitted. An empty allowedDomains list blocks all traffic (nothing is allowed).

func NewFilter

func NewFilter(deniedDomains []string) *Filter

NewFilter creates a denylist filter.

func (*Filter) Check added in v1.414.0

func (f *Filter) Check(host string) FilterResult

Check returns the FilterResult for the given host. host may include a port suffix (host:port); it is stripped before matching.

func (*Filter) IsDenied

func (f *Filter) IsDenied(host string) bool

IsDenied returns true when host should be blocked.

type FilterResult added in v1.414.0

type FilterResult int

FilterResult describes the outcome of a filter check.

const (
	FilterResultAllowed  FilterResult = iota // passed allowlist/denylist check
	FilterResultBypassed                     // always-allow bypass domain
	FilterResultBlocked                      // denied by allowlist or denylist
)

func (FilterResult) String added in v1.414.0

func (r FilterResult) String() string

type Proxy

type Proxy struct {
	// contains filtered or unexported fields
}

Proxy is a forward proxy that enforces a domain deny-list. It handles three types of incoming connections on a single port:

  1. HTTP CONNECT tunnels — sent by proxy-aware HTTPS clients (via HTTP_PROXY env var)
  2. HTTP forward requests — sent by proxy-aware HTTP clients (via HTTP_PROXY env var)
  3. Transparent TCP — redirected by iptables from port 80/443 - port 80: parsed as an HTTP request (Host header) - port 443: TLS ClientHello SNI peek

When created with active=false (deferred mode), all traffic is allowed until EnablePolicy is called.

func NewProxy

func NewProxy(filter *Filter, active bool) *Proxy

NewProxy creates a Proxy with the given filter. When active is true, the policy is enforced immediately. When active is false, all traffic is allowed until EnablePolicy is called.

func (*Proxy) Domains added in v1.434.0

func (p *Proxy) Domains() (allowed, denied []string)

Domains returns the current snapshot of accessed (allowed) and rejected (denied) domains.

func (*Proxy) EnablePolicy added in v1.420.0

func (p *Proxy) EnablePolicy()

EnablePolicy activates the configured filter. After this call, all connections are subject to domain filtering. Safe to call concurrently.

func (*Proxy) Run

func (p *Proxy) Run(lis net.Listener) error

Run starts the proxy on the given listener. It blocks until lis is closed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL