Feature: add process_path to script metadata
This commit is contained in:
parent
13d19ff101
commit
160e630f03
@ -256,6 +256,7 @@ interface Metadata {
|
||||
network: string // tcp
|
||||
host: string
|
||||
process_name: string
|
||||
process_path: string
|
||||
src_ip: string
|
||||
src_port: int
|
||||
dst_ip: string
|
||||
|
@ -267,6 +267,7 @@ RuleProvider_match(RuleProviderObject *self, PyObject *args)
|
||||
.type = PyUnicode_AsUTF8(PyDict_GetItemString(tmp, "type")), // PyDict_GetItemString() Return value: Borrowed reference.
|
||||
.network = PyUnicode_AsUTF8(PyDict_GetItemString(tmp, "network")),
|
||||
.process_name = PyUnicode_AsUTF8(PyDict_GetItemString(tmp, "process_name")),
|
||||
.process_path = PyUnicode_AsUTF8(PyDict_GetItemString(tmp, "process_path")),
|
||||
.host = PyUnicode_AsUTF8(PyDict_GetItemString(tmp, "host")),
|
||||
.src_ip = PyUnicode_AsUTF8(PyDict_GetItemString(tmp, "src_ip")),
|
||||
.src_port = (unsigned short)PyLong_AsUnsignedLong(PyDict_GetItemString(tmp, "src_port")),
|
||||
@ -584,6 +585,7 @@ const char *call_main(
|
||||
const char *type,
|
||||
const char *network,
|
||||
const char *process_name,
|
||||
const char *process_path,
|
||||
const char *host,
|
||||
const char *src_ip,
|
||||
unsigned short src_port,
|
||||
@ -605,6 +607,7 @@ const char *call_main(
|
||||
PyObject *p_type = PyUnicode_FromString(type); //Return value: New reference.
|
||||
PyObject *p_network = PyUnicode_FromString(network); //Return value: New reference.
|
||||
PyObject *p_process_name = PyUnicode_FromString(process_name); //Return value: New reference.
|
||||
PyObject *p_process_path = PyUnicode_FromString(process_path); //Return value: New reference.
|
||||
PyObject *p_host = PyUnicode_FromString(host); //Return value: New reference.
|
||||
PyObject *p_src_ip = PyUnicode_FromString(src_ip); //Return value: New reference.
|
||||
PyObject *p_src_port = PyLong_FromUnsignedLong((unsigned long)src_port); //Return value: New reference.
|
||||
@ -614,6 +617,7 @@ const char *call_main(
|
||||
PyDict_SetItemString(metadataDict, "type", p_type); //Parameter value: New reference.
|
||||
PyDict_SetItemString(metadataDict, "network", p_network); //Parameter value: New reference.
|
||||
PyDict_SetItemString(metadataDict, "process_name", p_process_name); //Parameter value: New reference.
|
||||
PyDict_SetItemString(metadataDict, "process_path", p_process_path); //Parameter value: New reference.
|
||||
PyDict_SetItemString(metadataDict, "host", p_host); //Parameter value: New reference.
|
||||
PyDict_SetItemString(metadataDict, "src_ip", p_src_ip); //Parameter value: New reference.
|
||||
PyDict_SetItemString(metadataDict, "src_port", p_src_port); //Parameter value: New reference.
|
||||
@ -623,6 +627,7 @@ const char *call_main(
|
||||
Py_DECREF(p_type);
|
||||
Py_DECREF(p_network);
|
||||
Py_DECREF(p_process_name);
|
||||
Py_DECREF(p_process_path);
|
||||
Py_DECREF(p_host);
|
||||
Py_DECREF(p_src_ip);
|
||||
Py_DECREF(p_src_port);
|
||||
@ -668,6 +673,7 @@ int call_shortcut(PyObject *shortcut_fn,
|
||||
const char *type,
|
||||
const char *network,
|
||||
const char *process_name,
|
||||
const char *process_path,
|
||||
const char *host,
|
||||
const char *src_ip,
|
||||
unsigned short src_port,
|
||||
@ -677,10 +683,11 @@ int call_shortcut(PyObject *shortcut_fn,
|
||||
PyObject *args;
|
||||
PyObject *result;
|
||||
|
||||
args = Py_BuildValue("{s:O, s:s, s:s, s:s, s:s, s:H, s:s, s:H}",
|
||||
args = Py_BuildValue("{s:O, s:s, s:s, s:s, s:s, s:s, s:H, s:s, s:H}",
|
||||
"ctx", clash_context,
|
||||
"network", network,
|
||||
"process_name", process_name,
|
||||
"process_path", process_path,
|
||||
"host", host,
|
||||
"src_ip", src_ip,
|
||||
"src_port", src_port,
|
||||
@ -727,9 +734,9 @@ void finalize_Python() {
|
||||
Py_CLEAR(clash_module);
|
||||
Py_FinalizeEx();
|
||||
|
||||
// clash_module = NULL;
|
||||
// main_fn = NULL;
|
||||
// clash_context = NULL;
|
||||
clash_module = NULL;
|
||||
main_fn = NULL;
|
||||
clash_context = NULL;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------- */
|
@ -186,6 +186,7 @@ func CallPyMainFunction(mtd *constant.Metadata) (string, error) {
|
||||
_type := C.CString(mtd.Type.String())
|
||||
network := C.CString(mtd.NetWork.String())
|
||||
processName := C.CString(mtd.Process)
|
||||
processPath := C.CString(mtd.ProcessPath)
|
||||
host := C.CString(mtd.Host)
|
||||
|
||||
srcPortGo, _ := strconv.ParseUint(mtd.SrcPort, 10, 16)
|
||||
@ -208,6 +209,7 @@ func CallPyMainFunction(mtd *constant.Metadata) (string, error) {
|
||||
C.free(unsafe.Pointer(_type))
|
||||
C.free(unsafe.Pointer(network))
|
||||
C.free(unsafe.Pointer(processName))
|
||||
C.free(unsafe.Pointer(processPath))
|
||||
C.free(unsafe.Pointer(host))
|
||||
C.free(unsafe.Pointer(srcIp))
|
||||
C.free(unsafe.Pointer(dstIp))
|
||||
@ -217,7 +219,7 @@ func CallPyMainFunction(mtd *constant.Metadata) (string, error) {
|
||||
gilState := PyGILState_Ensure()
|
||||
defer PyGILState_Release(gilState)
|
||||
|
||||
cRs := C.call_main(_type, network, processName, host, srcIp, srcPort, dstIp, dstPort)
|
||||
cRs := C.call_main(_type, network, processName, processPath, host, srcIp, srcPort, dstIp, dstPort)
|
||||
|
||||
rs := C.GoString(cRs)
|
||||
if rs == "-1" {
|
||||
@ -241,6 +243,7 @@ func CallPyShortcut(fn *PyObject, mtd *constant.Metadata) (bool, error) {
|
||||
_type := C.CString(mtd.Type.String())
|
||||
network := C.CString(mtd.NetWork.String())
|
||||
processName := C.CString(mtd.Process)
|
||||
processPath := C.CString(mtd.ProcessPath)
|
||||
host := C.CString(mtd.Host)
|
||||
|
||||
srcPortGo, _ := strconv.ParseUint(mtd.SrcPort, 10, 16)
|
||||
@ -263,6 +266,7 @@ func CallPyShortcut(fn *PyObject, mtd *constant.Metadata) (bool, error) {
|
||||
C.free(unsafe.Pointer(_type))
|
||||
C.free(unsafe.Pointer(network))
|
||||
C.free(unsafe.Pointer(processName))
|
||||
C.free(unsafe.Pointer(processPath))
|
||||
C.free(unsafe.Pointer(host))
|
||||
C.free(unsafe.Pointer(srcIp))
|
||||
C.free(unsafe.Pointer(dstIp))
|
||||
@ -272,7 +276,7 @@ func CallPyShortcut(fn *PyObject, mtd *constant.Metadata) (bool, error) {
|
||||
gilState := PyGILState_Ensure()
|
||||
defer PyGILState_Release(gilState)
|
||||
|
||||
cRs := C.call_shortcut(toc(fn), _type, network, processName, host, srcIp, srcPort, dstIp, dstPort)
|
||||
cRs := C.call_shortcut(toc(fn), _type, network, processName, processPath, host, srcIp, srcPort, dstIp, dstPort)
|
||||
|
||||
rs := int(cRs)
|
||||
if rs == -1 {
|
||||
|
@ -9,6 +9,7 @@ struct Metadata {
|
||||
const char *type; /* type socks5/http */
|
||||
const char *network; /* network tcp/udp */
|
||||
const char *process_name;
|
||||
const char *process_path;
|
||||
const char *host;
|
||||
const char *src_ip;
|
||||
unsigned short src_port;
|
||||
@ -43,6 +44,7 @@ const char *call_main(
|
||||
const char *type,
|
||||
const char *network,
|
||||
const char *process_name,
|
||||
const char *process_path,
|
||||
const char *host,
|
||||
const char *src_ip,
|
||||
unsigned short src_port,
|
||||
@ -53,6 +55,7 @@ int call_shortcut(PyObject *shortcut_fn,
|
||||
const char *type,
|
||||
const char *network,
|
||||
const char *process_name,
|
||||
const char *process_path,
|
||||
const char *host,
|
||||
const char *src_ip,
|
||||
unsigned short src_port,
|
||||
|
@ -85,17 +85,16 @@ func ruleProviderCallbackFn(cProviderName *C.char, cMetadata *C.struct_Metadata)
|
||||
//_type := C.GoString(cMetadata._type)
|
||||
//network := C.GoString(cMetadata.network)
|
||||
processName := C.GoString(cMetadata.process_name)
|
||||
processPath := C.GoString(cMetadata.process_path)
|
||||
host := C.GoString(cMetadata.host)
|
||||
srcIp := C.GoString(cMetadata.src_ip)
|
||||
srcPort := strconv.Itoa(int(cMetadata.src_port))
|
||||
dstIp := C.GoString(cMetadata.dst_ip)
|
||||
dstPort := strconv.Itoa(int(cMetadata.dst_port))
|
||||
|
||||
dst, err := netip.ParseAddr(dstIp)
|
||||
|
||||
addrType := constant.AtypDomainName
|
||||
if err == nil {
|
||||
if dst.Is4() {
|
||||
if h, err := netip.ParseAddr(host); err == nil {
|
||||
if h.Is4() {
|
||||
addrType = constant.AtypIPv4
|
||||
} else {
|
||||
addrType = constant.AtypIPv6
|
||||
@ -103,15 +102,17 @@ func ruleProviderCallbackFn(cProviderName *C.char, cMetadata *C.struct_Metadata)
|
||||
}
|
||||
|
||||
src, _ := netip.ParseAddr(srcIp)
|
||||
dst, _ := netip.ParseAddr(dstIp)
|
||||
|
||||
metadata := &constant.Metadata{
|
||||
Process: processName,
|
||||
SrcIP: src,
|
||||
DstIP: dst,
|
||||
SrcPort: srcPort,
|
||||
DstPort: dstPort,
|
||||
AddrType: addrType,
|
||||
Host: host,
|
||||
AddrType: addrType,
|
||||
SrcIP: src,
|
||||
DstIP: dst,
|
||||
SrcPort: srcPort,
|
||||
DstPort: dstPort,
|
||||
Host: host,
|
||||
Process: processName,
|
||||
ProcessPath: processPath,
|
||||
}
|
||||
|
||||
providerName := C.GoString(cProviderName)
|
||||
|
@ -909,20 +909,20 @@ time = ClashTime()
|
||||
return fmt.Errorf("initialized rule SCRIPT failure, shortcut [%s] code invalid syntax", k)
|
||||
}
|
||||
|
||||
content += "def " + strings.ToLower(k) + "(ctx, network, process_name, host, src_ip, src_port, dst_ip, dst_port):\n return " + v + "\n\n"
|
||||
content += "def " + strings.ToLower(k) + "(ctx, network, process_name, process_path, host, src_ip, src_port, dst_ip, dst_port):\n return " + v + "\n\n"
|
||||
}
|
||||
|
||||
err := os.WriteFile(C.Path.Script(), []byte(content), 0o644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("initialized script module failure, %s", err.Error())
|
||||
return fmt.Errorf("initialized script module failure, %w", err)
|
||||
}
|
||||
|
||||
if err = S.Py_Initialize(C.Path.GetExecutableFullPath(), C.Path.ScriptDir()); err != nil {
|
||||
return fmt.Errorf("initialized script module failure, %s", err.Error())
|
||||
return fmt.Errorf("initialized script module failure, %w", err)
|
||||
}
|
||||
|
||||
if err = S.LoadMainFunction(); err != nil {
|
||||
return fmt.Errorf("initialized script module failure, %s", err.Error())
|
||||
return fmt.Errorf("initialized script module failure, %w", err)
|
||||
}
|
||||
|
||||
log.Infoln("Start initial script module successful, version: %s", S.Py_GetVersion())
|
||||
|
Reference in New Issue
Block a user