vpn

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: AGPL-3.0 Imports: 21 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 vpn implements the built-in VPN protection: userspace WireGuard embedded directly in the process via wireguard-go + a gVisor netstack, so tunneling torrent traffic needs no NET_ADMIN capability or /dev/net/tun device — the same technique projects like Tailscale use. The "provider picker" the UI shows is a friendly wrapper over this one generic mechanism: every provider integration ultimately reduces to feeding this a WireGuard config, and the generic "paste your own config" option (always available) exercises the exact same path as a named provider.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Explain

func Explain(err error) string

Explain turns an error from bringing a tunnel up into a short sentence a person can act on. The technical detail belongs in the log, not on screen.

Types

type Config

type Config struct {
	PrivateKey          string // base64, as found in a standard WireGuard config file
	PeerPublicKey       string // base64
	PresharedKey        string // base64, optional
	Endpoint            string // host:port
	AllowedIPs          []string
	LocalAddresses      []string // this device's tunnel IP(s), e.g. "10.2.0.2/32"
	DNS                 []string
	PersistentKeepalive int // seconds, 0 = disabled
	MTU                 int // 0 = device.DefaultMTU
	ListenPort          int // 0 = let the OS pick (queryable afterward via Tunnel.ListenPort)
}

Config is a generic WireGuard peer config — deliberately provider- agnostic. A "provider picker" in Settings is just a UI convenience for producing one of these; the tunnel itself never special- cases a provider.

type KeyPair

type KeyPair struct {
	PrivateKey string
	PublicKey  string
}

KeyPair is a WireGuard key pair, base64-encoded the same way a wg-quick config file or `wg genkey`/`wg pubkey` would produce — this is what the Settings UI would generate for a user picking "generate a new key" (or, more commonly for this app, what gets pasted in from a VPN provider's own dashboard).

func GenerateKeyPair

func GenerateKeyPair() (KeyPair, error)

GenerateKeyPair creates a new random X25519 key pair.

type Manager

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

Manager owns the single live Tunnel (if any) for the whole app — only one VPN config is ever active at a time. It also remembers which connection is meant to be on, so a restart, a failed start or a silent server shows up as "not connected" instead of looking fine.

func NewManager

func NewManager() *Manager

func (*Manager) Activate

func (m *Manager) Activate(label string, cfg Config) error

Activate tears down any existing tunnel and brings up a new one for label/cfg.

func (*Manager) Broken

func (m *Manager) Broken(label, reason string)

Broken selects a connection that can't be started at all (for example its saved keys can't be read), so the app shows it as down with a reason instead of pretending nothing was selected.

func (*Manager) Close

func (m *Manager) Close()

Close stops the tunnel and any background start.

func (*Manager) Deactivate

func (m *Manager) Deactivate()

Deactivate tears down the current tunnel, if any.

func (*Manager) Restore

func (m *Manager) Restore(label string, cfg Config)

Restore selects a saved connection and brings it up in the background, trying again until it works or someone activates or deactivates. The state reads "connecting" meanwhile, so nothing claims the VPN is connected before it is. Used at start-up, when the network may not be ready yet.

func (*Manager) Status

func (m *Manager) Status() Status

func (*Manager) Tunnel

func (m *Manager) Tunnel() *Tunnel

Tunnel returns the live tunnel for dialing through, or nil if none is active. Callers (e.g. the torrent engine) should treat a nil tunnel or a failed Dial through it as "can't proceed" rather than silently falling back to a direct connection when the caller's configuration says traffic should be tunneled — that fallback is exactly what the kill-switch requirement rules out.

func (*Manager) WaitConnected

func (m *Manager) WaitConnected(ctx context.Context, d time.Duration) bool

WaitConnected waits up to d for the connection to work, and reports whether it does.

func (*Manager) Wanted

func (m *Manager) Wanted() bool

Wanted reports whether a connection is selected, whether or not it is up right now. Callers that must not fall back to a direct connection use it to tell "no VPN chosen" from "VPN chosen but down".

type Repo

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

func NewRepo

func NewRepo(db *sql.DB, box *crypto.Box) *Repo

func (*Repo) Active

func (r *Repo) Active() (StoredConfig, bool, error)

