netx

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: AGPL-3.0 Imports: 43 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package netx opens the networks komando reaches targets through (docs/features/vpn.md). WireGuard, SOCKS, HTTP proxies, L2TP, IPsec, IKEv2, Tailscale, OpenVPN and AnyConnect run inside komando, in user space, so the computer's routes are not changed. Only the openconnect family (globalprotect, fortinet, pulse, f5) uses an outside client when openconnect is installed; without it komando uses its own experimental client. A "system" VPN is one set up in the computer's own settings and does change routes. Session kinds run as a background session that later commands reuse.

Index

Constants

This section is empty.

Variables

View Source
var KeyLockDir = func() (string, error) {
	d, err := os.UserCacheDir()
	if err != nil {
		return "", err
	}
	return filepath.Join(d, "komando", "wireguard"), nil
}

KeyLockDir is the per-user folder of key locks; tests point it elsewhere.

View Source
var Kinds = map[string]Kind{
	"wireguard":     {Desc: "WireGuard, run inside komando (no admin rights, many at once)"},
	"socks":         {Desc: "a SOCKS5 proxy that is already running (`komando tunnel start TARGET -D`, a company proxy)"},
	"proxy":         {Desc: "an HTTP proxy (CONNECT), such as a company proxy: every connection is tunneled through it"},
	"system":        {Desc: "a VPN set up in your computer's settings; --service lets `vpn up` connect it"},
	"l2tp":          {Desc: "L2TP/IPsec with a pre-shared key, run inside komando (no admin rights)", Session: true},
	"ipsec":         {Desc: "Cisco-style IPsec (IKEv1, group name and XAUTH login), run inside komando", Session: true},
	"ikev2":         {Desc: "IKEv2/IPsec (pre-shared key or EAP login with a server certificate), run inside komando", Session: true},
	"tailscale":     {Desc: "Tailscale or Headscale, run inside komando (no tailscaled, no admin rights)", Session: true},
	"globalprotect": {Desc: "Palo Alto GlobalProtect", Session: true, Client: []string{"openconnect", "ocproxy"}, Protocol: "gp"},
	"fortinet":      {Desc: "Fortinet SSL VPN (FortiClient)", Session: true, Client: []string{"openconnect", "ocproxy"}, Protocol: "fortinet"},
	"anyconnect":    {Desc: "Cisco AnyConnect / ocserv, run inside komando (no openconnect)", Session: true},
	"pulse":         {Desc: "Pulse Secure / Ivanti Connect Secure", Session: true, Client: []string{"openconnect", "ocproxy"}, Protocol: "pulse"},
	"f5":            {Desc: "F5 BIG-IP SSL VPN", Session: true, Client: []string{"openconnect", "ocproxy"}, Protocol: "f5"},
	"openvpn":       {Desc: "OpenVPN, also Pritunl profiles, run inside komando (no openvpn program, Docker or admin rights)", Session: true},
}

Kinds are the VPN kinds komando knows.

View Source
var LockBusy func(name string)

LockBusy, when set, is told that Lock has to wait for another komando that is starting or stopping the same VPN.

View Source
var SessionSecret string

SessionSecret is what a person gave for this session: a typed OpenVPN password, or the cookie of a VPN login done at the terminal. It reaches `komando vpn serve` on its stdin, never on a command line.

View Source
var StartSession func(ctx context.Context, name string) error

StartSession is set by the CLI: it brings up a session for a VPN that can connect without a human (no MFA). Open calls it on demand.

View Source
var WriteFor func(ctx context.Context) store.Write

