wgengine: make the tun.Device required at construction.

Signed-off-by: David Anderson <danderson@tailscale.com>
This commit is contained in:
David Anderson 2021-03-27 00:25:22 -07:00
parent 25e0bb0a4e
commit 2df8adef9d
3 changed files with 10 additions and 14 deletions

View File

@ -29,6 +29,7 @@
"time"
"github.com/go-multierror/multierror"
"github.com/tailscale/wireguard-go/tun"
"tailscale.com/ipn/ipnserver"
"tailscale.com/logpolicy"
"tailscale.com/net/socks5"
@ -332,18 +333,19 @@ func tryEngine(logf logger.Logf, linkMon *monitor.Mon, name string) (e wgengine.
LinkMonitor: linkMon,
}
isUserspace = name == "userspace-networking"
var dev tun.Device
if isUserspace {
conf.TUN = tstun.NewFake()
dev = tstun.NewFake()
conf.Router = router.NewFake(logf)
} else {
dev, err := tstun.New(logf, name)
dev, err = tstun.New(logf, name)
if err != nil {
tstun.Diagnose(logf, name)
return nil, false, err
}
conf.TUN = dev
}
e, err = wgengine.NewUserspaceEngine(logf, conf)
e, err = wgengine.NewUserspaceEngine(logf, dev, conf)
if err != nil {
return nil, isUserspace, err
}

View File

@ -164,8 +164,7 @@ func startIPNServer(ctx context.Context, logid string) error {
if err != nil {
return nil, err
}
eng, err := wgengine.NewUserspaceEngine(logf, wgengine.Config{
TUN: dev,
eng, err := wgengine.NewUserspaceEngine(logf, dev, wgengine.Config{
ListenPort: 41641,
})
if err != nil {

View File

@ -155,8 +155,7 @@ type Config struct {
func NewFakeUserspaceEngine(logf logger.Logf, listenPort uint16) (Engine, error) {
logf("Starting userspace wireguard engine (with fake TUN device)")
return NewUserspaceEngine(logf, Config{
TUN: tstun.NewFake(),
return NewUserspaceEngine(logf, tstun.NewFake(), Config{
Router: router.NewFake(logf),
ListenPort: listenPort,
Fake: true,
@ -165,18 +164,14 @@ func NewFakeUserspaceEngine(logf logger.Logf, listenPort uint16) (Engine, error)
// NewUserspaceEngine creates the named tun device and returns a
// Tailscale Engine running on it.
func NewUserspaceEngine(logf logger.Logf, conf Config) (_ Engine, reterr error) {
if conf.TUN == nil {
return nil, errors.New("TUN is required")
}
func NewUserspaceEngine(logf logger.Logf, dev tun.Device, conf Config) (_ Engine, reterr error) {
var closePool closeOnErrorPool
defer closePool.closeAllIfError(&reterr)
// TODO: default to a no-op router, require caller to pass in
// effectful ones.
if conf.Router == nil {
r, err := router.New(logf, conf.TUN)
r, err := router.New(logf, dev)
if err != nil {
return nil, err
}
@ -184,7 +179,7 @@ func NewUserspaceEngine(logf logger.Logf, conf Config) (_ Engine, reterr error)
closePool.add(r)
}
tsTUNDev := tstun.Wrap(logf, conf.TUN)
tsTUNDev := tstun.Wrap(logf, dev)
closePool.add(tsTUNDev)
e := &userspaceEngine{