Fix system stack for ios

This commit is contained in:
世界 2023-04-17 20:01:56 +08:00
parent 499c0aed67
commit d880656b52
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
5 changed files with 13 additions and 21 deletions

View file

@ -2,7 +2,7 @@
Simple transparent proxy library. Simple transparent proxy library.
For Linux, Windows and macOS. For Linux, Windows, macOS and iOS.
## License ## License

View file

@ -34,7 +34,7 @@ func NewStack(
) (Stack, error) { ) (Stack, error) {
switch stack { switch stack {
case "": case "":
return defaultStack(options) return NewSystem(options)
case "gvisor": case "gvisor":
return NewGVisor(options) return NewGVisor(options)
case "system": case "system":

View file

@ -1,7 +0,0 @@
//go:build !darwin
package tun
func defaultStack(options StackOptions) (Stack, error) {
return NewSystem(options)
}

View file

@ -1,10 +0,0 @@
package tun
func defaultStack(options StackOptions) (Stack, error) {
if options.UnderPlatform {
// Apple Network Extension conflicts with system stack.
return NewGVisor(options)
} else {
return NewSystem(options)
}
}

View file

@ -10,6 +10,7 @@ import (
"github.com/sagernet/sing-tun/internal/clashtcpip" "github.com/sagernet/sing-tun/internal/clashtcpip"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf" "github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/control"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger" "github.com/sagernet/sing/common/logger"
M "github.com/sagernet/sing/common/metadata" M "github.com/sagernet/sing/common/metadata"
@ -20,6 +21,7 @@ import (
type System struct { type System struct {
ctx context.Context ctx context.Context
tun Tun tun Tun
tunName string
mtu uint32 mtu uint32
router Router router Router
handler Handler handler Handler
@ -38,6 +40,7 @@ type System struct {
tcpNat *TCPNat tcpNat *TCPNat
udpNat *udpnat.Service[netip.AddrPort] udpNat *udpnat.Service[netip.AddrPort]
routeMapping *RouteMapping routeMapping *RouteMapping
underPlatform bool
} }
type Session struct { type Session struct {
@ -51,6 +54,7 @@ func NewSystem(options StackOptions) (Stack, error) {
stack := &System{ stack := &System{
ctx: options.Context, ctx: options.Context,
tun: options.Tun, tun: options.Tun,
tunName: options.Name,
mtu: options.MTU, mtu: options.MTU,
udpTimeout: options.UDPTimeout, udpTimeout: options.UDPTimeout,
router: options.Router, router: options.Router,
@ -58,6 +62,7 @@ func NewSystem(options StackOptions) (Stack, error) {
logger: options.Logger, logger: options.Logger,
inet4Prefixes: options.Inet4Address, inet4Prefixes: options.Inet4Address,
inet6Prefixes: options.Inet6Address, inet6Prefixes: options.Inet6Address,
underPlatform: options.UnderPlatform,
routeMapping: NewRouteMapping(options.UDPTimeout), routeMapping: NewRouteMapping(options.UDPTimeout),
} }
if len(options.Inet4Address) > 0 { if len(options.Inet4Address) > 0 {
@ -88,8 +93,12 @@ func (s *System) Close() error {
} }
func (s *System) Start() error { func (s *System) Start() error {
var listener net.ListenConfig
if s.underPlatform {
listener.Control = control.Append(listener.Control, control.BindToInterface(control.DefaultInterfaceFinder(), s.tunName, -1))
}
if s.inet4Address.IsValid() { if s.inet4Address.IsValid() {
tcpListener, err := net.Listen("tcp4", net.JoinHostPort(s.inet4ServerAddress.String(), "0")) tcpListener, err := listener.Listen(s.ctx, "tcp4", net.JoinHostPort(s.inet4ServerAddress.String(), "0"))
if err != nil { if err != nil {
return err return err
} }
@ -98,7 +107,7 @@ func (s *System) Start() error {
go s.acceptLoop(tcpListener) go s.acceptLoop(tcpListener)
} }
if s.inet6Address.IsValid() { if s.inet6Address.IsValid() {
tcpListener, err := net.Listen("tcp6", net.JoinHostPort(s.inet6ServerAddress.String(), "0")) tcpListener, err := listener.Listen(s.ctx, "tcp6", net.JoinHostPort(s.inet6ServerAddress.String(), "0"))
if err != nil { if err != nil {
return err return err
} }