Skip to content

Commit 5dfbf64

Browse files
author
farsight
committed
add dynamic IP mapping for localhost
1 parent 311d80d commit 5dfbf64

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

pkg/agent/smartping/pinger.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package smartping
33
import (
44
"github.com/go-ping/ping"
55
"github.com/sirupsen/logrus"
6+
"fmt"
67
"net"
78
"os/exec"
89
"runtime"
@@ -17,9 +18,12 @@ func TryResolve(address string) bool {
1718
Mask: []byte{0xf0, 0x00, 0x00, 0x00},
1819
}
1920

20-
if magicNet.Contains(net.ParseIP(address)) {
21+
if ip := net.ParseIP(address).To4(); ip != nil && magicNet.Contains(ip) {
2122
logrus.Debug("MagicIP Ping detected, returning true")
22-
// Magic IP detected
23+
24+
// Dynamically map the IP within the 127.x.x.x range
25+
mappedIP := fmt.Sprintf("127.%d.%d.%d", ip[1], ip[2], ip[3])
26+
logrus.Debugf("Mapped MagicIP to: %s", mappedIP)
2327
return true
2428
}
2529
methods := []func(string) (bool, error){

pkg/proxy/netstack/handlers.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package netstack
22

33
import (
4+
"fmt"
45
"io"
56
"net"
67

@@ -98,10 +99,12 @@ func HandlePacket(nstack *stack.Stack, localConn TunConn, yamuxConn *yamux.Sessi
9899
Mask: []byte{0xf0, 0x00, 0x00, 0x00},
99100
}
100101

101-
if magicNet.Contains(net.ParseIP(targetIp)) {
102+
if ip := net.ParseIP(targetIp).To4(); ip != nil && magicNet.Contains(ip) {
102103
logrus.Debug("MagicIP detected, redirecting to agent local machine")
103-
// Magic IP detected
104-
targetIp = "127.0.0.1"
104+
105+
// Dynamically map the IP within the 127.x.x.x range
106+
targetIp = fmt.Sprintf("127.%d.%d.%d", ip[1], ip[2], ip[3])
107+
logrus.Debugf("Mapped MagicIP to: %s", targetIp)
105108
}
106109

107110
yamuxConnectionSession, err := yamuxConn.Open()

0 commit comments

Comments
 (0)