gluetun

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 15 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 gluetun provides a thin HTTP client for the Gluetun container's control server (https://github.com/qdm12/gluetun-wiki/blob/main/setup/advanced/control-server.md).

Pulse's dashboard surfaces VPN state — connected/IP/country/port-forwarded — alongside the rest of the stack. Disabled mode (nil baseURL) yields a nil *Client; callers must handle nil gracefully so the dashboard hides the VPN panel when no Gluetun is configured.

Index

Constants

This section is empty.

Variables

View Source
var ErrDisabled = errors.New("gluetun: disabled (no URL configured)")

ErrDisabled is returned when a method is called on a nil/disabled client.

View Source
var ErrRegionsUnavailable = errors.New("region changes are not available")

ErrRegionsUnavailable means region changes aren't set up (no servers directory, or Gluetun refused the settings routes).

View Source
var ErrRotateTooSoon = fmt.Errorf("rotated too recently")

ErrRotateTooSoon is returned when a rotation is requested inside the cool-down window. Wrapped with the remaining wait.

Functions

This section is empty.

Types

type Client

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

Client talks to a Gluetun control server.

func NewClient

func NewClient(baseURL string, logger *slog.Logger) *Client

NewClient returns a Gluetun client. If baseURL is empty, returns nil — callers should treat that as "disabled" and skip VPN status reporting. baseURL must include scheme (e.g. "http://vpn:8000"). A nil logger is fine; calls fall back to slog.Default().

func (*Client) PublicIP

func (c *Client) PublicIP(ctx context.Context) (string, error)

PublicIP returns the tunnel's current public IP (empty while the tunnel is down or the IP-fetch hasn't completed). Uses the slow client — after a reconnect the control server's IP endpoint can lag well past the 1s Status budget.

func (*Client) SetAPIKey

func (c *Client) SetAPIKey(key string)

SetAPIKey sets the key sent as X-API-Key on every control-server request. Empty sends none.

func (*Client) SetOpenVPNStatus

func (c *Client) SetOpenVPNStatus(ctx context.Context, status string) error

SetOpenVPNStatus starts or stops the OpenVPN tunnel via the control server. status must be "running" or "stopped". Uses the slow client — stopping/starting the tunnel can take several seconds to acknowledge.

func (*Client) SetRegions

func (c *Client) SetRegions(ctx context.Context, regions []string) error

SetRegions limits server selection to regions and applies it live: Gluetun validates the change and reconnects the tunnel. It lasts until the container restarts, when its environment applies again — see Rotator.KeepPreferredRegion.

func (*Client) Status

func (c *Client) Status(ctx context.Context) (*Status, error)

Status aggregates several Gluetun endpoints in parallel, returning a best-effort snapshot. A slow or unreachable endpoint never blocks the others past 1s — the corresponding fields are left as zero values rather than failing the whole call.

The Reachable field on the returned Status is true if at least one endpoint responded successfully — callers can use it to distinguish "VPN not configured" from "we configured it but Gluetun's control server isn't responding".

func (*Client) VPNSettings

func (c *Client) VPNSettings(ctx context.Context) (VPNSelection, error)

VPNSettings reads the provider, region filter and port-forwarding flag from GET /v1/vpn/settings.

type RegionCatalog

type RegionCatalog struct {
	Dir string // empty: no catalog, region changes unavailable
}

RegionCatalog reads the server lists Gluetun keeps on disk (/gluetun/servers/<provider>.json), mounted read-only into Pulse. Gluetun's control server doesn't list servers, and these files are exactly what it validates a region change against.

func (RegionCatalog) Regions

func (rc RegionCatalog) Regions(provider string, portForwardOnly bool) ([]string, error)

Regions returns the provider's region names, sorted. With portForwardOnly, only regions with a server that supports port forwarding — a region without one would leave the torrent client unreachable.

type RegionInfo

type RegionInfo struct {
	Available bool     `json:"available"`
	Reason    string   `json:"reason,omitempty"` // why not available
	Provider  string   `json:"provider,omitempty"`
	Current   []string `json:"current"`
	Preferred string   `json:"preferred,omitempty"`
	Regions   []string `json:"regions"`
	// PortForwardOnly: the list is limited to regions that can forward a
	// port, because port forwarding is on.
	PortForwardOnly bool `json:"port_forward_only"`
}

