|
|
@ -141,15 +141,6 @@ type EngineConfig struct {
|
|
|
|
Fake bool
|
|
|
|
Fake bool
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Loggify struct {
|
|
|
|
|
|
|
|
f logger.Logf
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (l *Loggify) Write(b []byte) (int, error) {
|
|
|
|
|
|
|
|
l.f(string(b))
|
|
|
|
|
|
|
|
return len(b), nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func NewFakeUserspaceEngine(logf logger.Logf, listenPort uint16) (Engine, error) {
|
|
|
|
func NewFakeUserspaceEngine(logf logger.Logf, listenPort uint16) (Engine, error) {
|
|
|
|
logf("Starting userspace wireguard engine (FAKE tuntap device).")
|
|
|
|
logf("Starting userspace wireguard engine (FAKE tuntap device).")
|
|
|
|
conf := EngineConfig{
|
|
|
|
conf := EngineConfig{
|
|
|
@ -258,7 +249,7 @@ func newUserspaceEngineAdvanced(conf EngineConfig) (_ Engine, reterr error) {
|
|
|
|
|
|
|
|
|
|
|
|
// flags==0 because logf is already nested in another logger.
|
|
|
|
// flags==0 because logf is already nested in another logger.
|
|
|
|
// The outer one can display the preferred log prefixes, etc.
|
|
|
|
// The outer one can display the preferred log prefixes, etc.
|
|
|
|
dlog := log.New(&Loggify{logf}, "", 0)
|
|
|
|
dlog := logger.StdLogger(logf)
|
|
|
|
logger := device.Logger{
|
|
|
|
logger := device.Logger{
|
|
|
|
Debug: dlog,
|
|
|
|
Debug: dlog,
|
|
|
|
Info: dlog,
|
|
|
|
Info: dlog,
|
|
|
@ -309,6 +300,7 @@ func newUserspaceEngineAdvanced(conf EngineConfig) (_ Engine, reterr error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// wgdev takes ownership of tundev, will close it when closed.
|
|
|
|
// wgdev takes ownership of tundev, will close it when closed.
|
|
|
|
|
|
|
|
e.logf("Creating wireguard device...")
|
|
|
|
e.wgdev = device.NewDevice(e.tundev, opts)
|
|
|
|
e.wgdev = device.NewDevice(e.tundev, opts)
|
|
|
|
defer func() {
|
|
|
|
defer func() {
|
|
|
|
if reterr != nil {
|
|
|
|
if reterr != nil {
|
|
|
@ -318,6 +310,7 @@ func newUserspaceEngineAdvanced(conf EngineConfig) (_ Engine, reterr error) {
|
|
|
|
|
|
|
|
|
|
|
|
// Pass the underlying tun.(*NativeDevice) to the router:
|
|
|
|
// Pass the underlying tun.(*NativeDevice) to the router:
|
|
|
|
// routers do not Read or Write, but do access native interfaces.
|
|
|
|
// routers do not Read or Write, but do access native interfaces.
|
|
|
|
|
|
|
|
e.logf("Creating router...")
|
|
|
|
e.router, err = conf.RouterGen(logf, e.wgdev, e.tundev.Unwrap())
|
|
|
|
e.router, err = conf.RouterGen(logf, e.wgdev, e.tundev.Unwrap())
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
e.magicConn.Close()
|
|
|
|
e.magicConn.Close()
|
|
|
@ -344,7 +337,9 @@ func newUserspaceEngineAdvanced(conf EngineConfig) (_ Engine, reterr error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
e.logf("Bringing wireguard device up...")
|
|
|
|
e.wgdev.Up()
|
|
|
|
e.wgdev.Up()
|
|
|
|
|
|
|
|
e.logf("Bringing router up...")
|
|
|
|
if err := e.router.Up(); err != nil {
|
|
|
|
if err := e.router.Up(); err != nil {
|
|
|
|
e.magicConn.Close()
|
|
|
|
e.magicConn.Close()
|
|
|
|
e.wgdev.Close()
|
|
|
|
e.wgdev.Close()
|
|
|
@ -354,17 +349,22 @@ func newUserspaceEngineAdvanced(conf EngineConfig) (_ Engine, reterr error) {
|
|
|
|
// a no-op settings here.
|
|
|
|
// a no-op settings here.
|
|
|
|
// TODO(bradfitz): counter-point: it tests the router implementation early
|
|
|
|
// TODO(bradfitz): counter-point: it tests the router implementation early
|
|
|
|
// to see if any part of it might fail.
|
|
|
|
// to see if any part of it might fail.
|
|
|
|
|
|
|
|
e.logf("Clearing router settings...")
|
|
|
|
if err := e.router.Set(nil); err != nil {
|
|
|
|
if err := e.router.Set(nil); err != nil {
|
|
|
|
e.magicConn.Close()
|
|
|
|
e.magicConn.Close()
|
|
|
|
e.wgdev.Close()
|
|
|
|
e.wgdev.Close()
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
e.logf("Starting link monitor...")
|
|
|
|
e.linkMon.Start()
|
|
|
|
e.linkMon.Start()
|
|
|
|
|
|
|
|
e.logf("Starting magicsock...")
|
|
|
|
e.magicConn.Start()
|
|
|
|
e.magicConn.Start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
e.logf("Starting resolver...")
|
|
|
|
e.resolver.Start()
|
|
|
|
e.resolver.Start()
|
|
|
|
go e.pollResolver()
|
|
|
|
go e.pollResolver()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
e.logf("Engine created.")
|
|
|
|
return e, nil
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|