mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-11-20 08:39:14 +00:00
Routing: Fix process for Linux IPv4-mapped sockets (#7414)
https://github.com/XTLS/Xray-core/pull/7414#issuecomment-6987317102 --------- Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
b1905088c3
commit
36112cd886
@@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -31,7 +32,7 @@ func FindProcess(network, srcIP string, srcPort uint16, destIP string, destPort
|
||||
switch network {
|
||||
case "tcp":
|
||||
if net.ParseIP(srcIP).To4() != nil {
|
||||
procFiles = []string{"/proc/net/tcp"}
|
||||
procFiles = []string{"/proc/net/tcp", "/proc/net/tcp6"}
|
||||
} else {
|
||||
procFiles = []string{"/proc/net/tcp6"}
|
||||
}
|
||||
@@ -45,14 +46,18 @@ func FindProcess(network, srcIP string, srcPort uint16, destIP string, destPort
|
||||
panic("Unsupported network type for process lookup.")
|
||||
}
|
||||
|
||||
targetHexAddr := formatProcNetAddress(net.ParseIP(srcIP), Port(srcPort))
|
||||
targetHexAddrs := []string{formatProcNetAddress(net.ParseIP(srcIP).To16(), Port(srcPort))}
|
||||
if ipv4 := net.ParseIP(srcIP).To4(); ipv4 != nil {
|
||||
// the one above is then the IPv4-mapped IPv6 address that an IPv6 socket has it as, in the IPv6 file
|
||||
targetHexAddrs = append(targetHexAddrs, formatProcNetAddress(ipv4, Port(srcPort)))
|
||||
}
|
||||
|
||||
portFallback := ""
|
||||
if network == "udp" {
|
||||
portFallback = fmt.Sprintf(":%04X", srcPort)
|
||||
}
|
||||
|
||||
inode, err := findInodeInFiles(procFiles, targetHexAddr, portFallback)
|
||||
inode, err := findInodeInFiles(procFiles, targetHexAddrs, portFallback)
|
||||
if err != nil {
|
||||
return 0, "", "", errors.New("could not search in ", strings.Join(procFiles, " and ")).Base(err)
|
||||
}
|
||||
@@ -84,13 +89,9 @@ func FindProcess(network, srcIP string, srcPort uint16, destIP string, destPort
|
||||
return pid, procName, absPath, nil
|
||||
}
|
||||
|
||||
// formatProcNetAddress formats the 4 or 16 bytes of addr as they are: an IPv4-mapped IPv6 address stays one
|
||||
func formatProcNetAddress(addr net.IP, port Port) string {
|
||||
var ipBytes []byte
|
||||
if ipv4 := addr.To4(); ipv4 != nil {
|
||||
ipBytes = ipv4
|
||||
} else {
|
||||
ipBytes = addr.To16()
|
||||
}
|
||||
ipBytes := []byte(addr)
|
||||
|
||||
var formatted strings.Builder
|
||||
formatted.Grow(len(ipBytes)*2 + 5)
|
||||
@@ -101,7 +102,7 @@ func formatProcNetAddress(addr net.IP, port Port) string {
|
||||
return formatted.String()
|
||||
}
|
||||
|
||||
func findInodeInFiles(filePaths []string, targetHexAddr, portFallback string) (string, error) {
|
||||
func findInodeInFiles(filePaths []string, targetHexAddrs []string, portFallback string) (string, error) {
|
||||
onlyMatchedPort := ""
|
||||
|
||||
for i, filePath := range filePaths {
|
||||
@@ -122,7 +123,7 @@ func findInodeInFiles(filePaths []string, targetHexAddr, portFallback string) (s
|
||||
}
|
||||
|
||||
localAddress := fields[1]
|
||||
if localAddress == targetHexAddr {
|
||||
if slices.Contains(targetHexAddrs, localAddress) {
|
||||
return fields[9], nil
|
||||
}
|
||||
if portFallback != "" && strings.HasSuffix(localAddress, portFallback) {
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
//go:build linux && !android
|
||||
|
||||
package net
|
||||
|
||||
import (
|
||||
"errors"
|
||||
stdnet "net"
|
||||
"net/netip"
|
||||
"os"
|
||||
"syscall"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFindProcessIPv4Mapped(t *testing.T) {
|
||||
listener, err := stdnet.Listen("tcp4", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer listener.Close()
|
||||
peer, err := stdnet.ListenPacket("udp4", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer peer.Close()
|
||||
|
||||
// an IPv6 socket that connects to an IPv4 address, as the sockets of Java and .NET are
|
||||
for _, c := range []struct{ local, remote stdnet.Addr }{
|
||||
{&stdnet.TCPAddr{IP: stdnet.IPv6zero}, listener.Addr()},
|
||||
{&stdnet.UDPAddr{IP: stdnet.IPv6zero}, peer.LocalAddr()},
|
||||
} {
|
||||
network := c.remote.Network()
|
||||
conn, err := (&stdnet.Dialer{LocalAddr: c.local}).Dial(network, c.remote.String())
|
||||
if errors.Is(err, syscall.EAFNOSUPPORT) {
|
||||
t.Skip("no IPv6 sockets here")
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
local := netip.MustParseAddrPort(conn.LocalAddr().String())
|
||||
remote := netip.MustParseAddrPort(c.remote.String())
|
||||
pid, _, _, err := FindProcess(network, "127.0.0.1", local.Port(), "127.0.0.1", remote.Port())
|
||||
if err != nil {
|
||||
t.Errorf("%s: %v", network, err)
|
||||
} else if pid != os.Getpid() {
|
||||
t.Errorf("%s: found PID %d, want %d", network, pid, os.Getpid())
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user