RegionInfo is what the dashboard's region picker shows.

type RegionPreference

type RegionPreference interface {
	Get(ctx context.Context) (string, error) // "" when none chosen
	Set(ctx context.Context, region string) error
}

RegionPreference persists the region the user picked, so it can be put back after the VPN container restarts with its environment's regions.

type RegionResult

type RegionResult struct {
	Region  string `json:"region"`
	OldIP   string `json:"old_ip"`
	NewIP   string `json:"new_ip"`
	Changed bool   `json:"changed"`
}

RegionResult reports a region change.

type RotateResult

type RotateResult struct {
	OldIP    string `json:"old_ip"`
	NewIP    string `json:"new_ip"`
	Attempts int    `json:"attempts"`
	Changed  bool   `json:"changed"`
}

RotateResult reports what a rotation accomplished.

type Rotator

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

Rotator cycles the VPN tunnel until the provider assigns a different exit IP, then clears IP-bound scraper state (FlareSolverr sessions, result cache) and kicks off a session pre-warm.

func NewRotator

func NewRotator(client rotateClient, clearScraperState func(), prewarm func(ctx context.Context), bus *events.Bus, logger *slog.Logger) *Rotator

NewRotator wires a Rotator. client must be non-nil (pass nil to the caller's RouterConfig instead when gluetun is disabled).

func (*Rotator) ChangeRegion

func (r *Rotator) ChangeRegion(ctx context.Context, region string) (RegionResult, error)

ChangeRegion moves the tunnel to region, remembers the choice, and clears IP-bound scraper state. It shares rotation's lock and cool-down: both reconnect the tunnel, pause torrents and void Cloudflare sessions.

func (*Rotator) EnableRegions

func (r *Rotator) EnableRegions(client regionClient, catalog RegionCatalog, prefs RegionPreference)

EnableRegions turns on region changes. Without it the region methods report ErrRegionsUnavailable.

func (*Rotator) KeepPreferredRegion

func (r *Rotator) KeepPreferredRegion(ctx context.Context, interval time.Duration)

KeepPreferredRegion puts the chosen region back whenever Gluetun is found using other regions — after the VPN container restarts, its environment's SERVER_REGIONS apply again. Checks every interval until ctx ends.

func (*Rotator) Regions

func (r *Rotator) Regions(ctx context.Context) RegionInfo

Regions describes the current and available regions.

func (*Rotator) Rotate

func (r *Rotator) Rotate(ctx context.Context) (RotateResult, error)

Rotate cycles the tunnel until the public IP changes, up to rotateMaxAttempts cycles. A same-IP outcome after all attempts is NOT an error — the tunnel is healthy, the provider just pinned the server; Changed=false lets the caller word that honestly. IP-bound scraper state is cleared after any successful reconnect, changed or not (the tunnel bounce invalidated the sessions either way).

type Status

type Status struct {
	// Reachable is true if at least one control-server endpoint responded.
	// Distinguishes "configured but unreachable" from real disconnection.
	Reachable         bool   `json:"reachable"`
	Connected         bool   `json:"connected"`
	PublicIP          string `json:"public_ip"`
	Country           string `json:"country"`
	Region            string `json:"region"`
	City              string `json:"city"`
	Hostname          string `json:"hostname"`
	Organization      string `json:"organization"`
	PortForwarded     int    `json:"port_forwarded"`
	DNSStatus         string `json:"dns_status"`
	Provider          string `json:"provider"`
	KillSwitchEnabled bool   `json:"kill_switch_enabled"`
	Version           string `json:"version"`
}

Status is the aggregated VPN status Pulse exposes on the dashboard. Zero values render as "unknown" in the UI — every field is best-effort.

type VPNSelection

type VPNSelection struct {
	Provider       string
	Regions        []string
	PortForwarding bool
}

VPNSelection is the part of Gluetun's VPN settings Pulse reads: which provider, which regions it may pick servers from, and whether port forwarding is on. The full settings document also carries the VPN credentials; they are never decoded.

Jump to

Keyboard shortcuts

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