mirror of
https://github.com/netbirdio/netbird.git
synced 2026-07-16 11:39:57 +00:00
* [management,proxy] Agent network: per-account LLM gateway (policy, metering, multi-provider) (#6555) * [agent-network] Shared proto, OpenAPI schema, and generated types * [agent-network] Management: store, manager, synthesizer, policy engine, provider catalog, HTTP/gRPC API Adds the account-scoped agent-network module: provider/policy/budget CRUD and store, the reverse-proxy service synthesizer, policy selection + limit enforcement, the provider catalog (incl. Vertex AI and AWS Bedrock entries), and the management HTTP + proxy gRPC surfaces. * [management] Fix agent-network proxy-peer fan-out on affected-peer recompute The affected-peers resolver loaded only persisted reverse-proxy services, but agent-network services are synthesized on demand and never persisted. As a result the embedded proxy peer was never folded into the affected set when a client's group changed, so the proxy received no network-map update for a newly authorised client and rejected its handshake until a full resync (restart). loadProxyServices now merges the synthesized agent-network services (injected via a registration hook to avoid an import cycle), so proxy peers learn newly authorised clients immediately. * [proxy] Reverse-proxy middleware framework, chain, and request plumbing The per-target middleware chain (slots, dispatcher, mutation gate, metadata merger), body capture, access-log terminal sink, and the proxy wiring that builds + runs chains for synthesized agent-network services. * [proxy] LLM parsers, pricing, and builtin middlewares (OpenAI, Anthropic, Vertex AI, AWS Bedrock) Request/response parsers and SSE/event-stream metering, the embedded pricing table, and the builtin middleware set: request parser, router, policy limit-check/record, cost meter, guardrail, identity inject, response parser. Includes the path-routed providers — Google Vertex AI (keyfile:: service-account OAuth minting) and AWS Bedrock (bearer auth, invoke/converse/streaming, optional /bedrock prefix) — plus the Models allowlist and unmeterable-publisher deny. * [proxy] IPv6 in-place apply and TCP accept-loop hardening on netstack listeners * [agent-network] End-to-end test suite, module docs, and deployment preset * [agent-network] Fix codespell typos and exclude false positives - labelgen word pool: vermillion -> vermilion, racoon -> raccoon. - codespell ignore list: add flate (Go compress/flate package), recordin (a test-local identifier), and unparseable (a valid alternative spelling used consistently across identifiers + a metadata-value constant). * [management] Set LastSeen on injected proxy peer in realstack test (MySQL strict-mode) The injected embedded proxy peer had a PeerStatus with a zero LastSeen, which serializes to '0000-00-00' and is rejected by MySQL in strict mode (SQLite tolerates it). Set LastSeen to a valid time so SaveAccount succeeds on both engines. * [agent-network] Remove e2e shell-script suite from this branch The end-to-end shell scripts under scripts/e2e/ are maintained in a separate testing suite and are not part of this change set. * [agent-network] Polish module docs: remove internal review scaffolding, fix links, verify diagrams Strip PR-review framing, commit references, absolute paths, and stale internal references from the agent-network module docs; fix broken relative links; verify all diagrams against the current architecture. Remove the internal AI-reviewer prompt file. * [management] Refine session expiration handling to support 3-state encoding for SSO deadlines * [agent-network] Relocate agentnetwork package to internals/modules Move management/server/agentnetwork (and its catalog/, labelgen/, types/ subpackages) to management/internals/modules/agentnetwork, alongside the reverse-proxy module, and rewrite all importers. Pure relocation: package names, the synthesizer + affectedpeers registration hook, and store access (shared store.Store) are unchanged, so no import cycle is introduced (affectedpeers still depends only on the agentnetwork/types leaf). * [agent-network] Co-locate HTTP handlers in the module (RegisterEndpoints) Move the agent-network HTTP handlers from server/http/handlers/agentnetwork into the module at internals/modules/agentnetwork/handlers (package handlers) and rename the entrypoint AddEndpoints -> RegisterEndpoints, matching the reverse-proxy module convention. Wiring in http/handler.go updated accordingly. * Update getting started to point to rc when agent network enabled * Add a reference to a commercial license * Fix docs localhost link * Fix docs localhost link * Add private services domain note * [management] Add agent-network telemetry metrics (#6561) Surface agent-network adoption and usage in the self-hosted metrics worker: distinct accounts, providers, policies, budget rules, accounts with log collection enabled, and aggregated input/output tokens plus cost. Tokens and cost are summed from agent_network_request_usage (the always-written per-request ledger) so the figures are accurate regardless of the log-collection toggle and carry no double-counting. All values come from a handful of indexed aggregate queries run only on the worker's periodic tick. Adds store.AgentNetworkMetrics with GetAgentNetworkMetrics on the Store interface, the SqlStore implementation, and a zero-valued FileStore stub. * Update NetBird server and proxy image versions to 0.74.0-rc.2 * [management,proxy] Reduce agent-network cognitive complexity (#6566) Address the SonarCloud quality-gate findings in new agent-network code by extracting focused helpers. No behavior change. - synthesizer.go: split buildIdentityInjectConfigJSON into per-shape rule builders; extract mergeGuardrail from mergeGuardrails to cut nesting depth. - llm_identity_inject: extract injectionEmitsAnything validation predicate from New. - llm_response_parser/streaming.go: extract applyOpenAIStreamUsage and applyAnthropicStreamUsage (via a named anthropicStreamUsage type) and simplify the OpenAI scanner loop. - reverseproxy.go: decompose ServeHTTP into serveRouteError, buildTargetContext, serveDirect, serveWithChain, captureRequestForChain, serveDeny, newResponseWriter, observeResponse, and forwardUpstream, preserving the defer ordering so response observation still reads the captured writer before it is released. * [management] Move agent-network access-log ingest into the agentnetwork module (#6568) The agent-network access-log ingest path (metaKey wire contract, flatten, usage derivation, and the dual-write of the usage ledger + settings-gated full row) lived in the reverseproxy accesslogs manager, even though the agentnetwork module already owns the rest of that domain — types, read (ListAccessLogs / GetUsageOverview), the budget-counter writes, and retention cleanup. Move it next to the rest: a stateless agentnetwork.IngestAccessLog(ctx, store, entry) that the reverseproxy SaveAccessLog delegates to when the entry is agent-network. Removes the agentNetworkTypes import from the reverseproxy manager. No behavior change; the write/read table separation is unchanged. Adds real-store coverage for the disable->enable log-collection toggle (usage ledger always written, full row gated) plus the metadata parse and group-dedup helpers, which previously had no dedicated tests. * Add session view support in the access log * [management,proxy] Container-based agent-network e2e harness (#6577) * [e2e] Add container-based agent-network e2e harness (Pillar 1) Introduce a self-contained, OIDC-free e2e harness that stands up NetBird in containers, so suites no longer depend on the hand-maintained Tilt stack or a real IdP. - harness brings up the combined server (management + signal + relay + STUN + embedded IdP) in a single container built from combined/Dockerfile.multistage, and mints an admin PAT through the unauthenticated /api/setup bootstrap (NB_SETUP_PAT_ENABLED). API access goes through the existing shared/management/client/rest typed client. - the image is built via the docker CLI (BuildKit) so the Dockerfile's cache mounts are honored; testcontainers then runs the tagged image. - everything is behind the `e2e` build tag so normal builds and unit tests never pull in testcontainers. Adds BuildKit cache mounts to combined/Dockerfile.multistage so source changes recompile incrementally rather than from scratch. Pillar 1 proven by TestCombinedBootstrap: server builds, boots, mints a PAT, and the PAT authenticates a real management API call. * [e2e] Add management-side agent-network scenarios (Pillar 2) Port the API-driven agent-network scenarios from the bash suites to Go, sharing one combined server per package run (TestMain) with each test owning its resource cleanup. Drives the /api/agent-network/* endpoints through the shared REST client's NewRequest primitive with the generated api types. Scenarios: - provider lifecycle (create/get/list/delete + 404 after delete) - provider validation (missing api_key, unknown catalog id → 4xx) - settings collection-toggle round-trip with cluster/subdomain immutability - policy window floor (reject <60s enabled limit, accept at 60s) - consumption read endpoint returns an array All deterministic and dependency-free (dummy provider keys; no upstream calls), so they run headless in CI. * [e2e] Add live chat-through-proxy scenario (Pillar 3) Stand up the full agent-network data path in containers and drive a real chat-completion through the gateway: - harness: a shared docker network (combined server reachable by alias), a proxy container built from the published reverse-proxy image (NB_PROXY_PRIVATE, NB_PROXY_ALLOW_INSECURE, NB_RELAY_TRANSPORT=ws to match the combined server's WS-multiplexed relay) with a generated self-signed wildcard cert, and a netbird client container that joins via a setup key. - the combined image, proxy image, and client image default to the published rc.2 releases (overridable via NB_E2E_*_IMAGE; a bare local tag is built from source instead). Geolocation download is disabled so the server starts without external fetches. - one shared domain is used for the management exposed address, the proxy domain, and the agent-network cluster; the proxy token is minted via the server CLI (global) to match the manual install. TestChatCompletionThroughProxy provisions provider+policy+group+setup key, runs proxy+client, drives an OpenAI chat-completion through the tunnel, and asserts a 200 plus the ingested access-log row. Requires OPENAI_TOKEN (skips otherwise). The provider must be created with enabled=true explicitly — the create default is false despite the API doc. * [e2e] Run the live chat scenario across a provider matrix Replace the single-provider chat test with a data-driven matrix that runs the same scenario through every provider whose credentials are present in the environment (keys/URLs sourced from ~/.llm-keys locally, Actions secrets in CI): - OpenAI (chat), Anthropic (messages), Vercel, OpenRouter, Cloudflare (OpenAI-compatible gateways), and Bedrock (path-routed, bearer, via the messages shape) — covering both wire shapes and the gateway routing. - all providers are created enabled with a unique model string so the proxy's connect-time snapshot carries them all and model->provider routing is unambiguous (provider toggles after connect don't reconcile to a connected proxy). - the client supports both wire shapes (/v1/chat/completions and /v1/messages); Cloudflare gets the openai provider segment appended to its gateway URL. Each provider must return 200 through the tunnel and produce an ingested access-log row. Vertex is intentionally excluded from the uniform matrix: it needs a bespoke rawPredict request shape rather than the shared chat/messages path, so it warrants a dedicated scenario. * [ci] Add manual workflow for the agent-network e2e suite The e2e suite (build tag `e2e`) stands up the combined server + proxy + client in Docker and drives live chat-completions, so it is slow and needs provider credentials. Gate it out of normal CI (it already is, via the build tag) and run it on demand via workflow_dispatch. Provider scenarios skip when their secret is unset, so it degrades gracefully. * [e2e] Add Vertex to the provider matrix; run e2e on ubuntu-latest Vertex (Anthropic-on-Vertex) doesn't share the chat/messages wire shapes: the model travels in a rawPredict path and the proxy mints the service account's OAuth token. Add a Vertex client method that posts /v1/projects/<project>/locations/<region>/publishers/anthropic/models/<model>:rawPredict with the Vertex anthropic_version body, and wire it into the matrix as a path-routed provider (created without a models array). It is keyed off GOOGLE_VERTEX_SA_BASE64 + GOOGLE_VERTEX_PROJECT (region defaults to "global", model to a pinned claude snapshot, both overridable). Also bump the e2e workflow runner to ubuntu-latest and add the Vertex secrets. * Add docker/docker and docker/go-connections as direct dependencies in go.mod * [ci] Trigger agent-network e2e workflow on push to main and pull requests * [e2e] Fix proxy cert permission denied on Linux CI runners The proxy bind-mounts a temp dir of self-signed certs. MkdirTemp creates it 0700 and the key was 0600, which Docker Desktop on macOS ignores but a non-root proxy container on Linux runners cannot traverse/read, so the cert watcher failed with "open /certs/tls.crt: permission denied" and the container exited. Widen the cert dir to 0755 and write the throwaway key 0644 so the proxy uid can read the bind-mounted material. * [e2e] Build images from source by default instead of pulling rc.2 The agent-network code under test lives in this branch, so the e2e should exercise it rather than a frozen published release. Flip the harness default: combined/proxy/client are now built from their in-repo Dockerfiles (combined/Dockerfile.multistage, proxy/Dockerfile.multistage, e2e/harness/Dockerfile.client) under local tags. Pulling a published image stays available by setting NB_E2E_*_IMAGE to a registry reference. Builds now go through buildx --load so the Dockerfile cache mounts are honored and the result is loaded for testcontainers. The CI workflow adds a container-driver builder and a local layer cache (NB_E2E_BUILDX_CACHE) persisted via actions/cache, which caches the base/apt/dep-download layers across runs. The Go compile still re-runs each time, as BuildKit mount caches cannot be exported to the GitHub cache. * [e2e] Cover real providers in lifecycle + assert real consumption metering - TestProviderLifecycle now runs per available real provider (create → get → list → delete → 404) instead of a single dummy provider, exercising each catalog's create and field round-trip. Create is offline, so it stays fast and burns no provider quota; falls back to a synthetic OpenAI provider when no keys are set. - TestProvidersMatrix attaches a token limit (high caps, 60s window) to its policy, which switches on usage metering, and asserts consumption rows are recorded with positive token counts after the live traffic. Consumption is account-scoped (keyed by source group / user and window, not per provider), so the assertion is aggregate. - TestProviderValidation gains invalid-upstream and blank-name cases. Create validation is uniform across catalogs (no per-provider required-field rules), so per-provider rejection cases would be redundant. * [e2e] Assert session id propagates per provider Each matrix request now sends a unique session id as the universal x-session-id header and asserts it round-trips into that provider's access-log row. This guards the session-grouping contract end to end for every provider (header extraction runs in llm_request_parser ahead of the parser-specific body extraction, so it is provider-agnostic). * [e2e] Drop accidentally committed sync-phases dashboard netbird-sync-phases.json was swept into the Pillar 1 commit by a broad git add; it belongs to the unrelated sync-phases metrics work, not this e2e harness. Remove it from the branch so the PR diff is scoped to the e2e changes. * [e2e] Revert accidentally committed sync-phase ingest spec The netbird_sync_phase measurement spec in metrics ingest was swept into the Pillar 1 commit; it belongs to the unrelated sync-phases metrics work, not this e2e harness. Its emission side never landed here, so the spec was orphaned anyway. Restore ingest/main.go to its origin/main state. * Fix golint issues * Fix sonar * Add access log session test * Fix access log tests --------- Co-authored-by: braginini <bangvalo@gmail.com> Co-authored-by: Zoltan Papp <zoltan.pmail@gmail.com>
916 lines
31 KiB
Go
916 lines
31 KiB
Go
package roundtrip
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"net/netip"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/hashicorp/go-multierror"
|
|
log "github.com/sirupsen/logrus"
|
|
"golang.org/x/exp/maps"
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
grpcstatus "google.golang.org/grpc/status"
|
|
|
|
"github.com/netbirdio/netbird/client/embed"
|
|
nberrors "github.com/netbirdio/netbird/client/errors"
|
|
"github.com/netbirdio/netbird/proxy/internal/types"
|
|
"github.com/netbirdio/netbird/shared/management/proto"
|
|
"github.com/netbirdio/netbird/util"
|
|
)
|
|
|
|
const deviceNamePrefix = "ingress-proxy-"
|
|
|
|
const clientStopTimeout = 30 * time.Second
|
|
|
|
const createProxyPeerTimeout = 30 * time.Second
|
|
|
|
// backendKey identifies a backend by its host:port from the target URL.
|
|
type backendKey string
|
|
|
|
// ServiceKey uniquely identifies a service (HTTP reverse proxy or L4 service)
|
|
// that holds a reference to an embedded NetBird client. Callers should use the
|
|
// DomainServiceKey and L4ServiceKey constructors to avoid namespace collisions.
|
|
type ServiceKey string
|
|
|
|
// DomainServiceKey returns a ServiceKey for an HTTP/TLS domain-based service.
|
|
func DomainServiceKey(domain string) ServiceKey {
|
|
return ServiceKey("domain:" + domain)
|
|
}
|
|
|
|
// L4ServiceKey returns a ServiceKey for an L4 service (TCP/UDP).
|
|
func L4ServiceKey(id types.ServiceID) ServiceKey {
|
|
return ServiceKey("l4:" + id)
|
|
}
|
|
|
|
var (
|
|
// ErrNoAccountID is returned when a request context is missing the account ID.
|
|
ErrNoAccountID = errors.New("no account ID in request context")
|
|
// ErrNoPeerConnection is returned when no embedded client exists for the account.
|
|
ErrNoPeerConnection = errors.New("no peer connection found")
|
|
// ErrClientStartFailed is returned when the embedded client fails to start.
|
|
ErrClientStartFailed = errors.New("client start failed")
|
|
// ErrTooManyInflight is returned when the per-backend in-flight limit is reached.
|
|
ErrTooManyInflight = errors.New("too many in-flight requests")
|
|
)
|
|
|
|
// serviceInfo holds metadata about a registered service.
|
|
type serviceInfo struct {
|
|
serviceID types.ServiceID
|
|
}
|
|
|
|
type serviceNotification struct {
|
|
key ServiceKey
|
|
serviceID types.ServiceID
|
|
}
|
|
|
|
// clientEntry holds an embedded NetBird client and tracks which services use it.
|
|
type clientEntry struct {
|
|
client *embed.Client
|
|
transport *http.Transport
|
|
// insecureTransport is a clone of transport with TLS verification disabled,
|
|
// used when per-target skip_tls_verify is set.
|
|
insecureTransport *http.Transport
|
|
services map[ServiceKey]serviceInfo
|
|
createdAt time.Time
|
|
started bool
|
|
// inbound is opaque per-account state owned by the NetBird parent's
|
|
// ReadyHandler. The roundtrip package never inspects this value; it
|
|
// only stores it so RemovePeer / StopAll can hand it back to the
|
|
// matching StopHandler. Nil when no inbound integration is active.
|
|
inbound any
|
|
// Per-backend in-flight limiting keyed by target host:port.
|
|
// TODO: clean up stale entries when backend targets change.
|
|
inflightMu sync.Mutex
|
|
inflightMap map[backendKey]chan struct{}
|
|
maxInflight int
|
|
}
|
|
|
|
// IdentityForIP resolves a tunnel IP to the peer identity locally known by
|
|
// this account's embedded client. Returns (pubKey, fqdn) on success.
|
|
// ok=false means the IP is not in the account's roster — callers can use
|
|
// that as a fast deny without round-tripping management. The returned
|
|
// strings carry only what the embedded peerstore exposes; user identity
|
|
// (UserID / Email / Groups) still flows through ValidateTunnelPeer.
|
|
func (e *clientEntry) IdentityForIP(ip netip.Addr) (pubKey, fqdn string, ok bool) {
|
|
if e == nil || e.client == nil || !ip.IsValid() {
|
|
return "", "", false
|
|
}
|
|
return e.client.IdentityForIP(ip)
|
|
}
|
|
|
|
// acquireInflight attempts to acquire an in-flight slot for the given backend.
|
|
// It returns a release function that must always be called, and true on success.
|
|
func (e *clientEntry) acquireInflight(backend backendKey) (release func(), ok bool) {
|
|
noop := func() {}
|
|
if e.maxInflight <= 0 {
|
|
return noop, true
|
|
}
|
|
|
|
e.inflightMu.Lock()
|
|
sem, exists := e.inflightMap[backend]
|
|
if !exists {
|
|
sem = make(chan struct{}, e.maxInflight)
|
|
e.inflightMap[backend] = sem
|
|
}
|
|
e.inflightMu.Unlock()
|
|
|
|
select {
|
|
case sem <- struct{}{}:
|
|
return func() { <-sem }, true
|
|
default:
|
|
return noop, false
|
|
}
|
|
}
|
|
|
|
// ClientConfig holds configuration for the embedded NetBird client.
|
|
type ClientConfig struct {
|
|
MgmtAddr string
|
|
WGPort uint16
|
|
PreSharedKey string
|
|
Performance embed.Performance
|
|
// BlockInbound mirrors embed.Options.BlockInbound. Set to true on the
|
|
// standalone proxy where the embedded client never accepts inbound;
|
|
// set to false on the private/embedded proxy so the engine creates
|
|
// the ACL manager and applies management's per-policy firewall rules
|
|
// (which is what gates per-account inbound listeners on the netstack).
|
|
BlockInbound bool
|
|
}
|
|
|
|
type statusNotifier interface {
|
|
NotifyStatus(ctx context.Context, accountID types.AccountID, serviceID types.ServiceID, connected bool) error
|
|
}
|
|
|
|
type managementClient interface {
|
|
CreateProxyPeer(ctx context.Context, req *proto.CreateProxyPeerRequest, opts ...grpc.CallOption) (*proto.CreateProxyPeerResponse, error)
|
|
}
|
|
|
|
// NetBird provides an http.RoundTripper implementation
|
|
// backed by underlying NetBird connections.
|
|
// Clients are keyed by AccountID, allowing multiple services to share the same connection.
|
|
type NetBird struct {
|
|
ctx context.Context
|
|
proxyID string
|
|
proxyAddr string
|
|
clientCfg ClientConfig
|
|
logger *log.Logger
|
|
mgmtClient managementClient
|
|
transportCfg transportConfig
|
|
|
|
clientsMux sync.RWMutex
|
|
clients map[types.AccountID]*clientEntry
|
|
lifecycleMu sync.Map
|
|
initLogOnce sync.Once
|
|
statusNotifier statusNotifier
|
|
// readyHandler runs after the embedded client for an account reports
|
|
// Ready. The opaque return value is stored on clientEntry and handed
|
|
// back to stopHandler when the entry is torn down. Nil disables the
|
|
// hook entirely (default for the standalone proxy).
|
|
readyHandler func(ctx context.Context, accountID types.AccountID, client *embed.Client) any
|
|
// stopHandler runs when an account's last service is removed (or the
|
|
// transport is shutting down). Receives whatever readyHandler returned.
|
|
stopHandler func(accountID types.AccountID, state any)
|
|
|
|
// OnAddPeer, when set, is called after AddPeer completes for a new account
|
|
// (i.e. when a new client was actually created, not when an existing one
|
|
// was reused). The duration covers keygen + gRPC CreateProxyPeer + embed.New.
|
|
OnAddPeer func(d time.Duration, err error)
|
|
|
|
// startClient runs the post-create client startup. Nil uses runClientStartup;
|
|
// tests override it to avoid a real embed client.Start.
|
|
startClient func(accountID types.AccountID, client *embed.Client)
|
|
}
|
|
|
|
// ClientDebugInfo contains debug information about a client.
|
|
type ClientDebugInfo struct {
|
|
AccountID types.AccountID
|
|
ServiceCount int
|
|
ServiceKeys []string
|
|
HasClient bool
|
|
CreatedAt time.Time
|
|
}
|
|
|
|
// accountIDContextKey is the context key for storing the account ID.
|
|
type accountIDContextKey struct{}
|
|
|
|
// skipTLSVerifyContextKey is the context key for requesting insecure TLS.
|
|
type skipTLSVerifyContextKey struct{}
|
|
|
|
// AddPeer registers a service for an account. If the account doesn't have a client yet,
|
|
// one is created by authenticating with the management server using the provided token.
|
|
// Multiple services can share the same client.
|
|
func (n *NetBird) AddPeer(ctx context.Context, accountID types.AccountID, key ServiceKey, authToken string, serviceID types.ServiceID) error {
|
|
si := serviceInfo{serviceID: serviceID}
|
|
|
|
if n.registerExistingClient(accountID, key, si) {
|
|
return nil
|
|
}
|
|
|
|
lifecycle := n.accountLifecycle(accountID)
|
|
lifecycle.Lock()
|
|
transferred := false
|
|
defer func() {
|
|
if !transferred {
|
|
lifecycle.Unlock()
|
|
}
|
|
}()
|
|
|
|
if n.registerExistingClient(accountID, key, si) {
|
|
return nil
|
|
}
|
|
|
|
createStart := time.Now()
|
|
entry, err := n.createClientEntry(ctx, accountID, key, authToken, si)
|
|
if n.OnAddPeer != nil {
|
|
n.OnAddPeer(time.Since(createStart), err)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
n.clientsMux.Lock()
|
|
n.clients[accountID] = entry
|
|
n.clientsMux.Unlock()
|
|
|
|
n.logger.WithFields(log.Fields{
|
|
"account_id": accountID,
|
|
"service_key": key,
|
|
}).Info("created new client for account")
|
|
|
|
transferred = true
|
|
go func() {
|
|
defer lifecycle.Unlock()
|
|
n.startClientStartup(accountID, entry.client)
|
|
}()
|
|
|
|
return nil
|
|
}
|
|
|
|
func (n *NetBird) startClientStartup(accountID types.AccountID, client *embed.Client) {
|
|
if n.startClient != nil {
|
|
n.startClient(accountID, client)
|
|
return
|
|
}
|
|
n.runClientStartup(accountID, client)
|
|
}
|
|
|
|
// registerExistingClient registers the service against an already-present
|
|
// client for the account and returns true when it did. It notifies management
|
|
// of the new service when the client is already started.
|
|
func (n *NetBird) registerExistingClient(accountID types.AccountID, key ServiceKey, si serviceInfo) bool {
|
|
n.clientsMux.Lock()
|
|
entry, exists := n.clients[accountID]
|
|
if !exists {
|
|
n.clientsMux.Unlock()
|
|
return false
|
|
}
|
|
entry.services[key] = si
|
|
started := entry.started
|
|
n.clientsMux.Unlock()
|
|
|
|
n.logger.WithFields(log.Fields{
|
|
"account_id": accountID,
|
|
"service_key": key,
|
|
}).Debug("registered service with existing client")
|
|
|
|
if started && n.statusNotifier != nil {
|
|
if err := n.statusNotifier.NotifyStatus(context.Background(), accountID, si.serviceID, true); err != nil {
|
|
n.logger.WithFields(log.Fields{
|
|
"account_id": accountID,
|
|
"service_key": key,
|
|
}).WithError(err).Warn("failed to notify status for existing client")
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// accountLifecycle returns the per-account lifecycle mutex, serialising client
|
|
// creation against teardown so a slow client.Stop cannot race a new
|
|
// client.Start for the same account, without blocking clientsMux.
|
|
func (n *NetBird) accountLifecycle(accountID types.AccountID) *sync.Mutex {
|
|
mu, _ := n.lifecycleMu.LoadOrStore(accountID, &sync.Mutex{})
|
|
return mu.(*sync.Mutex)
|
|
}
|
|
|
|
// createClientEntry generates a WireGuard keypair, authenticates with management,
|
|
// and creates an embedded NetBird client. Must be called with the account's
|
|
// lifecycle mutex held.
|
|
func (n *NetBird) createClientEntry(ctx context.Context, accountID types.AccountID, key ServiceKey, authToken string, si serviceInfo) (*clientEntry, error) {
|
|
serviceID := si.serviceID
|
|
n.logger.WithFields(log.Fields{
|
|
"account_id": accountID,
|
|
"service_id": serviceID,
|
|
}).Debug("generating WireGuard keypair for new peer")
|
|
|
|
privateKey, err := wgtypes.GeneratePrivateKey()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("generate wireguard private key: %w", err)
|
|
}
|
|
publicKey := privateKey.PublicKey()
|
|
|
|
n.logger.WithFields(log.Fields{
|
|
"account_id": accountID,
|
|
"service_id": serviceID,
|
|
"public_key": publicKey.String(),
|
|
}).Debug("authenticating new proxy peer with management")
|
|
|
|
createCtx, cancel := context.WithTimeout(ctx, createProxyPeerTimeout)
|
|
defer cancel()
|
|
resp, err := n.mgmtClient.CreateProxyPeer(createCtx, &proto.CreateProxyPeerRequest{
|
|
ServiceId: string(serviceID),
|
|
AccountId: string(accountID),
|
|
Token: authToken,
|
|
WireguardPublicKey: publicKey.String(),
|
|
Cluster: n.proxyAddr,
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("authenticate proxy peer with management: %w", err)
|
|
}
|
|
if resp != nil && !resp.GetSuccess() {
|
|
errMsg := "unknown error"
|
|
if resp.ErrorMessage != nil {
|
|
errMsg = *resp.ErrorMessage
|
|
}
|
|
return nil, fmt.Errorf("proxy peer authentication failed: %s", errMsg)
|
|
}
|
|
|
|
n.logger.WithFields(log.Fields{
|
|
"account_id": accountID,
|
|
"service_id": serviceID,
|
|
"public_key": publicKey.String(),
|
|
}).Info("proxy peer authenticated successfully with management")
|
|
|
|
// Embedded client log level: warn by default (quiet in production); set
|
|
// NB_PROXY_CLIENT_LOG_LEVEL (e.g. "trace") to surface the embedded NetBird
|
|
// client's relay / signal / handshake detail for local debugging.
|
|
clientLogLevel := log.WarnLevel.String()
|
|
if v := strings.TrimSpace(os.Getenv("NB_PROXY_CLIENT_LOG_LEVEL")); v != "" {
|
|
if lvl, err := log.ParseLevel(v); err == nil {
|
|
clientLogLevel = lvl.String()
|
|
} else {
|
|
n.logger.Warnf("invalid NB_PROXY_CLIENT_LOG_LEVEL %q, using %q: %v", v, clientLogLevel, err)
|
|
}
|
|
}
|
|
|
|
n.initLogOnce.Do(func() {
|
|
if err := util.InitLog(clientLogLevel, util.LogConsole); err != nil {
|
|
n.logger.WithField("account_id", accountID).Warnf("failed to initialize embedded client logging: %v", err)
|
|
}
|
|
})
|
|
|
|
// Create embedded NetBird client with the generated private key.
|
|
// The peer has already been created via CreateProxyPeer RPC with the public key.
|
|
wgPort := int(n.clientCfg.WGPort)
|
|
embedOpts := embed.Options{
|
|
DeviceName: deviceNamePrefix + n.proxyID,
|
|
ManagementURL: n.clientCfg.MgmtAddr,
|
|
PrivateKey: privateKey.String(),
|
|
LogLevel: clientLogLevel,
|
|
BlockInbound: n.clientCfg.BlockInbound,
|
|
// The embedded proxy peer must never be a stepping stone into
|
|
// the proxy host's LAN: it only exists to reach NetBird mesh
|
|
// targets or, when direct_upstream is set, the host network
|
|
// stack via the MultiTransport's direct branch (which bypasses
|
|
// the engine routing entirely).
|
|
BlockLANAccess: true,
|
|
WireguardPort: &wgPort,
|
|
PreSharedKey: n.clientCfg.PreSharedKey,
|
|
Performance: n.clientCfg.Performance,
|
|
}
|
|
logEmbedOptions(n.logger, accountID, serviceID, publicKey.String(), embedOpts)
|
|
client, err := embed.New(embedOpts)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create netbird client: %w", err)
|
|
}
|
|
|
|
// Create a transport using the client dialer. We do this instead of using
|
|
// the client's HTTPClient to avoid issues with request validation that do
|
|
// not work with reverse proxied requests.
|
|
transport := &http.Transport{
|
|
DialContext: dialWithTimeout(client.DialContext),
|
|
ForceAttemptHTTP2: true,
|
|
MaxIdleConns: n.transportCfg.maxIdleConns,
|
|
MaxIdleConnsPerHost: n.transportCfg.maxIdleConnsPerHost,
|
|
MaxConnsPerHost: n.transportCfg.maxConnsPerHost,
|
|
IdleConnTimeout: n.transportCfg.idleConnTimeout,
|
|
TLSHandshakeTimeout: n.transportCfg.tlsHandshakeTimeout,
|
|
ExpectContinueTimeout: n.transportCfg.expectContinueTimeout,
|
|
ResponseHeaderTimeout: n.transportCfg.responseHeaderTimeout,
|
|
WriteBufferSize: n.transportCfg.writeBufferSize,
|
|
ReadBufferSize: n.transportCfg.readBufferSize,
|
|
DisableCompression: n.transportCfg.disableCompression,
|
|
}
|
|
|
|
insecureTransport := transport.Clone()
|
|
insecureTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} //nolint:gosec
|
|
|
|
return &clientEntry{
|
|
client: client,
|
|
services: map[ServiceKey]serviceInfo{key: si},
|
|
transport: transport,
|
|
insecureTransport: insecureTransport,
|
|
createdAt: time.Now(),
|
|
started: false,
|
|
inflightMap: make(map[backendKey]chan struct{}),
|
|
maxInflight: n.transportCfg.maxInflight,
|
|
}, nil
|
|
}
|
|
|
|
// runClientStartup starts the client and notifies registered services on
|
|
// success. This function runs in a goroutine launched from AddPeer, so it
|
|
// must never inherit the caller's request-scoped context — a canceled
|
|
// request must not abort the inbound listener bring-up or the management
|
|
// status notification. The embedded client.Start gets its own bounded
|
|
// startCtx; once Start succeeds, notifyClientReady takes over with a
|
|
// fresh context.Background() (see that function for the contract).
|
|
func (n *NetBird) runClientStartup(accountID types.AccountID, client *embed.Client) {
|
|
startCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
|
|
if err := client.Start(startCtx); err != nil {
|
|
if errors.Is(err, context.DeadlineExceeded) {
|
|
n.logger.WithField("account_id", accountID).Warn("netbird client start timed out, will retry on first request")
|
|
} else {
|
|
n.logger.WithField("account_id", accountID).WithError(err).Error("failed to start netbird client")
|
|
}
|
|
return
|
|
}
|
|
|
|
n.notifyClientReady(accountID, client)
|
|
}
|
|
|
|
// notifyClientReady marks the account's client as started, fires the
|
|
// readyHandler hook, and notifies management of the new tunnel
|
|
// connection for every registered service. It is split out of
|
|
// runClientStartup so a regression test can drive the post-Start tail
|
|
// without needing a live embedded client. The contract that the
|
|
// hooks/notifier see context.Background() — never the AddPeer caller's
|
|
// ctx — lives here.
|
|
func (n *NetBird) notifyClientReady(accountID types.AccountID, client *embed.Client) {
|
|
n.clientsMux.Lock()
|
|
entry, exists := n.clients[accountID]
|
|
if exists {
|
|
entry.started = true
|
|
}
|
|
var toNotify []serviceNotification
|
|
if exists {
|
|
for key, info := range entry.services {
|
|
toNotify = append(toNotify, serviceNotification{key: key, serviceID: info.serviceID})
|
|
}
|
|
}
|
|
readyHandler := n.readyHandler
|
|
n.clientsMux.Unlock()
|
|
|
|
if readyHandler != nil {
|
|
state := readyHandler(n.ctx, accountID, client)
|
|
n.clientsMux.Lock()
|
|
if e, ok := n.clients[accountID]; ok {
|
|
e.inbound = state
|
|
} else if state != nil && n.stopHandler != nil {
|
|
// Account was removed while readyHandler ran; tear down the
|
|
// resources it just brought up.
|
|
stop := n.stopHandler
|
|
n.clientsMux.Unlock()
|
|
stop(accountID, state)
|
|
n.clientsMux.Lock()
|
|
}
|
|
n.clientsMux.Unlock()
|
|
}
|
|
|
|
if n.statusNotifier == nil {
|
|
return
|
|
}
|
|
for _, sn := range toNotify {
|
|
if err := n.statusNotifier.NotifyStatus(n.ctx, accountID, sn.serviceID, true); err != nil {
|
|
n.logger.WithFields(log.Fields{
|
|
"account_id": accountID,
|
|
"service_key": sn.key,
|
|
}).WithError(err).Warn("failed to notify tunnel connection status")
|
|
} else {
|
|
n.logger.WithFields(log.Fields{
|
|
"account_id": accountID,
|
|
"service_key": sn.key,
|
|
}).Info("notified management about tunnel connection")
|
|
}
|
|
}
|
|
}
|
|
|
|
// RemovePeer unregisters a service from an account. The client is only stopped
|
|
// when no services are using it anymore.
|
|
func (n *NetBird) RemovePeer(ctx context.Context, accountID types.AccountID, key ServiceKey) error {
|
|
lifecycle := n.accountLifecycle(accountID)
|
|
lifecycle.Lock()
|
|
transferred := false
|
|
defer func() {
|
|
if !transferred {
|
|
lifecycle.Unlock()
|
|
}
|
|
}()
|
|
|
|
n.clientsMux.Lock()
|
|
|
|
entry, exists := n.clients[accountID]
|
|
if !exists {
|
|
n.clientsMux.Unlock()
|
|
n.logger.WithField("account_id", accountID).Debug("remove peer: account not found")
|
|
return nil
|
|
}
|
|
|
|
si, svcExists := entry.services[key]
|
|
if !svcExists {
|
|
n.clientsMux.Unlock()
|
|
n.logger.WithFields(log.Fields{
|
|
"account_id": accountID,
|
|
"service_key": key,
|
|
}).Debug("remove peer: service not registered")
|
|
return nil
|
|
}
|
|
|
|
delete(entry.services, key)
|
|
|
|
stopClient := len(entry.services) == 0
|
|
if stopClient {
|
|
n.logger.WithField("account_id", accountID).Info("stopping client, no more services")
|
|
delete(n.clients, accountID)
|
|
} else {
|
|
n.logger.WithFields(log.Fields{
|
|
"account_id": accountID,
|
|
"service_key": key,
|
|
"remaining_services": len(entry.services),
|
|
}).Debug("unregistered service, client still in use")
|
|
}
|
|
n.clientsMux.Unlock()
|
|
|
|
n.notifyDisconnect(ctx, accountID, key, si.serviceID)
|
|
|
|
if stopClient {
|
|
transferred = true
|
|
go n.stopClientLocked(accountID, lifecycle, entry)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// stopClientLocked releases a client's resources off the caller's goroutine so a
|
|
// slow client.Stop cannot wedge the mapping receive loop (which calls RemovePeer
|
|
// synchronously). It unlocks lifecycle when done so a new client.Start for the
|
|
// same account waits for this teardown.
|
|
func (n *NetBird) stopClientLocked(accountID types.AccountID, lifecycle *sync.Mutex, entry *clientEntry) {
|
|
defer lifecycle.Unlock()
|
|
|
|
if entry.inbound != nil && n.stopHandler != nil {
|
|
n.stopHandler(accountID, entry.inbound)
|
|
}
|
|
if entry.transport != nil {
|
|
entry.transport.CloseIdleConnections()
|
|
}
|
|
if entry.insecureTransport != nil {
|
|
entry.insecureTransport.CloseIdleConnections()
|
|
}
|
|
if entry.client == nil {
|
|
return
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), clientStopTimeout)
|
|
defer cancel()
|
|
if err := entry.client.Stop(ctx); err != nil {
|
|
n.logger.WithField("account_id", accountID).WithError(err).Warn("failed to stop netbird client")
|
|
}
|
|
}
|
|
|
|
func (n *NetBird) notifyDisconnect(ctx context.Context, accountID types.AccountID, key ServiceKey, serviceID types.ServiceID) {
|
|
if n.statusNotifier == nil {
|
|
return
|
|
}
|
|
if err := n.statusNotifier.NotifyStatus(ctx, accountID, serviceID, false); err != nil {
|
|
if s, ok := grpcstatus.FromError(err); ok && s.Code() == codes.NotFound {
|
|
n.logger.WithField("service_key", key).Debug("service already removed, skipping disconnect notification")
|
|
} else {
|
|
n.logger.WithFields(log.Fields{
|
|
"account_id": accountID,
|
|
"service_key": key,
|
|
}).WithError(err).Warn("failed to notify tunnel disconnection status")
|
|
}
|
|
}
|
|
}
|
|
|
|
// RoundTrip implements http.RoundTripper. It looks up the client for the account
|
|
// specified in the request context and uses it to dial the backend.
|
|
func (n *NetBird) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
accountID := AccountIDFromContext(req.Context())
|
|
if accountID == "" {
|
|
return nil, ErrNoAccountID
|
|
}
|
|
|
|
// Copy references while holding lock, then unlock early to avoid blocking
|
|
// other requests during the potentially slow RoundTrip.
|
|
n.clientsMux.RLock()
|
|
entry, exists := n.clients[accountID]
|
|
if !exists {
|
|
n.clientsMux.RUnlock()
|
|
return nil, fmt.Errorf("%w for account: %s", ErrNoPeerConnection, accountID)
|
|
}
|
|
client := entry.client
|
|
transport := entry.transport
|
|
if skipTLSVerifyFromContext(req.Context()) {
|
|
transport = entry.insecureTransport
|
|
}
|
|
n.clientsMux.RUnlock()
|
|
|
|
release, ok := entry.acquireInflight(backendKey(req.URL.Host))
|
|
defer release()
|
|
if !ok {
|
|
return nil, ErrTooManyInflight
|
|
}
|
|
|
|
// Attempt to start the client, if the client is already running then
|
|
// it will return an error that we ignore, if this hits a timeout then
|
|
// this request is unprocessable.
|
|
startCtx, cancel := context.WithTimeout(req.Context(), 30*time.Second)
|
|
defer cancel()
|
|
if err := client.Start(startCtx); err != nil {
|
|
if !errors.Is(err, embed.ErrClientAlreadyStarted) {
|
|
return nil, fmt.Errorf("%w: %w", ErrClientStartFailed, err)
|
|
}
|
|
}
|
|
|
|
start := time.Now()
|
|
resp, err := transport.RoundTrip(req)
|
|
duration := time.Since(start)
|
|
|
|
if err != nil {
|
|
n.logger.Debugf("roundtrip: method=%s host=%s url=%s account=%s duration=%s err=%v",
|
|
req.Method, req.Host, req.URL.String(), accountID, duration.Truncate(time.Millisecond), err)
|
|
return nil, err
|
|
}
|
|
|
|
n.logger.Debugf("roundtrip: method=%s host=%s url=%s account=%s status=%d duration=%s",
|
|
req.Method, req.Host, req.URL.String(), accountID, resp.StatusCode, duration.Truncate(time.Millisecond))
|
|
return resp, nil
|
|
}
|
|
|
|
// StopAll stops all clients.
|
|
func (n *NetBird) StopAll(ctx context.Context) error {
|
|
n.clientsMux.Lock()
|
|
defer n.clientsMux.Unlock()
|
|
|
|
stopHandler := n.stopHandler
|
|
var merr *multierror.Error
|
|
for accountID, entry := range n.clients {
|
|
if entry.inbound != nil && stopHandler != nil {
|
|
stopHandler(accountID, entry.inbound)
|
|
}
|
|
entry.transport.CloseIdleConnections()
|
|
entry.insecureTransport.CloseIdleConnections()
|
|
if err := entry.client.Stop(ctx); err != nil {
|
|
n.logger.WithFields(log.Fields{
|
|
"account_id": accountID,
|
|
}).WithError(err).Warn("failed to stop netbird client during shutdown")
|
|
merr = multierror.Append(merr, err)
|
|
}
|
|
}
|
|
maps.Clear(n.clients)
|
|
|
|
return nberrors.FormatErrorOrNil(merr)
|
|
}
|
|
|
|
// HasClient returns true if there is a client for the given account.
|
|
func (n *NetBird) HasClient(accountID types.AccountID) bool {
|
|
n.clientsMux.RLock()
|
|
defer n.clientsMux.RUnlock()
|
|
_, exists := n.clients[accountID]
|
|
return exists
|
|
}
|
|
|
|
// ServiceCount returns the number of services registered for the given account.
|
|
// Returns 0 if the account has no client.
|
|
func (n *NetBird) ServiceCount(accountID types.AccountID) int {
|
|
n.clientsMux.RLock()
|
|
defer n.clientsMux.RUnlock()
|
|
entry, exists := n.clients[accountID]
|
|
if !exists {
|
|
return 0
|
|
}
|
|
return len(entry.services)
|
|
}
|
|
|
|
// ClientCount returns the total number of active clients.
|
|
func (n *NetBird) ClientCount() int {
|
|
n.clientsMux.RLock()
|
|
defer n.clientsMux.RUnlock()
|
|
return len(n.clients)
|
|
}
|
|
|
|
// GetClient returns the embed.Client for the given account ID.
|
|
func (n *NetBird) GetClient(accountID types.AccountID) (*embed.Client, bool) {
|
|
n.clientsMux.RLock()
|
|
defer n.clientsMux.RUnlock()
|
|
entry, exists := n.clients[accountID]
|
|
if !exists {
|
|
return nil, false
|
|
}
|
|
return entry.client, true
|
|
}
|
|
|
|
// IdentityForIP resolves a tunnel IP to a peer identity local to the given
|
|
// account. Delegates to clientEntry.IdentityForIP. Returns ok=false when
|
|
// the account has no client or the IP is not in its peerstore.
|
|
func (n *NetBird) IdentityForIP(accountID types.AccountID, ip netip.Addr) (pubKey, fqdn string, ok bool) {
|
|
n.clientsMux.RLock()
|
|
entry, exists := n.clients[accountID]
|
|
n.clientsMux.RUnlock()
|
|
if !exists {
|
|
return "", "", false
|
|
}
|
|
return entry.IdentityForIP(ip)
|
|
}
|
|
|
|
// ListClientsForDebug returns information about all clients for debug purposes.
|
|
func (n *NetBird) ListClientsForDebug() map[types.AccountID]ClientDebugInfo {
|
|
n.clientsMux.RLock()
|
|
defer n.clientsMux.RUnlock()
|
|
|
|
result := make(map[types.AccountID]ClientDebugInfo)
|
|
for accountID, entry := range n.clients {
|
|
keys := make([]string, 0, len(entry.services))
|
|
for k := range entry.services {
|
|
keys = append(keys, string(k))
|
|
}
|
|
result[accountID] = ClientDebugInfo{
|
|
AccountID: accountID,
|
|
ServiceCount: len(entry.services),
|
|
ServiceKeys: keys,
|
|
HasClient: entry.client != nil,
|
|
CreatedAt: entry.createdAt,
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
// ListClientsForStartup returns all embed.Client instances for health checks.
|
|
func (n *NetBird) ListClientsForStartup() map[types.AccountID]*embed.Client {
|
|
n.clientsMux.RLock()
|
|
defer n.clientsMux.RUnlock()
|
|
|
|
result := make(map[types.AccountID]*embed.Client)
|
|
for accountID, entry := range n.clients {
|
|
if entry.client != nil {
|
|
result[accountID] = entry.client
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
// NewNetBird creates a new NetBird transport. Set clientCfg.WGPort to 0 for a random
|
|
// OS-assigned port. A fixed port only works with single-account deployments;
|
|
// multiple accounts will fail to bind the same port.
|
|
func NewNetBird(ctx context.Context, proxyID, proxyAddr string, clientCfg ClientConfig, logger *log.Logger, notifier statusNotifier, mgmtClient managementClient) *NetBird {
|
|
if logger == nil {
|
|
logger = log.StandardLogger()
|
|
}
|
|
return &NetBird{
|
|
ctx: ctx,
|
|
proxyID: proxyID,
|
|
proxyAddr: proxyAddr,
|
|
clientCfg: clientCfg,
|
|
logger: logger,
|
|
clients: make(map[types.AccountID]*clientEntry),
|
|
statusNotifier: notifier,
|
|
mgmtClient: mgmtClient,
|
|
transportCfg: loadTransportConfig(logger),
|
|
}
|
|
}
|
|
|
|
// SetClientLifecycle registers callbacks that run when an embedded
|
|
// client becomes ready and when its entry is torn down. The opaque value
|
|
// returned by ready is stored on the entry and handed back to stop on
|
|
// cleanup. Must be called before AddPeer. A nil pair leaves the
|
|
// outbound-only behaviour intact.
|
|
func (n *NetBird) SetClientLifecycle(ready func(ctx context.Context, accountID types.AccountID, client *embed.Client) any, stop func(accountID types.AccountID, state any)) {
|
|
n.clientsMux.Lock()
|
|
defer n.clientsMux.Unlock()
|
|
n.readyHandler = ready
|
|
n.stopHandler = stop
|
|
}
|
|
|
|
// dialWithTimeout wraps a DialContext function so that any dial timeout
|
|
// stored in the context (via types.WithDialTimeout) is applied only to
|
|
// the connection establishment phase, not the full request lifetime.
|
|
func dialWithTimeout(dial func(ctx context.Context, network, addr string) (net.Conn, error)) func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
return func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
if d, ok := types.DialTimeoutFromContext(ctx); ok {
|
|
var cancel context.CancelFunc
|
|
ctx, cancel = context.WithTimeout(ctx, d)
|
|
defer cancel()
|
|
}
|
|
return dial(ctx, network, addr)
|
|
}
|
|
}
|
|
|
|
// WithAccountID adds the account ID to the context.
|
|
func WithAccountID(ctx context.Context, accountID types.AccountID) context.Context {
|
|
return context.WithValue(ctx, accountIDContextKey{}, accountID)
|
|
}
|
|
|
|
// AccountIDFromContext retrieves the account ID from the context.
|
|
func AccountIDFromContext(ctx context.Context) types.AccountID {
|
|
v := ctx.Value(accountIDContextKey{})
|
|
if v == nil {
|
|
return ""
|
|
}
|
|
accountID, ok := v.(types.AccountID)
|
|
if !ok {
|
|
return ""
|
|
}
|
|
return accountID
|
|
}
|
|
|
|
// WithSkipTLSVerify marks the context to use an insecure transport that skips
|
|
// TLS certificate verification for the backend connection.
|
|
func WithSkipTLSVerify(ctx context.Context) context.Context {
|
|
return context.WithValue(ctx, skipTLSVerifyContextKey{}, true)
|
|
}
|
|
|
|
func skipTLSVerifyFromContext(ctx context.Context) bool {
|
|
v, _ := ctx.Value(skipTLSVerifyContextKey{}).(bool)
|
|
return v
|
|
}
|
|
|
|
// directUpstreamContextKey signals that the request should bypass the embedded
|
|
// NetBird WireGuard client and dial via the host's network stack instead.
|
|
// Set by the reverse-proxy rewrite step when the matched target carries
|
|
// PathTarget.DirectUpstream; consumed by MultiTransport.
|
|
type directUpstreamContextKey struct{}
|
|
|
|
// WithDirectUpstream marks the context so MultiTransport routes the request
|
|
// through its stdlib transport instead of the embedded NetBird roundtripper.
|
|
func WithDirectUpstream(ctx context.Context) context.Context {
|
|
return context.WithValue(ctx, directUpstreamContextKey{}, true)
|
|
}
|
|
|
|
// DirectUpstreamFromContext reports whether the context has been marked to
|
|
// bypass the embedded NetBird client.
|
|
func DirectUpstreamFromContext(ctx context.Context) bool {
|
|
v, _ := ctx.Value(directUpstreamContextKey{}).(bool)
|
|
return v
|
|
}
|
|
|
|
// logEmbedOptions emits a single structured INFO line summarising every
|
|
// operationally meaningful flag handed to embed.New for this per-account
|
|
// client. Secrets (PrivateKey, PreSharedKey) are reduced to a "present"
|
|
// boolean — never logged verbatim. Use this when an embedded peer
|
|
// silently misbehaves: most failure modes (inbound drops, wrong
|
|
// management URL, v6 unexpectedly on, userspace flipped, port clash)
|
|
// are obvious from these flags before any traffic flows.
|
|
func logEmbedOptions(logger *log.Logger, accountID types.AccountID, serviceID types.ServiceID, publicKey string, opts embed.Options) {
|
|
wgPort := 0
|
|
if opts.WireguardPort != nil {
|
|
wgPort = *opts.WireguardPort
|
|
}
|
|
mtu := uint16(0)
|
|
if opts.MTU != nil {
|
|
mtu = *opts.MTU
|
|
}
|
|
perfBuffers := uint32(0)
|
|
if opts.Performance.PreallocatedBuffersPerPool != nil {
|
|
perfBuffers = *opts.Performance.PreallocatedBuffersPerPool
|
|
}
|
|
perfBatch := uint32(0)
|
|
if opts.Performance.MaxBatchSize != nil {
|
|
perfBatch = *opts.Performance.MaxBatchSize
|
|
}
|
|
logger.WithFields(log.Fields{
|
|
"account_id": accountID,
|
|
"service_id": serviceID,
|
|
"public_key": publicKey,
|
|
"device_name": opts.DeviceName,
|
|
"management_url": opts.ManagementURL,
|
|
"log_level": opts.LogLevel,
|
|
"wg_port": wgPort,
|
|
"mtu": mtu,
|
|
"block_inbound": opts.BlockInbound,
|
|
"block_lan_access": opts.BlockLANAccess,
|
|
"disable_ipv6": opts.DisableIPv6,
|
|
"disable_client_routes": opts.DisableClientRoutes,
|
|
"no_userspace": opts.NoUserspace,
|
|
"config_path_set": opts.ConfigPath != "",
|
|
"state_path_set": opts.StatePath != "",
|
|
"private_key_present": opts.PrivateKey != "",
|
|
"presharedkey_present": opts.PreSharedKey != "",
|
|
"setup_key_present": opts.SetupKey != "",
|
|
"jwt_token_present": opts.JWTToken != "",
|
|
"dns_labels": opts.DNSLabels,
|
|
"perf_buffers_per_pool": perfBuffers,
|
|
"perf_max_batch_size": perfBatch,
|
|
}).Info("starting embedded netbird client for account")
|
|
}
|