2020-07-19 13:17:05 +08:00
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2020-07-22 20:35:27 +08:00
|
|
|
"fmt"
|
2020-07-19 13:17:05 +08:00
|
|
|
"net"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
|
2020-07-22 20:35:27 +08:00
|
|
|
"github.com/Dreamacro/clash/common/cache"
|
2020-07-19 13:17:05 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/Dreamacro/clash/log"
|
|
|
|
)
|
|
|
|
|
2020-07-22 20:35:27 +08:00
|
|
|
// store process name for when dealing with multiple PROCESS-NAME rules
|
|
|
|
var processCache = cache.NewLRUCache(cache.WithAge(2), cache.WithSize(64))
|
|
|
|
|
2020-07-19 13:17:05 +08:00
|
|
|
type Process struct {
|
|
|
|
adapter string
|
|
|
|
process string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *Process) RuleType() C.RuleType {
|
|
|
|
return C.Process
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *Process) Match(metadata *C.Metadata) bool {
|
2020-07-22 20:35:27 +08:00
|
|
|
key := fmt.Sprintf("%s:%s:%s", metadata.NetWork.String(), metadata.SrcIP.String(), metadata.SrcPort)
|
|
|
|
cached, hit := processCache.Get(key)
|
|
|
|
if !hit {
|
2020-07-29 11:27:18 +08:00
|
|
|
name, err := getExecPathFromAddress(metadata)
|
2020-07-22 20:35:27 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Debugln("[%s] getExecPathFromAddress error: %s", C.Process.String(), err.Error())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
cached = name
|
2020-07-19 13:17:05 +08:00
|
|
|
}
|
|
|
|
|
2020-07-22 20:35:27 +08:00
|
|
|
return strings.EqualFold(cached.(string), ps.process)
|
2020-07-19 13:17:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) Adapter() string {
|
|
|
|
return p.adapter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Process) Payload() string {
|
|
|
|
return p.process
|
|
|
|
}
|
|
|
|
|
2020-07-27 11:57:55 +08:00
|
|
|
func (p *Process) ShouldResolveIP() bool {
|
|
|
|
return false
|
2020-07-19 13:17:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewProcess(process string, adapter string) (*Process, error) {
|
|
|
|
return &Process{
|
|
|
|
adapter: adapter,
|
|
|
|
process: process,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
procpidpathinfo = 0xb
|
|
|
|
procpidpathinfosize = 1024
|
|
|
|
proccallnumpidinfo = 0x2
|
|
|
|
)
|
|
|
|
|
|
|
|
func getExecPathFromPID(pid uint32) (string, error) {
|
|
|
|
buf := make([]byte, procpidpathinfosize)
|
|
|
|
_, _, errno := syscall.Syscall6(
|
|
|
|
syscall.SYS_PROC_INFO,
|
|
|
|
proccallnumpidinfo,
|
|
|
|
uintptr(pid),
|
|
|
|
procpidpathinfo,
|
|
|
|
0,
|
|
|
|
uintptr(unsafe.Pointer(&buf[0])),
|
|
|
|
procpidpathinfosize)
|
|
|
|
if errno != 0 {
|
|
|
|
return "", errno
|
|
|
|
}
|
|
|
|
firstZero := bytes.IndexByte(buf, 0)
|
|
|
|
if firstZero <= 0 {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Base(string(buf[:firstZero])), nil
|
|
|
|
}
|
|
|
|
|
2020-07-29 11:27:18 +08:00
|
|
|
func getExecPathFromAddress(metadata *C.Metadata) (string, error) {
|
|
|
|
ip := metadata.SrcIP
|
|
|
|
port, err := strconv.Atoi(metadata.SrcPort)
|
2020-07-19 13:17:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-07-29 11:27:18 +08:00
|
|
|
var spath string
|
|
|
|
switch metadata.NetWork {
|
|
|
|
case C.TCP:
|
|
|
|
spath = "net.inet.tcp.pcblist_n"
|
|
|
|
case C.UDP:
|
2020-07-19 13:17:05 +08:00
|
|
|
spath = "net.inet.udp.pcblist_n"
|
2020-07-29 11:27:18 +08:00
|
|
|
default:
|
|
|
|
return "", ErrInvalidNetwork
|
2020-07-19 13:17:05 +08:00
|
|
|
}
|
|
|
|
|
2020-07-29 11:27:18 +08:00
|
|
|
isIPv4 := ip.To4() != nil
|
|
|
|
|
2020-07-19 13:17:05 +08:00
|
|
|
value, err := syscall.Sysctl(spath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := []byte(value)
|
|
|
|
|
2020-07-29 11:27:18 +08:00
|
|
|
// from darwin-xnu/bsd/netinet/in_pcblist.c:get_pcblist_n
|
|
|
|
// size/offset are round up (aligned) to 8 bytes in darwin
|
|
|
|
// rup8(sizeof(xinpcb_n)) + rup8(sizeof(xsocket_n)) +
|
|
|
|
// 2 * rup8(sizeof(xsockbuf_n)) + rup8(sizeof(xsockstat_n))
|
|
|
|
itemSize := 384
|
|
|
|
if metadata.NetWork == C.TCP {
|
|
|
|
// rup8(sizeof(xtcpcb_n))
|
|
|
|
itemSize += 208
|
|
|
|
}
|
|
|
|
// skip the first and last xinpgen(24 bytes) block
|
|
|
|
for i := 24; i < len(buf)-24; i += itemSize {
|
|
|
|
// offset of xinpcb_n and xsocket_n
|
|
|
|
inp, so := i, i+104
|
2020-07-19 13:17:05 +08:00
|
|
|
|
|
|
|
srcPort := binary.BigEndian.Uint16(buf[inp+18 : inp+20])
|
|
|
|
if uint16(port) != srcPort {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// xinpcb_n.inp_vflag
|
|
|
|
flag := buf[inp+44]
|
|
|
|
|
|
|
|
var srcIP net.IP
|
2020-07-29 11:27:18 +08:00
|
|
|
switch {
|
|
|
|
case flag&0x1 > 0 && isIPv4:
|
2020-07-19 13:17:05 +08:00
|
|
|
// ipv4
|
|
|
|
srcIP = net.IP(buf[inp+76 : inp+80])
|
2020-07-29 11:27:18 +08:00
|
|
|
case flag&0x2 > 0 && !isIPv4:
|
2020-07-19 13:17:05 +08:00
|
|
|
// ipv6
|
|
|
|
srcIP = net.IP(buf[inp+64 : inp+80])
|
2020-07-29 11:27:18 +08:00
|
|
|
default:
|
2020-07-19 13:17:05 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ip.Equal(srcIP) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// xsocket_n.so_last_pid
|
2020-07-29 11:27:18 +08:00
|
|
|
pid := readNativeUint32(buf[so+68 : so+72])
|
2020-07-19 13:17:05 +08:00
|
|
|
return getExecPathFromPID(pid)
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", errors.New("process not found")
|
|
|
|
}
|
|
|
|
|
2020-07-29 11:27:18 +08:00
|
|
|
func readNativeUint32(b []byte) uint32 {
|
|
|
|
return *(*uint32)(unsafe.Pointer(&b[0]))
|
2020-07-19 13:17:05 +08:00
|
|
|
}
|