Active returns the currently active config, if any.

func (*Repo) Create

func (r *Repo) Create(label, provider string, cfg Config) (StoredConfig, error)

func (*Repo) Deactivate

func (r *Repo) Deactivate() error

func (*Repo) Delete

func (r *Repo) Delete(id int64) error

func (*Repo) List

func (r *Repo) List() ([]StoredConfig, error)

func (*Repo) SetActive

func (r *Repo) SetActive(id int64) error

SetActive marks id as the sole active config, deactivating any other.

type State

type State string

State says whether the VPN is doing its job right now.

const (
	// StateOff means no connection is selected.
	StateOff State = "off"
	// StateConnecting means a connection is selected and being brought up, or
	// its server has not answered yet.
	StateConnecting State = "connecting"
	// StateConnected means the tunnel is up and its server answered recently.
	StateConnected State = "connected"
	// StateDown means a connection is selected but it is not working.
	StateDown State = "down"
)

type Status

type Status struct {
	// Connected is true only when the tunnel is up and its server has answered
	// recently. A tunnel that exists but hears nothing is not connected.
	Connected bool
	State     State
	// Label is the connection that is meant to be on, even while it isn't up.
	Label       string
	ConnectedAt time.Time
	// Reason says, in plain words, why a selected connection isn't connected.
	Reason string
}

Status reports current connection state for the UI ("status visibility... shown clearly in the UI, not just in logs").

type StoredConfig

type StoredConfig struct {
	ID       int64
	Label    string
	Provider string
	Active   bool
	Config   Config
}

StoredConfig is a saved VPN config ("multiple provider configs stored, one active at a time, switchable from Settings").

type Tunnel

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

Tunnel is a live WireGuard connection. Callers dial out through it via DialContext, which is all that's needed to route torrent traffic (or, optionally, Usenet/general app traffic) through the tunnel instead of the host network directly.

func Up

func Up(cfg Config) (*Tunnel, error)

Up brings a tunnel to the configured peer. It does not verify the handshake completes — that happens asynchronously, same as any WireGuard client; callers should treat a Dial failing/timing out as the signal the tunnel isn't actually passing traffic (this is also what the kill switch in the download engine checks).

func (*Tunnel) Close

func (t *Tunnel) Close()

Close tears down the tunnel. Any in-flight Dial-ed connections through it will start failing — the natural kill-switch behavior ("if the tunnel drops, torrent traffic must pause immediately") as long as callers treat a dial/read/write error on a tunneled connection as fatal for that transfer rather than silently falling back to a direct connection.

func (*Tunnel) DialContext

func (t *Tunnel) DialContext(ctx context.Context, network, addr string) (net.Conn, error)

DialContext dials addr through the tunnel — use this as the dialer for anything that should go over the VPN (torrent traffic by default, Usenet/general traffic optionally).

func (*Tunnel) LastHandshake

func (t *Tunnel) LastHandshake() (time.Time, error)

LastHandshake returns when the VPN server last completed a handshake with this tunnel, or the zero time if it never has. A handshake only happens when the server really answers, so this is the honest signal that the tunnel works; a tunnel that is merely "up" may still be talking to nothing.

func (*Tunnel) ListenPort

func (t *Tunnel) ListenPort() (int, error)

ListenPort returns the UDP port this tunnel's WireGuard endpoint is actually bound to — useful when Config.ListenPort was 0 (OS-assigned) and a peer needs to be told where to reach this instance.

func (*Tunnel) ListenTCP

func (t *Tunnel) ListenTCP(addr string) (net.Listener, error)

ListenTCP listens on addr within the tunnel's own virtual network stack (mainly useful for tests that need a reachable-through-the-tunnel endpoint; production use of this package is all outbound Dial calls).

func (*Tunnel) PublicIP

func (t *Tunnel) PublicIP(ctx context.Context) (string, error)

PublicIP asks a third-party IP-echo service, dialed through the tunnel, what address it's seeing — the standard way any VPN app confirms traffic is actually egressing via the provider rather than leaking ("status visibility"). This is an on-demand outbound request the user explicitly triggers by viewing VPN status, not a background poll.

Jump to

Keyboard shortcuts

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