daemon

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: GPL-3.0 Imports: 11 Imported by: 1 module, 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

Index

Constants

View Source
const DirEnvVar = "VPNGATE_DAEMON_DIR"

DirEnvVar overrides the base directory Dir() resolves under. Tests (in this package and elsewhere) set it to isolate state files instead of touching the real default location.

Variables

This section is empty.

Functions

func ConfigPath

func ConfigPath() string

ConfigPath returns the path to the daemon's persisted OpenVPN config.

func DetachAttr

func DetachAttr() *syscall.SysProcAttr

DetachAttr returns the SysProcAttr that starts a child process in its own session, detached from the parent's controlling terminal and process group, so it survives the parent exiting.

func Dir

func Dir() string

Dir returns the directory vpngate uses for daemon state, the persisted OpenVPN config, and the daemon log. It deliberately avoids both $HOME and os.TempDir()/$TMPDIR: daemon mode is typically launched with sudo (root), while status/disconnect are typically run as the invoking user, and both $HOME and $TMPDIR can differ between the two — on macOS in particular, $TMPDIR is a per-user path assigned by launchd (e.g. /var/folders/.../T/) that sudo does not preserve by default, so os.TempDir() alone resolves to two different directories for the same daemon depending on who's asking. defaultBaseDir() (unix/windows split) picks a single fixed, machine-wide location instead, so every invocation agrees regardless of privilege.

func IsAlive

func IsAlive(pid int) bool

IsAlive reports whether pid identifies a running process.

func LogPath

func LogPath() string

LogPath returns the path to the daemon's OpenVPN log file.

func PidPath

func PidPath() string

PidPath returns the path of the file holding the supervisor's PID. The supervisor writes it at startup, before its first connection attempt, and removes it on exit. It lets a foreground `connect -d` tell "daemon died during startup" apart from "daemon still negotiating", and guards against starting a second daemon while the first hasn't written its state file yet (state is only written once the tunnel is up).

func ReadPid

func ReadPid() (int, error)

ReadPid returns the PID recorded by WritePid. Check os.IsNotExist(err) for "no daemon starting".

func Remove

func Remove() error

Remove deletes StatePath(). It is not an error if the file is already gone.

func RemovePid

func RemovePid() error

RemovePid deletes PidPath(). It is not an error if the file is already gone.

func Save

func Save(state State) error

Save writes state to StatePath(), creating Dir() if needed. The write is atomic, so a crash mid-save never leaves a half-written state file for the next `status` invocation to choke on.

func SendStop

func SendStop(addr string, timeout time.Duration) error

SendStop dials addr and asks the daemon to disconnect and exit. It returns once the daemon has acknowledged the request, not once teardown is complete.

func StatePath

func StatePath() string

StatePath returns the path to the daemon's state file.

func WritePid

func WritePid(pid int) error

WritePid records pid in PidPath(), creating Dir() if needed.

Types

type ControlServer

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

ControlServer is the supervisor side of the control protocol: a loopback TCP listener that answers STATUS and STOP requests from separate `vpngate status`/`vpngate disconnect` invocations. Neither of those commands touches OpenVPN's management socket directly — only the supervisor can tell "OpenVPN exited, reconnect" apart from "the user asked to disconnect".

func NewControlServer

func NewControlServer(listener net.Listener, onStatus func() (Snapshot, error), onStop func()) *ControlServer

NewControlServer wraps an already-open listener. onStatus answers STATUS requests; onStop (may be nil) is invoked in its own goroutine, after the client has already been acknowledged, to tear the daemon down.

func (*ControlServer) Addr

func (c *ControlServer) Addr() string

Addr returns the address clients should dial.

func (*ControlServer) Serve

func (c *ControlServer) Serve()

Serve accepts connections until the listener is closed.

type Management

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

Management is a client for OpenVPN's plaintext management protocol. It is used only by the supervisor process, which is the sole client of a given OpenVPN instance's management socket — status/disconnect never dial it directly (see docs/superpowers/specs/2026-07-22-daemon-mode-design.md).

func DialManagement

func DialManagement(addr string, timeout time.Duration) (*Management, error)

DialManagement connects to addr and discards OpenVPN's greeting line.

func (*Management) Close

func (m *Management) Close() error

Close closes the underlying connection.

func (*Management) Disconnect

func (m *Management) Disconnect() error

Disconnect asks OpenVPN to shut down cleanly.

func (*Management) State

func (m *Management) State() (string, error)

State queries OpenVPN's current connection state (e.g. "CONNECTED", "RECONNECTING", "EXITING").

type Snapshot

type Snapshot struct {
	State       string    `json:"state"`
	HostName    string    `json:"hostname"`
	IPAddr      string    `json:"ip_addr"`
	CountryLong string    `json:"country"`
	StartedAt   time.Time `json:"started_at"`
	PID         int       `json:"pid"`
}

Snapshot is the supervisor's answer to a STATUS control request.

func SendStatus

func SendStatus(addr string, timeout time.Duration) (Snapshot, error)

SendStatus dials addr and returns the daemon's current snapshot.

type State

type State struct {
	PID         int       `json:"pid"`
	OpenVPNPID  int       `json:"openvpn_pid"`
	ControlAddr string    `json:"control_addr"`
	HostName    string    `json:"hostname"`
	IPAddr      string    `json:"ip_addr"`
	CountryLong string    `json:"country"`
	StartedAt   time.Time `json:"started_at"`
}

State is the on-disk record of a running daemon, written by the supervisor once OpenVPN reports a successful connection and read by `status`/`disconnect` in separate process invocations.

func Load

func Load() (State, error)

Load reads and parses StatePath(). Callers should check os.IsNotExist(err) to distinguish "no daemon running" from a real error.

Jump to

Keyboard shortcuts

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