diff --git a/config/config.go b/config/config.go index ce1e0fb7..358e055a 100644 --- a/config/config.go +++ b/config/config.go @@ -279,6 +279,8 @@ type RawTun struct { IncludeUIDRange []string `yaml:"include-uid-range" json:"include-uid-range,omitempty"` ExcludeUID []uint32 `yaml:"exclude-uid" json:"exclude-uid,omitempty"` ExcludeUIDRange []string `yaml:"exclude-uid-range" json:"exclude-uid-range,omitempty"` + ExcludePort []uint16 `yaml:"exclude-port" json:"exclude-port,omitempty"` + ExcludePortRange []string `yaml:"exclude-port-range" json:"exclude-port-range,omitempty"` IncludeAndroidUser []int `yaml:"include-android-user" json:"include-android-user,omitempty"` IncludePackage []string `yaml:"include-package" json:"include-package,omitempty"` ExcludePackage []string `yaml:"exclude-package" json:"exclude-package,omitempty"` @@ -1560,6 +1562,8 @@ func parseTun(rawTun RawTun, general *General) error { IncludeUIDRange: rawTun.IncludeUIDRange, ExcludeUID: rawTun.ExcludeUID, ExcludeUIDRange: rawTun.ExcludeUIDRange, + ExcludePort: rawTun.ExcludePort, + ExcludePortRange: rawTun.ExcludePortRange, IncludeAndroidUser: rawTun.IncludeAndroidUser, IncludePackage: rawTun.IncludePackage, ExcludePackage: rawTun.ExcludePackage, diff --git a/go.mod b/go.mod index 74044708..8a6ff594 100644 --- a/go.mod +++ b/go.mod @@ -28,7 +28,7 @@ require ( github.com/metacubex/sing-quic v0.0.0-20250404030904-b2cc8aab562c github.com/metacubex/sing-shadowsocks v0.2.8 github.com/metacubex/sing-shadowsocks2 v0.2.2 - github.com/metacubex/sing-tun v0.4.6-0.20250402154347-cff06ec2a65b + github.com/metacubex/sing-tun v0.4.6-0.20250407014348-50cddb7347dc github.com/metacubex/sing-vmess v0.1.14-0.20250228002636-abc39e113b82 github.com/metacubex/sing-wireguard v0.0.0-20241126021510-0827d417b589 github.com/metacubex/tfo-go v0.0.0-20241231083714-66613d49c422 diff --git a/go.sum b/go.sum index df7b6028..a2c1ff6c 100644 --- a/go.sum +++ b/go.sum @@ -121,8 +121,8 @@ github.com/metacubex/sing-shadowsocks v0.2.8 h1:wIhlaigswzjPw4hej75sEvWte3QR0+AJ github.com/metacubex/sing-shadowsocks v0.2.8/go.mod h1:X3x88XtJpBxG0W0/ECOJL6Ib0SJ3xdniAkU/6/RMWU0= github.com/metacubex/sing-shadowsocks2 v0.2.2 h1:eaf42uVx4Lr21S6MDYs0ZdTvGA0GEhDpb9no4+gdXPo= github.com/metacubex/sing-shadowsocks2 v0.2.2/go.mod h1:BhOug03a/RbI7y6hp6q+6ITM1dXjnLTmeWBHSTwvv2Q= -github.com/metacubex/sing-tun v0.4.6-0.20250402154347-cff06ec2a65b h1:5WnSaOhE4xsNjn4SDIvjhULPF788y4amSeJT6vW7GGQ= -github.com/metacubex/sing-tun v0.4.6-0.20250402154347-cff06ec2a65b/go.mod h1:V0N4rr0dWPBEE20ESkTXdbtx2riQYcb6YtwC5w/9wl0= +github.com/metacubex/sing-tun v0.4.6-0.20250407014348-50cddb7347dc h1:Zn0Fst8C/uBJmwXArUMN7b9yQJXJY9S0nRJtTiYIq6Q= +github.com/metacubex/sing-tun v0.4.6-0.20250407014348-50cddb7347dc/go.mod h1:V0N4rr0dWPBEE20ESkTXdbtx2riQYcb6YtwC5w/9wl0= github.com/metacubex/sing-vmess v0.1.14-0.20250228002636-abc39e113b82 h1:zZp5uct9+/0Hb1jKGyqDjCU4/72t43rs7qOq3Rc9oU8= github.com/metacubex/sing-vmess v0.1.14-0.20250228002636-abc39e113b82/go.mod h1:nE7Mdzj/QUDwgRi/8BASPtsxtIFZTHA4Yst5GgwbGCQ= github.com/metacubex/sing-wireguard v0.0.0-20241126021510-0827d417b589 h1:Z6bNy0HLTjx6BKIkV48sV/yia/GP8Bnyb5JQuGgSGzg= diff --git a/listener/config/tun.go b/listener/config/tun.go index 3901bb1d..88e8116a 100644 --- a/listener/config/tun.go +++ b/listener/config/tun.go @@ -50,6 +50,8 @@ type Tun struct { IncludeUIDRange []string `yaml:"include-uid-range" json:"include-uid-range,omitempty"` ExcludeUID []uint32 `yaml:"exclude-uid" json:"exclude-uid,omitempty"` ExcludeUIDRange []string `yaml:"exclude-uid-range" json:"exclude-uid-range,omitempty"` + ExcludePort []uint16 `yaml:"exclude-port" json:"exclude-port,omitempty"` + ExcludePortRange []string `yaml:"exclude-port-range" json:"exclude-port-range,omitempty"` IncludeAndroidUser []int `yaml:"include-android-user" json:"include-android-user,omitempty"` IncludePackage []string `yaml:"include-package" json:"include-package,omitempty"` ExcludePackage []string `yaml:"exclude-package" json:"exclude-package,omitempty"` diff --git a/listener/inbound/tun.go b/listener/inbound/tun.go index 1b0a6a80..84eb7b11 100644 --- a/listener/inbound/tun.go +++ b/listener/inbound/tun.go @@ -39,6 +39,8 @@ type TunOption struct { IncludeUIDRange []string `inbound:"include-uid-range,omitempty"` ExcludeUID []uint32 `inbound:"exclude-uid,omitempty"` ExcludeUIDRange []string `inbound:"exclude-uid-range,omitempty"` + ExcludePort []uint16 `yaml:"exclude-port" json:"exclude-port,omitempty"` + ExcludePortRange []string `yaml:"exclude-port-range" json:"exclude-port-range,omitempty"` IncludeAndroidUser []int `inbound:"include-android-user,omitempty"` IncludePackage []string `inbound:"include-package,omitempty"` ExcludePackage []string `inbound:"exclude-package,omitempty"` @@ -137,6 +139,8 @@ func NewTun(options *TunOption) (*Tun, error) { IncludeUIDRange: options.IncludeUIDRange, ExcludeUID: options.ExcludeUID, ExcludeUIDRange: options.ExcludeUIDRange, + ExcludePort: options.ExcludePort, + ExcludePortRange: options.ExcludePortRange, IncludeAndroidUser: options.IncludeAndroidUser, IncludePackage: options.IncludePackage, ExcludePackage: options.ExcludePackage, diff --git a/listener/sing_tun/server.go b/listener/sing_tun/server.go index 0ab5afee..ab9476b6 100644 --- a/listener/sing_tun/server.go +++ b/listener/sing_tun/server.go @@ -21,6 +21,7 @@ import ( LC "github.com/metacubex/mihomo/listener/config" "github.com/metacubex/mihomo/listener/sing" "github.com/metacubex/mihomo/log" + "golang.org/x/exp/constraints" tun "github.com/metacubex/sing-tun" "github.com/metacubex/sing-tun/control" @@ -211,6 +212,14 @@ func New(options LC.Tun, tunnel C.Tunnel, additions ...inbound.Addition) (l *Lis return nil, E.Cause(err, "parse exclude_uid_range") } } + excludePort := uidToRange(options.ExcludePort) + if len(options.ExcludePortRange) > 0 { + var err error + excludePort, err = parseRange(excludePort, options.ExcludePortRange) + if err != nil { + return nil, E.Cause(err, "parse exclude_port_range") + } + } var dnsAdds []netip.AddrPort @@ -339,6 +348,7 @@ func New(options LC.Tun, tunnel C.Tunnel, additions ...inbound.Addition) (l *Lis ExcludeInterface: options.ExcludeInterface, IncludeUID: includeUID, ExcludeUID: excludeUID, + ExcludePort: excludePort, IncludeAndroidUser: options.IncludeAndroidUser, IncludePackage: options.IncludePackage, ExcludePackage: options.ExcludePackage, @@ -566,13 +576,13 @@ func (d *cDialerInterfaceFinder) FindInterfaceName(destination netip.Addr) strin return "" } -func uidToRange(uidList []uint32) []ranges.Range[uint32] { - return common.Map(uidList, func(uid uint32) ranges.Range[uint32] { +func uidToRange[T constraints.Integer](uidList []T) []ranges.Range[T] { + return common.Map(uidList, func(uid T) ranges.Range[T] { return ranges.NewSingle(uid) }) } -func parseRange(uidRanges []ranges.Range[uint32], rangeList []string) ([]ranges.Range[uint32], error) { +func parseRange[T constraints.Integer](uidRanges []ranges.Range[T], rangeList []string) ([]ranges.Range[T], error) { for _, uidRange := range rangeList { if !strings.Contains(uidRange, ":") { return nil, E.New("missing ':' in range: ", uidRange) @@ -593,7 +603,7 @@ func parseRange(uidRanges []ranges.Range[uint32], rangeList []string) ([]ranges. if err != nil { return nil, E.Cause(err, "parse range end") } - uidRanges = append(uidRanges, ranges.New(uint32(start), uint32(end))) + uidRanges = append(uidRanges, ranges.New(T(start), T(end))) } return uidRanges, nil }