2020-12-17 22:17:27 +08:00
|
|
|
package process
|
2020-07-31 20:01:19 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
2022-04-20 01:52:51 +08:00
|
|
|
"net/netip"
|
2020-07-31 20:01:19 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-09-07 17:43:34 +08:00
|
|
|
"sync"
|
2020-07-31 20:01:19 +08:00
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
|
2022-04-20 01:52:51 +08:00
|
|
|
"github.com/Dreamacro/clash/common/nnip"
|
2020-07-31 20:01:19 +08:00
|
|
|
"github.com/Dreamacro/clash/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// store process name for when dealing with multiple PROCESS-NAME rules
|
2020-09-07 17:43:34 +08:00
|
|
|
var (
|
|
|
|
defaultSearcher *searcher
|
|
|
|
|
|
|
|
once sync.Once
|
|
|
|
)
|
2020-07-31 20:01:19 +08:00
|
|
|
|
2022-11-12 20:43:48 +08:00
|
|
|
func resolveSocketByNetlink(network string, ip netip.Addr, srcPort int) (uint32, uint32, error) {
|
2022-04-22 16:51:01 +08:00
|
|
|
return 0, 0, ErrPlatformNotSupport
|
|
|
|
}
|
|
|
|
|
2022-11-12 20:43:48 +08:00
|
|
|
func findProcessName(network string, ip netip.Addr, srcPort int) (*uint32, string, error) {
|
2020-09-07 17:43:34 +08:00
|
|
|
once.Do(func() {
|
2020-12-17 22:17:27 +08:00
|
|
|
if err := initSearcher(); err != nil {
|
2020-09-07 17:43:34 +08:00
|
|
|
log.Errorln("Initialize PROCESS-NAME failed: %s", err.Error())
|
|
|
|
log.Warnln("All PROCESS-NAME rules will be skipped")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
2020-12-17 22:17:27 +08:00
|
|
|
|
2021-04-14 17:57:17 +08:00
|
|
|
if defaultSearcher == nil {
|
2022-11-12 20:43:48 +08:00
|
|
|
return nil, "", ErrPlatformNotSupport
|
2021-04-14 17:57:17 +08:00
|
|
|
}
|
|
|
|
|
2020-12-17 22:17:27 +08:00
|
|
|
var spath string
|
|
|
|
isTCP := network == TCP
|
|
|
|
switch network {
|
|
|
|
case TCP:
|
|
|
|
spath = "net.inet.tcp.pcblist"
|
|
|
|
case UDP:
|
|
|
|
spath = "net.inet.udp.pcblist"
|
|
|
|
default:
|
2022-11-12 20:43:48 +08:00
|
|
|
return nil, "", ErrInvalidNetwork
|
2020-12-17 22:17:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
value, err := syscall.Sysctl(spath)
|
|
|
|
if err != nil {
|
2022-05-19 20:43:41 +08:00
|
|
|
return -1, "", err
|
2020-12-17 22:17:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
buf := []byte(value)
|
|
|
|
pid, err := defaultSearcher.Search(buf, ip, uint16(srcPort), isTCP)
|
|
|
|
if err != nil {
|
2022-05-19 20:43:41 +08:00
|
|
|
return -1, "", err
|
2020-12-17 22:17:27 +08:00
|
|
|
}
|
|
|
|
|
2022-05-19 20:43:41 +08:00
|
|
|
pp, err := getExecPathFromPID(pid)
|
|
|
|
return -1, pp, err
|
2020-07-31 20:01:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func getExecPathFromPID(pid uint32) (string, error) {
|
|
|
|
buf := make([]byte, 2048)
|
|
|
|
size := uint64(len(buf))
|
|
|
|
// CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, pid
|
|
|
|
mib := [4]uint32{1, 14, 12, pid}
|
|
|
|
|
|
|
|
_, _, errno := syscall.Syscall6(
|
|
|
|
syscall.SYS___SYSCTL,
|
|
|
|
uintptr(unsafe.Pointer(&mib[0])),
|
|
|
|
uintptr(len(mib)),
|
|
|
|
uintptr(unsafe.Pointer(&buf[0])),
|
|
|
|
uintptr(unsafe.Pointer(&size)),
|
|
|
|
0,
|
|
|
|
0)
|
|
|
|
if errno != 0 || size == 0 {
|
|
|
|
return "", errno
|
|
|
|
}
|
|
|
|
|
2022-03-12 19:07:53 +08:00
|
|
|
return string(buf[:size-1]), nil
|
2020-07-31 20:01:19 +08:00
|
|
|
}
|
|
|
|
|
2020-09-07 17:43:34 +08:00
|
|
|
func readNativeUint32(b []byte) uint32 {
|
|
|
|
return *(*uint32)(unsafe.Pointer(&b[0]))
|
|
|
|
}
|
|
|
|
|
|
|
|
type searcher struct {
|
|
|
|
// sizeof(struct xinpgen)
|
|
|
|
headSize int
|
|
|
|
// sizeof(struct xtcpcb)
|
|
|
|
tcpItemSize int
|
|
|
|
// sizeof(struct xinpcb)
|
|
|
|
udpItemSize int
|
|
|
|
udpInpOffset int
|
|
|
|
port int
|
|
|
|
ip int
|
|
|
|
vflag int
|
|
|
|
socket int
|
|
|
|
|
|
|
|
// sizeof(struct xfile)
|
|
|
|
fileItemSize int
|
|
|
|
data int
|
|
|
|
pid int
|
|
|
|
}
|
|
|
|
|
2022-04-20 01:52:51 +08:00
|
|
|
func (s *searcher) Search(buf []byte, ip netip.Addr, port uint16, isTCP bool) (uint32, error) {
|
2020-09-07 17:43:34 +08:00
|
|
|
var itemSize int
|
|
|
|
var inpOffset int
|
|
|
|
|
|
|
|
if isTCP {
|
|
|
|
// struct xtcpcb
|
|
|
|
itemSize = s.tcpItemSize
|
|
|
|
inpOffset = 8
|
|
|
|
} else {
|
|
|
|
// struct xinpcb
|
|
|
|
itemSize = s.udpItemSize
|
|
|
|
inpOffset = s.udpInpOffset
|
|
|
|
}
|
|
|
|
|
2022-04-20 01:52:51 +08:00
|
|
|
isIPv4 := ip.Is4()
|
2020-09-07 17:43:34 +08:00
|
|
|
// skip the first xinpgen block
|
|
|
|
for i := s.headSize; i+itemSize <= len(buf); i += itemSize {
|
2020-07-31 20:01:19 +08:00
|
|
|
inp := i + inpOffset
|
|
|
|
|
2020-09-07 17:43:34 +08:00
|
|
|
srcPort := binary.BigEndian.Uint16(buf[inp+s.port : inp+s.port+2])
|
2020-07-31 20:01:19 +08:00
|
|
|
|
2020-09-07 17:43:34 +08:00
|
|
|
if port != srcPort {
|
2020-07-31 20:01:19 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// xinpcb.inp_vflag
|
2020-09-07 17:43:34 +08:00
|
|
|
flag := buf[inp+s.vflag]
|
2020-07-31 20:01:19 +08:00
|
|
|
|
2022-04-20 01:52:51 +08:00
|
|
|
var srcIP netip.Addr
|
2020-07-31 20:01:19 +08:00
|
|
|
switch {
|
|
|
|
case flag&0x1 > 0 && isIPv4:
|
|
|
|
// ipv4
|
2022-04-20 01:52:51 +08:00
|
|
|
srcIP = nnip.IpToAddr(buf[inp+s.ip : inp+s.ip+4])
|
2020-07-31 20:01:19 +08:00
|
|
|
case flag&0x2 > 0 && !isIPv4:
|
|
|
|
// ipv6
|
2022-04-20 01:52:51 +08:00
|
|
|
srcIP = nnip.IpToAddr(buf[inp+s.ip-12 : inp+s.ip+4])
|
2020-07-31 20:01:19 +08:00
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-04-20 01:52:51 +08:00
|
|
|
if ip != srcIP {
|
2020-07-31 20:01:19 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// xsocket.xso_so, interpreted as big endian anyway since it's only used for comparison
|
2020-09-07 17:43:34 +08:00
|
|
|
socket := binary.BigEndian.Uint64(buf[inp+s.socket : inp+s.socket+8])
|
|
|
|
return s.searchSocketPid(socket)
|
|
|
|
}
|
2020-12-17 22:17:27 +08:00
|
|
|
return 0, ErrNotFound
|
2020-09-07 17:43:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *searcher) searchSocketPid(socket uint64) (uint32, error) {
|
|
|
|
value, err := syscall.Sysctl("kern.file")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := []byte(value)
|
|
|
|
|
|
|
|
// struct xfile
|
|
|
|
itemSize := s.fileItemSize
|
|
|
|
for i := 0; i+itemSize <= len(buf); i += itemSize {
|
|
|
|
// xfile.xf_data
|
|
|
|
data := binary.BigEndian.Uint64(buf[i+s.data : i+s.data+8])
|
|
|
|
if data == socket {
|
|
|
|
// xfile.xf_pid
|
|
|
|
pid := readNativeUint32(buf[i+s.pid : i+s.pid+4])
|
|
|
|
return pid, nil
|
2020-07-31 20:01:19 +08:00
|
|
|
}
|
|
|
|
}
|
2020-12-17 22:17:27 +08:00
|
|
|
return 0, ErrNotFound
|
2020-09-07 17:43:34 +08:00
|
|
|
}
|
2020-07-31 20:01:19 +08:00
|
|
|
|
2020-09-07 17:43:34 +08:00
|
|
|
func newSearcher(major int) *searcher {
|
2021-03-24 01:00:21 +08:00
|
|
|
var s *searcher
|
2020-09-07 17:43:34 +08:00
|
|
|
switch major {
|
|
|
|
case 11:
|
|
|
|
s = &searcher{
|
|
|
|
headSize: 32,
|
|
|
|
tcpItemSize: 1304,
|
|
|
|
udpItemSize: 632,
|
|
|
|
port: 198,
|
|
|
|
ip: 228,
|
|
|
|
vflag: 116,
|
|
|
|
socket: 88,
|
|
|
|
fileItemSize: 80,
|
|
|
|
data: 56,
|
|
|
|
pid: 8,
|
|
|
|
udpInpOffset: 8,
|
|
|
|
}
|
|
|
|
case 12:
|
2021-04-14 17:57:17 +08:00
|
|
|
fallthrough
|
|
|
|
case 13:
|
2020-09-07 17:43:34 +08:00
|
|
|
s = &searcher{
|
|
|
|
headSize: 64,
|
|
|
|
tcpItemSize: 744,
|
|
|
|
udpItemSize: 400,
|
|
|
|
port: 254,
|
|
|
|
ip: 284,
|
|
|
|
vflag: 392,
|
|
|
|
socket: 16,
|
|
|
|
fileItemSize: 128,
|
|
|
|
data: 56,
|
|
|
|
pid: 8,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
2020-07-31 20:01:19 +08:00
|
|
|
}
|
|
|
|
|
2020-09-07 17:43:34 +08:00
|
|
|
func initSearcher() error {
|
|
|
|
osRelease, err := syscall.Sysctl("kern.osrelease")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dot := strings.Index(osRelease, ".")
|
|
|
|
if dot != -1 {
|
|
|
|
osRelease = osRelease[:dot]
|
|
|
|
}
|
|
|
|
major, err := strconv.Atoi(osRelease)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defaultSearcher = newSearcher(major)
|
|
|
|
if defaultSearcher == nil {
|
|
|
|
return fmt.Errorf("unsupported freebsd version %d", major)
|
|
|
|
}
|
|
|
|
return nil
|
2020-07-31 20:01:19 +08:00
|
|
|
}
|