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-04-29 20:52:20 +00:00
|
|
|
"tailscale.com/types/wgkey"
|
2021-01-29 20:16:36 +00:00
|
|
|
)
|
|
|
|
|
2021-04-30 22:34:03 +00:00
|
|
|
//go:generate go run tailscale.com/cmd/cloner -type=Config,Peer -output=clone.go
|
|
|
|
|
2021-02-05 20:44:43 +00:00
|
|
|
// EndpointDiscoSuffix is appended to the hex representation of a peer's discovery key
|
|
|
|
// and is then the sole wireguard endpoint for peers with a non-zero discovery key.
|
2021-04-30 23:27:43 +00:00
|
|
|
// This form is then recognize by magicsock's ParseEndpoint.
|
2021-02-05 20:44:43 +00:00
|
|
|
const EndpointDiscoSuffix = ".disco.tailscale:12345"
|
|
|
|
|
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-04-29 20:52:20 +00:00
|
|
|
PrivateKey wgkey.Private
|
2021-01-29 20:16:36 +00:00
|
|
|
Addresses []netaddr.IPPrefix
|
|
|
|
MTU uint16
|
|
|
|
DNS []netaddr.IP
|
|
|
|
Peers []Peer
|
|
|
|
}
|
|
|
|
|
|
|
|
type Peer struct {
|
2021-04-29 20:52:20 +00:00
|
|
|
PublicKey wgkey.Key
|
2021-01-29 20:16:36 +00:00
|
|
|
AllowedIPs []netaddr.IPPrefix
|
|
|
|
Endpoints string // comma-separated host/port pairs: "1.2.3.4:56,[::]:80"
|
|
|
|
PersistentKeepalive uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
// PeerWithKey returns the Peer with key k and reports whether it was found.
|
2021-04-29 20:52:20 +00:00
|
|
|
func (config Config) PeerWithKey(k wgkey.Key) (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
|
|
|
|
}
|