Rename WgState to State and NewWgState to New
This also caused renaming local variables to avoid conflicting with the wg package name itself.
This commit is contained in:
parent
5d1acc1fab
commit
b0e41d5c35
14
main.go
14
main.go
|
@ -33,7 +33,7 @@ func main() {
|
||||||
}
|
}
|
||||||
logrus.SetLevel(logLevel)
|
logrus.SetLevel(logLevel)
|
||||||
|
|
||||||
wg, err := wg.NewWGConfig(config.Interface, config.WireguardPort)
|
wgstate, err := wg.New(config.Interface, config.WireguardPort)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithError(err).Fatal("could not instantiate wireguard controller")
|
logrus.WithError(err).Fatal("could not instantiate wireguard controller")
|
||||||
}
|
}
|
||||||
|
@ -43,10 +43,10 @@ func main() {
|
||||||
logrus.WithError(err).Fatal("could not create cluster")
|
logrus.WithError(err).Fatal("could not create cluster")
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.AssignOverlayAddr((*net.IPNet)(config.OverlayNet), cluster.LocalName)
|
wgstate.AssignOverlayAddr((*net.IPNet)(config.OverlayNet), cluster.LocalName)
|
||||||
localNode := common.Node{}
|
localNode := common.Node{}
|
||||||
localNode.OverlayAddr = wg.OverlayAddr
|
localNode.OverlayAddr = wgstate.OverlayAddr
|
||||||
localNode.PubKey = wg.PubKey.String()
|
localNode.PubKey = wgstate.PubKey.String()
|
||||||
cluster.Update(localNode)
|
cluster.Update(localNode)
|
||||||
|
|
||||||
nodec := cluster.Members() // avoid deadlocks by starting before join
|
nodec := cluster.Members() // avoid deadlocks by starting before join
|
||||||
|
@ -76,9 +76,9 @@ func main() {
|
||||||
nodes = append(nodes, node)
|
nodes = append(nodes, node)
|
||||||
logrus.Infof("\taddr: %s, overlay: %s, pubkey: %s", node.Addr, node.OverlayAddr, node.PubKey)
|
logrus.Infof("\taddr: %s, overlay: %s, pubkey: %s", node.Addr, node.OverlayAddr, node.PubKey)
|
||||||
}
|
}
|
||||||
if err := wg.SetUpInterface(nodes); err != nil {
|
if err := wgstate.SetUpInterface(nodes); err != nil {
|
||||||
logrus.WithError(err).Error("could not up interface")
|
logrus.WithError(err).Error("could not up interface")
|
||||||
wg.DownInterface()
|
wgstate.DownInterface()
|
||||||
}
|
}
|
||||||
if !config.NoEtcHosts {
|
if !config.NoEtcHosts {
|
||||||
if err := writeToEtcHosts(nodes); err != nil {
|
if err := writeToEtcHosts(nodes); err != nil {
|
||||||
|
@ -93,7 +93,7 @@ func main() {
|
||||||
logrus.WithError(err).Error("could not remove stale hosts entries")
|
logrus.WithError(err).Error("could not remove stale hosts entries")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := wg.DownInterface(); err != nil {
|
if err := wgstate.DownInterface(); err != nil {
|
||||||
logrus.WithError(err).Error("could not down interface")
|
logrus.WithError(err).Error("could not down interface")
|
||||||
}
|
}
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
|
|
|
@ -12,8 +12,8 @@ import (
|
||||||
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
||||||
)
|
)
|
||||||
|
|
||||||
// WgState holds the configured state of a Wesher Wireguard interface
|
// State holds the configured state of a Wesher Wireguard interface
|
||||||
type WgState struct {
|
type State struct {
|
||||||
iface string
|
iface string
|
||||||
client *wgctrl.Client
|
client *wgctrl.Client
|
||||||
OverlayAddr net.IPNet
|
OverlayAddr net.IPNet
|
||||||
|
@ -22,10 +22,10 @@ type WgState struct {
|
||||||
PubKey wgtypes.Key
|
PubKey wgtypes.Key
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewWGConfig creates a new Wesher Wireguard state
|
// New creates a new Wesher Wireguard state
|
||||||
// The Wireguard keys are generated for every new interface
|
// The Wireguard keys are generated for every new interface
|
||||||
// The interface must later be setup using SetUpInterface
|
// The interface must later be setup using SetUpInterface
|
||||||
func NewWGConfig(iface string, port int) (*WgState, error) {
|
func New(iface string, port int) (*State, error) {
|
||||||
client, err := wgctrl.New()
|
client, err := wgctrl.New()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "could not instantiate wireguard client")
|
return nil, errors.Wrap(err, "could not instantiate wireguard client")
|
||||||
|
@ -37,14 +37,14 @@ func NewWGConfig(iface string, port int) (*WgState, error) {
|
||||||
}
|
}
|
||||||
pubKey := privKey.PublicKey()
|
pubKey := privKey.PublicKey()
|
||||||
|
|
||||||
wgState := WgState{
|
state := State{
|
||||||
iface: iface,
|
iface: iface,
|
||||||
client: client,
|
client: client,
|
||||||
Port: port,
|
Port: port,
|
||||||
PrivKey: privKey,
|
PrivKey: privKey,
|
||||||
PubKey: pubKey,
|
PubKey: pubKey,
|
||||||
}
|
}
|
||||||
return &wgState, nil
|
return &state, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AssignOverlayAddr assigns a new address to the interface
|
// AssignOverlayAddr assigns a new address to the interface
|
||||||
|
@ -52,7 +52,7 @@ func NewWGConfig(iface string, port int) (*WgState, error) {
|
||||||
// provided name deterministically
|
// provided name deterministically
|
||||||
// Currently, the address is assigned by hashing the name and mapping that
|
// Currently, the address is assigned by hashing the name and mapping that
|
||||||
// hash in the target network space
|
// hash in the target network space
|
||||||
func (wg *WgState) AssignOverlayAddr(ipnet *net.IPNet, name string) {
|
func (s *State) AssignOverlayAddr(ipnet *net.IPNet, name string) {
|
||||||
// TODO: this is way too brittle and opaque
|
// TODO: this is way too brittle and opaque
|
||||||
bits, size := ipnet.Mask.Size()
|
bits, size := ipnet.Mask.Size()
|
||||||
ip := make([]byte, len(ipnet.IP))
|
ip := make([]byte, len(ipnet.IP))
|
||||||
|
@ -66,21 +66,21 @@ func (wg *WgState) AssignOverlayAddr(ipnet *net.IPNet, name string) {
|
||||||
ip[len(ip)-i] = hb[len(hb)-i]
|
ip[len(ip)-i] = hb[len(hb)-i]
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.OverlayAddr = net.IPNet{
|
s.OverlayAddr = net.IPNet{
|
||||||
IP: net.IP(ip),
|
IP: net.IP(ip),
|
||||||
Mask: net.CIDRMask(size, size), // either /32 or /128, depending if ipv4 or ipv6
|
Mask: net.CIDRMask(size, size), // either /32 or /128, depending if ipv4 or ipv6
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DownInterface shuts down the associated network interface
|
// DownInterface shuts down the associated network interface
|
||||||
func (wg *WgState) DownInterface() error {
|
func (s *State) DownInterface() error {
|
||||||
if _, err := wg.client.Device(wg.iface); err != nil {
|
if _, err := s.client.Device(s.iface); err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
return nil // device already gone; noop
|
return nil // device already gone; noop
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
link, err := netlink.LinkByName(wg.iface)
|
link, err := netlink.LinkByName(s.iface)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -88,39 +88,39 @@ func (wg *WgState) DownInterface() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetUpInterface creates and setup the associated network interface
|
// SetUpInterface creates and setup the associated network interface
|
||||||
func (wg *WgState) SetUpInterface(nodes []common.Node) error {
|
func (s *State) SetUpInterface(nodes []common.Node) error {
|
||||||
if err := netlink.LinkAdd(&wireguard{LinkAttrs: netlink.LinkAttrs{Name: wg.iface}}); err != nil && !os.IsExist(err) {
|
if err := netlink.LinkAdd(&wireguard{LinkAttrs: netlink.LinkAttrs{Name: s.iface}}); err != nil && !os.IsExist(err) {
|
||||||
return errors.Wrapf(err, "could not create interface %s", wg.iface)
|
return errors.Wrapf(err, "could not create interface %s", s.iface)
|
||||||
}
|
}
|
||||||
|
|
||||||
peerCfgs, err := wg.nodesToPeerConfigs(nodes)
|
peerCfgs, err := s.nodesToPeerConfigs(nodes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "error converting received node information to wireguard format")
|
return errors.Wrap(err, "error converting received node information to wireguard format")
|
||||||
}
|
}
|
||||||
if err := wg.client.ConfigureDevice(wg.iface, wgtypes.Config{
|
if err := s.client.ConfigureDevice(s.iface, wgtypes.Config{
|
||||||
PrivateKey: &wg.PrivKey,
|
PrivateKey: &s.PrivKey,
|
||||||
ListenPort: &wg.Port,
|
ListenPort: &s.Port,
|
||||||
ReplacePeers: true,
|
ReplacePeers: true,
|
||||||
Peers: peerCfgs,
|
Peers: peerCfgs,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return errors.Wrapf(err, "could not set wireguard configuration for %s", wg.iface)
|
return errors.Wrapf(err, "could not set wireguard configuration for %s", s.iface)
|
||||||
}
|
}
|
||||||
|
|
||||||
link, err := netlink.LinkByName(wg.iface)
|
link, err := netlink.LinkByName(s.iface)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "could not get link information for %s", wg.iface)
|
return errors.Wrapf(err, "could not get link information for %s", s.iface)
|
||||||
}
|
}
|
||||||
if err := netlink.AddrReplace(link, &netlink.Addr{
|
if err := netlink.AddrReplace(link, &netlink.Addr{
|
||||||
IPNet: &wg.OverlayAddr,
|
IPNet: &s.OverlayAddr,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return errors.Wrapf(err, "could not set address for %s", wg.iface)
|
return errors.Wrapf(err, "could not set address for %s", s.iface)
|
||||||
}
|
}
|
||||||
// TODO: make MTU configurable?
|
// TODO: make MTU configurable?
|
||||||
if err := netlink.LinkSetMTU(link, 1420); err != nil {
|
if err := netlink.LinkSetMTU(link, 1420); err != nil {
|
||||||
return errors.Wrapf(err, "could not set MTU for %s", wg.iface)
|
return errors.Wrapf(err, "could not set MTU for %s", s.iface)
|
||||||
}
|
}
|
||||||
if err := netlink.LinkSetUp(link); err != nil {
|
if err := netlink.LinkSetUp(link); err != nil {
|
||||||
return errors.Wrapf(err, "could not enable interface %s", wg.iface)
|
return errors.Wrapf(err, "could not enable interface %s", s.iface)
|
||||||
}
|
}
|
||||||
for _, node := range nodes {
|
for _, node := range nodes {
|
||||||
netlink.RouteAdd(&netlink.Route{
|
netlink.RouteAdd(&netlink.Route{
|
||||||
|
@ -133,7 +133,7 @@ func (wg *WgState) SetUpInterface(nodes []common.Node) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wg *WgState) nodesToPeerConfigs(nodes []common.Node) ([]wgtypes.PeerConfig, error) {
|
func (s *State) nodesToPeerConfigs(nodes []common.Node) ([]wgtypes.PeerConfig, error) {
|
||||||
peerCfgs := make([]wgtypes.PeerConfig, len(nodes))
|
peerCfgs := make([]wgtypes.PeerConfig, len(nodes))
|
||||||
for i, node := range nodes {
|
for i, node := range nodes {
|
||||||
pubKey, err := wgtypes.ParseKey(node.PubKey)
|
pubKey, err := wgtypes.ParseKey(node.PubKey)
|
||||||
|
@ -145,7 +145,7 @@ func (wg *WgState) nodesToPeerConfigs(nodes []common.Node) ([]wgtypes.PeerConfig
|
||||||
ReplaceAllowedIPs: true,
|
ReplaceAllowedIPs: true,
|
||||||
Endpoint: &net.UDPAddr{
|
Endpoint: &net.UDPAddr{
|
||||||
IP: node.Addr,
|
IP: node.Addr,
|
||||||
Port: wg.Port,
|
Port: s.Port,
|
||||||
},
|
},
|
||||||
AllowedIPs: []net.IPNet{
|
AllowedIPs: []net.IPNet{
|
||||||
node.OverlayAddr,
|
node.OverlayAddr,
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_wgState_assignOverlayAddr(t *testing.T) {
|
func Test_State_AssignOverlayAddr(t *testing.T) {
|
||||||
type args struct {
|
type args struct {
|
||||||
ipnet *net.IPNet
|
ipnet *net.IPNet
|
||||||
name string
|
name string
|
||||||
|
@ -31,11 +31,11 @@ func Test_wgState_assignOverlayAddr(t *testing.T) {
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
wg := &WgState{}
|
s := &State{}
|
||||||
wg.AssignOverlayAddr(tt.args.ipnet, tt.args.name)
|
s.AssignOverlayAddr(tt.args.ipnet, tt.args.name)
|
||||||
|
|
||||||
if !reflect.DeepEqual(wg.OverlayAddr.IP.String(), tt.want) {
|
if !reflect.DeepEqual(s.OverlayAddr.IP.String(), tt.want) {
|
||||||
t.Errorf("assignOverlayAddr() set = %s, want %s", wg.OverlayAddr, tt.want)
|
t.Errorf("assignOverlayAddr() set = %s, want %s", s.OverlayAddr, tt.want)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -43,38 +43,38 @@ func Test_wgState_assignOverlayAddr(t *testing.T) {
|
||||||
|
|
||||||
// This is just to ensure - if we ever change the hashing function - that it spreads the results in a way that at least
|
// This is just to ensure - if we ever change the hashing function - that it spreads the results in a way that at least
|
||||||
// avoids the most obvious collisions.
|
// avoids the most obvious collisions.
|
||||||
func Test_wgState_assignOverlayAddr_no_obvious_collisions(t *testing.T) {
|
func Test_State_AssignOverlayAddr_no_obvious_collisions(t *testing.T) {
|
||||||
_, ipnet, _ := net.ParseCIDR("10.0.0.0/24")
|
_, ipnet, _ := net.ParseCIDR("10.0.0.0/24")
|
||||||
assignments := make(map[string]string)
|
assignments := make(map[string]string)
|
||||||
for _, n := range []string{"test", "test1", "test2", "1test", "2test"} {
|
for _, n := range []string{"test", "test1", "test2", "1test", "2test"} {
|
||||||
wg := &WgState{}
|
s := &State{}
|
||||||
wg.AssignOverlayAddr(ipnet, n)
|
s.AssignOverlayAddr(ipnet, n)
|
||||||
if assigned, ok := assignments[wg.OverlayAddr.String()]; ok {
|
if assigned, ok := assignments[s.OverlayAddr.String()]; ok {
|
||||||
t.Errorf("IP assignment collision: hash(%s) = hash(%s)", n, assigned)
|
t.Errorf("IP assignment collision: hash(%s) = hash(%s)", n, assigned)
|
||||||
}
|
}
|
||||||
assignments[wg.OverlayAddr.String()] = n
|
assignments[s.OverlayAddr.String()] = n
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This should ensure the obvious fact that the same name should map to the same IP if called twice.
|
// This should ensure the obvious fact that the same name should map to the same IP if called twice.
|
||||||
func Test_wgState_assignOverlayAddr_consistent(t *testing.T) {
|
func Test_State_AssignOverlayAddr_consistent(t *testing.T) {
|
||||||
_, ipnet, _ := net.ParseCIDR("10.0.0.0/8")
|
_, ipnet, _ := net.ParseCIDR("10.0.0.0/8")
|
||||||
wg1 := &WgState{}
|
s1 := &State{}
|
||||||
wg1.AssignOverlayAddr(ipnet, "test")
|
s1.AssignOverlayAddr(ipnet, "test")
|
||||||
wg2 := &WgState{}
|
s2 := &State{}
|
||||||
wg2.AssignOverlayAddr(ipnet, "test")
|
s2.AssignOverlayAddr(ipnet, "test")
|
||||||
if wg1.OverlayAddr.String() != wg2.OverlayAddr.String() {
|
if s1.OverlayAddr.String() != s2.OverlayAddr.String() {
|
||||||
t.Errorf("assignOverlayAddr() %s != %s", wg1.OverlayAddr, wg2.OverlayAddr)
|
t.Errorf("assignOverlayAddr() %s != %s", s1.OverlayAddr, s2.OverlayAddr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_wgState_assignOverlayAddr_repeatable(t *testing.T) {
|
func Test_State_AssignOverlayAddr_repeatable(t *testing.T) {
|
||||||
_, ipnet, _ := net.ParseCIDR("10.0.0.0/8")
|
_, ipnet, _ := net.ParseCIDR("10.0.0.0/8")
|
||||||
wg := &WgState{}
|
s := &State{}
|
||||||
wg.AssignOverlayAddr(ipnet, "test")
|
s.AssignOverlayAddr(ipnet, "test")
|
||||||
gen1 := wg.OverlayAddr.String()
|
gen1 := s.OverlayAddr.String()
|
||||||
wg.AssignOverlayAddr(ipnet, "test")
|
s.AssignOverlayAddr(ipnet, "test")
|
||||||
gen2 := wg.OverlayAddr.String()
|
gen2 := s.OverlayAddr.String()
|
||||||
if gen1 != gen2 {
|
if gen1 != gen2 {
|
||||||
t.Errorf("assignOverlayAddr() %s != %s", gen1, gen2)
|
t.Errorf("assignOverlayAddr() %s != %s", gen1, gen2)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue