Skip to content

Commit

Permalink
fix: udp loopback detector not working
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Apr 11, 2024
1 parent 16fadd2 commit 91a7ffa
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
23 changes: 21 additions & 2 deletions component/iface/iface.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var (

var interfaces = singledo.NewSingle[map[string]*Interface](time.Second * 20)

func ResolveInterface(name string) (*Interface, error) {
func Interfaces() (map[string]*Interface, error) {
value, err, _ := interfaces.Do(func() (map[string]*Interface, error) {
ifaces, err := net.Interfaces()
if err != nil {
Expand Down Expand Up @@ -69,11 +69,15 @@ func ResolveInterface(name string) (*Interface, error) {

return r, nil
})
return value, err
}

func ResolveInterface(name string) (*Interface, error) {
ifaces, err := Interfaces()
if err != nil {
return nil, err
}

ifaces := value
iface, ok := ifaces[name]
if !ok {
return nil, ErrIfaceNotFound
Expand All @@ -82,6 +86,21 @@ func ResolveInterface(name string) (*Interface, error) {
return iface, nil
}

func IsLocalIp(ip netip.Addr) (bool, error) {
ifaces, err := Interfaces()
if err != nil {
return false, err
}
for _, iface := range ifaces {
for _, addr := range iface.Addrs {
if addr.Contains(ip) {
return true, nil
}
}
}
return false, nil
}

func FlushCache() {
interfaces.Reset()
}
Expand Down
21 changes: 16 additions & 5 deletions component/loopback/detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/netip"

"github.com/metacubex/mihomo/common/callback"
"github.com/metacubex/mihomo/component/iface"
C "github.com/metacubex/mihomo/constant"

"github.com/puzpuzpuz/xsync/v3"
Expand All @@ -15,13 +16,13 @@ var ErrReject = errors.New("reject loopback connection")

type Detector struct {
connMap *xsync.MapOf[netip.AddrPort, struct{}]
packetConnMap *xsync.MapOf[netip.AddrPort, struct{}]
packetConnMap *xsync.MapOf[uint16, struct{}]
}

func NewDetector() *Detector {
return &Detector{
connMap: xsync.NewMapOf[netip.AddrPort, struct{}](),
packetConnMap: xsync.NewMapOf[netip.AddrPort, struct{}](),
packetConnMap: xsync.NewMapOf[uint16, struct{}](),
}
}

Expand Down Expand Up @@ -49,9 +50,10 @@ func (l *Detector) NewPacketConn(conn C.PacketConn) C.PacketConn {
if !connAddr.IsValid() {
return conn
}
l.packetConnMap.Store(connAddr, struct{}{})
port := connAddr.Port()
l.packetConnMap.Store(port, struct{}{})
return callback.NewCloseCallbackPacketConn(conn, func() {
l.packetConnMap.Delete(connAddr)
l.packetConnMap.Delete(port)
})
}

Expand All @@ -71,7 +73,16 @@ func (l *Detector) CheckPacketConn(metadata *C.Metadata) error {
if !connAddr.IsValid() {
return nil
}
if _, ok := l.packetConnMap.Load(connAddr); ok {

isLocalIp, err := iface.IsLocalIp(connAddr.Addr())
if err != nil {
return err
}
if !isLocalIp && !connAddr.Addr().IsLoopback() {
return nil
}

if _, ok := l.packetConnMap.Load(connAddr.Port()); ok {
return fmt.Errorf("%w to: %s", ErrReject, metadata.RemoteAddress())
}
return nil
Expand Down

0 comments on commit 91a7ffa

Please sign in to comment.