From b0e41d5c354a24ba794972928e7a71b72834860c Mon Sep 17 00:00:00 2001 From: kaiyou Date: Thu, 7 May 2020 14:40:22 +0200 Subject: [PATCH] Rename WgState to State and NewWgState to New This also caused renaming local variables to avoid conflicting with the wg package name itself. --- main.go | 14 ++++++------ wg/wireguard.go | 54 ++++++++++++++++++++++---------------------- wg/wireguard_test.go | 46 ++++++++++++++++++------------------- 3 files changed, 57 insertions(+), 57 deletions(-) diff --git a/main.go b/main.go index caa1670..3862a53 100644 --- a/main.go +++ b/main.go @@ -33,7 +33,7 @@ func main() { } logrus.SetLevel(logLevel) - wg, err := wg.NewWGConfig(config.Interface, config.WireguardPort) + wgstate, err := wg.New(config.Interface, config.WireguardPort) if err != nil { logrus.WithError(err).Fatal("could not instantiate wireguard controller") } @@ -43,10 +43,10 @@ func main() { 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.OverlayAddr = wg.OverlayAddr - localNode.PubKey = wg.PubKey.String() + localNode.OverlayAddr = wgstate.OverlayAddr + localNode.PubKey = wgstate.PubKey.String() cluster.Update(localNode) nodec := cluster.Members() // avoid deadlocks by starting before join @@ -76,9 +76,9 @@ func main() { nodes = append(nodes, node) 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") - wg.DownInterface() + wgstate.DownInterface() } if !config.NoEtcHosts { if err := writeToEtcHosts(nodes); err != nil { @@ -93,7 +93,7 @@ func main() { 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") } os.Exit(0) diff --git a/wg/wireguard.go b/wg/wireguard.go index 494bfa6..1e9ac37 100644 --- a/wg/wireguard.go +++ b/wg/wireguard.go @@ -12,8 +12,8 @@ import ( "golang.zx2c4.com/wireguard/wgctrl/wgtypes" ) -// WgState holds the configured state of a Wesher Wireguard interface -type WgState struct { +// State holds the configured state of a Wesher Wireguard interface +type State struct { iface string client *wgctrl.Client OverlayAddr net.IPNet @@ -22,10 +22,10 @@ type WgState struct { 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 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() if err != nil { 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() - wgState := WgState{ + state := State{ iface: iface, client: client, Port: port, PrivKey: privKey, PubKey: pubKey, } - return &wgState, nil + return &state, nil } // AssignOverlayAddr assigns a new address to the interface @@ -52,7 +52,7 @@ func NewWGConfig(iface string, port int) (*WgState, error) { // provided name deterministically // Currently, the address is assigned by hashing the name and mapping that // 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 bits, size := ipnet.Mask.Size() 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] } - wg.OverlayAddr = net.IPNet{ + s.OverlayAddr = net.IPNet{ IP: net.IP(ip), Mask: net.CIDRMask(size, size), // either /32 or /128, depending if ipv4 or ipv6 } } // DownInterface shuts down the associated network interface -func (wg *WgState) DownInterface() error { - if _, err := wg.client.Device(wg.iface); err != nil { +func (s *State) DownInterface() error { + if _, err := s.client.Device(s.iface); err != nil { if os.IsNotExist(err) { return nil // device already gone; noop } return err } - link, err := netlink.LinkByName(wg.iface) + link, err := netlink.LinkByName(s.iface) if err != nil { return err } @@ -88,39 +88,39 @@ func (wg *WgState) DownInterface() error { } // SetUpInterface creates and setup the associated network interface -func (wg *WgState) SetUpInterface(nodes []common.Node) error { - if err := netlink.LinkAdd(&wireguard{LinkAttrs: netlink.LinkAttrs{Name: wg.iface}}); err != nil && !os.IsExist(err) { - return errors.Wrapf(err, "could not create interface %s", wg.iface) +func (s *State) SetUpInterface(nodes []common.Node) error { + 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", s.iface) } - peerCfgs, err := wg.nodesToPeerConfigs(nodes) + peerCfgs, err := s.nodesToPeerConfigs(nodes) if err != nil { return errors.Wrap(err, "error converting received node information to wireguard format") } - if err := wg.client.ConfigureDevice(wg.iface, wgtypes.Config{ - PrivateKey: &wg.PrivKey, - ListenPort: &wg.Port, + if err := s.client.ConfigureDevice(s.iface, wgtypes.Config{ + PrivateKey: &s.PrivKey, + ListenPort: &s.Port, ReplacePeers: true, Peers: peerCfgs, }); 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 { - 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{ - IPNet: &wg.OverlayAddr, + IPNet: &s.OverlayAddr, }); 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? 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 { - 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 { netlink.RouteAdd(&netlink.Route{ @@ -133,7 +133,7 @@ func (wg *WgState) SetUpInterface(nodes []common.Node) error { 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)) for i, node := range nodes { pubKey, err := wgtypes.ParseKey(node.PubKey) @@ -145,7 +145,7 @@ func (wg *WgState) nodesToPeerConfigs(nodes []common.Node) ([]wgtypes.PeerConfig ReplaceAllowedIPs: true, Endpoint: &net.UDPAddr{ IP: node.Addr, - Port: wg.Port, + Port: s.Port, }, AllowedIPs: []net.IPNet{ node.OverlayAddr, diff --git a/wg/wireguard_test.go b/wg/wireguard_test.go index 3d672ae..194a2b7 100644 --- a/wg/wireguard_test.go +++ b/wg/wireguard_test.go @@ -6,7 +6,7 @@ import ( "testing" ) -func Test_wgState_assignOverlayAddr(t *testing.T) { +func Test_State_AssignOverlayAddr(t *testing.T) { type args struct { ipnet *net.IPNet name string @@ -31,11 +31,11 @@ func Test_wgState_assignOverlayAddr(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - wg := &WgState{} - wg.AssignOverlayAddr(tt.args.ipnet, tt.args.name) + s := &State{} + s.AssignOverlayAddr(tt.args.ipnet, tt.args.name) - if !reflect.DeepEqual(wg.OverlayAddr.IP.String(), tt.want) { - t.Errorf("assignOverlayAddr() set = %s, want %s", wg.OverlayAddr, tt.want) + if !reflect.DeepEqual(s.OverlayAddr.IP.String(), 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 // 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") assignments := make(map[string]string) for _, n := range []string{"test", "test1", "test2", "1test", "2test"} { - wg := &WgState{} - wg.AssignOverlayAddr(ipnet, n) - if assigned, ok := assignments[wg.OverlayAddr.String()]; ok { + s := &State{} + s.AssignOverlayAddr(ipnet, n) + if assigned, ok := assignments[s.OverlayAddr.String()]; ok { 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. -func Test_wgState_assignOverlayAddr_consistent(t *testing.T) { +func Test_State_AssignOverlayAddr_consistent(t *testing.T) { _, ipnet, _ := net.ParseCIDR("10.0.0.0/8") - wg1 := &WgState{} - wg1.AssignOverlayAddr(ipnet, "test") - wg2 := &WgState{} - wg2.AssignOverlayAddr(ipnet, "test") - if wg1.OverlayAddr.String() != wg2.OverlayAddr.String() { - t.Errorf("assignOverlayAddr() %s != %s", wg1.OverlayAddr, wg2.OverlayAddr) + s1 := &State{} + s1.AssignOverlayAddr(ipnet, "test") + s2 := &State{} + s2.AssignOverlayAddr(ipnet, "test") + if s1.OverlayAddr.String() != s2.OverlayAddr.String() { + 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") - wg := &WgState{} - wg.AssignOverlayAddr(ipnet, "test") - gen1 := wg.OverlayAddr.String() - wg.AssignOverlayAddr(ipnet, "test") - gen2 := wg.OverlayAddr.String() + s := &State{} + s.AssignOverlayAddr(ipnet, "test") + gen1 := s.OverlayAddr.String() + s.AssignOverlayAddr(ipnet, "test") + gen2 := s.OverlayAddr.String() if gen1 != gen2 { t.Errorf("assignOverlayAddr() %s != %s", gen1, gen2) }