Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/SagerNet/sing-tun into meta
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed May 9, 2023
2 parents d390b53 + 91df97a commit 30065d4
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 29 deletions.
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
build:
GOOS=darwin GOARCH=arm64 go build -v -tags with_gvisor .
GOOS=ios GOARCH=arm64 go build -v -tags with_gvisor .
GOOS=linux GOARCH=amd64 go build -v -tags with_gvisor .
GOOS=linux GOARCH=arm64 go build -v -tags with_gvisor .
GOOS=linux GOARCH=386 go build -v -tags with_gvisor .
GOOS=linux GOARCH=arm go build -v -tags with_gvisor .
GOOS=windows GOARCH=amd64 go build -v -tags with_gvisor .

fmt:
@gofumpt -l -w .
@gofmt -s -w .
Expand Down
3 changes: 2 additions & 1 deletion monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ type DefaultInterfaceMonitor interface {
}

type DefaultInterfaceMonitorOptions struct {
OverrideAndroidVPN bool
OverrideAndroidVPN bool
UnderNetworkExtension bool
}
11 changes: 8 additions & 3 deletions monitor_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,16 @@ func (m *defaultInterfaceMonitor) checkUpdate() error {
break
}
if defaultInterface == nil {
defaultInterface, err = getDefaultInterfaceBySocket()
if err != nil {
return err
if m.options.UnderNetworkExtension {
defaultInterface, err = getDefaultInterfaceBySocket()
if err != nil {
return err
}
}
}
if defaultInterface == nil {
return ErrNoRoute
}
oldInterface := m.defaultInterfaceName
oldIndex := m.defaultInterfaceIndex
m.defaultInterfaceIndex = defaultInterface.Index
Expand Down
27 changes: 14 additions & 13 deletions stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@ type Stack interface {
}

type StackOptions struct {
Context context.Context
Tun Tun
Name string
MTU uint32
Inet4Address []netip.Prefix
Inet6Address []netip.Prefix
EndpointIndependentNat bool
UDPTimeout int64
Router Router
Handler Handler
Logger logger.Logger
ForwarderBindInterface bool
InterfaceFinder control.InterfaceFinder
Context context.Context
Tun Tun
Name string
MTU uint32
Inet4Address []netip.Prefix
Inet6Address []netip.Prefix
EndpointIndependentNat bool
UDPTimeout int64
Router Router
Handler Handler
Logger logger.Logger
ForwarderBindInterface bool
InterfaceFinder control.InterfaceFinder
ExperimentalFixWindowsFirewall bool
}

func NewStack(
Expand Down
32 changes: 20 additions & 12 deletions system.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type System struct {
routeMapping *RouteMapping
bindInterface bool
interfaceFinder control.InterfaceFinder
fixWindowsFirewall bool
}

type Session struct {
Expand All @@ -53,18 +54,19 @@ type Session struct {

func NewSystem(options StackOptions) (Stack, error) {
stack := &System{
ctx: options.Context,
tun: options.Tun,
tunName: options.Name,
mtu: options.MTU,
udpTimeout: options.UDPTimeout,
router: options.Router,
handler: options.Handler,
logger: options.Logger,
inet4Prefixes: options.Inet4Address,
inet6Prefixes: options.Inet6Address,
bindInterface: options.ForwarderBindInterface,
interfaceFinder: options.InterfaceFinder,
ctx: options.Context,
tun: options.Tun,
tunName: options.Name,
mtu: options.MTU,
udpTimeout: options.UDPTimeout,
router: options.Router,
handler: options.Handler,
logger: options.Logger,
inet4Prefixes: options.Inet4Address,
inet6Prefixes: options.Inet6Address,
bindInterface: options.ForwarderBindInterface,
interfaceFinder: options.InterfaceFinder,
fixWindowsFirewall: options.ExperimentalFixWindowsFirewall,
}
if stack.router != nil {
stack.routeMapping = NewRouteMapping(options.UDPTimeout)
Expand Down Expand Up @@ -97,6 +99,12 @@ func (s *System) Close() error {
}

func (s *System) Start() error {
if s.fixWindowsFirewall {
err := fixWindowsFirewall()
if err != nil {
return E.Cause(err, "fix windows firewall for system stack")
}
}
var listener net.ListenConfig
if s.bindInterface {
listener.Control = control.Append(listener.Control, func(network, address string, conn syscall.RawConn) error {
Expand Down
7 changes: 7 additions & 0 deletions system_nonwindows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build !windows

package tun

func fixWindowsFirewall() error {
return nil
}
47 changes: 47 additions & 0 deletions system_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package tun

import (
"os"
"os/exec"
"path/filepath"

E "github.com/sagernet/sing/common/exceptions"
F "github.com/sagernet/sing/common/format"
"github.com/sagernet/sing/common/shell"
)

func fixWindowsFirewall() error {
const shellStringSplit = "\""
isPWSH := true
powershell, err := exec.LookPath("pwsh.exe")
if err != nil {
powershell, err = exec.LookPath("powershell.exe")
isPWSH = false
}
if err != nil {
return nil
}
ruleName := "sing-tun rule for " + os.Args[0]
commandPrefix := []string{"-NoProfile", "-NonInteractive"}
if isPWSH {
commandPrefix = append(commandPrefix, "-Command")
}
err = shell.Exec(powershell, append(commandPrefix,
F.ToString("Get-NetFirewallRule -Name ", shellStringSplit, ruleName, shellStringSplit))...).Run()
if err == nil {
return nil
}
fileName := filepath.Base(os.Args[0])
output, err := shell.Exec(powershell, append(commandPrefix,
F.ToString("New-NetFirewallRule",
" -Name ", shellStringSplit, ruleName, shellStringSplit,
" -DisplayName ", shellStringSplit, "sing-tun (", fileName, ")", shellStringSplit,
" -Program ", shellStringSplit, os.Args[0], shellStringSplit,
" -Direction Inbound",
" -Protocol TCP",
" -Action Allow"))...).Read()
if err != nil {
return E.Extend(err, output)
}
return nil
}

0 comments on commit 30065d4

Please sign in to comment.