mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-10-20 16:24:30 +00:00
Fix udp match
This commit is contained in:
@@ -26,20 +26,20 @@ func FindProcess(network, srcIP string, srcPort uint16, destIP string, destPort
|
|||||||
panic("Unsupported network type for process lookup.")
|
panic("Unsupported network type for process lookup.")
|
||||||
}
|
}
|
||||||
|
|
||||||
var procFile string
|
var procFiles []string
|
||||||
|
|
||||||
switch network {
|
switch network {
|
||||||
case "tcp":
|
case "tcp":
|
||||||
if net.ParseIP(srcIP).To4() != nil {
|
if net.ParseIP(srcIP).To4() != nil {
|
||||||
procFile = "/proc/net/tcp"
|
procFiles = []string{"/proc/net/tcp"}
|
||||||
} else {
|
} else {
|
||||||
procFile = "/proc/net/tcp6"
|
procFiles = []string{"/proc/net/tcp6"}
|
||||||
}
|
}
|
||||||
case "udp":
|
case "udp":
|
||||||
if net.ParseIP(srcIP).To4() != nil {
|
if net.ParseIP(srcIP).To4() != nil {
|
||||||
procFile = "/proc/net/udp"
|
procFiles = []string{"/proc/net/udp", "/proc/net/udp6"}
|
||||||
} else {
|
} else {
|
||||||
procFile = "/proc/net/udp6"
|
procFiles = []string{"/proc/net/udp6", "/proc/net/udp"}
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
panic("Unsupported network type for process lookup.")
|
panic("Unsupported network type for process lookup.")
|
||||||
@@ -50,12 +50,17 @@ func FindProcess(network, srcIP string, srcPort uint16, destIP string, destPort
|
|||||||
return 0, "", "", errors.New("failed to format address: ", err)
|
return 0, "", "", errors.New("failed to format address: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
inode, err := findInodeInFile(procFile, targetHexAddr)
|
portFallback := ""
|
||||||
|
if network == "udp" {
|
||||||
|
portFallback = fmt.Sprintf(":%04X", srcPort)
|
||||||
|
}
|
||||||
|
|
||||||
|
inode, err := findInodeInFiles(procFiles, targetHexAddr, portFallback)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, "", "", errors.New("could not search in ", procFile).Base(err)
|
return 0, "", "", errors.New("could not search in ", strings.Join(procFiles, " and ")).Base(err)
|
||||||
}
|
}
|
||||||
if inode == "" {
|
if inode == "" {
|
||||||
return 0, "", "", errors.New("connection for ", srcIP, ":", srcPort, " not found in ", procFile)
|
return 0, "", "", errors.New("connection for ", srcIP, ":", srcPort, " not found in ", strings.Join(procFiles, " or "))
|
||||||
}
|
}
|
||||||
|
|
||||||
pidStr, err := findPidByInode(inode)
|
pidStr, err := findPidByInode(inode)
|
||||||
@@ -102,31 +107,43 @@ func formatLittleEndianString(addr net.IP, port Port) (string, error) {
|
|||||||
return fmt.Sprintf("%s:%s", ipHex, portHex), nil
|
return fmt.Sprintf("%s:%s", ipHex, portHex), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func findInodeInFile(filePath, targetHexAddr string) (string, error) {
|
func findInodeInFiles(filePaths []string, targetHexAddr, portFallback string) (string, error) {
|
||||||
file, err := os.Open(filePath)
|
onlyMatchedPort := ""
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(file)
|
for i, filePath := range filePaths {
|
||||||
|
file, err := os.Open(filePath)
|
||||||
for scanner.Scan() {
|
if err != nil {
|
||||||
line := scanner.Text()
|
if i > 0 && os.IsNotExist(err) {
|
||||||
fields := strings.Fields(line)
|
continue
|
||||||
|
}
|
||||||
if len(fields) < 10 {
|
return "", err
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
localAddress := fields[1]
|
scanner := bufio.NewScanner(file)
|
||||||
if localAddress == targetHexAddr {
|
for scanner.Scan() {
|
||||||
inode := fields[9]
|
fields := strings.Fields(scanner.Text())
|
||||||
return inode, nil
|
if len(fields) < 10 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
localAddress := fields[1]
|
||||||
|
if localAddress == targetHexAddr {
|
||||||
|
return fields[9], nil
|
||||||
|
}
|
||||||
|
if portFallback != "" && strings.HasSuffix(localAddress, portFallback) {
|
||||||
|
wildcardIP := strings.TrimSuffix(localAddress, portFallback)
|
||||||
|
if (len(wildcardIP) == 8 || len(wildcardIP) == 32) && strings.Trim(wildcardIP, "0") == "" {
|
||||||
|
onlyMatchedPort = fields[9]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return "", err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", scanner.Err()
|
return onlyMatchedPort, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func findPidByInode(inode string) (string, error) {
|
func findPidByInode(inode string) (string, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user