Chore: script built

This commit is contained in:
yaling888
2021-10-21 20:22:23 +08:00
parent cbea46b0c8
commit 63d07db4bf
17 changed files with 139 additions and 249 deletions

View File

@ -0,0 +1,9 @@
//go:build build_local
// +build build_local
package script
/*
#cgo pkg-config: python3-embed
*/
import "C"

View File

@ -0,0 +1,25 @@
//go:build !build_local
// +build !build_local
package script
/*
//#cgo linux,amd64 pkg-config: python-3.9-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 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
*/
import "C"

View File

@ -37,8 +37,6 @@ void init_python(const char *path) {
import can be deferred until the embedded script
imports it. */
clash_module = PyImport_ImportModule("clash");
main_fn = load_func(CLASH_SCRIPT_MODULE_NAME, "main");
}
// Load function, same as "import module_name.func_name as obj" in Python
@ -87,6 +85,10 @@ void py_clear(PyObject *obj) {
Py_CLEAR(obj);
}
void load_main_func() {
main_fn = load_func(CLASH_SCRIPT_MODULE_NAME, "main");
}
/** callback function, that call go function by python3 script. **/
resolve_ip_callback resolve_ip_callback_fn;

View File

@ -1,10 +1,6 @@
package script
/*
#cgo pkg-config: python3-embed
//#cgo pkg-config: python3
//#cgo LDFLAGS: -lpython3
#include "clash_module.h"
extern const char *resolveIPCallbackFn(const char *host);
@ -42,6 +38,7 @@ import (
"os"
"runtime"
"strconv"
"strings"
"sync"
"syscall"
"unsafe"
@ -88,6 +85,7 @@ func Py_Initialize(path string) error {
C.finalize_Python()
}
path = strings.ReplaceAll(path, "\\", "/")
cPath := C.CString(path)
//defer C.free(unsafe.Pointer(cPath))
@ -182,6 +180,15 @@ func LoadShortcutFunction(shortcut string) (*PyObject, error) {
return togo(fnc), nil
}
func LoadMainFunction() error {
C.load_main_func()
err := PyLastError()
if err != nil {
return err
}
return nil
}
//CallPyMainFunction call python script main function
//return the proxy adapter name.
func CallPyMainFunction(mtd *constant.Metadata) (string, error) {
@ -226,7 +233,7 @@ func CallPyMainFunction(mtd *constant.Metadata) (string, error) {
err := PyLastError()
if err != nil {
log.Errorln("[Script] script code error: %s", err.Error())
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
killSelf()
return "", fmt.Errorf("script code error: %w", err)
} else {
return "", fmt.Errorf("script code error, result: %v", rs)
@ -281,7 +288,7 @@ func CallPyShortcut(fn *PyObject, mtd *constant.Metadata) (bool, error) {
err := PyLastError()
if err != nil {
log.Errorln("[Script] script shortcut code error: %s", err.Error())
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
killSelf()
return false, fmt.Errorf("script shortcut code error: %w", err)
} else {
return false, fmt.Errorf("script shortcut code error: result: %d", rs)
@ -319,3 +326,14 @@ func NewClashPyContext(ruleProvidersName []string) error {
return nil
}
func killSelf() {
p, err := os.FindProcess(os.Getpid())
if err != nil {
os.Exit(int(syscall.SIGINT))
return
}
p.Signal(syscall.SIGINT)
}

View File

@ -30,6 +30,7 @@ void set_log_callback(log_callback cb);
void append_inittab();
void init_python(const char *path);
void load_main_func();
void finalize_Python();
void py_clear(PyObject *obj);
const char *py_last_error();

View File

@ -98,7 +98,7 @@ func ruleProviderCallbackFn(cProviderName *C.char, cMetadata *C.struct_Metadata)
rule, ok := ruleProviders[providerName]
if !ok {
log.Warnln("rule provider [%s] not found", providerName)
log.Warnln("[Script] rule provider [%s] not found", providerName)
return C.int(0)
}