feat: add exclude port and exclude port range options (#1951)

Fixes #1769
This commit is contained in:
okhowang 2025-04-08 12:10:30 +08:00 committed by GitHub
parent 9e8f4ada47
commit a22efd5c91
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 27 additions and 7 deletions

View file

@ -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,

2
go.mod
View file

@ -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

4
go.sum
View file

@ -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=

View file

@ -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"`

View file

@ -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,

View file

@ -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 "<invalid>"
}
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
}