@@ -4,14 +4,25 @@ import (
4
4
"errors"
5
5
"fmt"
6
6
"net/netip"
7
+ "os"
8
+ "strconv"
7
9
8
10
"github.com/metacubex/mihomo/common/callback"
9
11
"github.com/metacubex/mihomo/component/iface"
10
12
C "github.com/metacubex/mihomo/constant"
13
+ "github.com/metacubex/mihomo/constant/features"
11
14
12
15
"github.com/puzpuzpuz/xsync/v3"
13
16
)
14
17
18
+ var disableLoopBackDetector , _ = strconv .ParseBool (os .Getenv ("DISABLE_LOOPBACK_DETECTOR" ))
19
+
20
+ func init () {
21
+ if features .CMFA {
22
+ disableLoopBackDetector = true
23
+ }
24
+ }
25
+
15
26
var ErrReject = errors .New ("reject loopback connection" )
16
27
17
28
type Detector struct {
@@ -20,13 +31,19 @@ type Detector struct {
20
31
}
21
32
22
33
func NewDetector () * Detector {
34
+ if disableLoopBackDetector {
35
+ return nil
36
+ }
23
37
return & Detector {
24
38
connMap : xsync .NewMapOf [netip.AddrPort , struct {}](),
25
39
packetConnMap : xsync .NewMapOf [uint16 , struct {}](),
26
40
}
27
41
}
28
42
29
43
func (l * Detector ) NewConn (conn C.Conn ) C.Conn {
44
+ if l == nil || l .connMap == nil {
45
+ return conn
46
+ }
30
47
metadata := C.Metadata {}
31
48
if metadata .SetRemoteAddr (conn .LocalAddr ()) != nil {
32
49
return conn
@@ -42,6 +59,9 @@ func (l *Detector) NewConn(conn C.Conn) C.Conn {
42
59
}
43
60
44
61
func (l * Detector ) NewPacketConn (conn C.PacketConn ) C.PacketConn {
62
+ if l == nil || l .packetConnMap == nil {
63
+ return conn
64
+ }
45
65
metadata := C.Metadata {}
46
66
if metadata .SetRemoteAddr (conn .LocalAddr ()) != nil {
47
67
return conn
@@ -58,6 +78,9 @@ func (l *Detector) NewPacketConn(conn C.PacketConn) C.PacketConn {
58
78
}
59
79
60
80
func (l * Detector ) CheckConn (metadata * C.Metadata ) error {
81
+ if l == nil || l .connMap == nil {
82
+ return nil
83
+ }
61
84
connAddr := metadata .SourceAddrPort ()
62
85
if ! connAddr .IsValid () {
63
86
return nil
@@ -69,6 +92,9 @@ func (l *Detector) CheckConn(metadata *C.Metadata) error {
69
92
}
70
93
71
94
func (l * Detector ) CheckPacketConn (metadata * C.Metadata ) error {
95
+ if l == nil || l .packetConnMap == nil {
96
+ return nil
97
+ }
72
98
connAddr := metadata .SourceAddrPort ()
73
99
if ! connAddr .IsValid () {
74
100
return nil
0 commit comments