WriteFor is set by the CLI: how the runtime record (a session's last use, a dead session gone) changes for the command ctx belongs to: store.Preview() under its --dry-run, so a preview writes nothing. Nil: store.Save().

Functions

func Alive

func Alive(pid int) bool

Alive reports whether a process exists.

func BuiltinSSL

func BuiltinSSL(kind string) bool

BuiltinSSL reports whether komando speaks the kind itself.

func BuiltinSSLFor added in v0.12.0

func BuiltinSSLFor(v *store.Vpn) bool

BuiltinSSLFor is BuiltinSSL for one VPN: a GlobalProtect VPN without a user signs in through a browser, which only komando's own client does, so it never goes to openconnect.

func Clashes

func Clashes(before, after []Route) []string

Clashes compares the routes before and after a VPN connected. A new route that overlaps one another interface already had means two networks claim the same addresses: one of them will not be reachable.

func Down

func Down(v *store.Vpn, err error) error

Down is the error for a VPN that cannot be used right now. The hint says what to do for its kind.

func DownHint

func DownHint(v *store.Vpn) string

DownHint is what to do when v does not answer.

func EditKeyUsers added in v0.17.0

func EditKeyUsers(pid int, procStart string, fn func(users []string) []string) error

EditKeyUsers changes the Users of the key record the process pid (started at procStart) holds; nothing when it holds none.

func ForgetTailscale added in v0.13.0

func ForgetTailscale(v *store.Vpn) error

ForgetTailscale removes a VPN's node state and folder (vpn rm).

func KindNames

func KindNames() []string

KindNames lists the kinds, sorted.

func LiveSession

func LiveSession(ctx context.Context, v *store.Vpn) *store.VpnSession

LiveSession returns the VPN's background session if it is running and not idle for longer than the vpn_idle setting. An idle one is stopped. It marks the session used, unless the command of ctx only previews (WriteFor).

func Lock

func Lock(name string) (unlock func(), err error)

Lock holds the per-VPN lock that starting and stopping a session take, so a stale stop can never hit a session another process just started.

func LoginRequired

func LoginRequired(v *store.Vpn) error

LoginRequired is the error for a VPN that needs a human to log in.

func ProcessStart added in v0.15.0

func ProcessStart(pid int) string

ProcessStart is when pid started, in clock ticks after the system booted (field 22 of /proc/PID/stat); "" when there is no such process.

func ProxyURL added in v0.7.0

func ProxyURL(address string) (*url.URL, error)

ProxyURL reads a proxy VPN's --address: host:port (plain HTTP to the proxy) or an http:// or https:// URL.

func ReapIdle

func ReapIdle()

ReapIdle closes sessions unused for longer than vpn_idle. It runs at the start of every komando command, so sessions do not stay up forever.

func RecordStart added in v0.15.0

func RecordStart(pid int) (string, error)

RecordStart is ProcessStart for a process komando just started, as its session records it: an error where this system reads start times but cannot read this one (the process already ended).

func ReleaseKey added in v0.17.0

func ReleaseKey(pub string, pid int) error

ReleaseKey removes the record of pub while it is still pid's.

func SSLVPNLogin

func SSLVPNLogin(ctx context.Context, v *store.Vpn, prompt func(label string, secret bool) (string, error),
	browser func(context.Context, sslvpn.BrowserPage) (string, error), notices io.Writer) (string, error)

SSLVPNLogin logs in at the terminal and returns the session cookie. browser shows a sign-in page to the person (GlobalProtect with SAML); nil, like a nil prompt, means nobody is there. notices gets what the browser sign-in tells the person (the stderr of the command that logs in); nil writes them nowhere.

func SecretHint

func SecretHint(ref string) string

SecretHint says how to provide a secret reference that does not resolve.

func SessionAlive

func SessionAlive(s *store.VpnSession) bool

SessionAlive reports whether a session's process runs: the pid is alive, runs the session's program, and started when the session's process did. A pid the system gave to another process later (after a crash, or named by an old copy of runtime.json put back) is not the session's, even when it runs the same program: its SOCKS port and login must not go to it, and it must not be signalled.

func StopSession

func StopSession(s *store.VpnSession) error

StopSession ends a session's process. A session that is no longer running is already stopped. It reports an error when it could not stop it; the caller keeps the session then.

func StripWGKeys

func StripWGKeys(text string) (clean, private, psk string, err error)

StripWGKeys removes PrivateKey and PresharedKey lines from a wg-quick file, returning the rest and the keys, so only the vault holds them. komando supports one preshared key per VPN.

func TailscaleDir

func TailscaleDir(v *store.Vpn) string

TailscaleDir is tsnet's folder for the VPN (no keys: see sealedTSState).

func TailscaleLogin

func TailscaleLogin(ctx context.Context, v *store.Vpn, show func(url string)) error

TailscaleLogin logs the node in through the browser: show gets the login page. It returns once the node is connected; its state is kept, so later sessions start without a login until the key expires.

func Terminate

func Terminate(pid int) error

Terminate asks one process to stop. It refuses every pid that is not a single other process: kill(-1) would signal every program of the user, kill(0) the whole process group, kill(1) init.

func WGPublicKey added in v0.17.0

func WGPublicKey(private string) (string, error)

WGPublicKey derives the base64 public key of a base64 private key.

Types

type Dialer

type Dialer func(ctx context.Context, network, addr string) (net.Conn, error)

Dialer opens a TCP connection through a network.

func Open

func Open(ctx context.Context, v *store.Vpn, timeout time.Duration) (Dialer, io.Closer, error)

Open starts v (or reuses its background session) and returns a dialer through it. Close the closer when done.

func OpenDirect

func OpenDirect(ctx context.Context, v *store.Vpn, timeout time.Duration) (Dialer, io.Closer, error)

OpenDirect opens an in-process kind (wireguard, socks, system) without looking for a session. `komando vpn serve` uses it.

func StartWG

func StartWG(c *WGConfig) (Dialer, io.Closer, error)

StartWG brings up a userspace WireGuard device for c and returns a dialer into its network.

type KeyHolder added in v0.17.0

type KeyHolder struct {
	PublicKey string `json:"public_key"`
	PID       int    `json:"pid"`
	ProcStart string `json:"proc_start,omitempty"` // ProcessStart of PID: a reused pid is another process
	Socks     string `json:"socks,omitempty"`
	Home      string `json:"home"`
	Vpn       string `json:"vpn"`
	// Users are the VPN entries of Home that use this session (the same
	// key); the last one's `vpn down` stops it.
	Users []string `json:"users,omitempty"`
}

KeyHolder is the komando process that runs a WireGuard session for one interface key, as the key lock records it. It never holds the private key.

func ClaimKey added in v0.17.0

func ClaimKey(h KeyHolder) (other *KeyHolder, err error)

ClaimKey records h as the holder of h.PublicKey. When another live process holds it, nothing is written and that holder is returned. A record whose process has ended is taken over.

func KeyInUse added in v0.17.0

func KeyInUse(pub string) (*KeyHolder, error)

KeyInUse is the live process holding pub, or nil.

func (*KeyHolder) Alive added in v0.17.0

func (h *KeyHolder) Alive() bool

Alive reports whether the recorded process still runs: the same pid with the same start time.

type Kind

type Kind struct {
	Desc     string
	Session  bool
	Client   []string
	Protocol string // openconnect --protocol
}

Kind describes a VPN kind. Session kinds run a client in the background (`komando vpn up`), reused by later commands; Client names the program that must be installed for it.

type Route

type Route struct {
	Prefix netip.Prefix
	Iface  string
}

Route is one IPv4 entry of the computer's routing table.

func Routes

func Routes() ([]Route, error)

Routes reads the computer's IPv4 routes (netstat on macOS, ip on Linux). Only VPNs that change the computer's network (system VPNs) need it: the in-process and container kinds never add routes.

type WGConfig

type WGConfig struct {
	PrivateKey string
	Addresses  []netip.Addr
	DNS        []netip.Addr
	MTU        int
	Peers      []WGPeer
}

WGConfig is a wg-quick file ([Interface] and [Peer] sections).

func ParseWG

func ParseWG(text string) (*WGConfig, error)

ParseWG reads a wg-quick file. Keys stay base64, as in the file.

func (*WGConfig) UAPI

func (c *WGConfig) UAPI() (string, error)

UAPI turns the config into WireGuard's configuration protocol. Endpoints are resolved here, since the protocol takes IP:port.

type WGPeer

type WGPeer struct {
	PublicKey    string
	PresharedKey string
	Endpoint     string
	AllowedIPs   []netip.Prefix
	Keepalive    int
}

type WGStats added in v0.17.0

type WGStats struct {
	LastHandshake time.Time
	TxBytes       uint64
	Keepalive     int
}

WGStats is what a WireGuard device reports about its peers: the newest handshake (zero before the first), the bytes sent to all peers, and the longest PersistentKeepalive in seconds (0 when none is set).

Jump to

Keyboard shortcuts

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