1- //go:build linux
1+ //go:build linux && !android
22
33package net
44
55import (
66 "bufio"
77 "encoding/hex"
88 "fmt"
9+ "net"
910 "os"
1011 "strconv"
1112 "strings"
1213
1314 "github.com/xtls/xray-core/common/errors"
1415)
1516
16- func FindProcess (dest Destination ) (PID int , Name string , AbsolutePath string , err error ) {
17- isLocal , err := IsLocal (dest . Address . IP ( ))
17+ func FindProcess (network , srcIP string , srcPort uint16 , destIP string , destPort uint16 ) (PID int , Name string , AbsolutePath string , err error ) {
18+ isLocal , err := IsLocal (net . ParseIP ( srcIP ))
1819 if err != nil {
1920 return 0 , "" , "" , errors .New ("failed to determine if address is local: " , err )
2021 }
2122 if ! isLocal {
2223 return 0 , "" , "" , ErrNotLocal
2324 }
24- if dest . Network != Network_TCP && dest . Network != Network_UDP {
25+ if network != "tcp" && network != "udp" {
2526 panic ("Unsupported network type for process lookup." )
2627 }
27- // the core should never has a domain as source(?
28- if dest .Address .Family () == AddressFamilyDomain {
29- panic ("Domain addresses are not supported for process lookup." )
30- }
28+
3129 var procFile string
3230
33- switch dest . Network {
34- case Network_TCP :
35- if dest . Address . Family () == AddressFamilyIPv4 {
31+ switch network {
32+ case "tcp" :
33+ if net . ParseIP ( srcIP ). To4 () != nil {
3634 procFile = "/proc/net/tcp"
37- }
38- if dest .Address .Family () == AddressFamilyIPv6 {
35+ } else {
3936 procFile = "/proc/net/tcp6"
4037 }
41- case Network_UDP :
42- if dest . Address . Family () == AddressFamilyIPv4 {
38+ case "udp" :
39+ if net . ParseIP ( srcIP ). To4 () != nil {
4340 procFile = "/proc/net/udp"
44- }
45- if dest .Address .Family () == AddressFamilyIPv6 {
41+ } else {
4642 procFile = "/proc/net/udp6"
4743 }
4844 default :
4945 panic ("Unsupported network type for process lookup." )
5046 }
5147
52- targetHexAddr , err := formatLittleEndianString (dest . Address , dest . Port )
48+ targetHexAddr , err := formatLittleEndianString (net . ParseIP ( srcIP ), Port ( srcPort ) )
5349 if err != nil {
5450 return 0 , "" , "" , errors .New ("failed to format address: " , err )
5551 }
@@ -59,7 +55,7 @@ func FindProcess(dest Destination) (PID int, Name string, AbsolutePath string, e
5955 return 0 , "" , "" , errors .New ("could not search in " , procFile ).Base (err )
6056 }
6157 if inode == "" {
62- return 0 , "" , "" , errors .New ("connection for " , dest . Address , ":" , dest . Port , " not found in " , procFile )
58+ return 0 , "" , "" , errors .New ("connection for " , srcIP , ":" , srcPort , " not found in " , procFile )
6359 }
6460
6561 pidStr , err := findPidByInode (inode )
@@ -86,16 +82,16 @@ func FindProcess(dest Destination) (PID int, Name string, AbsolutePath string, e
8682 return pid , procName , absPath , nil
8783}
8884
89- func formatLittleEndianString (addr Address , port Port ) (string , error ) {
90- ip := addr . IP ()
85+ func formatLittleEndianString (addr net. IP , port Port ) (string , error ) {
86+ ip := addr
9187 var ipBytes []byte
92- if addr . Family () == AddressFamilyIPv4 {
88+ if ip . To4 () != nil {
9389 ipBytes = ip .To4 ()
9490 } else {
9591 ipBytes = ip .To16 ()
9692 }
9793 if ipBytes == nil {
98- return "" , errors .New ("invalid IP format for " , addr . Family () , ": " , ip )
94+ return "" , errors .New ("invalid IP format for " , addr , ": " , ip )
9995 }
10096
10197 for i , j := 0 , len (ipBytes )- 1 ; i < j ; i , j = i + 1 , j - 1 {
0 commit comments