2021-01-29 20:16:36 +00:00
|
|
|
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package wgcfg has types and a parser for representing WireGuard config.
|
|
|
|
package wgcfg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"inet.af/netaddr"
|
2021-10-28 00:42:33 +00:00
|
|
|
"tailscale.com/types/key"
|
2021-01-29 20:16:36 +00:00
|
|
|
)
|
|
|
|
|
2022-05-01 23:15:14 +00:00
|
|
|
//go:generate go run tailscale.com/cmd/cloner -type=Config,Peer
|
2021-02-05 20:44:43 +00:00
|
|
|
|
2021-01-29 20:16:36 +00:00
|
|
|
// Config is a WireGuard configuration.
|
|
|
|
// It only supports the set of things Tailscale uses.
|
|
|
|
type Config struct {
|
|
|
|
Name string
|
2021-10-28 00:42:33 +00:00
|
|
|
PrivateKey key.NodePrivate
|
2021-01-29 20:16:36 +00:00
|
|
|
Addresses []netaddr.IPPrefix
|
|
|
|
MTU uint16
|
|
|
|
DNS []netaddr.IP
|
|
|
|
Peers []Peer
|
|
|
|
}
|
|
|
|
|
|
|
|
type Peer struct {
|
2021-10-28 00:42:33 +00:00
|
|
|
PublicKey key.NodePublic
|
2021-10-29 21:27:29 +00:00
|
|
|
DiscoKey key.DiscoPublic // present only so we can handle restarts within wgengine, not passed to WireGuard
|
2021-01-29 20:16:36 +00:00
|
|
|
AllowedIPs []netaddr.IPPrefix
|
|
|
|
PersistentKeepalive uint16
|
2021-11-11 02:42:16 +00:00
|
|
|
// wireguard-go's endpoint for this peer. It should always equal Peer.PublicKey.
|
|
|
|
// We represent it explicitly so that we can detect if they diverge and recover.
|
|
|
|
// There is no need to set WGEndpoint explicitly when constructing a Peer by hand.
|
|
|
|
// It is only populated when reading Peers from wireguard-go.
|
|
|
|
WGEndpoint key.NodePublic
|
2021-01-29 20:16:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PeerWithKey returns the Peer with key k and reports whether it was found.
|
2021-10-28 00:42:33 +00:00
|
|
|
func (config Config) PeerWithKey(k key.NodePublic) (Peer, bool) {
|
2021-01-29 20:16:36 +00:00
|
|
|
for _, p := range config.Peers {
|
|
|
|
if p.PublicKey == k {
|
|
|
|
return p, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Peer{}, false
|
|
|
|
}
|