Update TUN creation

This commit is contained in:
世界 2023-02-26 19:16:28 +08:00
parent a8f13bd956
commit 842730707c
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
14 changed files with 64 additions and 62 deletions

View file

@ -85,10 +85,10 @@ func buildiOS() {
} }
if !debugEnabled { if !debugEnabled {
args = append(args, args = append(args,
"-trimpath", "-ldflags=-s -w -buildid=", "-trimpath", "-ldflags=-s -w -buildid=", "-tags", "with_gvisor,with_clash_api",
) )
} else { } else {
args = append(args, "-tags", "debug") args = append(args, "-tags", "with_gvisor,with_clash_api,debug")
} }
args = append(args, "./experimental/libbox") args = append(args, "./experimental/libbox")

View file

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

View file

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

View file

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

View file

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

View file

@ -1,13 +1,13 @@
package libbox package libbox
import ( import (
"io" "net"
"net/netip" "net/netip"
"os"
"github.com/sagernet/sing-tun" "github.com/sagernet/sing-tun"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
) )
type TunOptions interface { type TunOptions interface {
@ -28,6 +28,16 @@ type RoutePrefix struct {
Prefix int32 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 { type RoutePrefixIterator interface {
Next() *RoutePrefix Next() *RoutePrefix
HasNext() bool HasNext() bool
@ -88,22 +98,3 @@ func (o *tunOptions) GetIncludePackage() StringIterator {
func (o *tunOptions) GetExcludePackage() StringIterator { func (o *tunOptions) GetExcludePackage() StringIterator {
return newIterator(o.ExcludePackage) 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,
})
}

2
go.mod
View file

@ -28,7 +28,7 @@ require (
github.com/sagernet/sing-dns v0.1.4 github.com/sagernet/sing-dns v0.1.4
github.com/sagernet/sing-shadowsocks v0.1.2-0.20230221080503-769c01d6bba9 github.com/sagernet/sing-shadowsocks v0.1.2-0.20230221080503-769c01d6bba9
github.com/sagernet/sing-shadowtls v0.0.0-20230221123345-78e50cd7b587 github.com/sagernet/sing-shadowtls v0.0.0-20230221123345-78e50cd7b587
github.com/sagernet/sing-tun v0.1.1 github.com/sagernet/sing-tun v0.1.2-0.20230226091124-0cdb0eed74d9
github.com/sagernet/sing-vmess v0.1.2 github.com/sagernet/sing-vmess v0.1.2
github.com/sagernet/smux v0.0.0-20220831015742-e0f1988e3195 github.com/sagernet/smux v0.0.0-20220831015742-e0f1988e3195
github.com/sagernet/tfo-go v0.0.0-20230207095944-549363a7327d github.com/sagernet/tfo-go v0.0.0-20230207095944-549363a7327d

4
go.sum
View file

@ -137,8 +137,8 @@ github.com/sagernet/sing-shadowsocks v0.1.2-0.20230221080503-769c01d6bba9 h1:qS3
github.com/sagernet/sing-shadowsocks v0.1.2-0.20230221080503-769c01d6bba9/go.mod h1:f3mHTy5shnVM9l8UocMlJgC/1G/zdj5FuEuVXhDinGU= github.com/sagernet/sing-shadowsocks v0.1.2-0.20230221080503-769c01d6bba9/go.mod h1:f3mHTy5shnVM9l8UocMlJgC/1G/zdj5FuEuVXhDinGU=
github.com/sagernet/sing-shadowtls v0.0.0-20230221123345-78e50cd7b587 h1:OjIXlHT2bblZfp+ciupM4xY9+Ccpj9FsuHRtKRBv+Pg= github.com/sagernet/sing-shadowtls v0.0.0-20230221123345-78e50cd7b587 h1:OjIXlHT2bblZfp+ciupM4xY9+Ccpj9FsuHRtKRBv+Pg=
github.com/sagernet/sing-shadowtls v0.0.0-20230221123345-78e50cd7b587/go.mod h1:Kn1VUIprdkwCgkS6SXYaLmIpKzQbqBIKJBMY+RvBhYc= github.com/sagernet/sing-shadowtls v0.0.0-20230221123345-78e50cd7b587/go.mod h1:Kn1VUIprdkwCgkS6SXYaLmIpKzQbqBIKJBMY+RvBhYc=
github.com/sagernet/sing-tun v0.1.1 h1:2Hg3GAyJWzQ7Ua1j74dE+mI06vaqSBO9yD4tkTjggn4= github.com/sagernet/sing-tun v0.1.2-0.20230226091124-0cdb0eed74d9 h1:tq1kc0HFj/jfhLfVC1NJI6lex2g6w2W+gVsFu6H2Kis=
github.com/sagernet/sing-tun v0.1.1/go.mod h1:WzW/SkT+Nh9uJn/FIYUE2YJHYuPwfbh8sATOzU9QDGw= github.com/sagernet/sing-tun v0.1.2-0.20230226091124-0cdb0eed74d9/go.mod h1:KnRkwaDHbb06zgeNPu0LQ8A+vA9myMxKEgHN1brCPHg=
github.com/sagernet/sing-vmess v0.1.2 h1:RbOZNAId2LrCai8epMoQXlf0XTrou0bfcw08hNBg6lM= github.com/sagernet/sing-vmess v0.1.2 h1:RbOZNAId2LrCai8epMoQXlf0XTrou0bfcw08hNBg6lM=
github.com/sagernet/sing-vmess v0.1.2/go.mod h1:9NSj8mZTx1JIY/HF9LaYRppUsVkysIN5tEFpNZujXxY= github.com/sagernet/sing-vmess v0.1.2/go.mod h1:9NSj8mZTx1JIY/HF9LaYRppUsVkysIN5tEFpNZujXxY=
github.com/sagernet/smux v0.0.0-20220831015742-e0f1988e3195 h1:5VBIbVw9q7aKbrFdT83mjkyvQ+VaRsQ6yflTepfln38= github.com/sagernet/smux v0.0.0-20220831015742-e0f1988e3195 h1:5VBIbVw9q7aKbrFdT83mjkyvQ+VaRsQ6yflTepfln38=

View file

@ -150,7 +150,7 @@ func (t *Tun) Start() error {
if t.platformInterface != nil { if t.platformInterface != nil {
tunInterface, err = t.platformInterface.OpenTun(t.tunOptions) tunInterface, err = t.platformInterface.OpenTun(t.tunOptions)
} else { } else {
tunInterface, err = tun.Open(t.tunOptions) tunInterface, err = tun.New(t.tunOptions)
} }
if err != nil { if err != nil {
return E.Cause(err, "configure tun interface") return E.Cause(err, "configure tun interface")

View file

@ -25,7 +25,7 @@ func NewFactory(formatter Formatter, writer io.Writer, platformWriter io.Writer)
formatter: formatter, formatter: formatter,
platformFormatter: Formatter{ platformFormatter: Formatter{
BaseTime: formatter.BaseTime, BaseTime: formatter.BaseTime,
DisableColors: C.IsIos, DisableColors: C.IsDarwin || C.IsIos,
}, },
writer: writer, writer: writer,
platformWriter: platformWriter, platformWriter: platformWriter,

View file

@ -6,6 +6,7 @@ import (
"os" "os"
"time" "time"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
F "github.com/sagernet/sing/common/format" F "github.com/sagernet/sing/common/format"
"github.com/sagernet/sing/common/observable" "github.com/sagernet/sing/common/observable"
@ -27,7 +28,8 @@ func NewObservableFactory(formatter Formatter, writer io.Writer, platformWriter
factory := &observableFactory{ factory := &observableFactory{
formatter: formatter, formatter: formatter,
platformFormatter: Formatter{ platformFormatter: Formatter{
BaseTime: formatter.BaseTime, BaseTime: formatter.BaseTime,
DisableColors: C.IsDarwin || C.IsIos,
}, },
writer: writer, writer: writer,
platformWriter: platformWriter, platformWriter: platformWriter,
@ -91,7 +93,7 @@ func (l *observableLogger) Log(ctx context.Context, level Level, args []any) {
} }
l.subscriber.Emit(Entry{level, messageSimple}) l.subscriber.Emit(Entry{level, messageSimple})
if l.platformWriter != nil { if l.platformWriter != nil {
l.platformWriter.Write([]byte(l.formatter.Format(ctx, level, l.tag, F.ToString(args...), nowTime))) l.platformWriter.Write([]byte(l.platformFormatter.Format(ctx, level, l.tag, F.ToString(args...), nowTime)))
} }
} }

View file

@ -46,7 +46,7 @@ func NewSystemDevice(router adapter.Router, interfaceName string, localPrefixes
if interfaceName == "" { if interfaceName == "" {
interfaceName = tun.CalculateInterfaceName("wg") interfaceName = tun.CalculateInterfaceName("wg")
} }
tunInterface, err := tun.Open(tun.Options{ tunInterface, err := tun.New(tun.Options{
Name: interfaceName, Name: interfaceName,
Inet4Address: inet4Addresses, Inet4Address: inet4Addresses,
Inet6Address: inet6Addresses, Inet6Address: inet6Addresses,