Skip to content

Commit 9e3bf14

Browse files
committed
chore: handle two interfaces have the same prefix but different address
1 parent 28c387a commit 9e3bf14

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

component/iface/iface.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ var (
2626
)
2727

2828
type ifaceCache struct {
29-
ifMap map[string]*Interface
30-
ifTable bart.Table[*Interface]
29+
ifMapByName map[string]*Interface
30+
ifMapByAddr map[netip.Addr]*Interface
31+
ifTable bart.Table[*Interface]
3132
}
3233

3334
var caches = singledo.NewSingle[*ifaceCache](time.Second * 20)
@@ -40,7 +41,8 @@ func getCache() (*ifaceCache, error) {
4041
}
4142

4243
cache := &ifaceCache{
43-
ifMap: make(map[string]*Interface),
44+
ifMapByName: make(map[string]*Interface),
45+
ifMapByAddr: make(map[netip.Addr]*Interface),
4446
}
4547

4648
for _, iface := range ifaces {
@@ -78,12 +80,13 @@ func getCache() (*ifaceCache, error) {
7880
Flags: iface.Flags,
7981
Addresses: ipNets,
8082
}
81-
cache.ifMap[iface.Name] = ifaceObj
83+
cache.ifMapByName[iface.Name] = ifaceObj
8284

8385
if iface.Flags&net.FlagUp == 0 {
8486
continue // interface down
8587
}
8688
for _, prefix := range ipNets {
89+
cache.ifMapByAddr[prefix.Addr()] = ifaceObj
8790
cache.ifTable.Insert(prefix, ifaceObj)
8891
}
8992
}
@@ -98,7 +101,7 @@ func Interfaces() (map[string]*Interface, error) {
98101
if err != nil {
99102
return nil, err
100103
}
101-
return cache.ifMap, nil
104+
return cache.ifMapByName, nil
102105
}
103106

104107
func ResolveInterface(name string) (*Interface, error) {
@@ -120,6 +123,11 @@ func ResolveInterfaceByAddr(addr netip.Addr) (*Interface, error) {
120123
if err != nil {
121124
return nil, err
122125
}
126+
// maybe two interfaces have the same prefix but different address
127+
// so direct check address equal before do a route lookup (longest prefix match)
128+
if iface, ok := cache.ifMapByAddr[addr]; ok {
129+
return iface, nil
130+
}
123131
iface, ok := cache.ifTable.Lookup(addr)
124132
if !ok {
125133
return nil, ErrIfaceNotFound
@@ -133,7 +141,8 @@ func IsLocalIp(addr netip.Addr) (bool, error) {
133141
if err != nil {
134142
return false, err
135143
}
136-
return cache.ifTable.Contains(addr), nil
144+
_, ok := cache.ifMapByAddr[addr]
145+
return ok, nil
137146
}
138147

139148
func FlushCache() {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ require (
2525
github.com/metacubex/gopacket v1.1.20-0.20230608035415-7e2f98a3e759
2626
github.com/metacubex/quic-go v0.52.1-0.20250522021943-aef454b9e639
2727
github.com/metacubex/randv2 v0.2.0
28-
github.com/metacubex/sing v0.5.3-0.20250504031621-1f99e54c15b7
28+
github.com/metacubex/sing v0.5.3
2929
github.com/metacubex/sing-mux v0.3.2
3030
github.com/metacubex/sing-quic v0.0.0-20250523120938-f1a248e5ec7f
3131
github.com/metacubex/sing-shadowsocks v0.2.9

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ github.com/metacubex/quic-go v0.52.1-0.20250522021943-aef454b9e639/go.mod h1:Kc6
116116
github.com/metacubex/randv2 v0.2.0 h1:uP38uBvV2SxYfLj53kuvAjbND4RUDfFJjwr4UigMiLs=
117117
github.com/metacubex/randv2 v0.2.0/go.mod h1:kFi2SzrQ5WuneuoLLCMkABtiBu6VRrMrWFqSPyj2cxY=
118118
github.com/metacubex/sing v0.5.2/go.mod h1:ypf0mjwlZm0sKdQSY+yQvmsbWa0hNPtkeqyRMGgoN+w=
119-
github.com/metacubex/sing v0.5.3-0.20250504031621-1f99e54c15b7 h1:m4nSxvw46JEgxMzzmnXams+ebwabcry4Ydep/zNiesQ=
120-
github.com/metacubex/sing v0.5.3-0.20250504031621-1f99e54c15b7/go.mod h1:ypf0mjwlZm0sKdQSY+yQvmsbWa0hNPtkeqyRMGgoN+w=
119+
github.com/metacubex/sing v0.5.3 h1:QWdN16WFKMk06x4nzkc8SvZ7y2x+TLQrpkPoHs+WSVM=
120+
github.com/metacubex/sing v0.5.3/go.mod h1:ypf0mjwlZm0sKdQSY+yQvmsbWa0hNPtkeqyRMGgoN+w=
121121
github.com/metacubex/sing-mux v0.3.2 h1:nJv52pyRivHcaZJKk2JgxpaVvj1GAXG81scSa9N7ncw=
122122
github.com/metacubex/sing-mux v0.3.2/go.mod h1:3rt1soewn0O6j89GCLmwAQFsq257u0jf2zQSPhTL3Bw=
123123
github.com/metacubex/sing-quic v0.0.0-20250523120938-f1a248e5ec7f h1:mP3vIm+9hRFI0C0Vl3pE0NESF/L85FDbuB0tGgUii6I=

0 commit comments

Comments
 (0)