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 | |
|---|---|---|---|
|
|
739a2dcd84 |
@@ -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 procFiles []string
|
var procFile string
|
||||||
|
|
||||||
switch network {
|
switch network {
|
||||||
case "tcp":
|
case "tcp":
|
||||||
if net.ParseIP(srcIP).To4() != nil {
|
if net.ParseIP(srcIP).To4() != nil {
|
||||||
procFiles = []string{"/proc/net/tcp"}
|
procFile = "/proc/net/tcp"
|
||||||
} else {
|
} else {
|
||||||
procFiles = []string{"/proc/net/tcp6"}
|
procFile = "/proc/net/tcp6"
|
||||||
}
|
}
|
||||||
case "udp":
|
case "udp":
|
||||||
if net.ParseIP(srcIP).To4() != nil {
|
if net.ParseIP(srcIP).To4() != nil {
|
||||||
procFiles = []string{"/proc/net/udp", "/proc/net/udp6"}
|
procFile = "/proc/net/udp"
|
||||||
} else {
|
} else {
|
||||||
procFiles = []string{"/proc/net/udp6", "/proc/net/udp"}
|
procFile = "/proc/net/udp6"
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
panic("Unsupported network type for process lookup.")
|
panic("Unsupported network type for process lookup.")
|
||||||
@@ -50,17 +50,12 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
portFallback := ""
|
inode, err := findInodeInFile(procFile, targetHexAddr)
|
||||||
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 ", strings.Join(procFiles, " and ")).Base(err)
|
return 0, "", "", errors.New("could not search in ", procFile).Base(err)
|
||||||
}
|
}
|
||||||
if inode == "" {
|
if inode == "" {
|
||||||
return 0, "", "", errors.New("connection for ", srcIP, ":", srcPort, " not found in ", strings.Join(procFiles, " or "))
|
return 0, "", "", errors.New("connection for ", srcIP, ":", srcPort, " not found in ", procFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
pidStr, err := findPidByInode(inode)
|
pidStr, err := findPidByInode(inode)
|
||||||
@@ -107,43 +102,31 @@ 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 findInodeInFiles(filePaths []string, targetHexAddr, portFallback string) (string, error) {
|
func findInodeInFile(filePath, targetHexAddr string) (string, error) {
|
||||||
onlyMatchedPort := ""
|
file, err := os.Open(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
for i, filePath := range filePaths {
|
scanner := bufio.NewScanner(file)
|
||||||
file, err := os.Open(filePath)
|
|
||||||
if err != nil {
|
for scanner.Scan() {
|
||||||
if i > 0 && os.IsNotExist(err) {
|
line := scanner.Text()
|
||||||
continue
|
fields := strings.Fields(line)
|
||||||
}
|
|
||||||
return "", err
|
if len(fields) < 10 {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(file)
|
localAddress := fields[1]
|
||||||
for scanner.Scan() {
|
if localAddress == targetHexAddr {
|
||||||
fields := strings.Fields(scanner.Text())
|
inode := fields[9]
|
||||||
if len(fields) < 10 {
|
return inode, nil
|
||||||
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 onlyMatchedPort, nil
|
return "", scanner.Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
func findPidByInode(inode string) (string, error) {
|
func findPidByInode(inode string) (string, error) {
|
||||||
|
|||||||
@@ -2,10 +2,10 @@ package socks
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net"
|
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/xtls/xray-core/common/net"
|
||||||
"github.com/xtls/xray-core/common/signal"
|
"github.com/xtls/xray-core/common/signal"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -14,6 +14,9 @@ func NewTempUDPConn(udpConn net.PacketConn, tcpConn net.Conn, expectedRemote *ne
|
|||||||
PacketConn: udpConn,
|
PacketConn: udpConn,
|
||||||
AssociatedTCPConn: tcpConn,
|
AssociatedTCPConn: tcpConn,
|
||||||
}
|
}
|
||||||
|
t.Timer = signal.CancelAfterInactivity(context.Background(), func() {
|
||||||
|
t.Close()
|
||||||
|
}, net.ConnIdleTimeout)
|
||||||
t.ExpectedRemote.Store(expectedRemote)
|
t.ExpectedRemote.Store(expectedRemote)
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
@@ -60,9 +63,7 @@ func (c *TempUDPConn) RemoteAddr() net.Addr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *TempUDPConn) SetTimeout(d time.Duration) {
|
func (c *TempUDPConn) SetTimeout(d time.Duration) {
|
||||||
c.Timer = signal.CancelAfterInactivity(context.Background(), func() {
|
c.Timer.SetTimeout(d)
|
||||||
c.Close()
|
|
||||||
}, d)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *TempUDPConn) Close() error {
|
func (c *TempUDPConn) Close() error {
|
||||||
|
|||||||
Reference in New Issue
Block a user