diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2019-05-16 16:47:28 +0200 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2019-05-16 17:14:52 +0200 |
commit | 106528596e28568afa2c61689d7a30347f65d0e3 (patch) | |
tree | d1c41aa36d5f6305b03d8274d85d6ab454c682bf | |
parent | service: print useragent in log (diff) | |
download | wireguard-windows-106528596e28568afa2c61689d7a30347f65d0e3.tar.xz wireguard-windows-106528596e28568afa2c61689d7a30347f65d0e3.zip |
service: silently ignore routes that won't have a gateway
-rw-r--r-- | service/ifaceconfig.go | 31 | ||||
-rw-r--r-- | service/service_manager.go | 8 | ||||
-rw-r--r-- | service/service_tunnel.go | 13 |
3 files changed, 32 insertions, 20 deletions
diff --git a/service/ifaceconfig.go b/service/ifaceconfig.go index 6655f263..115e454e 100644 --- a/service/ifaceconfig.go +++ b/service/ifaceconfig.go @@ -7,7 +7,6 @@ package service import ( "bytes" - "errors" "log" "net" "sort" @@ -202,12 +201,11 @@ func configureInterface(conf *conf.Config, tun *tun.NativeTun) error { return err } - routeCount := len(conf.Interface.Addresses) + estimatedRouteCount := len(conf.Interface.Addresses) for _, peer := range conf.Peers { - routeCount += len(peer.AllowedIPs) + estimatedRouteCount += len(peer.AllowedIPs) } - routes := make([]winipcfg.RouteData, routeCount) - routeCount = 0 + routes := make([]winipcfg.RouteData, 0, estimatedRouteCount) var firstGateway4 *net.IP var firstGateway6 *net.IP addresses := make([]*net.IPNet, len(conf.Interface.Addresses)) @@ -220,15 +218,14 @@ func configureInterface(conf *conf.Config, tun *tun.NativeTun) error { } else if addr.Bits() == 128 && firstGateway6 == nil { firstGateway6 = &gateway } - routes[routeCount] = winipcfg.RouteData{ + routes = append(routes, winipcfg.RouteData{ Destination: net.IPNet{ IP: gateway, Mask: ipnet.Mask, }, NextHop: gateway, Metric: 0, - } - routeCount++ + }) } foundDefault4 := false @@ -236,9 +233,9 @@ func configureInterface(conf *conf.Config, tun *tun.NativeTun) error { for _, peer := range conf.Peers { for _, allowedip := range peer.AllowedIPs { if (allowedip.Bits() == 32 && firstGateway4 == nil) || (allowedip.Bits() == 128 && firstGateway6 == nil) { - return errors.New("Due to a Windows limitation, one cannot have interface routes without an interface address") + continue } - routes[routeCount] = winipcfg.RouteData{ + route := winipcfg.RouteData{ Destination: allowedip.IPNet(), Metric: 0, } @@ -246,14 +243,14 @@ func configureInterface(conf *conf.Config, tun *tun.NativeTun) error { if allowedip.Cidr == 0 { foundDefault4 = true } - routes[routeCount].NextHop = *firstGateway4 + route.NextHop = *firstGateway4 } else if allowedip.Bits() == 128 { if allowedip.Cidr == 0 { foundDefault6 = true } - routes[routeCount].NextHop = *firstGateway6 + route.NextHop = *firstGateway6 } - routeCount++ + routes = append(routes, route) } } @@ -266,8 +263,7 @@ func configureInterface(conf *conf.Config, tun *tun.NativeTun) error { return err } - deduplicatedRoutes := make([]*winipcfg.RouteData, routeCount) - routeCount = 0 + deduplicatedRoutes := make([]*winipcfg.RouteData, 0, len(routes)) sort.Slice(routes, func(i, j int) bool { return routes[i].Metric < routes[j].Metric || bytes.Compare(routes[i].NextHop, routes[j].NextHop) == -1 || @@ -281,11 +277,10 @@ func configureInterface(conf *conf.Config, tun *tun.NativeTun) error { bytes.Equal(routes[i].Destination.Mask, routes[i-1].Destination.Mask) { continue } - deduplicatedRoutes[routeCount] = &routes[i] - routeCount++ + deduplicatedRoutes = append(deduplicatedRoutes, &routes[i]) } - err = iface.SetRoutes(deduplicatedRoutes[:routeCount]) + err = iface.SetRoutes(deduplicatedRoutes) if err != nil { return nil } diff --git a/service/service_manager.go b/service/service_manager.go index 58c2d863..5bd94e0e 100644 --- a/service/service_manager.go +++ b/service/service_manager.go @@ -7,10 +7,12 @@ package service import ( "errors" + "fmt" "log" "os" "runtime" "runtime/debug" + "strings" "sync" "syscall" "time" @@ -47,7 +49,11 @@ func (service *managerService) Execute(args []string, r <-chan svc.ChangeRequest } defer func() { if x := recover(); x != nil { - log.Printf("%v:\n%s", x, string(debug.Stack())) + for _, line := range append([]string{fmt.Sprint(x)}, strings.Split(string(debug.Stack()), "\n")...) { + if len(strings.TrimSpace(line)) > 0 { + log.Println(line) + } + } panic(x) } }() diff --git a/service/service_tunnel.go b/service/service_tunnel.go index 11234960..2d66bf32 100644 --- a/service/service_tunnel.go +++ b/service/service_tunnel.go @@ -105,8 +105,19 @@ func (service *tunnelService) Execute(args []string, r <-chan svc.ChangeRequest, return } defer func() { + logIt := func(a ...interface{}) { + if logger != nil { + logger.Error.Println(a...) + } else { + log.Println(a...) + } + } if x := recover(); x != nil { - log.Printf("%v:\n%s", x, string(debug.Stack())) + for _, line := range append([]string{fmt.Sprint(x)}, strings.Split(string(debug.Stack()), "\n")...) { + if len(strings.TrimSpace(line)) > 0 { + logIt(line) + } + } panic(x) } }() |