Skip to content

Commit

Permalink
Test disable firewall for interface
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed May 3, 2023
1 parent 209ec12 commit 6769085
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions system.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"net"
"net/netip"
"runtime"
"syscall"
"time"

Expand Down Expand Up @@ -97,6 +98,12 @@ func (s *System) Close() error {
}

func (s *System) Start() error {
if runtime.GOOS == "windows" {
err := fixFirewall()
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
25 changes: 25 additions & 0 deletions system_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package tun

import (
E "github.com/sagernet/sing/common/exceptions"
"os"

"github.com/sagernet/sing/common/shell"
)

func fixFirewall() error {
profiles := []string{"Public", "Private"}
for _, profile := range profiles {
output, err := shell.Exec("powershell.exe",
"New-NetFirewallRule", "-DisplayName", "sing-box: allow system tun stack for path "+os.Args[0],
"-Direction", "Inbound",
"-Program", os.Args[0],
"-Action", "Allow",
"-Profile", profile,
).CombinedOutput()
if err != nil {
return E.Extend(err, output)
}
}
return nil
}

0 comments on commit 6769085

Please sign in to comment.