Chore: script built

This commit is contained in:
yaling888
2021-10-27 23:10:11 +08:00
parent ab12b440aa
commit 5a27df899f
13 changed files with 131 additions and 97 deletions

View File

@ -1,25 +1,25 @@
//go:build !build_local
// +build !build_local
//go:build !build_local && cgo
// +build !build_local,cgo
package script
/*
//#cgo linux,amd64 pkg-config: python-3.9-embed
#cgo linux,amd64 pkg-config: python3-embed
#cgo darwin,amd64 CFLAGS: -I/build/python/python-3.9.7-darwin-amd64/include/python3.9
#cgo darwin,arm64 CFLAGS: -I/build/python/python-3.9.7-darwin-arm64/include/python3.9
#cgo windows,amd64 CFLAGS: -I/build/python/python-3.9.7-windows-amd64/include -DMS_WIN64
#cgo windows,386 CFLAGS: -I/build/python/python-3.9.7-windows-386/include
#cgo linux,amd64 CFLAGS: -I/home/runner/work/clash/clash/bin/python/python-3.9.7-linux-amd64/include/python3.9
#cgo linux,arm64 CFLAGS: -I/build/python/python-3.9.7-linux-arm64/include/python3.9
#cgo linux,386 CFLAGS: -I/build/python/python-3.9.7-linux-386/include/python3.9
//#cgo linux,amd64 CFLAGS: -I/home/runner/work/clash/clash/bin/python/python-3.9.7-linux-amd64/include/python3.9
//#cgo linux,arm64 CFLAGS: -I/build/python/python-3.9.7-linux-arm64/include/python3.9
//#cgo linux,386 CFLAGS: -I/build/python/python-3.9.7-linux-386/include/python3.9
#cgo darwin,amd64 LDFLAGS: -L/build/python/python-3.9.7-darwin-amd64/lib -lpython3.9 -ldl -framework CoreFoundation
#cgo darwin,arm64 LDFLAGS: -L/build/python/python-3.9.7-darwin-arm64/lib -lpython3.9 -ldl -framework CoreFoundation
#cgo windows,amd64 LDFLAGS: -L/build/python/python-3.9.7-windows-amd64/lib -lpython39 -lpthread -lm
#cgo windows,386 LDFLAGS: -L/build/python/python-3.9.7-windows-386/lib -lpython39 -lpthread -lm
#cgo linux,amd64 LDFLAGS: -L/home/runner/work/clash/clash/bin/python/python-3.9.7-linux-amd64/lib -lpython3.9 -lpthread -ldl -lutil -lm
#cgo linux,arm64 LDFLAGS: -L/build/python/python-3.9.7-linux-arm64/lib -lpython3.9 -lpthread -ldl -lutil -lm
#cgo linux,386 LDFLAGS: -L/build/python/python-3.9.7-linux-386/lib -lpython3.9 -lcrypt -lpthread -ldl -lutil -lm
//#cgo linux,amd64 LDFLAGS: -L/home/runner/work/clash/clash/bin/python/python-3.9.7-linux-amd64/lib -lpython3.9 -lpthread -ldl -lutil -lm
//#cgo linux,arm64 LDFLAGS: -L/build/python/python-3.9.7-linux-arm64/lib -lpython3.9 -lpthread -ldl -lutil -lm
//#cgo linux,386 LDFLAGS: -L/build/python/python-3.9.7-linux-386/lib -lpython3.9 -lpthread -ldl -lutil -lm
*/
import "C"

View File

