Make Interface.Inside an interface type (#252)

This commit updates the Interface.Inside type to be a new interface
type instead of a *Tun. This will allow for an inside interface
that does not use a tun device, such as a single-binary client that
can run without elevated privileges.
This commit is contained in:
forfuncsake 2020-07-28 22:53:16 +10:00 committed by GitHub
parent 4756c9613d
commit 9b06748506
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 63 additions and 6 deletions

View File

@ -2,6 +2,8 @@ package nebula
import (
"errors"
"io"
"net"
"os"
"time"
@ -10,10 +12,18 @@ import (
const mtu = 9001
type Inside interface {
io.ReadWriteCloser
Activate() error
CidrNet() *net.IPNet
DeviceName() string
WriteRaw([]byte) error
}
type InterfaceConfig struct {
HostMap *HostMap
Outside *udpConn
Inside *Tun
Inside Inside
certState *CertState
Cipher string
Firewall *Firewall
@ -31,7 +41,7 @@ type InterfaceConfig struct {
type Interface struct {
hostMap *HostMap
outside *udpConn
inside *Tun
inside Inside
certState *CertState
cipher string
firewall *Firewall
@ -101,7 +111,7 @@ func (f *Interface) Run(tunRoutines, udpRoutines int, buildVersion string) {
l.WithError(err).Error("Failed to get udp listen address")
}
l.WithField("interface", f.inside.Device).WithField("network", f.inside.Cidr.String()).
l.WithField("interface", f.inside.DeviceName()).WithField("network", f.inside.CidrNet().String()).
WithField("build", buildVersion).WithField("udpAddr", addr).
Info("Nebula interface is active")

View File

@ -27,7 +27,7 @@ func newTunFromFd(deviceFd int, cidr *net.IPNet, defaultMTU int, routes []route,
ifce = &Tun{
ReadWriteCloser: file,
fd: int(file.Fd()),
Device: "tun0",
Device: "android",
Cidr: cidr,
DefaultMTU: defaultMTU,
TXQueueLen: txQueueLen,
@ -64,6 +64,13 @@ func (c *Tun) WriteRaw(b []byte) error {
}
func (c Tun) Activate() error {
c.Device = "android"
return nil
}
func (c *Tun) CidrNet() *net.IPNet {
return c.Cidr
}
func (c *Tun) DeviceName() string {
return c.Device
}

View File

@ -68,6 +68,14 @@ func (c *Tun) Activate() error {
return nil
}
func (c *Tun) CidrNet() *net.IPNet {
return c.Cidr
}
func (c *Tun) DeviceName() string {
return c.Device
}
func (c *Tun) WriteRaw(b []byte) error {
_, err := c.Write(b)
return err

View File

@ -75,6 +75,14 @@ func (c *Tun) Activate() error {
return nil
}
func (c *Tun) CidrNet() *net.IPNet {
return c.Cidr
}
func (c *Tun) DeviceName() string {
return c.Device
}
func (c *Tun) WriteRaw(b []byte) error {
_, err := c.Write(b)
return err

View File

@ -30,13 +30,13 @@ func newTunFromFd(deviceFd int, cidr *net.IPNet, defaultMTU int, routes []route,
file := os.NewFile(uintptr(deviceFd), "/dev/tun")
ifce = &Tun{
Cidr: cidr,
Device: "iOS",
ReadWriteCloser: &tunReadCloser{f: file},
}
return
}
func (c *Tun) Activate() error {
c.Device = "iOS"
return nil
}
@ -103,3 +103,11 @@ func (t *tunReadCloser) Write(from []byte) (int, error) {
func (t *tunReadCloser) Close() error {
return t.f.Close()
}
func (c *Tun) CidrNet() *net.IPNet {
return c.Cidr
}
func (c *Tun) DeviceName() string {
return c.Device
}

View File

@ -288,6 +288,14 @@ func (c Tun) Activate() error {
return nil
}
func (c *Tun) CidrNet() *net.IPNet {
return c.Cidr
}
func (c *Tun) DeviceName() string {
return c.Device
}
func (c Tun) advMSS(r route) int {
mtu := r.mtu
if r.mtu == 0 {

View File

@ -88,6 +88,14 @@ func (c *Tun) Activate() error {
return nil
}
func (c *Tun) CidrNet() *net.IPNet {
return c.Cidr
}
func (c *Tun) DeviceName() string {
return c.Device
}
func (c *Tun) WriteRaw(b []byte) error {
_, err := c.Write(b)
return err