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 ¶
- func Explain(err error) string
- type Config
- type KeyPair
- type Manager
- func (m *Manager) Activate(label string, cfg Config) error
- func (m *Manager) Broken(label, reason string)
- func (m *Manager) Close()
- func (m *Manager) Deactivate()
- func (m *Manager) Restore(label string, cfg Config)
- func (m *Manager) Status() Status
- func (m *Manager) Tunnel() *Tunnel
- func (m *Manager) WaitConnected(ctx context.Context, d time.Duration) bool
- func (m *Manager) Wanted() bool
- type Repo
- type State
- type Status
- type StoredConfig
- type Tunnel
- func (t *Tunnel) Close()
- func (t *Tunnel) DialContext(ctx context.Context, network, addr string) (net.Conn, error)
- func (t *Tunnel) LastHandshake() (time.Time, error)
- func (t *Tunnel) ListenPort() (int, error)
- func (t *Tunnel) ListenTCP(addr string) (net.Listener, error)
- func (t *Tunnel) PublicIP(ctx context.Context) (string, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct {
PrivateKey string // base64, as found in a standard WireGuard config file
PeerPublicKey string // base64
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 ¶
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 ¶
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 ¶
Activate tears down any existing tunnel and brings up a new one for label/cfg.
func (*Manager) Broken ¶
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) Deactivate ¶
func (m *Manager) Deactivate()
Deactivate tears down the current tunnel, if any.
func (*Manager) Restore ¶
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) 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 ¶
WaitConnected waits up to d for the connection to work, and reports whether it does.
type Repo ¶
type Repo struct {
// contains filtered or unexported fields
}
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 (*Repo) List ¶
func (r *Repo) List() ([]StoredConfig, error)
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.