From e180bd67e6a8cf9a38849e3983c7aa2e686edeb6 Mon Sep 17 00:00:00 2001 From: David Crawshaw Date: Tue, 11 Feb 2020 15:08:07 -0500 Subject: [PATCH 01/15] controlclient: handle nil Logf option --- control/controlclient/auto.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/control/controlclient/auto.go b/control/controlclient/auto.go index ebcf120d8..7ff11926f 100644 --- a/control/controlclient/auto.go +++ b/control/controlclient/auto.go @@ -142,6 +142,9 @@ func NewNoStart(opts Options) (*Client, error) { if err != nil { return nil, err } + if opts.Logf == nil { + opts.Logf = func(fmt string, args ...interface{}) {} + } c := &Client{ direct: direct, timeNow: opts.TimeNow, From 490e586eac636e87f7bc8877a7ce06b3bcb71213 Mon Sep 17 00:00:00 2001 From: wardn Date: Wed, 12 Feb 2020 12:45:59 -0800 Subject: [PATCH 02/15] wgengine: add required NewFakeRouter parameter Signed-off-by: wardn --- wgengine/router_default.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wgengine/router_default.go b/wgengine/router_default.go index 74f993b39..7aced4a82 100644 --- a/wgengine/router_default.go +++ b/wgengine/router_default.go @@ -13,5 +13,5 @@ import ( ) func NewUserspaceRouter(logf logger.Logf, tunname string, dev *device.Device, tuntap tun.Device, netChanged func()) Router { - return NewFakeRouter(logf, tunname, dev, tuntap) + return NewFakeRouter(logf, tunname, dev, tuntap, netChanged) } From 819daf10e2cde732fb588a3dd679709e7b39f9bd Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 11 Feb 2020 15:21:24 -0800 Subject: [PATCH 03/15] wgengine: flesh out some docs Signed-off-by: Brad Fitzpatrick --- wgengine/faketun.go | 6 +++- wgengine/rusage.go | 5 ++- wgengine/rusage_nowindows.go | 2 +- wgengine/userspace.go | 6 ++-- wgengine/watchdog.go | 4 +-- wgengine/wgengine.go | 69 +++++++++++++++++++++++++++--------- 6 files changed, 68 insertions(+), 24 deletions(-) diff --git a/wgengine/faketun.go b/wgengine/faketun.go index 01a059f59..8bc04f81f 100644 --- a/wgengine/faketun.go +++ b/wgengine/faketun.go @@ -5,9 +5,10 @@ package wgengine import ( - "github.com/tailscale/wireguard-go/tun" "io" "os" + + "github.com/tailscale/wireguard-go/tun" ) type fakeTun struct { @@ -16,6 +17,9 @@ type fakeTun struct { closechan chan struct{} } +// NewFakeTun returns a fake TUN device that does not depend on the +// operating system or any special permissions. +// It primarily exists for testing. func NewFakeTun() tun.Device { return &fakeTun{ datachan: make(chan []byte), diff --git a/wgengine/rusage.go b/wgengine/rusage.go index 98df2afa4..698f9ebf4 100644 --- a/wgengine/rusage.go +++ b/wgengine/rusage.go @@ -11,7 +11,10 @@ import ( "tailscale.com/logger" ) -func RusagePrefixLog(logf logger.Logf) func(f string, argv ...interface{}) { +// RusagePrefixLog returns a Logf func wrapping the provided logf func that adds +// a prefixed log message to each line with the current binary memory usage +// and max RSS. +func RusagePrefixLog(logf logger.Logf) logger.Logf { return func(f string, argv ...interface{}) { var m runtime.MemStats runtime.ReadMemStats(&m) diff --git a/wgengine/rusage_nowindows.go b/wgengine/rusage_nowindows.go index f6dd89b4b..54f17e889 100644 --- a/wgengine/rusage_nowindows.go +++ b/wgengine/rusage_nowindows.go @@ -23,7 +23,7 @@ func rusageMaxRSS() float64 { rss /= 1 << 20 // ru_maxrss is bytes on darwin } else { // ru_maxrss is kilobytes elsewhere (linux, openbsd, etc) - rss /= 1024 + rss /= 1 << 10 } return rss } diff --git a/wgengine/userspace.go b/wgengine/userspace.go index 45437b44f..e21bae855 100644 --- a/wgengine/userspace.go +++ b/wgengine/userspace.go @@ -67,14 +67,14 @@ func NewUserspaceEngine(logf logger.Logf, tunname string, listenPort uint16, der tuntap, err := tun.CreateTUN(tunname, device.DefaultMTU) if err != nil { - log.Printf("CreateTUN: %v\n", err) + logf("CreateTUN: %v\n", err) return nil, err } - log.Printf("CreateTUN ok.\n") + logf("CreateTUN ok.\n") e, err := NewUserspaceEngineAdvanced(logf, tuntap, NewUserspaceRouter, listenPort, derp) if err != nil { - log.Printf("NewUserspaceEngineAdv: %v\n", err) + logf("NewUserspaceEngineAdv: %v\n", err) return nil, err } return e, err diff --git a/wgengine/watchdog.go b/wgengine/watchdog.go index ee4b23d98..5172f4b29 100644 --- a/wgengine/watchdog.go +++ b/wgengine/watchdog.go @@ -5,9 +5,9 @@ package wgengine import ( - "bytes" "log" "runtime/pprof" + "strings" "time" "github.com/tailscale/wireguard-go/wgcfg" @@ -45,7 +45,7 @@ func (e *watchdogEngine) watchdogErr(name string, fn func() error) error { t.Stop() return err case <-t.C: - buf := new(bytes.Buffer) + buf := new(strings.Builder) pprof.Lookup("goroutine").WriteTo(buf, 1) e.logf("wgengine watchdog stacks:\n%s", buf.String()) e.fatalf("wgengine: watchdog timeout on %s", name) diff --git a/wgengine/wgengine.go b/wgengine/wgengine.go index e431bdf41..83538ad84 100644 --- a/wgengine/wgengine.go +++ b/wgengine/wgengine.go @@ -14,6 +14,10 @@ import ( "tailscale.com/wgengine/filter" ) +// ByteCount is the number of bytes that have been sent or received. +// +// TODO: why is this a type? remove? +// TODO: document whether it's payload bytes only or if it includes framing overhead. type ByteCount int64 type PeerStatus struct { @@ -22,20 +26,29 @@ type PeerStatus struct { NodeKey tailcfg.NodeKey } +// Status is the Engine status. type Status struct { Peers []PeerStatus LocalAddrs []string // TODO(crawshaw): []wgcfg.Endpoint? } -type StatusCallback func(s *Status, err error) +// StatusCallback is the type of status callbacks used by +// Engine.SetStatusCallback. +// +// Exactly one of Status or error is non-nil. +type StatusCallback func(*Status, error) +// RouteSettings is the full WireGuard config data (set of peers keys, +// IP, etc in wgcfg.Config) plus the things that WireGuard doesn't do +// itself, like DNS stuff. type RouteSettings struct { - LocalAddr wgcfg.CIDR + LocalAddr wgcfg.CIDR // TODO: why is this here? how does it differ from wgcfg.Config's info? DNS []net.IP DNSDomains []string - Cfg wgcfg.Config + Cfg wgcfg.Config // TODO: value type here, but pointer below? } +// OnlyRelevantParts returns a string minimally describing the route settings. func (rs *RouteSettings) OnlyRelevantParts() string { var peers [][]wgcfg.CIDR for _, p := range rs.Cfg.Peers { @@ -45,31 +58,55 @@ func (rs *RouteSettings) OnlyRelevantParts() string { rs.LocalAddr, rs.DNS, rs.DNSDomains, peers) } +// Router is the TODO. type Router interface { + // Up brings the router up. + // TODO: more than once? after Close? Up() error - SetRoutes(rs RouteSettings) error + // SetRoutes sets the routes. + // TODO: while running? + SetRoutes(RouteSettings) error + // Close closes the router. + // TODO: return an error? does this block? Close() } +// Engine is the Tailscale WireGuard engine interface. type Engine interface { - // Reconfigure wireguard and make sure it's running. + // Reconfig reconfigures WireGuard and makes sure it's running. // This also handles setting up any kernel routes. + // + // The provided DNS domains are not part of wgcfg.Config, as + // WireGuard itself doesn't care about such things. + // + // This is called whenever the tailcontrol (control plane) + // sends an updated network map. Reconfig(cfg *wgcfg.Config, dnsDomains []string) error - // Update the packet filter. - SetFilter(filt *filter.Filter) - // Set the function to call when wireguard status changes. - SetStatusCallback(cb StatusCallback) - // Request a wireguard status update right away, sent to the callback. + + // SetFilter updates the packet filter. + SetFilter(*filter.Filter) + + // SetStatusCallback sets the function to call when the + // WireGuard status changes. + SetStatusCallback(StatusCallback) + + // RequestStatus requests a WireGuard status update right + // away, sent to the callback registered via SetStatusCallback. RequestStatus() - // Shut down this wireguard instance, remove any routes it added, etc. - // To bring it up again later, you'll need a new Engine. + + // Close shuts down this wireguard instance, remove any routes + // it added, etc. To bring it up again later, you'll need a + // new Engine. Close() - // Wait until the Engine is .Close()ed or aborts with an error. - // You don't have to call this. + + // Wait waits until the Engine's Close method is called or the + // engine aborts with an error. You don't have to call this. + // TODO: return an error? Wait() + // LinkChange informs the engine that the system network // link has changed. The isExpensive parameter is set on links - // where sending packets uses substantial power or dollars - // (such as LTE on a phone). + // where sending packets uses substantial power or money, + // such as mobile data on a phone. LinkChange(isExpensive bool) } From 7e5e32775a02503fbaafe22d8b22b6eb7e7dfae1 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 11 Feb 2020 15:21:24 -0800 Subject: [PATCH 04/15] wgengine: flesh out some docs, minor cleanups Signed-off-by: Brad Fitzpatrick --- wgengine/router_fake.go | 6 +++--- wgengine/router_linux.go | 10 +++++++++- wgengine/userspace.go | 4 ++-- wgengine/wgengine.go | 17 ++++++++++------- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/wgengine/router_fake.go b/wgengine/router_fake.go index 8157e929a..e6227e977 100644 --- a/wgengine/router_fake.go +++ b/wgengine/router_fake.go @@ -16,11 +16,10 @@ type fakeRouter struct { } func NewFakeRouter(logf logger.Logf, tunname string, dev *device.Device, tuntap tun.Device, netChanged func()) Router { - r := fakeRouter{ + return &fakeRouter{ logf: logf, tunname: tunname, } - return &r } func (r *fakeRouter) Up() error { @@ -33,6 +32,7 @@ func (r *fakeRouter) SetRoutes(rs RouteSettings) error { return nil } -func (r *fakeRouter) Close() { +func (r *fakeRouter) Close() error { r.logf("Warning: fakeRouter.Close: not implemented.\n") + return nil } diff --git a/wgengine/router_linux.go b/wgengine/router_linux.go index edd93d570..5c76171c8 100644 --- a/wgengine/router_linux.go +++ b/wgengine/router_linux.go @@ -57,6 +57,8 @@ func cmd(args ...string) *exec.Cmd { func (r *linuxRouter) Up() error { out, err := cmd("ip", "link", "set", r.tunname, "up").CombinedOutput() if err != nil { + // TODO: this should return an error; why is it calling log.Fatalf? + // Audit callers to make sure they're handling errors. log.Fatalf("running ip link failed: %v\n%s", err, out) } @@ -154,6 +156,7 @@ func (r *linuxRouter) SetRoutes(rs RouteSettings) error { r.local = rs.LocalAddr r.routes = newRoutes + // TODO: this: if false { if err := r.replaceResolvConf(rs.DNS, rs.DNSDomains); err != nil { errq = fmt.Errorf("replacing resolv.conf failed: %v", err) @@ -162,12 +165,17 @@ func (r *linuxRouter) SetRoutes(rs RouteSettings) error { return errq } -func (r *linuxRouter) Close() { +func (r *linuxRouter) Close() error { + var ret error r.mon.Close() if err := r.restoreResolvConf(); err != nil { r.logf("failed to restore system resolv.conf: %v", err) + if ret == nil { + ret = err + } } // TODO(apenwarr): clean up iptables etc. + return ret } const ( diff --git a/wgengine/userspace.go b/wgengine/userspace.go index e21bae855..d2e6242f9 100644 --- a/wgengine/userspace.go +++ b/wgengine/userspace.go @@ -205,7 +205,7 @@ func (e *userspaceEngine) Reconfig(cfg *wgcfg.Config, dnsDomains []string) error e.peerSequence[i] = p.PublicKey } - // TODO(apenwarr): get rid of silly uapi stuff for in-process comms + // TODO(apenwarr): get rid of uapi stuff for in-process comms uapi, err := cfg.ToUAPI() if err != nil { return err @@ -239,7 +239,7 @@ func (e *userspaceEngine) Reconfig(cfg *wgcfg.Config, dnsDomains []string) error rs := RouteSettings{ LocalAddr: cidr, - Cfg: *cfg, + Cfg: cfg, DNS: cfg.Interface.Dns, DNSDomains: dnsDomains, } diff --git a/wgengine/wgengine.go b/wgengine/wgengine.go index 83538ad84..1971bd8d4 100644 --- a/wgengine/wgengine.go +++ b/wgengine/wgengine.go @@ -45,7 +45,7 @@ type RouteSettings struct { LocalAddr wgcfg.CIDR // TODO: why is this here? how does it differ from wgcfg.Config's info? DNS []net.IP DNSDomains []string - Cfg wgcfg.Config // TODO: value type here, but pointer below? + Cfg *wgcfg.Config } // OnlyRelevantParts returns a string minimally describing the route settings. @@ -58,17 +58,20 @@ func (rs *RouteSettings) OnlyRelevantParts() string { rs.LocalAddr, rs.DNS, rs.DNSDomains, peers) } -// Router is the TODO. +// Router is responsible for managing the system route table. +// +// There's only one instance, and one per-OS implementation. type Router interface { // Up brings the router up. - // TODO: more than once? after Close? Up() error - // SetRoutes sets the routes. - // TODO: while running? + + // SetRoutes is called regularly on network map updates. + // It's how you kernel route table entries are populated for + // each peer. SetRoutes(RouteSettings) error + // Close closes the router. - // TODO: return an error? does this block? - Close() + Close() error } // Engine is the Tailscale WireGuard engine interface. From d404f1caedc1ef1597b7f8fe6dc4682896e37816 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 12 Feb 2020 14:46:59 -0800 Subject: [PATCH 05/15] cmd/tsshd: add basic SSH server --- cmd/tsshd/tsshd.go | 204 +++++++++++++++++++++++++++++++++++++++++++++ go.mod | 4 + go.sum | 8 ++ 3 files changed, 216 insertions(+) create mode 100644 cmd/tsshd/tsshd.go diff --git a/cmd/tsshd/tsshd.go b/cmd/tsshd/tsshd.go new file mode 100644 index 000000000..edd976d6f --- /dev/null +++ b/cmd/tsshd/tsshd.go @@ -0,0 +1,204 @@ +// Copyright (c) 2020 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. + +// The tsshd binary is an SSH server that accepts connections +// from anybody on the same Tailscale network. +// +// It does not use passwords or SSH public key. +// +// Any user name is accepted; users are logged in as whoever is +// running this daemon. +// +// Warning: use at your own risk. This code has had very few eyeballs +// on it. +package main + +import ( + "flag" + "fmt" + "io" + "io/ioutil" + "log" + "net" + "os" + "os/exec" + "strings" + "syscall" + "time" + "unsafe" + + "github.com/gliderlabs/ssh" + "github.com/kr/pty" + gossh "golang.org/x/crypto/ssh" +) + +var ( + port = flag.Int("port", 2200, "port to listen on") + hostKey = flag.String("hostkey", "", "SSH host key") +) + +func main() { + flag.Parse() + if *hostKey == "" { + log.Fatalf("missing required --hostkey") + } + hostKey, err := ioutil.ReadFile(*hostKey) + if err != nil { + log.Fatal(err) + } + signer, err := gossh.ParsePrivateKey(hostKey) + if err != nil { + log.Printf("failed to parse SSH host key: %v; running running SSH server", err) + return + } + + warned := false + for { + addr, iface, err := tailscaleInterface() + if err != nil { + log.Fatalf("listing interfaces: %v", err) + } + if addr == nil { + if !warned { + log.Printf("no tailscale interface found; polling until one is available") + warned = true + } + // TODO: use netlink or other OS-specific mechanism to efficiently + // wait for change in interfaces. Polling every N seconds is good enough + // for now. + time.Sleep(5 * time.Second) + continue + } + warned = false + listen := net.JoinHostPort(addr.String(), fmt.Sprint(*port)) + log.Printf("tailscale ssh server listening on %v, %v", iface.Name, listen) + s := &ssh.Server{ + Addr: listen, + Handler: handleSSH, + } + s.AddHostKey(signer) + + err = s.ListenAndServe() + log.Fatalf("tailscale sshd failed: %v", err) + } + +} + +// tailscaleInterface returns an err on a fatal problem, and all zero values +// if no suitable inteface is found. +func tailscaleInterface() (net.IP, *net.Interface, error) { + ifs, err := net.Interfaces() + if err != nil { + return nil, nil, err + } + for _, iface := range ifs { + if !maybeTailscaleInterfaceName(iface.Name) { + continue + } + addrs, err := iface.Addrs() + if err != nil { + continue + } + for _, a := range addrs { + if ipnet, ok := a.(*net.IPNet); ok && isTailscaleIP(ipnet.IP) { + return ipnet.IP, &iface, nil + } + } + } + return nil, nil, nil +} + +// maybeTailscaleInterfaceName reports whether s is an interface +// name that might be used by Tailscale. +func maybeTailscaleInterfaceName(s string) bool { + return strings.HasPrefix(s, "wg") || + strings.HasPrefix(s, "ts") || + strings.HasPrefix(s, "tailscale") +} + +func isTailscaleIP(ip net.IP) bool { + return cgNAT.Contains(ip) +} + +var cgNAT = func() *net.IPNet { + _, ipNet, err := net.ParseCIDR("100.64.0.0/10") + if err != nil { + panic(err) + } + return ipNet +}() + +func handleSSH(s ssh.Session) { + user := s.User() + addr := s.RemoteAddr() + ta, ok := addr.(*net.TCPAddr) + if !ok { + log.Printf("tsshd: rejecting non-TCP addr %T %v", addr, addr) + s.Exit(1) + return + } + if !isTailscaleIP(ta.IP) { + log.Printf("tsshd: rejecting non-Tailscale addr %v", ta.IP) + s.Exit(1) + return + } + + log.Printf("new session for %q from %v", user, ta) + defer log.Printf("closing session for %q from %v", user, ta) + ptyReq, winCh, isPty := s.Pty() + if !isPty { + fmt.Fprintf(s, "TODO scp etc") + s.Exit(1) + return + } + + userWantsShell := len(s.Command()) == 0 + + if userWantsShell { + shell, err := shellOfUser(s.User()) + if err != nil { + fmt.Fprintf(s, "failed to find shell: %v\n", err) + s.Exit(1) + return + } + cmd := exec.Command(shell) + cmd.Env = append(cmd.Env, fmt.Sprintf("TERM=%s", ptyReq.Term)) + f, err := pty.Start(cmd) + if err != nil { + log.Printf("running shell: %v", err) + s.Exit(1) + return + } + defer f.Close() + go func() { + for win := range winCh { + setWinsize(f, win.Width, win.Height) + } + }() + go func() { + io.Copy(f, s) // stdin + }() + io.Copy(s, f) // stdout + cmd.Process.Kill() + if err := cmd.Wait(); err != nil { + s.Exit(1) + } + s.Exit(0) + return + } + + fmt.Fprintf(s, "TODO: args\n") + s.Exit(1) + return +} + +func shellOfUser(user string) (string, error) { + // TODO + return "/bin/bash", nil +} + +func setWinsize(f *os.File, w, h int) { + syscall.Syscall(syscall.SYS_IOCTL, f.Fd(), uintptr(syscall.TIOCSWINSZ), + uintptr(unsafe.Pointer(&struct{ h, w, x, y uint16 }{uint16(h), uint16(w), 0, 0}))) +} diff --git a/go.mod b/go.mod index c99636c5c..52bdc65c7 100644 --- a/go.mod +++ b/go.mod @@ -3,11 +3,15 @@ module tailscale.com go 1.13 require ( + github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 // indirect github.com/apenwarr/fixconsole v0.0.0-20191012055117-5a9f6489cc29 + github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect + github.com/gliderlabs/ssh v0.2.2 github.com/go-ole/go-ole v1.2.4 github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e github.com/google/go-cmp v0.4.0 github.com/klauspost/compress v1.9.8 + github.com/kr/pty v1.1.1 github.com/mdlayher/netlink v1.1.0 github.com/pborman/getopt v0.0.0-20190409184431-ee0cd42419d3 github.com/tailscale/hujson v0.0.0-20190930033718-5098e564d9b3 diff --git a/go.sum b/go.sum index 4a33de9bd..2ec3f95c4 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,16 @@ cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo4JG6LR5AXSUEsOjtdm0kw0FtQtMJA= +github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= github.com/apenwarr/fixconsole v0.0.0-20191012055117-5a9f6489cc29 h1:muXWUcay7DDy1/hEQWrYlBy+g0EuwT70sBHg65SeUc4= github.com/apenwarr/fixconsole v0.0.0-20191012055117-5a9f6489cc29/go.mod h1:JYWahgHer+Z2xbsgHPtaDYVWzeHDminu+YIBWkxpCAY= github.com/apenwarr/w32 v0.0.0-20190407065021-aa00fece76ab h1:CMGzRRCjnD50RjUFSArBLuCxiDvdp7b8YPAcikBEQ+k= github.com/apenwarr/w32 v0.0.0-20190407065021-aa00fece76ab/go.mod h1:nfFtvHn2Hgs9G1u0/J6LHQv//EksNC+7G8vXmd1VTJ8= +github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ= +github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc= +github.com/gliderlabs/ssh v0.2.2 h1:6zsha5zo/TWhRhwqCD3+EarCAgZ2yN28ipRnGPnwkI0= +github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0= github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI= github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM= github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e h1:1r7pUrabqp18hOBcwBwiTsbnFeTZHV9eER/QT5JVZxY= @@ -23,6 +29,7 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o github.com/klauspost/compress v1.9.8 h1:VMAMUUOh+gaxKTMk+zqbjsSjsIcUcL/LF4o63i82QyA= github.com/klauspost/compress v1.9.8/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/mdlayher/netlink v0.0.0-20190409211403-11939a169225/go.mod h1:eQB3mZE4aiYnlUsyGGCOpPETfdQq4Jhsgf1fk3cwQaA= @@ -66,6 +73,7 @@ golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d h1:TzXSXBo42m9gQenoE3b9BG golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= From 13dc12814ef5990d46642e8670c0300c21ea583d Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 12 Feb 2020 14:51:47 -0800 Subject: [PATCH 06/15] cmd/tsshd: fix log text --- cmd/tsshd/tsshd.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/tsshd/tsshd.go b/cmd/tsshd/tsshd.go index edd976d6f..d10c4a8e3 100644 --- a/cmd/tsshd/tsshd.go +++ b/cmd/tsshd/tsshd.go @@ -49,7 +49,7 @@ func main() { } signer, err := gossh.ParsePrivateKey(hostKey) if err != nil { - log.Printf("failed to parse SSH host key: %v; running running SSH server", err) + log.Printf("failed to parse SSH host key: %v", err) return } From 15f8c8dde41bf86a25c6722dc429e059190e4bbb Mon Sep 17 00:00:00 2001 From: Martin Baillie Date: Wed, 12 Feb 2020 17:57:45 +1100 Subject: [PATCH 07/15] wgengine: add initial rough OpenBSD implementation Signed-off-by: Martin Baillie --- wgengine/router_default.go | 2 +- wgengine/router_openbsd.go | 176 +++++++++++++++++++++++++++++++++++++ 2 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 wgengine/router_openbsd.go diff --git a/wgengine/router_default.go b/wgengine/router_default.go index 7aced4a82..06618460d 100644 --- a/wgengine/router_default.go +++ b/wgengine/router_default.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build !windows,!linux,!darwin +// +build !windows,!linux,!darwin,!openbsd package wgengine diff --git a/wgengine/router_openbsd.go b/wgengine/router_openbsd.go new file mode 100644 index 000000000..eb42575c5 --- /dev/null +++ b/wgengine/router_openbsd.go @@ -0,0 +1,176 @@ +// Copyright (c) 2020 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 wgengine + +import ( + "fmt" + "log" + "net" + "os/exec" + + "github.com/tailscale/wireguard-go/device" + "github.com/tailscale/wireguard-go/tun" + "github.com/tailscale/wireguard-go/wgcfg" + "tailscale.com/logger" +) + +// For now this router only supports the userspace WireGuard implementations. +// +// There is an experimental kernel version in the works: +// https://git.zx2c4.com/wireguard-openbsd. +// +// TODO(mbaillie): netlink-style monitoring might be possible through +// `ifstated(8)`/`devd(8)`, or become possible with the OpenBSD kernel +// implementation. This merits further investigation. + +type openbsdRouter struct { + logf func(fmt string, args ...interface{}) + tunname string + local wgcfg.CIDR + routes map[wgcfg.CIDR]struct{} +} + +func NewUserspaceRouter(logf logger.Logf, tunname string, _ *device.Device, tuntap tun.Device, _ func()) Router { + r := openbsdRouter{ + logf: logf, + tunname: tunname, + } + return &r +} + +// TODO(mbaillie): extract as identical to linux version +func cmd(args ...string) *exec.Cmd { + if len(args) == 0 { + log.Fatalf("exec.Cmd(%#v) invalid; need argv[0]\n", args) + } + return exec.Command(args[0], args[1:]...) +} + +func (r *openbsdRouter) Up() error { + // TODO(mbaillie): MTU set elsewhere? + + ifup := []string{"ifconfig", r.tunname, "up"} + if out, err := cmd(ifup...).CombinedOutput(); err != nil { + r.logf("running ifconfig failed: %v\n%s", err, out) + return err + } + return nil +} + +func (r *openbsdRouter) SetRoutes(rs RouteSettings) error { + var errq error + + if rs.LocalAddr != r.local { + if r.local != (wgcfg.CIDR{}) { + addrdel := []string{"ifconfig", r.tunname, + "inet", r.local.String(), "-alias"} + out, err := cmd(addrdel...).CombinedOutput() + if err != nil { + r.logf("addr del failed: %v: %v\n%s", addrdel, err, out) + if errq == nil { + errq = err + } + } + + routedel := []string{"route", "-q", "-n", + "del", "-inet", r.local.String(), + "-iface", r.local.IP.String()} + if out, err := cmd(routedel...).CombinedOutput(); err != nil { + r.logf("route del failed: %v: %v\n%s", routedel, err, out) + if errq == nil { + errq = err + } + } + } + + addradd := []string{"ifconfig", r.tunname, + "inet", rs.LocalAddr.String(), "alias"} + out, err := cmd(addradd...).CombinedOutput() + if err != nil { + r.logf("addr add failed: %v: %v\n%s", addradd, err, out) + if errq == nil { + errq = err + } + } + + routeadd := []string{"route", "-q", "-n", + "add", "-inet", rs.LocalAddr.String(), + "-iface", rs.LocalAddr.IP.String()} + if out, err := cmd(routeadd...).CombinedOutput(); err != nil { + r.logf("route add failed: %v: %v\n%s", routeadd, err, out) + if errq == nil { + errq = err + } + } + } + + newRoutes := make(map[wgcfg.CIDR]struct{}) + for _, peer := range rs.Cfg.Peers { + for _, route := range peer.AllowedIPs { + newRoutes[route] = struct{}{} + } + } + for route := range r.routes { + if _, keep := newRoutes[route]; !keep { + net := route.IPNet() + nip := net.IP.Mask(net.Mask) + nstr := fmt.Sprintf("%v/%d", nip, route.Mask) + routedel := []string{"route", "-q", "-n", + "del", "-inet", nstr, + "-iface", rs.LocalAddr.IP.String()} + out, err := cmd(routedel...).CombinedOutput() + if err != nil { + r.logf("route del failed: %v: %v\n%s", routedel, err, out) + if errq == nil { + errq = err + } + } + } + } + for route := range newRoutes { + if _, exists := r.routes[route]; !exists { + net := route.IPNet() + nip := net.IP.Mask(net.Mask) + nstr := fmt.Sprintf("%v/%d", nip, route.Mask) + routeadd := []string{"route", "-q", "-n", + "add", "-inet", nstr, + "-iface", rs.LocalAddr.IP.String()} + out, err := cmd(routeadd...).CombinedOutput() + if err != nil { + r.logf("addr add failed: %v: %v\n%s", routeadd, err, out) + if errq == nil { + errq = err + } + } + } + } + + r.local = rs.LocalAddr + r.routes = newRoutes + + if err := r.replaceResolvConf(rs.DNS, rs.DNSDomains); err != nil { + errq = fmt.Errorf("replacing resolv.conf failed: %v", err) + } + + return errq +} + +func (r *openbsdRouter) Close() { + out, err := cmd("ifconfig", r.tunname, "down").CombinedOutput() + if err != nil { + r.logf("running ifconfig failed: %v\n%s", err, out) + } + + if err := r.restoreResolvConf(); err != nil { + r.logf("failed to restore system resolv.conf: %v", err) + } + + // TODO(mbaillie): wipe routes +} + +// TODO(mbaillie): these are no-ops for now. They could re-use the Linux funcs +// (sans systemd parts), but I note Linux DNS is disabled(?) so leaving for now. +func (r *openbsdRouter) replaceResolvConf(_ []net.IP, _ []string) error { return nil } +func (r *openbsdRouter) restoreResolvConf() error { return nil } From 3d5ef74f26703c0c8c9804104d9609805eadbb8a Mon Sep 17 00:00:00 2001 From: Martin Baillie Date: Wed, 12 Feb 2020 18:09:24 +1100 Subject: [PATCH 08/15] tailscaled: add tunnel name and port flags OpenBSD tunnel names are prefixed with `tun`. Controlling the port allows for deterministic configuration of firewall rules (using `pf` in this case). Signed-off-by: Martin Baillie --- cmd/tailscaled/ipnd.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cmd/tailscaled/ipnd.go b/cmd/tailscaled/ipnd.go index 466c074b2..8d2b68b77 100644 --- a/cmd/tailscaled/ipnd.go +++ b/cmd/tailscaled/ipnd.go @@ -20,11 +20,14 @@ import ( "tailscale.com/ipn/ipnserver" "tailscale.com/logpolicy" "tailscale.com/wgengine" + "tailscale.com/wgengine/magicsock" ) func main() { fake := getopt.BoolLong("fake", 0, "fake tunnel+routing instead of tuntap") debug := getopt.StringLong("debug", 0, "", "Address of debug server") + tunname := getopt.StringLong("tun", 0, "ts0", "tunnel interface name") + listenport := getopt.Uint16Long("port", 'p', magicsock.DefaultPort, "WireGuard port (0=autoselect)") logf := wgengine.RusagePrefixLog(log.Printf) @@ -47,7 +50,7 @@ func main() { if *fake { e, err = wgengine.NewFakeUserspaceEngine(logf, 0, false) } else { - e, err = wgengine.NewUserspaceEngine(logf, "ts0", 0, false) + e, err = wgengine.NewUserspaceEngine(logf, *tunname, *listenport, false) } if err != nil { log.Fatalf("wgengine.New: %v\n", err) From afa83358f7355dba20bbe518d850ace44b09bbe2 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Wed, 12 Feb 2020 15:58:49 -0800 Subject: [PATCH 09/15] wgenging: use logger.Logf type in openbsd router. Signed-off-by: David Anderson --- wgengine/router_openbsd.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wgengine/router_openbsd.go b/wgengine/router_openbsd.go index eb42575c5..5cd99bd0b 100644 --- a/wgengine/router_openbsd.go +++ b/wgengine/router_openbsd.go @@ -26,7 +26,7 @@ import ( // implementation. This merits further investigation. type openbsdRouter struct { - logf func(fmt string, args ...interface{}) + logf logger.Logf tunname string local wgcfg.CIDR routes map[wgcfg.CIDR]struct{} From 5c2deb66bed021a25e26ffdb343470c1044f8e17 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Wed, 12 Feb 2020 15:59:13 -0800 Subject: [PATCH 10/15] wgengine: remove MTU TODO. The MTU is currently set when creating the tun device, elsewhere in the code. Maybe someday we'll want some kind of per-platform MTU configuration here, but not in the short-medium term. Signed-off-by: David Anderson --- wgengine/router_openbsd.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/wgengine/router_openbsd.go b/wgengine/router_openbsd.go index 5cd99bd0b..9a7f76a18 100644 --- a/wgengine/router_openbsd.go +++ b/wgengine/router_openbsd.go @@ -49,8 +49,6 @@ func cmd(args ...string) *exec.Cmd { } func (r *openbsdRouter) Up() error { - // TODO(mbaillie): MTU set elsewhere? - ifup := []string{"ifconfig", r.tunname, "up"} if out, err := cmd(ifup...).CombinedOutput(); err != nil { r.logf("running ifconfig failed: %v\n%s", err, out) From 67c1b2c6ef3a2ae25b5cffd9c49d04e665e6c6c7 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Wed, 12 Feb 2020 16:01:54 -0800 Subject: [PATCH 11/15] cmd/tsshd: fix lint. Signed-off-by: David Anderson --- cmd/tsshd/tsshd.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/tsshd/tsshd.go b/cmd/tsshd/tsshd.go index d10c4a8e3..b6049f4ee 100644 --- a/cmd/tsshd/tsshd.go +++ b/cmd/tsshd/tsshd.go @@ -190,7 +190,6 @@ func handleSSH(s ssh.Session) { fmt.Fprintf(s, "TODO: args\n") s.Exit(1) - return } func shellOfUser(user string) (string, error) { From 36f5433476dc67e0e03a179f7d342c5de3b2acc8 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Wed, 12 Feb 2020 16:16:05 -0800 Subject: [PATCH 12/15] wgengine: fix the build on darwin, windows, openbsd. 7e5e3277 changed the Router interface definition, but forgot to fix up these platform files. Signed-off-by: David Anderson --- wgengine/router_darwin.go | 3 ++- wgengine/router_openbsd.go | 4 +++- wgengine/router_windows.go | 3 ++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/wgengine/router_darwin.go b/wgengine/router_darwin.go index c4c09ace4..0c17f28b1 100644 --- a/wgengine/router_darwin.go +++ b/wgengine/router_darwin.go @@ -32,5 +32,6 @@ func (r *darwinRouter) SetRoutes(rs RouteSettings) error { return nil } -func (r *darwinRouter) Close() { +func (r *darwinRouter) Close() error { + return nil } diff --git a/wgengine/router_openbsd.go b/wgengine/router_openbsd.go index 9a7f76a18..efb19a179 100644 --- a/wgengine/router_openbsd.go +++ b/wgengine/router_openbsd.go @@ -155,7 +155,7 @@ func (r *openbsdRouter) SetRoutes(rs RouteSettings) error { return errq } -func (r *openbsdRouter) Close() { +func (r *openbsdRouter) Close() error { out, err := cmd("ifconfig", r.tunname, "down").CombinedOutput() if err != nil { r.logf("running ifconfig failed: %v\n%s", err, out) @@ -166,6 +166,8 @@ func (r *openbsdRouter) Close() { } // TODO(mbaillie): wipe routes + + return nil } // TODO(mbaillie): these are no-ops for now. They could re-use the Linux funcs diff --git a/wgengine/router_windows.go b/wgengine/router_windows.go index c05b81b3e..eefa433ec 100644 --- a/wgengine/router_windows.go +++ b/wgengine/router_windows.go @@ -51,8 +51,9 @@ func (r *winRouter) SetRoutes(rs RouteSettings) error { return nil } -func (r *winRouter) Close() { +func (r *winRouter) Close() error { if r.routeChangeCallback != nil { r.routeChangeCallback.Unregister() } + return nil } From 745c8c92b111cc4b490a49d1811bc498095f35e1 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 12 Feb 2020 21:11:41 -0800 Subject: [PATCH 13/15] wgengine: fix windows build for winipcfg-go Signed-off-by: Brad Fitzpatrick --- cmd/tsshd/tsshd.go | 2 ++ go.mod | 3 ++- go.sum | 5 +++++ wgengine/ifconfig_windows.go | 4 ++-- wgengine/router_windows.go | 4 ++-- wgengine/rtnlmon/mon.go | 2 ++ 6 files changed, 15 insertions(+), 5 deletions(-) diff --git a/cmd/tsshd/tsshd.go b/cmd/tsshd/tsshd.go index b6049f4ee..b96df803a 100644 --- a/cmd/tsshd/tsshd.go +++ b/cmd/tsshd/tsshd.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build !windows + // The tsshd binary is an SSH server that accepts connections // from anybody on the same Tailscale network. // diff --git a/go.mod b/go.mod index 52bdc65c7..98762b5c8 100644 --- a/go.mod +++ b/go.mod @@ -15,10 +15,11 @@ require ( github.com/mdlayher/netlink v1.1.0 github.com/pborman/getopt v0.0.0-20190409184431-ee0cd42419d3 github.com/tailscale/hujson v0.0.0-20190930033718-5098e564d9b3 + github.com/tailscale/winipcfg-go v0.0.0-20200213045944-185b07f8233f github.com/tailscale/wireguard-go v0.0.0-20200211020303-f39bc8eeee1b golang.org/x/crypto v0.0.0-20200210222208-86ce3cb69678 golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d - golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 + golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4 gortc.io/stun v1.22.1 honnef.co/go/tools v0.0.1-2019.2.3 // indirect ) diff --git a/go.sum b/go.sum index 2ec3f95c4..e2dac0d49 100644 --- a/go.sum +++ b/go.sum @@ -41,6 +41,8 @@ github.com/pborman/getopt v0.0.0-20190409184431-ee0cd42419d3/go.mod h1:85jBQOZwp github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/tailscale/hujson v0.0.0-20190930033718-5098e564d9b3 h1:rdtXEo9yffOjh4vZQJw3heaY+ggXKp+zvMX5fihh6lI= github.com/tailscale/hujson v0.0.0-20190930033718-5098e564d9b3/go.mod h1:STqf+YV0ADdzk4ejtXFsGqDpATP9JoL0OB+hiFQbkdE= +github.com/tailscale/winipcfg-go v0.0.0-20200213045944-185b07f8233f h1:q2ynfOHxHaaMnkZ1YHswWeO6wEk7IyOnkFozytZ1ztc= +github.com/tailscale/winipcfg-go v0.0.0-20200213045944-185b07f8233f/go.mod h1:x880GWw5fvrl2DVTQ04ttXQD4DuppTt1Yz6wLibbjNE= github.com/tailscale/wireguard-go v0.0.0-20191108062213-b93cdd0582db h1:oP0crfwOb3WZSVrMVm/o51NXN2JirDlcdlNEIPTmgI0= github.com/tailscale/wireguard-go v0.0.0-20200207221558-a158079b156a h1:5TWA3nl2QUfL9OiE3tlBpqJd4GYd4hbGtDNkWQQ2fyc= github.com/tailscale/wireguard-go v0.0.0-20200207221558-a158079b156a/go.mod h1:QPS8HjBzzAXoQNndUNx2efJaQbCCz8nI2Cv1ksTUHyY= @@ -76,6 +78,7 @@ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190310054646-10058d7d4faa/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190405154228-4b34438f7a67/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190411185658-b44545bcd369/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -85,6 +88,8 @@ golang.org/x/sys v0.0.0-20191003212358-c178f38b412c/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 h1:LfCXLvNmTYH9kEmVgqbnsWfruoXZIrh4YBgqVHtDvw0= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4 h1:sfkvUWPNGwSV+8/fNqctR5lS2AqCSqYwXdrjCxp/dXo= +golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= diff --git a/wgengine/ifconfig_windows.go b/wgengine/ifconfig_windows.go index 707a21e38..6ac368f88 100644 --- a/wgengine/ifconfig_windows.go +++ b/wgengine/ifconfig_windows.go @@ -16,13 +16,13 @@ import ( "time" "unsafe" - "github.com/go-ole/go-ole" + ole "github.com/go-ole/go-ole" + winipcfg "github.com/tailscale/winipcfg-go" "github.com/tailscale/wireguard-go/device" "github.com/tailscale/wireguard-go/tun" "github.com/tailscale/wireguard-go/wgcfg" "golang.org/x/sys/windows" "golang.org/x/sys/windows/registry" - "golang.zx2c4.com/winipcfg" "tailscale.com/wgengine/winnet" ) diff --git a/wgengine/router_windows.go b/wgengine/router_windows.go index eefa433ec..7e04d823e 100644 --- a/wgengine/router_windows.go +++ b/wgengine/router_windows.go @@ -7,9 +7,9 @@ package wgengine import ( "log" + winipcfg "github.com/tailscale/winipcfg-go" "github.com/tailscale/wireguard-go/device" "github.com/tailscale/wireguard-go/tun" - "golang.zx2c4.com/winipcfg" "tailscale.com/logger" ) @@ -43,7 +43,7 @@ func (r *winRouter) Up() error { } func (r *winRouter) SetRoutes(rs RouteSettings) error { - err := ConfigureInterface(&rs.Cfg, r.nativeTun, rs.DNS, rs.DNSDomains) + err := ConfigureInterface(rs.Cfg, r.nativeTun, rs.DNS, rs.DNSDomains) if err != nil { r.logf("ConfigureInterface: %v\n", err) return err diff --git a/wgengine/rtnlmon/mon.go b/wgengine/rtnlmon/mon.go index 080ed2362..b06f1f47f 100644 --- a/wgengine/rtnlmon/mon.go +++ b/wgengine/rtnlmon/mon.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build !windows + // Package rtnlmon watches for "interesting" changes to the network // stack and fires a callback. package rtnlmon From ae89a7a65d8fc470c77888a58e790d42755a0c77 Mon Sep 17 00:00:00 2001 From: Jonathan Wilkins Date: Wed, 12 Feb 2020 20:10:22 +0000 Subject: [PATCH 14/15] meta: build different GOOSes in CI First pass at #37 without support for cross-compilation of tests Signed-off-by: evalexpr --- .github/workflows/linux.yml | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index 756998719..8a0e21f79 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -28,20 +28,18 @@ jobs: - name: Basic build run: go build ./cmd/... + - name: macOS build + env: + GOOS: darwin + GOARCH: amd64 + run: go build ./cmd/... + + - name: Windows build + env: + GOOS: windows + GOARCH: amd64 + run: go build ./cmd/... + - name: Test build run: go test ./... - - uses: k0kubun/action-slack@v2.0.0 - with: - payload: | - { - "attachments": [{ - "text": "${{ job.status }}: ${{ github.workflow }} " + - "() " + - "of ${{ github.repository }}@" + "${{ github.ref }}".split('/').reverse()[0] + " by ${{ github.event.head_commit.committer.name }}", - "color": "danger" - }] - } - env: - SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} - if: failure() && github.event_name == 'push' From e5481f33d302e830556d6a8cc7498fd278223a22 Mon Sep 17 00:00:00 2001 From: evalexpr Date: Wed, 12 Feb 2020 21:39:58 +0000 Subject: [PATCH 15/15] meta: cross-compile tests for other geese For now this is just darwin/windows, but we'll eventually add support for {free,open}bsd Signed-off-by: evalexpr --- .github/workflows/linux.yml | 20 ++++++++++++++++++- test.sh | 38 +++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100755 test.sh diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index 8a0e21f79..8b31f8115 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -40,6 +40,24 @@ jobs: GOARCH: amd64 run: go build ./cmd/... - - name: Test build + - name: Cross-compile tests for other geese + run: ./test.sh + + - name: Run tests on linux run: go test ./... + - uses: k0kubun/action-slack@v2.0.0 + with: + payload: | + { + "attachments": [{ + "text": "${{ job.status }}: ${{ github.workflow }} " + + "() " + + "of ${{ github.repository }}@" + "${{ github.ref }}".split('/').reverse()[0] + " by ${{ github.event.head_commit.committer.name }}", + "color": "danger" + }] + } + env: + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} + if: failure() && github.event_name == 'push' + diff --git a/test.sh b/test.sh new file mode 100755 index 000000000..3c8af9794 --- /dev/null +++ b/test.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash + +function remove_test_files { + rm -f ./*test{,.exe} +} + +function fail { + printf '%s\n' "$1" >&2 + # If we fail, clean up after ourselves + remove_test_files + exit 1 +} + +function main { + test_dirs=() + while IFS= read -r -d '' file + do + dir=$(dirname "$file") + if [[ ! " ${test_dirs[*]} " =~ ${dir} ]]; then + test_dirs+=("$dir") + fi + done < <(find . -type f -iname '*_test.go' -print0) + + for goos in openbsd darwin windows + do + for dir in "${test_dirs[@]}"; do + echo "Testing GOOS=$goos in dir $dir" + GOOS="$goos" go test -c "./$dir" || fail "Test failed using $goos and $dir" + done + done + + # If all goes well, we should still clean up the test files + echo "Test complete" + remove_test_files +} + +main "$@" +