wg

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package wg は VPS(および将来の agent の kernel backend、Phase 7)の WireGuard インタフェースを 宣言に収束させる(仕様 4, 9 節、設計文書 7a.7 節)。インタフェースの作成とアドレス・MTU は netlink で、鍵・ポート・ピアは wgctrl で扱う。停止時には何も削除しない。internal/vpsd を import しない(internal/platform/linux の bind 中ポート検査だけを使う)。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeleteLink(iface string) (bool, error)

DeleteLink は iface を削除する。無ければ false, nil(撤去を途中からでも走らせられるように)。 所有判定は呼び出し側(Owned)で済ませてから呼ぶこと。

func Ensure

func Ensure(cfg Config) (changes []string, err error)

Ensure は wg0 を宣言に収束させ、変えた点を返す。なければ作り、あれば差分だけ直す。 手作業で変えられたアドレス、MTU、ポート、ピア、秘密鍵はここで宣言に戻る。 ただし収束するのは「自分が作ったインタフェース」だけで、既存の同名インタフェースは 鍵が一致する(=過去に自分が作った)ときにしか触らない。一致しなければ何も書かずに conflictError を返す(仕様 9 節)。

func OtherDeviceWithKey

func OtherDeviceWithKey(iface string, key wgtypes.Key) (string, bool)

OtherDeviceWithKey は、iface 以外に同じ秘密鍵を持つ WireGuard デバイス(=改名で残った自分の 旧インタフェース)があればその名前を返す。列挙できなければ黙って false。

func Owned

func Owned(iface string, expectedKey wgtypes.Key) (owned, exists bool, err error)

Owned は iface が存在し、その秘密鍵が expectedKey と一致する(=wgft が作った)かを返す。 撤去(teardown)で、他人の wg を消さないための事前判定に使う。

func Status

func Status(iface string) (*wgtypes.Device, error)

Status は wg0 の現在の状態(表示と監視用)。

Types

type Config

type Config struct {
	Interface  string
	PrivateKey wgtypes.Key
	ListenPort int
	Address    netip.Prefix // 10.200.0.1/24
	MTU        int
	Peers      []Peer
	// AdoptExisting が真のときだけ、鍵の一致しない既存インタフェースを引き継ぐ。
	// 既定は偽で、他人のインタフェースは収束させず、何も書かずに中止する。
	AdoptExisting bool
	// KeepPeers が真のとき、ピアには触れず(Peers は読まない)、インタフェース、鍵、ポート、
	// アドレス、MTU だけを収束させる。起動時のインタフェースの立ち上げに使い、ピアの変更は
	// 公開の前後に分けて行うトランザクションに任せる(設計文書 7a.3 節)。
	KeepPeers bool
}

Config は wg0 の宣言。

type DeviceState added in v0.5.0

type DeviceState struct {
	Exists bool
	// Kind is the link type ("wireguard" for a WireGuard device).
	Kind string
	// The rest is read only for a WireGuard device.
	PrivateKey wgtypes.Key
	ListenPort int
	Addresses  []netip.Prefix // IPv4 only, as Ensure converges them
	Up         bool
	Peers      []Peer // a peer whose AllowedIPs is not exactly one /32 has an invalid Address
}

DeviceState is what Inspect reads back of iface: enough to tell whether it still is what the last transaction converged it to (design.md 7a.3 節: 実際の状態への収束).

func Inspect added in v0.5.0

func Inspect(iface string) (DeviceState, error)

Inspect reads iface without changing anything: whether it exists, its link type and, for a WireGuard device, its key, listen port, IPv4 addresses, up flag and peers. It is a few netlink reads, cheap enough for every Observe.

type Peer

type Peer struct {
	PublicKey wgtypes.Key
	Address   netip.Addr
}

Peer はエージェント 1 つ分のピア。AllowedIPs は Address の /32 だけ。

Jump to

Keyboard shortcuts

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