mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-28 22:25:07 +03:00
Merge branch 'develop' into future
This commit is contained in:
commit
6ab0639b82
10 changed files with 94 additions and 8 deletions
|
@ -6,6 +6,8 @@ func (m *TunAdapter) _applyOption(opt SetupOption) {
|
|||
m.config.name = v
|
||||
case InterfaceMTU:
|
||||
m.config.mtu = v
|
||||
case FileDescriptor:
|
||||
m.config.fd = int32(v)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -15,6 +17,8 @@ type SetupOption interface {
|
|||
|
||||
type InterfaceName string
|
||||
type InterfaceMTU uint64
|
||||
type FileDescriptor int32
|
||||
|
||||
func (a InterfaceName) isSetupOption() {}
|
||||
func (a InterfaceMTU) isSetupOption() {}
|
||||
func (a InterfaceName) isSetupOption() {}
|
||||
func (a InterfaceMTU) isSetupOption() {}
|
||||
func (a FileDescriptor) isSetupOption() {}
|
||||
|
|
|
@ -44,6 +44,7 @@ type TunAdapter struct {
|
|||
isOpen bool
|
||||
isEnabled bool // Used by the writer to drop sessionTraffic if not enabled
|
||||
config struct {
|
||||
fd int32
|
||||
name InterfaceName
|
||||
mtu InterfaceMTU
|
||||
}
|
||||
|
@ -126,7 +127,13 @@ func (tun *TunAdapter) _start() error {
|
|||
if tun.rwc.MaxMTU() < mtu {
|
||||
mtu = tun.rwc.MaxMTU()
|
||||
}
|
||||
if err := tun.setup(string(tun.config.name), addr, mtu); err != nil {
|
||||
var err error
|
||||
if tun.config.fd > 0 {
|
||||
err = tun.setupFD(tun.config.fd, addr, mtu)
|
||||
} else {
|
||||
err = tun.setup(string(tun.config.name), addr, mtu)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tun.MTU() != mtu {
|
||||
|
|
|
@ -5,6 +5,7 @@ package tun
|
|||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -88,6 +89,11 @@ func (tun *TunAdapter) setup(ifname string, addr string, mtu uint64) error {
|
|||
return tun.setupAddress(addr)
|
||||
}
|
||||
|
||||
// Configures the "utun" adapter from an existing file descriptor.
|
||||
func (tun *TunAdapter) setupFD(fd int32, addr string, mtu uint64) error {
|
||||
return fmt.Errorf("setup via FD not supported on this platform")
|
||||
}
|
||||
|
||||
func (tun *TunAdapter) setupAddress(addr string) error {
|
||||
var sfd int
|
||||
var err error
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
//go:build !mobile
|
||||
// +build !mobile
|
||||
//go:build darwin || ios
|
||||
// +build darwin ios
|
||||
|
||||
package tun
|
||||
|
||||
|
@ -7,6 +7,7 @@ package tun
|
|||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unsafe"
|
||||
|
@ -34,6 +35,31 @@ func (tun *TunAdapter) setup(ifname string, addr string, mtu uint64) error {
|
|||
return tun.setupAddress(addr)
|
||||
}
|
||||
|
||||
// Configures the "utun" adapter from an existing file descriptor.
|
||||
func (tun *TunAdapter) setupFD(fd int32, addr string, mtu uint64) error {
|
||||
dfd, err := unix.Dup(int(fd))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = unix.SetNonblock(dfd, true)
|
||||
if err != nil {
|
||||
unix.Close(dfd)
|
||||
return err
|
||||
}
|
||||
iface, err := wgtun.CreateTUNFromFile(os.NewFile(uintptr(dfd), "/dev/tun"), 0)
|
||||
if err != nil {
|
||||
unix.Close(dfd)
|
||||
return err
|
||||
}
|
||||
tun.iface = iface
|
||||
if m, err := iface.MTU(); err == nil {
|
||||
tun.mtu = getSupportedMTU(uint64(m))
|
||||
} else {
|
||||
tun.mtu = 0
|
||||
}
|
||||
return nil // tun.setupAddress(addr)
|
||||
}
|
||||
|
||||
const (
|
||||
darwin_SIOCAIFADDR_IN6 = 2155899162 // netinet6/in6_var.h
|
||||
darwin_IN6_IFF_NODAD = 0x0020 // netinet6/in6_var.h
|
||||
|
|
|
@ -6,6 +6,8 @@ package tun
|
|||
// The linux platform specific tun parts
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/vishvananda/netlink"
|
||||
wgtun "golang.zx2c4.com/wireguard/tun"
|
||||
)
|
||||
|
@ -28,6 +30,11 @@ func (tun *TunAdapter) setup(ifname string, addr string, mtu uint64) error {
|
|||
return tun.setupAddress(addr)
|
||||
}
|
||||
|
||||
// Configures the "utun" adapter from an existing file descriptor.
|
||||
func (tun *TunAdapter) setupFD(fd int32, addr string, mtu uint64) error {
|
||||
return fmt.Errorf("setup via FD not supported on this platform")
|
||||
}
|
||||
|
||||
// Configures the TUN adapter with the correct IPv6 address and MTU. Netlink
|
||||
// is used to do this, so there is not a hard requirement on "ip" or "ifconfig"
|
||||
// to exist on the system, but this will fail if Netlink is not present in the
|
||||
|
|
|
@ -7,6 +7,8 @@ package tun
|
|||
// If your platform supports tun devices, you could try configuring it manually
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
wgtun "golang.zx2c4.com/wireguard/tun"
|
||||
)
|
||||
|
||||
|
@ -25,6 +27,11 @@ func (tun *TunAdapter) setup(ifname string, addr string, mtu uint64) error {
|
|||
return tun.setupAddress(addr)
|
||||
}
|
||||
|
||||
// Configures the "utun" adapter from an existing file descriptor.
|
||||
func (tun *TunAdapter) setupFD(fd int32, addr string, mtu uint64) error {
|
||||
return fmt.Errorf("setup via FD not supported on this platform")
|
||||
}
|
||||
|
||||
// We don't know how to set the IPv6 address on an unknown platform, therefore
|
||||
// write about it to stdout and don't try to do anything further.
|
||||
func (tun *TunAdapter) setupAddress(addr string) error {
|
||||
|
|
|
@ -6,6 +6,7 @@ package tun
|
|||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
|
||||
|
@ -50,6 +51,11 @@ func (tun *TunAdapter) setup(ifname string, addr string, mtu uint64) error {
|
|||
})
|
||||
}
|
||||
|
||||
// Configures the "utun" adapter from an existing file descriptor.
|
||||
func (tun *TunAdapter) setupFD(fd int32, addr string, mtu uint64) error {
|
||||
return fmt.Errorf("setup via FD not supported on this platform")
|
||||
}
|
||||
|
||||
// Sets the MTU of the TUN adapter.
|
||||
func (tun *TunAdapter) setupMTU(mtu uint64) error {
|
||||
if tun.iface == nil || tun.Name() == "" {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue