Update TUN creation

This commit is contained in:
世界
2023-02-26 19:16:28 +08:00
parent a8f13bd956
commit 842730707c
14 changed files with 64 additions and 62 deletions

View File

@@ -1,4 +1,4 @@
//go:build ios
//go:build darwin
package libbox

View File

@@ -1,4 +1,4 @@
//go:build ios
//go:build darwin
package libbox

View File

@@ -2,7 +2,7 @@ package libbox
type PlatformInterface interface {
AutoDetectInterfaceControl(fd int32) error
OpenTun(options TunOptions) (TunInterface, error)
OpenTun(options TunOptions) (int32, error)
WriteLog(message string)
UseProcFS() bool
FindConnectionOwner(ipProtocol int32, sourceAddress string, sourcePort int32, destinationAddress string, destinationPort int32) (int32, error)

View File

@@ -3,7 +3,6 @@ package libbox
import (
"context"
"net/netip"
"os"
"runtime"
"syscall"
@@ -76,17 +75,12 @@ func (w *platformInterfaceWrapper) OpenTun(options tun.Options) (tun.Tun, error)
}
optionsWrapper := tunOptions(options)
tunInterface, err := w.iif.OpenTun(&optionsWrapper)
tunFd, err := w.iif.OpenTun(&optionsWrapper)
if err != nil {
return nil, err
}
tunFd := tunInterface.FileDescriptor()
return &nativeTun{
tunFd: int(tunFd),
tunFile: os.NewFile(uintptr(tunFd), "tun"),
tunMTU: options.MTU,
closer: tunInterface,
}, nil
options.FileDescriptor = int(tunFd)
return tun.New(options)
}
func (w *platformInterfaceWrapper) Write(p []byte) (n int, err error) {

View File

@@ -1,13 +1,13 @@
package libbox
import (
"io"
"net"
"net/netip"
"os"
"github.com/sagernet/sing-tun"
"github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
)
type TunOptions interface {
@@ -28,6 +28,16 @@ type RoutePrefix struct {
Prefix int32
}
func (p *RoutePrefix) Mask() string {
var bits int
if M.ParseSocksaddr(p.Address).Addr.Is6() {
bits = 128
} else {
bits = 32
}
return net.IP(net.CIDRMask(int(p.Prefix), bits)).String()
}
type RoutePrefixIterator interface {
Next() *RoutePrefix
HasNext() bool
@@ -88,22 +98,3 @@ func (o *tunOptions) GetIncludePackage() StringIterator {
func (o *tunOptions) GetExcludePackage() StringIterator {
return newIterator(o.ExcludePackage)
}
type nativeTun struct {
tunFd int
tunFile *os.File
tunMTU uint32
closer io.Closer
}
func (t *nativeTun) Read(p []byte) (n int, err error) {
return t.tunFile.Read(p)
}
func (t *nativeTun) Write(p []byte) (n int, err error) {
return t.tunFile.Write(p)
}
func (t *nativeTun) Close() error {
return t.closer.Close()
}

View File

@@ -0,0 +1,34 @@
package libbox
import (
"golang.org/x/sys/unix"
)
// kanged from wireauard-apple
const utunControlName = "com.apple.net.utun_control"
func GetTunnelFileDescriptor() int32 {
ctlInfo := &unix.CtlInfo{}
copy(ctlInfo.Name[:], utunControlName)
for fd := 0; fd < 1024; fd++ {
addr, err := unix.Getpeername(fd)
if err != nil {
continue
}
addrCTL, loaded := addr.(*unix.SockaddrCtl)
if !loaded {
continue
}
if ctlInfo.Id == 0 {
err = unix.IoctlCtlInfo(fd, ctlInfo)
if err != nil {
continue
}
}
if addrCTL.ID == ctlInfo.Id {
return int32(fd)
}
}
return -1
}

View File

@@ -1,19 +0,0 @@
//go:build with_gvisor && linux
package libbox
import (
"github.com/sagernet/sing-tun"
"gvisor.dev/gvisor/pkg/tcpip/link/fdbased"
"gvisor.dev/gvisor/pkg/tcpip/stack"
)
var _ tun.GVisorTun = (*nativeTun)(nil)
func (t *nativeTun) NewEndpoint() (stack.LinkEndpoint, error) {
return fdbased.New(&fdbased.Options{
FDs: []int{t.tunFd},
MTU: t.tunMTU,
})
}