mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-10-22 17:24:26 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c9a57e931d |
@@ -4,7 +4,7 @@ package net
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"encoding/hex"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
@@ -45,10 +45,7 @@ func FindProcess(network, srcIP string, srcPort uint16, destIP string, destPort
|
|||||||
panic("Unsupported network type for process lookup.")
|
panic("Unsupported network type for process lookup.")
|
||||||
}
|
}
|
||||||
|
|
||||||
targetHexAddr, err := formatLittleEndianString(net.ParseIP(srcIP), Port(srcPort))
|
targetHexAddr := formatProcNetAddress(net.ParseIP(srcIP), Port(srcPort))
|
||||||
if err != nil {
|
|
||||||
return 0, "", "", errors.New("failed to format address: ", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
inode, err := findInodeInFile(procFile, targetHexAddr)
|
inode, err := findInodeInFile(procFile, targetHexAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -82,24 +79,21 @@ func FindProcess(network, srcIP string, srcPort uint16, destIP string, destPort
|
|||||||
return pid, procName, absPath, nil
|
return pid, procName, absPath, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatLittleEndianString(addr net.IP, port Port) (string, error) {
|
func formatProcNetAddress(addr net.IP, port Port) string {
|
||||||
ip := addr
|
|
||||||
var ipBytes []byte
|
var ipBytes []byte
|
||||||
if ip.To4() != nil {
|
if ipv4 := addr.To4(); ipv4 != nil {
|
||||||
ipBytes = ip.To4()
|
ipBytes = ipv4
|
||||||
} else {
|
} else {
|
||||||
ipBytes = ip.To16()
|
ipBytes = addr.To16()
|
||||||
}
|
|
||||||
if ipBytes == nil {
|
|
||||||
return "", errors.New("invalid IP format for ", addr, ": ", ip)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, j := 0, len(ipBytes)-1; i < j; i, j = i+1, j-1 {
|
var formatted strings.Builder
|
||||||
ipBytes[i], ipBytes[j] = ipBytes[j], ipBytes[i]
|
formatted.Grow(len(ipBytes)*2 + 5)
|
||||||
|
for i := 0; i < len(ipBytes); i += 4 {
|
||||||
|
fmt.Fprintf(&formatted, "%08X", binary.NativeEndian.Uint32(ipBytes[i:i+4]))
|
||||||
}
|
}
|
||||||
portHex := fmt.Sprintf("%04X", uint16(port))
|
fmt.Fprintf(&formatted, ":%04X", uint16(port))
|
||||||
ipHex := strings.ToUpper(hex.EncodeToString(ipBytes))
|
return formatted.String()
|
||||||
return fmt.Sprintf("%s:%s", ipHex, portHex), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func findInodeInFile(filePath, targetHexAddr string) (string, error) {
|
func findInodeInFile(filePath, targetHexAddr string) (string, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user