2022-03-17 12:31:16 +00:00
|
|
|
//go:build !android
|
|
|
|
|
2020-12-17 14:17:27 +00:00
|
|
|
package process
|
2020-07-22 11:05:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2021-10-09 12:35:06 +00:00
|
|
|
"os"
|
2020-07-22 11:05:10 +00:00
|
|
|
"path"
|
2022-03-05 10:25:16 +00:00
|
|
|
"strings"
|
2020-07-22 11:05:10 +00:00
|
|
|
"syscall"
|
2022-03-05 10:25:16 +00:00
|
|
|
"unicode"
|
2020-07-22 11:05:10 +00:00
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/common/pool"
|
|
|
|
)
|
|
|
|
|
|
|
|
// from https://github.com/vishvananda/netlink/blob/bca67dfc8220b44ef582c9da4e9172bf1c9ec973/nl/nl_linux.go#L52-L62
|
2021-07-11 11:42:54 +00:00
|
|
|
var nativeEndian = func() binary.ByteOrder {
|
2020-07-22 11:05:10 +00:00
|
|
|
var x uint32 = 0x01020304
|
|
|
|
if *(*byte)(unsafe.Pointer(&x)) == 0x01 {
|
2021-07-11 11:42:54 +00:00
|
|
|
return binary.BigEndian
|
2020-07-22 11:05:10 +00:00
|
|
|
}
|
2021-07-11 11:42:54 +00:00
|
|
|
|
|
|
|
return binary.LittleEndian
|
|
|
|
}()
|
2020-07-22 11:05:10 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
sizeOfSocketDiagRequest = syscall.SizeofNlMsghdr + 8 + 48
|
|
|
|
socketDiagByFamily = 20
|
|
|
|
pathProc = "/proc"
|
|
|
|
)
|
|
|
|
|
2020-12-17 14:17:27 +00:00
|
|
|
func findProcessName(network string, ip net.IP, srcPort int) (string, error) {
|
2022-03-09 05:41:50 +00:00
|
|
|
inode, uid, err := resolveSocketByNetlink(network, ip, srcPort)
|
2020-07-22 11:05:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-03-09 05:41:50 +00:00
|
|
|
return resolveProcessNameByProcSearch(inode, uid)
|
2020-07-22 11:05:10 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 05:41:50 +00:00
|
|
|
func resolveSocketByNetlink(network string, ip net.IP, srcPort int) (int32, int32, error) {
|
2020-07-22 11:05:10 +00:00
|
|
|
var family byte
|
|
|
|
var protocol byte
|
|
|
|
|
2020-12-17 14:17:27 +00:00
|
|
|
switch network {
|
|
|
|
case TCP:
|
2020-07-22 11:05:10 +00:00
|
|
|
protocol = syscall.IPPROTO_TCP
|
2020-12-17 14:17:27 +00:00
|
|
|
case UDP:
|
2020-07-22 11:05:10 +00:00
|
|
|
protocol = syscall.IPPROTO_UDP
|
|
|
|
default:
|
|
|
|
return 0, 0, ErrInvalidNetwork
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:17:27 +00:00
|
|
|
if ip.To4() != nil {
|
2020-07-22 11:05:10 +00:00
|
|
|
family = syscall.AF_INET
|
|
|
|
} else {
|
|
|
|
family = syscall.AF_INET6
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:17:27 +00:00
|
|
|
req := packSocketDiagRequest(family, protocol, ip, uint16(srcPort))
|
2020-07-22 11:05:10 +00:00
|
|
|
|
|
|
|
socket, err := syscall.Socket(syscall.AF_NETLINK, syscall.SOCK_DGRAM, syscall.NETLINK_INET_DIAG)
|
|
|
|
if err != nil {
|
2022-03-09 05:41:50 +00:00
|
|
|
return 0, 0, fmt.Errorf("dial netlink: %w", err)
|
2020-07-22 11:05:10 +00:00
|
|
|
}
|
|
|
|
defer syscall.Close(socket)
|
|
|
|
|
2022-03-12 11:07:53 +00:00
|
|
|
syscall.SetsockoptTimeval(socket, syscall.SOL_SOCKET, syscall.SO_SNDTIMEO, &syscall.Timeval{Usec: 100})
|
|
|
|
syscall.SetsockoptTimeval(socket, syscall.SOL_SOCKET, syscall.SO_RCVTIMEO, &syscall.Timeval{Usec: 100})
|
2020-07-22 11:05:10 +00:00
|
|
|
|
|
|
|
if err := syscall.Connect(socket, &syscall.SockaddrNetlink{
|
|
|
|
Family: syscall.AF_NETLINK,
|
|
|
|
Pad: 0,
|
|
|
|
Pid: 0,
|
|
|
|
Groups: 0,
|
|
|
|
}); err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := syscall.Write(socket, req); err != nil {
|
2022-03-09 05:41:50 +00:00
|
|
|
return 0, 0, fmt.Errorf("write request: %w", err)
|
2020-07-22 11:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rb := pool.Get(pool.RelayBufferSize)
|
|
|
|
defer pool.Put(rb)
|
|
|
|
|
|
|
|
n, err := syscall.Read(socket, rb)
|
|
|
|
if err != nil {
|
2022-03-09 05:41:50 +00:00
|
|
|
return 0, 0, fmt.Errorf("read response: %w", err)
|
2020-07-22 11:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
messages, err := syscall.ParseNetlinkMessage(rb[:n])
|
|
|
|
if err != nil {
|
2022-03-09 05:41:50 +00:00
|
|
|
return 0, 0, fmt.Errorf("parse netlink message: %w", err)
|
2020-07-22 11:05:10 +00:00
|
|
|
} else if len(messages) == 0 {
|
2022-03-09 05:41:50 +00:00
|
|
|
return 0, 0, fmt.Errorf("unexcepted netlink response")
|
2020-07-22 11:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
message := messages[0]
|
|
|
|
if message.Header.Type&syscall.NLMSG_ERROR != 0 {
|
2022-03-09 05:41:50 +00:00
|
|
|
return 0, 0, fmt.Errorf("netlink message: NLMSG_ERROR")
|
2020-07-22 11:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uid, inode := unpackSocketDiagResponse(&messages[0])
|
2022-03-09 05:41:50 +00:00
|
|
|
if uid < 0 || inode < 0 {
|
|
|
|
return 0, 0, fmt.Errorf("invalid uid(%d) or inode(%d)", uid, inode)
|
|
|
|
}
|
2020-07-22 11:05:10 +00:00
|
|
|
|
2022-03-17 15:24:07 +00:00
|
|
|
return inode, uid, nil
|
2020-07-22 11:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func packSocketDiagRequest(family, protocol byte, source net.IP, sourcePort uint16) []byte {
|
|
|
|
s := make([]byte, 16)
|
|
|
|
|
|
|
|
if v4 := source.To4(); v4 != nil {
|
|
|
|
copy(s, v4)
|
|
|
|
} else {
|
|
|
|
copy(s, source)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, sizeOfSocketDiagRequest)
|
|
|
|
|
|
|
|
nativeEndian.PutUint32(buf[0:4], sizeOfSocketDiagRequest)
|
|
|
|
nativeEndian.PutUint16(buf[4:6], socketDiagByFamily)
|
|
|
|
nativeEndian.PutUint16(buf[6:8], syscall.NLM_F_REQUEST|syscall.NLM_F_DUMP)
|
|
|
|
nativeEndian.PutUint32(buf[8:12], 0)
|
|
|
|
nativeEndian.PutUint32(buf[12:16], 0)
|
|
|
|
|
|
|
|
buf[16] = family
|
|
|
|
buf[17] = protocol
|
|
|
|
buf[18] = 0
|
|
|
|
buf[19] = 0
|
|
|
|
nativeEndian.PutUint32(buf[20:24], 0xFFFFFFFF)
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint16(buf[24:26], sourcePort)
|
|
|
|
binary.BigEndian.PutUint16(buf[26:28], 0)
|
|
|
|
|
|
|
|
copy(buf[28:44], s)
|
|
|
|
copy(buf[44:60], net.IPv6zero)
|
|
|
|
|
|
|
|
nativeEndian.PutUint32(buf[60:64], 0)
|
|
|
|
nativeEndian.PutUint64(buf[64:72], 0xFFFFFFFFFFFFFFFF)
|
|
|
|
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2022-03-09 05:41:50 +00:00
|
|
|
func unpackSocketDiagResponse(msg *syscall.NetlinkMessage) (inode, uid int32) {
|
2020-07-22 11:05:10 +00:00
|
|
|
if len(msg.Data) < 72 {
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
data := msg.Data
|
|
|
|
|
2022-03-09 05:41:50 +00:00
|
|
|
uid = int32(nativeEndian.Uint32(data[64:68]))
|
|
|
|
inode = int32(nativeEndian.Uint32(data[68:72]))
|
2020-07-22 11:05:10 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-09 05:41:50 +00:00
|
|
|
func resolveProcessNameByProcSearch(inode, uid int32) (string, error) {
|
2021-10-09 12:35:06 +00:00
|
|
|
files, err := os.ReadDir(pathProc)
|
2020-07-22 11:05:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer := make([]byte, syscall.PathMax)
|
|
|
|
socket := []byte(fmt.Sprintf("socket:[%d]", inode))
|
|
|
|
|
|
|
|
for _, f := range files {
|
2020-07-22 12:22:34 +00:00
|
|
|
if !f.IsDir() || !isPid(f.Name()) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-10-09 12:35:06 +00:00
|
|
|
info, err := f.Info()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if info.Sys().(*syscall.Stat_t).Uid != uint32(uid) {
|
2020-07-22 11:05:10 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
processPath := path.Join(pathProc, f.Name())
|
|
|
|
fdPath := path.Join(processPath, "fd")
|
|
|
|
|
2021-10-09 12:35:06 +00:00
|
|
|
fds, err := os.ReadDir(fdPath)
|
2020-07-22 11:05:10 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fd := range fds {
|
|
|
|
n, err := syscall.Readlink(path.Join(fdPath, fd.Name()), buffer)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-08-25 14:36:38 +00:00
|
|
|
if bytes.Equal(buffer[:n], socket) {
|
2022-03-12 11:07:53 +00:00
|
|
|
return os.Readlink(path.Join(processPath, "exe"))
|
2020-07-22 11:05:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-09 05:41:50 +00:00
|
|
|
return "", fmt.Errorf("process of uid(%d),inode(%d) not found", uid, inode)
|
2020-07-22 11:05:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func isPid(s string) bool {
|
2022-03-05 10:25:16 +00:00
|
|
|
return strings.IndexFunc(s, func(r rune) bool {
|
|
|
|
return !unicode.IsDigit(r)
|
|
|
|
}) == -1
|
2020-07-22 11:05:10 +00:00
|
|
|
}
|