소스 검색

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.
forfuncsake 5 년 전
부모
커밋
9b06748506
7개의 변경된 파일63개의 추가작업 그리고 6개의 파일을 삭제
  1. 13 3
      interface.go
  2. 9 2
      tun_android.go
  3. 8 0
      tun_darwin.go
  4. 8 0
      tun_freebsd.go
  5. 9 1
      tun_ios.go
  6. 8 0
      tun_linux.go
  7. 8 0
      tun_windows.go

+ 13 - 3
interface.go

@@ -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")
 

+ 9 - 2
tun_android.go

@@ -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
+}

+ 8 - 0
tun_darwin.go

@@ -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

+ 8 - 0
tun_freebsd.go

@@ -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

+ 9 - 1
tun_ios.go

@@ -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
+}

+ 8 - 0
tun_linux.go

@@ -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 {

+ 8 - 0
tun_windows.go

@@ -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