fix Windows firewall rules and allow to disable firewall manipulation

This commit is contained in:
dyhkwong 2025-03-04 15:04:28 +08:00
parent c8c2984261
commit e94baeac0c
3 changed files with 34 additions and 18 deletions

View file

@ -46,6 +46,7 @@ type System struct {
interfaceFinder control.InterfaceFinder
frontHeadroom int
txChecksumOffload bool
disableFirewallFix bool
}
type Session struct {
@ -57,18 +58,19 @@ type Session struct {
func NewSystem(options StackOptions) (Stack, error) {
stack := &System{
ctx: options.Context,
tun: options.Tun,
tunName: options.TunOptions.Name,
mtu: int(options.TunOptions.MTU),
udpTimeout: options.UDPTimeout,
handler: options.Handler,
logger: options.Logger,
inet4Prefixes: options.TunOptions.Inet4Address,
inet6Prefixes: options.TunOptions.Inet6Address,
broadcastAddr: BroadcastAddr(options.TunOptions.Inet4Address),
bindInterface: options.ForwarderBindInterface,
interfaceFinder: options.InterfaceFinder,
ctx: options.Context,
tun: options.Tun,
tunName: options.TunOptions.Name,
mtu: int(options.TunOptions.MTU),
udpTimeout: options.UDPTimeout,
handler: options.Handler,
logger: options.Logger,
inet4Prefixes: options.TunOptions.Inet4Address,
inet6Prefixes: options.TunOptions.Inet6Address,
broadcastAddr: BroadcastAddr(options.TunOptions.Inet4Address),
bindInterface: options.ForwarderBindInterface,
interfaceFinder: options.InterfaceFinder,
disableFirewallFix: options.TunOptions.EXP_DisableFirewallFix,
}
if len(options.TunOptions.Inet4Address) > 0 {
if !HasNextAddress(options.TunOptions.Inet4Address[0], 1) {
@ -107,9 +109,12 @@ func (s *System) Start() error {
}
func (s *System) start() error {
err := fixWindowsFirewall()
if err != nil {
return E.Cause(err, "fix windows firewall for system stack")
var err error
if !s.disableFirewallFix {
err = fixWindowsFirewall()
if err != nil {
s.logger.Error(E.Cause(err, "fix windows firewall for system stack"))
}
}
var listener net.ListenConfig
if s.bindInterface {

View file

@ -15,15 +15,25 @@ func fixWindowsFirewall() error {
if err != nil {
return err
}
rule := winfw.FWRule{
_, err = winfw.FirewallRuleAddAdvanced(winfw.FWRule{
Name: "sing-tun (" + absPath + ")",
ApplicationName: absPath,
Enabled: true,
Protocol: winfw.NET_FW_IP_PROTOCOL_TCP,
Direction: winfw.NET_FW_RULE_DIR_IN,
Action: winfw.NET_FW_ACTION_ALLOW,
})
if err != nil {
return err
}
_, err = winfw.FirewallRuleAddAdvanced(rule)
_, err = winfw.FirewallRuleAddAdvanced(winfw.FWRule{
Name: "sing-tun UDP (" + absPath + ")",
ApplicationName: absPath,
Enabled: true,
Protocol: winfw.NET_FW_IP_PROTOCOL_UDP,
Direction: winfw.NET_FW_RULE_DIR_IN,
Action: winfw.NET_FW_ACTION_ALLOW,
})
return err
}

3
tun.go
View file

@ -87,7 +87,8 @@ type Options struct {
_TXChecksumOffload bool
// For library usages.
EXP_DisableDNSHijack bool
EXP_DisableDNSHijack bool
EXP_DisableFirewallFix bool
}
func (o *Options) Inet4GatewayAddr() netip.Addr {