@ -8,23 +8,30 @@ PyObject *main_fn;
PyObject *clash_context;
// init_python
void init_python(const char *path) {
void init_python(const char *program, const char *path) {
// Py_NoSiteFlag = 1;
// Py_FrozenFlag = 1;
// Py_IgnoreEnvironmentFlag = 1;
// Py_IsolatedFlag = 1;
append_inittab();
Py_Initialize();
wchar_t *program = Py_DecodeLocale("clash", NULL);
if (program != NULL) {
Py_SetProgramName(program);
PyMem_RawFree(program);
wchar_t *programName = Py_DecodeLocale(program, NULL);
if (programName != NULL) {
Py_SetProgramName(programName);
PyMem_RawFree(programName);
}
// wchar_t *newPath = Py_DecodeLocale(path, NULL);
// if (newPath != NULL) {
// Py_SetPath(newPath);
// PyMem_RawFree(newPath);
// }
// Py_Initialize();
Py_InitializeEx(0);
char *pathPrefix = "import sys; sys.path.append('";
char *pathSuffix = "')";
char *newPath = (char *) malloc(strlen(pathPrefix) + strlen(path) + strlen(pathSuffix));

View File

@ -74,7 +74,7 @@ func (pyObject *PyObject) Clear() {
}
// Py_Initialize initialize Python3
func Py_Initialize(path string) error {
func Py_Initialize(program string, path string) error {
lock.Lock()
defer lock.Unlock()
@ -89,8 +89,7 @@ func Py_Initialize(path string) error {
cPath := C.CString(path)
//defer C.free(unsafe.Pointer(cPath))
C.init_python(cPath)
//C.Py_Initialize()
C.init_python(C.CString(program), cPath)
err := PyLastError()
if err != nil {
@ -129,17 +128,10 @@ func Py_Finalize() {
}
}
//Py_GetVersion get
func Py_GetVersion() string {
cversion := C.Py_GetVersion()
return C.GoString(cversion)
}
func PyRun_SimpleString(command string) int {
ccommand := C.CString(command)
defer C.free(unsafe.Pointer(ccommand))
// C.PyRun_SimpleString is a macro, using C.PyRun_SimpleStringFlags instead
return int(C.PyRun_SimpleStringFlags(ccommand, nil))
return strings.Split(C.GoString(cversion), "\n")[0]
}
// loadPyFunc loads a Python function by module and function name
@ -311,13 +303,19 @@ func initPython3Callback() {
//NewClashPyContext new clash context for python
func NewClashPyContext(ruleProvidersName []string) error {
cStringArr := make([]*C.char, len(ruleProvidersName))
length := len(ruleProvidersName)
cStringArr := make([]*C.char, length)
for i, v := range ruleProvidersName {
cStringArr[i] = C.CString(v)
defer C.free(unsafe.Pointer(cStringArr[i]))
}
rs := int(C.new_clash_py_context((**C.char)(unsafe.Pointer(&cStringArr[0])), C.int(len(ruleProvidersName))))
cArrPointer := unsafe.Pointer(nil)
if length > 0 {
cArrPointer = unsafe.Pointer(&cStringArr[0])
}
rs := int(C.new_clash_py_context((**C.char)(cArrPointer), C.int(length)))
if rs == 0 {
err := PyLastError()
@ -335,5 +333,5 @@ func killSelf() {
return
}
p.Signal(syscall.SIGINT)
_ = p.Signal(syscall.SIGINT)
}

View File

@ -29,7 +29,7 @@ void set_log_callback(log_callback cb);
/*---------------------------------------------------------------*/
void append_inittab();
void init_python(const char *path);
void init_python(const char *program, const char *path);
void load_main_func();
void finalize_Python();
void py_clear(PyObject *obj);

View File

@ -85,13 +85,25 @@ func ruleProviderCallbackFn(cProviderName *C.char, cMetadata *C.struct_Metadata)
dstIp := C.GoString(cMetadata.dst_ip)
dstPort := strconv.Itoa(int(cMetadata.dst_port))
dst := net.ParseIP(dstIp)
addrType := constant.AtypDomainName
if dst != nil {
if dst.To4() != nil {
addrType = constant.AtypIPv4
} else {
addrType = constant.AtypIPv6
}
}
metadata := &constant.Metadata{
Process: processName,
SrcIP: net.ParseIP(srcIp),
DstIP: net.ParseIP(dstIp),
SrcPort: srcPort,
DstPort: dstPort,
Host: host,
Process: processName,
SrcIP: net.ParseIP(srcIp),
DstIP: dst,
SrcPort: srcPort,
DstPort: dstPort,
AddrType: addrType,
Host: host,
}
providerName := C.GoString(cProviderName)