Skip to content

Commit bca5c31

Browse files
committed
支持DST-MAC
1 parent fa91973 commit bca5c31

File tree

5 files changed

+124
-63
lines changed

5 files changed

+124
-63
lines changed

constant/rule.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ const (
99
GEOSITE
1010
GEOIP
1111
SrcGEOIP
12-
IPASN
1312
SrcMAC
13+
DstMAC
14+
IPASN
1415
SrcIPASN
1516
IPCIDR
1617
SrcIPCIDR
@@ -55,10 +56,12 @@ func (rt RuleType) String() string {
5556
return "GeoIP"
5657
case SrcGEOIP:
5758
return "SrcGeoIP"
58-
case IPASN:
59-
return "IPASN"
6059
case SrcMAC:
6160
return "SrcMAC"
61+
case DstMAC:
62+
return "DstMAC"
63+
case IPASN:
64+
return "IPASN"
6265
case SrcIPASN:
6366
return "SrcIPASN"
6467
case IPCIDR:

rules/common/mac.go

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,27 @@ var arpTable = make(map[string]string)
1919
const reloadInterval = 5 * time.Minute
2020

2121
var startOnce sync.Once
22+
2223
func init() {
2324
}
2425

25-
type SrcMAC struct {
26+
type MacAddr struct {
2627
*Base
27-
mac string
28-
adapter string
28+
mac string
29+
adapter string
30+
isSourceIP bool
2931
}
3032

31-
func (d *SrcMAC) RuleType() C.RuleType {
32-
return C.SrcMAC
33+
func (d *MacAddr) RuleType() C.RuleType {
34+
if d.isSourceIP {
35+
return C.SrcMAC
36+
} else {
37+
return C.DstMAC
38+
}
3339
}
3440

3541
func getLoadArpTableFunc() func() (string, error) {
36-
const ipv6Error = "can't load ipv6 arp table, SRC-MAC rule can't match src ipv6 address"
42+
const ipv6Error = "can't load ipv6 arp table, SRC-MAC/DST-MAC rule can't match src ipv6 address"
3743

3844
getIpv4Only := func() (string, error) {
3945
return cmd.ExecCmd("arp -a")
@@ -95,39 +101,45 @@ func getLoadArpTableFunc() func() (string, error) {
95101
}
96102
}
97103

98-
func (d *SrcMAC) Match(metadata *C.Metadata) (bool, string) {
104+
func (d *MacAddr) Match(metadata *C.Metadata) (bool, string) {
99105
table := getArpTable()
100-
srcIP := metadata.SrcIP.String()
101-
mac, exists := table[srcIP]
106+
var ip string
107+
if d.isSourceIP {
108+
ip = metadata.SrcIP.String()
109+
} else {
110+
ip = metadata.DstIP.String()
111+
}
112+
mac, exists := table[ip]
102113
if exists {
103114
if mac == d.mac {
104115
return true, d.adapter
105116
}
106117
} else {
107-
log.Warnln("can't find the IP address in arp table: %s", srcIP)
118+
log.Infoln("can't find the IP address in arp table: %s", ip)
108119
}
109120
return false, d.adapter
110121
}
111122

112-
func (d *SrcMAC) Adapter() string {
123+
func (d *MacAddr) Adapter() string {
113124
return d.adapter
114125
}
115126

116-
func (d *SrcMAC) Payload() string {
127+
func (d *MacAddr) Payload() string {
117128
return d.mac
118129
}
119130

120131
var macRegex = regexp.MustCompile(`^([0-9a-f]{2}:){5}[0-9a-f]{2}$`)
121132

122-
func NewMAC(mac string, adapter string) (*SrcMAC, error) {
133+
func NewMAC(mac string, adapter string, isSrc bool) (*MacAddr, error) {
123134
macAddr := strings.ReplaceAll(strings.ToLower(mac), "-", ":")
124135
if !macRegex.MatchString(macAddr) {
125136
return nil, errors.New("mac address format error: " + mac)
126137
}
127-
return &SrcMAC{
128-
Base: &Base{},
129-
mac: macAddr,
130-
adapter: adapter,
138+
return &MacAddr{
139+
Base: &Base{},
140+
mac: macAddr,
141+
adapter: adapter,
142+
isSourceIP: isSrc,
131143
}, nil
132144
}
133145

rules/parser.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ func ParseRule(tp, payload, target string, params []string, subRules map[string]
2626
parsed, parseErr = RC.NewGEOIP(payload, target, false, noResolve)
2727
case "SRC-GEOIP":
2828
parsed, parseErr = RC.NewGEOIP(payload, target, true, true)
29+
case "SRC-MAC":
30+
parsed, parseErr = RC.NewMAC(payload, target, true)
31+
case "DST-MAC":
32+
parsed, parseErr = RC.NewMAC(payload, target, false)
2933
case "IP-ASN":
3034
noResolve := RC.HasNoResolve(params)
3135
parsed, parseErr = RC.NewIPASN(payload, target, false, noResolve)
32-
case "SRC-MAC":
33-
parsed, parseErr = RC.NewMAC(payload, target)
3436
case "SRC-IP-ASN":
3537
parsed, parseErr = RC.NewIPASN(payload, target, true, true)
3638
case "IP-CIDR", "IP-CIDR6":

test/go.mod

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@ require (
66
github.com/docker/docker v20.10.21+incompatible
77
github.com/docker/go-connections v0.4.0
88
github.com/metacubex/mihomo v0.0.0
9-
github.com/miekg/dns v1.1.57
10-
github.com/stretchr/testify v1.8.4
11-
golang.org/x/net v0.18.0
9+
github.com/miekg/dns v1.1.61
10+
github.com/stretchr/testify v1.9.0
11+
golang.org/x/net v0.26.0
1212
)
1313

1414
replace github.com/metacubex/mihomo => ../
1515

1616
require (
1717
github.com/3andne/restls-client-go v0.1.6 // indirect
1818
github.com/Microsoft/go-winio v0.6.0 // indirect
19-
github.com/RyuaNerin/go-krypto v1.0.2 // indirect
19+
github.com/RyuaNerin/go-krypto v1.2.4 // indirect
2020
github.com/Yawning/aez v0.0.0-20211027044916-e49e68abd344 // indirect
2121
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect
22-
github.com/andybalholm/brotli v1.0.5 // indirect
22+
github.com/andybalholm/brotli v1.0.6 // indirect
2323
github.com/bahlo/generic-list-go v0.2.0 // indirect
2424
github.com/buger/jsonparser v1.1.1 // indirect
2525
github.com/cilium/ebpf v0.12.3 // indirect
2626
github.com/coreos/go-iptables v0.7.0 // indirect
2727
github.com/davecgh/go-spew v1.1.1 // indirect
2828
github.com/distribution/reference v0.5.0 // indirect
29-
github.com/dlclark/regexp2 v1.10.0 // indirect
29+
github.com/dlclark/regexp2 v1.11.0 // indirect
3030
github.com/docker/distribution v2.8.3+incompatible // indirect
3131
github.com/docker/go-units v0.4.0 // indirect
3232
github.com/ericlagergren/aegis v0.0.0-20230312195928-b4ce538b56f9 // indirect
@@ -39,32 +39,32 @@ require (
3939
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
4040
github.com/gobwas/httphead v0.1.0 // indirect
4141
github.com/gobwas/pool v0.2.1 // indirect
42-
github.com/gobwas/ws v1.3.1 // indirect
43-
github.com/gofrs/uuid/v5 v5.0.0 // indirect
42+
github.com/gobwas/ws v1.4.0 // indirect
43+
github.com/gofrs/uuid/v5 v5.2.0 // indirect
4444
github.com/gogo/protobuf v1.3.2 // indirect
4545
github.com/google/btree v1.1.2 // indirect
4646
github.com/google/go-cmp v0.6.0 // indirect
4747
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
4848
github.com/hashicorp/yamux v0.1.1 // indirect
49-
github.com/insomniacslk/dhcp v0.0.0-20231016090811-6a2c8fbdcc1c // indirect
49+
github.com/insomniacslk/dhcp v0.0.0-20240529192340-51bc6136a0a6 // indirect
5050
github.com/josharian/native v1.1.0 // indirect
5151
github.com/jpillora/backoff v1.0.0 // indirect
52-
github.com/klauspost/compress v1.16.7 // indirect
53-
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
52+
github.com/klauspost/compress v1.17.4 // indirect
53+
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
5454
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
5555
github.com/lunixbochs/struc v0.0.0-20200707160740-784aaebc1d40 // indirect
5656
github.com/mailru/easyjson v0.7.7 // indirect
5757
github.com/mdlayher/netlink v1.7.2 // indirect
5858
github.com/mdlayher/socket v0.4.1 // indirect
5959
github.com/metacubex/gopacket v1.1.20-0.20230608035415-7e2f98a3e759 // indirect
60-
github.com/metacubex/gvisor v0.0.0-20231001104248-0f672c3fb8d8 // indirect
61-
github.com/metacubex/quic-go v0.40.1-0.20231130135418-0c1b47cf9394 // indirect
62-
github.com/metacubex/sing-quic v0.0.0-20231130141855-0022295e524b // indirect
63-
github.com/metacubex/sing-shadowsocks v0.2.5 // indirect
64-
github.com/metacubex/sing-shadowsocks2 v0.1.4 // indirect
65-
github.com/metacubex/sing-tun v0.1.15-0.20231103033938-170591e8d5bd // indirect
66-
github.com/metacubex/sing-vmess v0.1.9-0.20230921005247-a0488d7dac74 // indirect
67-
github.com/metacubex/sing-wireguard v0.0.0-20231001110902-321836559170 // indirect
60+
github.com/metacubex/gvisor v0.0.0-20240320004321-933faba989ec // indirect
61+
github.com/metacubex/quic-go v0.45.1-0.20240610004319-163fee60637e // indirect
62+
github.com/metacubex/sing-quic v0.0.0-20240518034124-7696d3f7da72 // indirect
63+
github.com/metacubex/sing-shadowsocks v0.2.7 // indirect
64+
github.com/metacubex/sing-shadowsocks2 v0.2.1 // indirect
65+
github.com/metacubex/sing-tun v0.2.7-0.20240719141246-19c49ac9589d // indirect
66+
github.com/metacubex/sing-vmess v0.1.9-0.20240719134745-1df6fb20bbf9 // indirect
67+
github.com/metacubex/sing-wireguard v0.0.0-20240618022557-a6efaa37127a // indirect
6868
github.com/moby/term v0.5.0 // indirect
6969
github.com/morikuni/aec v1.0.0 // indirect
7070
github.com/mroth/weightedrand/v2 v2.1.0 // indirect
@@ -78,22 +78,22 @@ require (
7878
github.com/pkg/errors v0.9.1 // indirect
7979
github.com/pmezard/go-difflib v1.0.0 // indirect
8080
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
81-
github.com/puzpuzpuz/xsync/v3 v3.0.2 // indirect
81+
github.com/puzpuzpuz/xsync/v3 v3.2.0 // indirect
8282
github.com/quic-go/qpack v0.4.0 // indirect
8383
github.com/quic-go/qtls-go1-20 v0.4.1 // indirect
8484
github.com/sagernet/bbolt v0.0.0-20231014093535-ea5cb2fe9f0a // indirect
8585
github.com/sagernet/go-tun2socks v1.16.12-0.20220818015926-16cb67876a61 // indirect
86-
github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97 // indirect
87-
github.com/sagernet/sing v0.2.18-0.20231108041402-4fbbd193203c // indirect
88-
github.com/sagernet/sing-mux v0.1.5-0.20231109075101-6b086ed6bb07 // indirect
86+
github.com/sagernet/netlink v0.0.0-20240612041022-b9a21c07ac6a // indirect
87+
github.com/sagernet/sing v0.5.0-alpha.13 // indirect
88+
github.com/sagernet/sing-mux v0.2.1-0.20240124034317-9bfb33698bb6 // indirect
8989
github.com/sagernet/sing-shadowtls v0.1.4 // indirect
90-
github.com/sagernet/smux v0.0.0-20230312102458-337ec2a5af37 // indirect
90+
github.com/sagernet/smux v0.0.0-20231208180855-7041f6ea79e7 // indirect
9191
github.com/sagernet/tfo-go v0.0.0-20230816093905-5a5c285d44a6 // indirect
9292
github.com/sagernet/utls v0.0.0-20230309024959-6732c2ab36f2 // indirect
93-
github.com/sagernet/wireguard-go v0.0.0-20230807125731-5d4a7ef2dc5f // indirect
94-
github.com/samber/lo v1.38.1 // indirect
93+
github.com/sagernet/wireguard-go v0.0.0-20231209092712-9a439356a62e // indirect
94+
github.com/samber/lo v1.39.0 // indirect
9595
github.com/scjalliance/comshim v0.0.0-20230315213746-5e51f40bd3b9 // indirect
96-
github.com/shirou/gopsutil/v3 v3.23.10 // indirect
96+
github.com/shirou/gopsutil/v3 v3.24.5 // indirect
9797
github.com/shoenig/go-m1cpu v0.1.6 // indirect
9898
github.com/sina-ghaderi/poly1305 v0.0.0-20220724002748-c5926b03988b // indirect
9999
github.com/sina-ghaderi/rabaead v0.0.0-20220730151906-ab6e06b96e8c // indirect
@@ -102,22 +102,22 @@ require (
102102
github.com/tklauser/go-sysconf v0.3.12 // indirect
103103
github.com/tklauser/numcpus v0.6.1 // indirect
104104
github.com/u-root/uio v0.0.0-20230220225925-ffce2a382923 // indirect
105-
github.com/vishvananda/netns v0.0.0-20211101163701-50045581ed74 // indirect
105+
github.com/vishvananda/netns v0.0.4 // indirect
106106
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
107-
github.com/yusufpapurcu/wmi v1.2.3 // indirect
107+
github.com/yusufpapurcu/wmi v1.2.4 // indirect
108108
github.com/zhangyunhao116/fastrand v0.3.0 // indirect
109109
gitlab.com/yawning/bsaes.git v0.0.0-20190805113838-0a714cd429ec // indirect
110-
go.uber.org/mock v0.3.0 // indirect
111-
go4.org/netipx v0.0.0-20230824141953-6213f710f925 // indirect
112-
golang.org/x/crypto v0.16.0 // indirect
113-
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
114-
golang.org/x/mod v0.14.0 // indirect
115-
golang.org/x/sync v0.5.0 // indirect
116-
golang.org/x/sys v0.15.0 // indirect
117-
golang.org/x/text v0.14.0 // indirect
118-
golang.org/x/time v0.3.0 // indirect
119-
golang.org/x/tools v0.15.0 // indirect
120-
google.golang.org/protobuf v1.31.0 // indirect
110+
go.uber.org/mock v0.4.0 // indirect
111+
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect
112+
golang.org/x/crypto v0.24.0 // indirect
113+
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
114+
golang.org/x/mod v0.18.0 // indirect
115+
golang.org/x/sync v0.7.0 // indirect
116+
golang.org/x/sys v0.22.0 // indirect
117+
golang.org/x/text v0.16.0 // indirect
118+
golang.org/x/time v0.5.0 // indirect
119+
golang.org/x/tools v0.22.0 // indirect
120+
google.golang.org/protobuf v1.34.2 // indirect
121121
gopkg.in/yaml.v3 v3.0.1 // indirect
122-
lukechampine.com/blake3 v1.2.1 // indirect
122+
lukechampine.com/blake3 v1.3.0 // indirect
123123
)

0 commit comments

Comments
 (0)