mirror of
https://github.com/cwinfo/yggdrasil-go.git
synced 2024-12-23 00:35:39 +00:00
Merge branch 'develop' into future
This commit is contained in:
commit
6ab0639b82
@ -37,7 +37,7 @@ if [ $IOS ]; then
|
|||||||
echo "Building framework for iOS"
|
echo "Building framework for iOS"
|
||||||
go get golang.org/x/mobile/bind
|
go get golang.org/x/mobile/bind
|
||||||
gomobile bind \
|
gomobile bind \
|
||||||
-target ios -tags mobile -o Yggdrasil.xcframework \
|
-target ios,macos -tags mobile -o Yggdrasil.xcframework \
|
||||||
-ldflags="$LDFLAGS $STRIP" -gcflags="$GCFLAGS" \
|
-ldflags="$LDFLAGS $STRIP" -gcflags="$GCFLAGS" \
|
||||||
./contrib/mobile ./src/config;
|
./contrib/mobile ./src/config;
|
||||||
fi
|
fi
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net"
|
"net"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"runtime/debug"
|
||||||
|
|
||||||
"github.com/gologme/log"
|
"github.com/gologme/log"
|
||||||
|
|
||||||
@ -13,6 +14,7 @@ import (
|
|||||||
"github.com/yggdrasil-network/yggdrasil-go/src/core"
|
"github.com/yggdrasil-network/yggdrasil-go/src/core"
|
||||||
"github.com/yggdrasil-network/yggdrasil-go/src/ipv6rwc"
|
"github.com/yggdrasil-network/yggdrasil-go/src/ipv6rwc"
|
||||||
"github.com/yggdrasil-network/yggdrasil-go/src/multicast"
|
"github.com/yggdrasil-network/yggdrasil-go/src/multicast"
|
||||||
|
"github.com/yggdrasil-network/yggdrasil-go/src/tun"
|
||||||
"github.com/yggdrasil-network/yggdrasil-go/src/version"
|
"github.com/yggdrasil-network/yggdrasil-go/src/version"
|
||||||
|
|
||||||
_ "golang.org/x/mobile/bind"
|
_ "golang.org/x/mobile/bind"
|
||||||
@ -28,7 +30,9 @@ type Yggdrasil struct {
|
|||||||
iprwc *ipv6rwc.ReadWriteCloser
|
iprwc *ipv6rwc.ReadWriteCloser
|
||||||
config *config.NodeConfig
|
config *config.NodeConfig
|
||||||
multicast *multicast.Multicast
|
multicast *multicast.Multicast
|
||||||
|
tun *tun.TunAdapter // optional
|
||||||
log MobileLogger
|
log MobileLogger
|
||||||
|
logger *log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// StartAutoconfigure starts a node with a randomly generated config
|
// StartAutoconfigure starts a node with a randomly generated config
|
||||||
@ -39,6 +43,8 @@ func (m *Yggdrasil) StartAutoconfigure() error {
|
|||||||
// StartJSON starts a node with the given JSON config. You can get JSON config
|
// StartJSON starts a node with the given JSON config. You can get JSON config
|
||||||
// (rather than HJSON) by using the GenerateConfigJSON() function
|
// (rather than HJSON) by using the GenerateConfigJSON() function
|
||||||
func (m *Yggdrasil) StartJSON(configjson []byte) error {
|
func (m *Yggdrasil) StartJSON(configjson []byte) error {
|
||||||
|
debug.SetMemoryLimit(1024 * 1024 * 40)
|
||||||
|
|
||||||
logger := log.New(m.log, "", 0)
|
logger := log.New(m.log, "", 0)
|
||||||
logger.EnableLevel("error")
|
logger.EnableLevel("error")
|
||||||
logger.EnableLevel("warn")
|
logger.EnableLevel("warn")
|
||||||
@ -88,9 +94,9 @@ func (m *Yggdrasil) StartJSON(configjson []byte) error {
|
|||||||
Priority: uint8(intf.Priority),
|
Priority: uint8(intf.Priority),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
m.multicast, err = multicast.New(m.core, logger, options...)
|
m.multicast, err = multicast.New(m.core, m.logger, options...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorln("An error occurred starting multicast:", err)
|
m.logger.Errorln("An error occurred starting multicast:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,6 +159,11 @@ func (m *Yggdrasil) Stop() error {
|
|||||||
if err := m.multicast.Stop(); err != nil {
|
if err := m.multicast.Stop(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if m.tun != nil {
|
||||||
|
if err := m.tun.Stop(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
m.core.Stop()
|
m.core.Stop()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,8 @@ void Log(const char *text) {
|
|||||||
import "C"
|
import "C"
|
||||||
import (
|
import (
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
|
"github.com/yggdrasil-network/yggdrasil-go/src/tun"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MobileLogger struct {
|
type MobileLogger struct {
|
||||||
@ -26,3 +28,13 @@ func (nsl MobileLogger) Write(p []byte) (n int, err error) {
|
|||||||
C.Log(cstr)
|
C.Log(cstr)
|
||||||
return len(p), nil
|
return len(p), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Yggdrasil) TakeOverTUN(fd int32) error {
|
||||||
|
options := []tun.SetupOption{
|
||||||
|
tun.FileDescriptor(fd),
|
||||||
|
tun.InterfaceMTU(m.iprwc.MTU()),
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
m.tun, err = tun.New(m.iprwc, m.logger, options...)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
@ -6,6 +6,8 @@ func (m *TunAdapter) _applyOption(opt SetupOption) {
|
|||||||
m.config.name = v
|
m.config.name = v
|
||||||
case InterfaceMTU:
|
case InterfaceMTU:
|
||||||
m.config.mtu = v
|
m.config.mtu = v
|
||||||
|
case FileDescriptor:
|
||||||
|
m.config.fd = int32(v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -15,6 +17,8 @@ type SetupOption interface {
|
|||||||
|
|
||||||
type InterfaceName string
|
type InterfaceName string
|
||||||
type InterfaceMTU uint64
|
type InterfaceMTU uint64
|
||||||
|
type FileDescriptor int32
|
||||||
|
|
||||||
func (a InterfaceName) isSetupOption() {}
|
func (a InterfaceName) isSetupOption() {}
|
||||||
func (a InterfaceMTU) isSetupOption() {}
|
func (a InterfaceMTU) isSetupOption() {}
|
||||||
|
func (a FileDescriptor) isSetupOption() {}
|
||||||
|
@ -44,6 +44,7 @@ type TunAdapter struct {
|
|||||||
isOpen bool
|
isOpen bool
|
||||||
isEnabled bool // Used by the writer to drop sessionTraffic if not enabled
|
isEnabled bool // Used by the writer to drop sessionTraffic if not enabled
|
||||||
config struct {
|
config struct {
|
||||||
|
fd int32
|
||||||
name InterfaceName
|
name InterfaceName
|
||||||
mtu InterfaceMTU
|
mtu InterfaceMTU
|
||||||
}
|
}
|
||||||
@ -126,7 +127,13 @@ func (tun *TunAdapter) _start() error {
|
|||||||
if tun.rwc.MaxMTU() < mtu {
|
if tun.rwc.MaxMTU() < mtu {
|
||||||
mtu = tun.rwc.MaxMTU()
|
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
|
return err
|
||||||
}
|
}
|
||||||
if tun.MTU() != mtu {
|
if tun.MTU() != mtu {
|
||||||
|
@ -5,6 +5,7 @@ package tun
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"fmt"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -88,6 +89,11 @@ func (tun *TunAdapter) setup(ifname string, addr string, mtu uint64) error {
|
|||||||
return tun.setupAddress(addr)
|
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 {
|
func (tun *TunAdapter) setupAddress(addr string) error {
|
||||||
var sfd int
|
var sfd int
|
||||||
var err error
|
var err error
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
//go:build !mobile
|
//go:build darwin || ios
|
||||||
// +build !mobile
|
// +build darwin ios
|
||||||
|
|
||||||
package tun
|
package tun
|
||||||
|
|
||||||
@ -7,6 +7,7 @@ package tun
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
@ -34,6 +35,31 @@ func (tun *TunAdapter) setup(ifname string, addr string, mtu uint64) error {
|
|||||||
return tun.setupAddress(addr)
|
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 (
|
const (
|
||||||
darwin_SIOCAIFADDR_IN6 = 2155899162 // netinet6/in6_var.h
|
darwin_SIOCAIFADDR_IN6 = 2155899162 // netinet6/in6_var.h
|
||||||
darwin_IN6_IFF_NODAD = 0x0020 // 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
|
// The linux platform specific tun parts
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/vishvananda/netlink"
|
"github.com/vishvananda/netlink"
|
||||||
wgtun "golang.zx2c4.com/wireguard/tun"
|
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)
|
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
|
// 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"
|
// 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
|
// 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
|
// If your platform supports tun devices, you could try configuring it manually
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
wgtun "golang.zx2c4.com/wireguard/tun"
|
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)
|
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
|
// 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.
|
// write about it to stdout and don't try to do anything further.
|
||||||
func (tun *TunAdapter) setupAddress(addr string) error {
|
func (tun *TunAdapter) setupAddress(addr string) error {
|
||||||
|
@ -6,6 +6,7 @@ package tun
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"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.
|
// Sets the MTU of the TUN adapter.
|
||||||
func (tun *TunAdapter) setupMTU(mtu uint64) error {
|
func (tun *TunAdapter) setupMTU(mtu uint64) error {
|
||||||
if tun.iface == nil || tun.Name() == "" {
|
if tun.iface == nil || tun.Name() == "" {
|
||||||
|
Loading…
Reference in New Issue
Block a user