Skip to content

Commit a22efd5

Browse files
authored
feat: add exclude port and exclude port range options (#1951)
Fixes #1769
1 parent 9e8f4ad commit a22efd5

File tree

6 files changed

+27
-7
lines changed

6 files changed

+27
-7
lines changed

config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ type RawTun struct {
279279
IncludeUIDRange []string `yaml:"include-uid-range" json:"include-uid-range,omitempty"`
280280
ExcludeUID []uint32 `yaml:"exclude-uid" json:"exclude-uid,omitempty"`
281281
ExcludeUIDRange []string `yaml:"exclude-uid-range" json:"exclude-uid-range,omitempty"`
282+
ExcludePort []uint16 `yaml:"exclude-port" json:"exclude-port,omitempty"`
283+
ExcludePortRange []string `yaml:"exclude-port-range" json:"exclude-port-range,omitempty"`
282284
IncludeAndroidUser []int `yaml:"include-android-user" json:"include-android-user,omitempty"`
283285
IncludePackage []string `yaml:"include-package" json:"include-package,omitempty"`
284286
ExcludePackage []string `yaml:"exclude-package" json:"exclude-package,omitempty"`
@@ -1560,6 +1562,8 @@ func parseTun(rawTun RawTun, general *General) error {
15601562
IncludeUIDRange: rawTun.IncludeUIDRange,
15611563
ExcludeUID: rawTun.ExcludeUID,
15621564
ExcludeUIDRange: rawTun.ExcludeUIDRange,
1565+
ExcludePort: rawTun.ExcludePort,
1566+
ExcludePortRange: rawTun.ExcludePortRange,
15631567
IncludeAndroidUser: rawTun.IncludeAndroidUser,
15641568
IncludePackage: rawTun.IncludePackage,
15651569
ExcludePackage: rawTun.ExcludePackage,

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ require (
2828
github.com/metacubex/sing-quic v0.0.0-20250404030904-b2cc8aab562c
2929
github.com/metacubex/sing-shadowsocks v0.2.8
3030
github.com/metacubex/sing-shadowsocks2 v0.2.2
31-
github.com/metacubex/sing-tun v0.4.6-0.20250402154347-cff06ec2a65b
31+
github.com/metacubex/sing-tun v0.4.6-0.20250407014348-50cddb7347dc
3232
github.com/metacubex/sing-vmess v0.1.14-0.20250228002636-abc39e113b82
3333
github.com/metacubex/sing-wireguard v0.0.0-20241126021510-0827d417b589
3434
github.com/metacubex/tfo-go v0.0.0-20241231083714-66613d49c422

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ github.com/metacubex/sing-shadowsocks v0.2.8 h1:wIhlaigswzjPw4hej75sEvWte3QR0+AJ
121121
github.com/metacubex/sing-shadowsocks v0.2.8/go.mod h1:X3x88XtJpBxG0W0/ECOJL6Ib0SJ3xdniAkU/6/RMWU0=
122122
github.com/metacubex/sing-shadowsocks2 v0.2.2 h1:eaf42uVx4Lr21S6MDYs0ZdTvGA0GEhDpb9no4+gdXPo=
123123
github.com/metacubex/sing-shadowsocks2 v0.2.2/go.mod h1:BhOug03a/RbI7y6hp6q+6ITM1dXjnLTmeWBHSTwvv2Q=
124-
github.com/metacubex/sing-tun v0.4.6-0.20250402154347-cff06ec2a65b h1:5WnSaOhE4xsNjn4SDIvjhULPF788y4amSeJT6vW7GGQ=
125-
github.com/metacubex/sing-tun v0.4.6-0.20250402154347-cff06ec2a65b/go.mod h1:V0N4rr0dWPBEE20ESkTXdbtx2riQYcb6YtwC5w/9wl0=
124+
github.com/metacubex/sing-tun v0.4.6-0.20250407014348-50cddb7347dc h1:Zn0Fst8C/uBJmwXArUMN7b9yQJXJY9S0nRJtTiYIq6Q=
125+
github.com/metacubex/sing-tun v0.4.6-0.20250407014348-50cddb7347dc/go.mod h1:V0N4rr0dWPBEE20ESkTXdbtx2riQYcb6YtwC5w/9wl0=
126126
github.com/metacubex/sing-vmess v0.1.14-0.20250228002636-abc39e113b82 h1:zZp5uct9+/0Hb1jKGyqDjCU4/72t43rs7qOq3Rc9oU8=
127127
github.com/metacubex/sing-vmess v0.1.14-0.20250228002636-abc39e113b82/go.mod h1:nE7Mdzj/QUDwgRi/8BASPtsxtIFZTHA4Yst5GgwbGCQ=
128128
github.com/metacubex/sing-wireguard v0.0.0-20241126021510-0827d417b589 h1:Z6bNy0HLTjx6BKIkV48sV/yia/GP8Bnyb5JQuGgSGzg=

listener/config/tun.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ type Tun struct {
5050
IncludeUIDRange []string `yaml:"include-uid-range" json:"include-uid-range,omitempty"`
5151
ExcludeUID []uint32 `yaml:"exclude-uid" json:"exclude-uid,omitempty"`
5252
ExcludeUIDRange []string `yaml:"exclude-uid-range" json:"exclude-uid-range,omitempty"`
53+
ExcludePort []uint16 `yaml:"exclude-port" json:"exclude-port,omitempty"`
54+
ExcludePortRange []string `yaml:"exclude-port-range" json:"exclude-port-range,omitempty"`
5355
IncludeAndroidUser []int `yaml:"include-android-user" json:"include-android-user,omitempty"`
5456
IncludePackage []string `yaml:"include-package" json:"include-package,omitempty"`
5557
ExcludePackage []string `yaml:"exclude-package" json:"exclude-package,omitempty"`

listener/inbound/tun.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ type TunOption struct {
3939
IncludeUIDRange []string `inbound:"include-uid-range,omitempty"`
4040
ExcludeUID []uint32 `inbound:"exclude-uid,omitempty"`
4141
ExcludeUIDRange []string `inbound:"exclude-uid-range,omitempty"`
42+
ExcludePort []uint16 `yaml:"exclude-port" json:"exclude-port,omitempty"`
43+
ExcludePortRange []string `yaml:"exclude-port-range" json:"exclude-port-range,omitempty"`
4244
IncludeAndroidUser []int `inbound:"include-android-user,omitempty"`
4345
IncludePackage []string `inbound:"include-package,omitempty"`
4446
ExcludePackage []string `inbound:"exclude-package,omitempty"`
@@ -137,6 +139,8 @@ func NewTun(options *TunOption) (*Tun, error) {
137139
IncludeUIDRange: options.IncludeUIDRange,
138140
ExcludeUID: options.ExcludeUID,
139141
ExcludeUIDRange: options.ExcludeUIDRange,
142+
ExcludePort: options.ExcludePort,
143+
ExcludePortRange: options.ExcludePortRange,
140144
IncludeAndroidUser: options.IncludeAndroidUser,
141145
IncludePackage: options.IncludePackage,
142146
ExcludePackage: options.ExcludePackage,

listener/sing_tun/server.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
LC "github.com/metacubex/mihomo/listener/config"
2222
"github.com/metacubex/mihomo/listener/sing"
2323
"github.com/metacubex/mihomo/log"
24+
"golang.org/x/exp/constraints"
2425

2526
tun "github.com/metacubex/sing-tun"
2627
"github.com/metacubex/sing-tun/control"
@@ -211,6 +212,14 @@ func New(options LC.Tun, tunnel C.Tunnel, additions ...inbound.Addition) (l *Lis
211212
return nil, E.Cause(err, "parse exclude_uid_range")
212213
}
213214
}
215+
excludePort := uidToRange(options.ExcludePort)
216+
if len(options.ExcludePortRange) > 0 {
217+
var err error
218+
excludePort, err = parseRange(excludePort, options.ExcludePortRange)
219+
if err != nil {
220+
return nil, E.Cause(err, "parse exclude_port_range")
221+
}
222+
}
214223

215224
var dnsAdds []netip.AddrPort
216225

@@ -339,6 +348,7 @@ func New(options LC.Tun, tunnel C.Tunnel, additions ...inbound.Addition) (l *Lis
339348
ExcludeInterface: options.ExcludeInterface,
340349
IncludeUID: includeUID,
341350
ExcludeUID: excludeUID,
351+
ExcludePort: excludePort,
342352
IncludeAndroidUser: options.IncludeAndroidUser,
343353
IncludePackage: options.IncludePackage,
344354
ExcludePackage: options.ExcludePackage,
@@ -566,13 +576,13 @@ func (d *cDialerInterfaceFinder) FindInterfaceName(destination netip.Addr) strin
566576
return "<invalid>"
567577
}
568578

569-
func uidToRange(uidList []uint32) []ranges.Range[uint32] {
570-
return common.Map(uidList, func(uid uint32) ranges.Range[uint32] {
579+
func uidToRange[T constraints.Integer](uidList []T) []ranges.Range[T] {
580+
return common.Map(uidList, func(uid T) ranges.Range[T] {
571581
return ranges.NewSingle(uid)
572582
})
573583
}
574584

575-
func parseRange(uidRanges []ranges.Range[uint32], rangeList []string) ([]ranges.Range[uint32], error) {
585+
func parseRange[T constraints.Integer](uidRanges []ranges.Range[T], rangeList []string) ([]ranges.Range[T], error) {
576586
for _, uidRange := range rangeList {
577587
if !strings.Contains(uidRange, ":") {
578588
return nil, E.New("missing ':' in range: ", uidRange)
@@ -593,7 +603,7 @@ func parseRange(uidRanges []ranges.Range[uint32], rangeList []string) ([]ranges.
593603
if err != nil {
594604
return nil, E.Cause(err, "parse range end")
595605
}
596-
uidRanges = append(uidRanges, ranges.New(uint32(start), uint32(end)))
606+
uidRanges = append(uidRanges, ranges.New(T(start), T(end)))
597607
}
598608
return uidRanges, nil
599609
}

0 commit comments

Comments
 (0)