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 ¶
- func DeleteLink(iface string) (bool, error)
- func Ensure(cfg Config) (changes []string, err error)
- func OtherDeviceWithKey(iface string, key wgtypes.Key) (string, bool)
- func Owned(iface string, expectedKey wgtypes.Key) (owned, exists bool, err error)
- func Status(iface string) (*wgtypes.Device, error)
- type Config
- type DeviceState
- type Peer
- type StartupRefusal
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DeleteLink ¶
DeleteLink は iface を削除する。無ければ false, nil(撤去を途中からでも走らせられるように)。 所有判定は呼び出し側(Owned)で済ませてから呼ぶこと。
func Ensure ¶
Ensure は wg0 を宣言に収束させ、変えた点を返す。なければ作り、あれば差分だけ直す。 手作業で変えられたアドレス、MTU、ポート、ピア、秘密鍵はここで宣言に戻る。 ただし収束するのは「自分が作ったインタフェース」だけで、既存の同名インタフェースは 鍵が一致する(=過去に自分が作った)ときにしか触らない。一致しなければ何も書かずに StartupRefusal を返す(仕様 9 節)。
func OtherDeviceWithKey ¶
OtherDeviceWithKey は、iface 以外に同じ秘密鍵を持つ WireGuard デバイス(=改名で残った自分の 旧インタフェース)があればその名前を返す。列挙できなければ黙って false。
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 が真のときだけ、鍵の一致しない既存インタフェースを引き継ぐ。
// 既定は偽で、他人のインタフェースは収束させず StartupRefusal で中止する。
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 StartupRefusal ¶
StartupRefusal は、他人の wg インタフェースやポート・アドレスの衝突を見つけて 収束を拒み、起動を中止させる理由。呼び出し側はこれを専用の終了コードに写す。 何も書き換える前に返るので、既存の設定は無傷のままである。
func (*StartupRefusal) Error ¶
func (e *StartupRefusal) Error() string
Error は中止の理由と、収束するはずだった差分のドライランを 1 つの文にする。