cleaned up the logging

This commit is contained in:
Neven Miculinic 2019-03-26 13:57:33 +01:00
parent f952657bc4
commit 5c86e5479e
2 changed files with 42 additions and 34 deletions

View File

@ -1,2 +1,8 @@
# wg-quick-go # wg-quick-go
wg-quick like library in go for embedding wg-quick like library in go for embedding
# Roadmap
* [ ] full wg-quick feature parity
* [x] Up
* [x] Down

68
wg.go
View File

@ -5,10 +5,9 @@ import (
"encoding/base64" "encoding/base64"
"github.com/mdlayher/wireguardctrl" "github.com/mdlayher/wireguardctrl"
"github.com/mdlayher/wireguardctrl/wgtypes" "github.com/mdlayher/wireguardctrl/wgtypes"
log "github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/vishvananda/netlink" "github.com/vishvananda/netlink"
"net" "net"
"os/exec"
"syscall" "syscall"
"text/template" "text/template"
) )
@ -16,12 +15,12 @@ import (
type Config struct { type Config struct {
wgtypes.Config wgtypes.Config
// Address — a comma-separated list of IP (v4 or v6) addresses (optionally with CIDR masks) to be assigned to the interface. May be specified multiple times. // Address list of IP (v4 or v6) addresses (optionally with CIDR masks) to be assigned to the interface. May be specified multiple times.
Address []*net.IPNet Address []*net.IPNet
// — a comma-separated list of IP (v4 or v6) addresses to be set as the interfaces DNS servers. May be specified multiple times. Upon bringing the interface up, this runs resolvconf -a tun.INTERFACE -m 0 -x and upon bringing it down, this runs resolvconf -d tun.INTERFACE. If these particular invocations of resolvconf(8) are undesirable, the PostUp and PostDown keys below may be used instead. // list of IP (v4 or v6) addresses to be set as the interfaces DNS servers. May be specified multiple times. Upon bringing the interface up, this runs resolvconf -a tun.INTERFACE -m 0 -x and upon bringing it down, this runs resolvconf -d tun.INTERFACE. If these particular invocations of resolvconf(8) are undesirable, the PostUp and PostDown keys below may be used instead.
DNS []net.IP DNS []net.IP
// — if not specified, the MTU is automatically determined from the endpoint addresses or the system default route, which is usually a sane choice. However, to manually specify an MTU to override this automatic discovery, this value may be specified explicitly. // —if not specified, the MTU is automatically determined from the endpoint addresses or the system default route, which is usually a sane choice. However, to manually specify an MTU to override this automatic discovery, this value may be specified explicitly.
MTU int MTU int
// Table — Controls the routing table to which routes are added. There are two special values: off disables the creation of routes altogether, and auto (the default) adds routes to the default table and enables special handling of default routes. // Table — Controls the routing table to which routes are added. There are two special values: off disables the creation of routes altogether, and auto (the default) adds routes to the default table and enables special handling of default routes.
@ -83,6 +82,7 @@ func serializeKey(key *wgtypes.Key) string {
return base64.StdEncoding.EncodeToString(key[:]) return base64.StdEncoding.EncodeToString(key[:])
} }
// Parses base64 encoded key
func ParseKey(key string) (wgtypes.Key, error) { func ParseKey(key string) (wgtypes.Key, error) {
var pkey wgtypes.Key var pkey wgtypes.Key
pkeySlice, err := base64.StdEncoding.DecodeString(key) pkeySlice, err := base64.StdEncoding.DecodeString(key)
@ -99,12 +99,13 @@ var cfgTemplate = template.Must(
Funcs(template.FuncMap(map[string]interface{}{"wgKey": serializeKey})). Funcs(template.FuncMap(map[string]interface{}{"wgKey": serializeKey})).
Parse(wgtypeTemplateSpec)) Parse(wgtypeTemplateSpec))
func (cfg *Config) Up(iface string) error { // Sync the config to the current setup for given interface
func (cfg *Config) Sync(iface string, logger logrus.FieldLogger) error {
log := logger.WithField("iface", iface)
link, err := netlink.LinkByName(iface) link, err := netlink.LinkByName(iface)
if err != nil { if err != nil {
if _, ok := err.(netlink.LinkNotFoundError); !ok { if _, ok := err.(netlink.LinkNotFoundError); !ok {
log.Error(err, "cannot read link, probably doesn't exist") log.WithError(err).Error("cannot read link")
return err return err
} }
log.Info("link not found, creating") log.Info("link not found, creating")
@ -115,24 +116,21 @@ func (cfg *Config) Up(iface string) error {
LinkType: "wireguard", LinkType: "wireguard",
} }
if err := netlink.LinkAdd(wgLink); err != nil { if err := netlink.LinkAdd(wgLink); err != nil {
log.Error(err, "cannot create link", "iface", iface) log.WithError(err).Error("cannot create link")
return err return err
} }
if err := exec.Command("ip", "link", "add", "dev", iface, "type", "wireguard").Run(); err != nil {
}
link, err = netlink.LinkByName(iface) link, err = netlink.LinkByName(iface)
if err != nil { if err != nil {
log.Error(err, "cannot read link") log.WithError(err).Error("cannot read link")
return err return err
} }
} }
log.Info("link", "type", link.Type(), "attrs", link.Attrs())
if err := netlink.LinkSetUp(link); err != nil { if err := netlink.LinkSetUp(link); err != nil {
log.Error(err, "cannot set link up", "type", link.Type(), "attrs", link.Attrs()) log.WithError(err).Error("cannot set link up")
return err return err
} }
log.Info("set device up", "iface", iface) log.Info("set device up")
cl, err := wireguardctrl.New() cl, err := wireguardctrl.New()
if err != nil { if err != nil {
@ -141,16 +139,16 @@ func (cfg *Config) Up(iface string) error {
} }
if err := cl.ConfigureDevice(iface, cfg.Config); err != nil { if err := cl.ConfigureDevice(iface, cfg.Config); err != nil {
log.Error(err, "cannot configure device", "iface", iface) log.WithError(err).Error("cannot configure device")
return err return err
} }
if err := syncAddress(link, cfg); err != nil { if err := syncAddress(link, cfg, log); err != nil {
log.Error(err, "cannot sync addresses") log.Error(err, "cannot sync addresses")
return err return err
} }
if err := syncRoutes(link, cfg); err != nil { if err := syncRoutes(link, cfg, log); err != nil {
log.Error(err, "cannot sync routes") log.Error(err, "cannot sync routes")
return err return err
} }
@ -160,7 +158,7 @@ func (cfg *Config) Up(iface string) error {
} }
func syncAddress(link netlink.Link, cfg *Config) error { func syncAddress(link netlink.Link, cfg *Config, log logrus.FieldLogger) error {
addrs, err := netlink.AddrList(link, syscall.AF_INET) addrs, err := netlink.AddrList(link, syscall.AF_INET)
if err != nil { if err != nil {
log.Error(err, "cannot read link address") log.Error(err, "cannot read link address")
@ -173,40 +171,42 @@ func syncAddress(link netlink.Link, cfg *Config) error {
} }
for _, addr := range cfg.Address { for _, addr := range cfg.Address {
log := log.WithField("addr", addr)
_, present := presentAddresses[addr.String()] _, present := presentAddresses[addr.String()]
presentAddresses[addr.String()] = 2 presentAddresses[addr.String()] = 2
if present { if present {
log.Info("address present", "addr", addr, "iface", link.Attrs().Name) log.Info("address present")
continue continue
} }
if err := netlink.AddrAdd(link, &netlink.Addr{ if err := netlink.AddrAdd(link, &netlink.Addr{
IPNet: addr, IPNet: addr,
}); err != nil { }); err != nil {
log.Error(err, "cannot add addr", "iface", link.Attrs().Name) log.WithError(err).Error("cannot add addr")
return err return err
} }
log.Info("address added", "addr", addr, "iface", link.Attrs().Name) log.Info("address added")
} }
for addr, p := range presentAddresses { for addr, p := range presentAddresses {
log := log.WithField("addr", addr)
if p < 2 { if p < 2 {
nlAddr, err := netlink.ParseAddr(addr) nlAddr, err := netlink.ParseAddr(addr)
if err != nil { if err != nil {
log.Error(err, "cannot parse del addr", "iface", link.Attrs().Name, "addr", addr) log.WithError(err).Error("cannot parse del addr")
return err return err
} }
if err := netlink.AddrAdd(link, nlAddr); err != nil { if err := netlink.AddrAdd(link, nlAddr); err != nil {
log.Error(err, "cannot delete addr", "iface", link.Attrs().Name, "addr", addr) log.WithError(err).Error("cannot delete addr")
return err return err
} }
log.Info("address deleted", "addr", addr, "iface", link.Attrs().Name) log.Info("addr deleted")
} }
} }
return nil return nil
} }
func syncRoutes(link netlink.Link, cfg *Config) error { func syncRoutes(link netlink.Link, cfg *Config, log logrus.FieldLogger) error {
routes, err := netlink.RouteList(link, syscall.AF_INET) routes, err := netlink.RouteList(link, syscall.AF_INET)
if err != nil { if err != nil {
log.Error(err, "cannot read existing routes") log.Error(err, "cannot read existing routes")
@ -222,38 +222,40 @@ func syncRoutes(link netlink.Link, cfg *Config) error {
for _, rt := range peer.AllowedIPs { for _, rt := range peer.AllowedIPs {
_, present := presentRoutes[rt.String()] _, present := presentRoutes[rt.String()]
presentRoutes[rt.String()] = 2 presentRoutes[rt.String()] = 2
log := log.WithField("route", rt.String())
if present { if present {
log.Info("route present", "iface", link.Attrs().Name, "route", rt.String()) log.Info("route present")
continue continue
} }
if err := netlink.RouteAdd(&netlink.Route{ if err := netlink.RouteAdd(&netlink.Route{
LinkIndex: link.Attrs().Index, LinkIndex: link.Attrs().Index,
Dst: &rt, Dst: &rt,
}); err != nil { }); err != nil {
log.Error(err, "cannot setup route", "iface", link.Attrs().Name, "route", rt.String()) log.WithError(err).Error("cannot setup route")
return err return err
} }
log.Info("route added", "iface", link.Attrs().Name, "route", rt.String()) log.Info("route added")
} }
} }
// Clean extra routes // Clean extra routes
for rtStr, p := range presentRoutes { for rtStr, p := range presentRoutes {
_, rt, err := net.ParseCIDR(rtStr) _, rt, err := net.ParseCIDR(rtStr)
log := log.WithField("route", rt.String())
if err != nil { if err != nil {
log.Info("cannot parse route", "iface", link.Attrs().Name, "route", rtStr) log.WithError(err).Error("cannot parse route")
return err return err
} }
if p < 2 { if p < 2 {
log.Info("extra manual route found", "iface", link.Attrs().Name, "route", rt.String()) log.Info("extra manual route found")
if err := netlink.RouteDel(&netlink.Route{ if err := netlink.RouteDel(&netlink.Route{
LinkIndex: link.Attrs().Index, LinkIndex: link.Attrs().Index,
Dst: rt, Dst: rt,
}); err != nil { }); err != nil {
log.Error(err, "cannot setup route", "iface", link.Attrs().Name, "route", rt.String()) log.WithError(err).Error("cannot setup route")
return err return err
} }
log.Info("route deleted", "iface", link.Attrs().Name, "route", rt) log.Info("route deleted")
} }
} }
return nil return nil