From 1cf4c35e0f15098071ef5431885fbbf935029624 Mon Sep 17 00:00:00 2001 From: cdle Date: Sat, 1 Jul 2023 22:47:35 +0800 Subject: [PATCH] x --- core/node_ crypto.go | 131 +++++++++++++++++++++++++++++++++++++++ core/plugin_core.go | 2 +- core/plugin_paterner.go | 44 ------------- core/plugin_ptr.go | 53 ++++++++++++++++ core/plugin_subscribe.go | 2 +- core/plugin_utils.go | 17 ++--- utils/init.go | 14 +++++ 7 files changed, 210 insertions(+), 53 deletions(-) create mode 100644 core/node_ crypto.go delete mode 100644 core/plugin_paterner.go create mode 100644 core/plugin_ptr.go diff --git a/core/node_ crypto.go b/core/node_ crypto.go new file mode 100644 index 0000000..c6d4ccf --- /dev/null +++ b/core/node_ crypto.go @@ -0,0 +1,131 @@ +package core + +import ( + "crypto/hmac" + "crypto/md5" + "crypto/sha1" + "crypto/sha256" + "crypto/sha512" + "encoding/base64" + "fmt" + "hash" +) + +type Crypto struct{} + +type Hmac struct { + Algorithm string + hash hash.Hash +} + +func (h *Hmac) Digest(class string) string { + value := h.hash.Sum(nil) + switch class { + case "hex": + return fmt.Sprintf("%x", value) + case "base64": + return base64.StdEncoding.EncodeToString(value) + } + return "" +} + +func (h *Hmac) Update(data string) { + h.hash.Write([]byte(data)) +} + +func (c *Crypto) CreateHmac(algorithm, key string) *Hmac { + var hashFunc func() hash.Hash + switch algorithm { + case "md5": + hashFunc = md5.New + case "sha1": + hashFunc = sha1.New + case "sha256": + hashFunc = sha256.New + case "sha512": + hashFunc = sha512.New + default: + return nil + } + return &Hmac{ + hash: hmac.New(hashFunc, []byte(key)), + Algorithm: algorithm, + } +} + +// func (c *Crypto) CreateCipheriv(algorithm, key, data string) *Hmac { +// switch algorithm { +// case "aes-256-cbc": +// block, err := aes.NewCipher([]byte(key)) +// if err != nil { +// return "", err +// } +// ciphertext := make([]byte, aes.BlockSize+len(data)) +// iv := ciphertext[:aes.BlockSize] +// if _, err := io.ReadFull(rand.Reader, iv); err != nil { +// return "", err +// } +// stream := cipher.NewCFBEncrypter(block, iv) +// stream.XORKeyStream(ciphertext[aes.BlockSize:], []byte(data)) +// return base64.StdEncoding.EncodeToString(ciphertext), nil +// } +// } + +// type Cipher struct{} + +// func (c *Cipher) AesEncrypt(key, data string) (string, error) { +// block, err := aes.NewCipher([]byte(key)) +// if err != nil { +// return "", err +// } +// ciphertext := make([]byte, aes.BlockSize+len(data)) +// iv := ciphertext[:aes.BlockSize] +// if _, err := io.ReadFull(rand.Reader, iv); err != nil { +// return "", err +// } +// stream := cipher.NewCFBEncrypter(block, iv) +// stream.XORKeyStream(ciphertext[aes.BlockSize:], []byte(data)) +// return base64.StdEncoding.EncodeToString(ciphertext), nil +// } + +// func (c *Cipher) AesDecrypt(key, ciphertext string) (string, error) { +// block, err := aes.NewCipher([]byte(key)) +// if err != nil { +// return "", err +// } +// data, err := base64.StdEncoding.DecodeString(ciphertext) +// if err != nil { +// return "", err +// } +// if len(data) < aes.BlockSize { +// return "", fmt.Errorf("ciphertext too short") +// } +// iv := data[:aes.BlockSize] +// data = data[aes.BlockSize:] +// stream := cipher.NewCFBDecrypter(block, iv) +// stream.XORKeyStream(data, data) +// return string(data), nil +// } + +// func (c *Cipher) Rc4Encrypt(key, data string) (string, error) { +// cipher, err := rc4.NewCipher([]byte(key)) +// if err != nil { +// return "", err +// } +// ciphertext := make([]byte, len(data)) +// cipher.XORKeyStream(ciphertext, []byte(data)) +// return base64.StdEncoding.EncodeToString(ciphertext), nil +// } + +// func (c *Cipher) Rc4Decrypt(key, ciphertext string) (string, error) { +// cipher, err := rc4.NewCipher([]byte(key)) +// if err != nil { +// return "", err +// } +// data, err := base64.StdEncoding.DecodeString(ciphertext) +// if err != nil { +// return "", err +// } +// cipher.XORKeyStream(data, data) +// return string(data), nil +// } diff --git a/core/plugin_core.go b/core/plugin_core.go index 9acf18b..6306827 100644 --- a/core/plugin_core.go +++ b/core/plugin_core.go @@ -584,7 +584,7 @@ func initPlugin(data string, uuid string) (*common.Function, []func(), error) { } _, err := vm.RunProgram(prg) if err != nil { - console.Error(strings.ReplaceAll(strings.ReplaceAll(err.Error(), "node_modules/", ""), "github.com/dop251/goja_nodejs/require", "")) + pluginConsole(uuid).Error(strings.ReplaceAll(strings.ReplaceAll(err.Error(), "node_modules/", ""), "github.com/dop251/goja_nodejs/require", "")) } }) return nil diff --git a/core/plugin_paterner.go b/core/plugin_paterner.go deleted file mode 100644 index de9d6e0..0000000 --- a/core/plugin_paterner.go +++ /dev/null @@ -1,44 +0,0 @@ -package core - -import "regexp" - -func getPaterner(uuid, path string) { - var ss = regexp.MustCompile(`\S+`).FindAllString(path, -1) - path = ss[0] - var data []byte - var address = "" - ls := plugin_list - for _, f := range ls { - if f.UUID == uuid { - address = f.Address - break - } - } - if address != "" { - for _, f := range ls { - if f.Address == address && (f.Title == path || f.UUID == path) { - data = plugins.GetBytes(f.UUID) - if data != nil { - return - } - } - } - if data == nil { - for _, l := range ls { - if l.Address == address && (l.Title == path || l.UUID == path) { - data = fetchScript(l.Address, l.UUID) - if data == nil { - console.Warn("无法从订阅源获取 %s 的协作脚本 %s ", GetScriptNameByUUID(uuid), path) - } else { - console.Log("已从订阅源获取 %s 的协作脚本 %s", GetScriptNameByUUID(uuid), path) - plugins.Set(l.UUID, string(data)) - } - return - } - } - } - } - if data == nil { - console.Warn("找不到 %s 的协作脚本 %s", GetScriptNameByUUID(uuid), path) - } -} diff --git a/core/plugin_ptr.go b/core/plugin_ptr.go new file mode 100644 index 0000000..ef34784 --- /dev/null +++ b/core/plugin_ptr.go @@ -0,0 +1,53 @@ +package core + +import "regexp" + +func getPaterner(uuid, path string) (string, string, bool) { + var ss = regexp.MustCompile(`\S+`).FindAllString(path, -1) + path = ss[0] + var data []byte + var address = "" + ls := plugin_list + for _, f := range ls { + if f.UUID == uuid { + address = f.Address + break + } + } + + for _, f := range ls { //同源 + if f.Address == address && (f.Title == path || f.UUID == path) { + data = plugins.GetBytes(f.UUID) + if data != nil { + return string(data), f.UUID, true //本地取 + } + data = fetchScript(f.Address, f.UUID) + if data == nil { + console.Warn("无法从订阅源获取 %s 的协作脚本 %s ", GetScriptNameByUUID(uuid), path) + return "", f.UUID, false + } else { + console.Log("已从订阅源获取 %s 的协作脚本 %s", GetScriptNameByUUID(uuid), path) + plugins.Set(f.UUID, string(data)) + return string(data), f.UUID, true + } + } + } + for _, f := range ls { //异源 + if f.Address != address && (f.Title == path || f.UUID == path) { + data = plugins.GetBytes(f.UUID) + if data != nil { + return string(data), f.UUID, true //本地取 + } + data = fetchScript(f.Address, f.UUID) + if data == nil { + console.Warn("无法从订阅源获取 %s 的协作脚本 %s ", GetScriptNameByUUID(uuid), path) + return "", f.UUID, false + } else { + console.Log("已从订阅源获取 %s 的协作脚本 %s", GetScriptNameByUUID(uuid), path) + plugins.Set(f.UUID, string(data)) + return string(data), f.UUID, true + } + } + } + return "", "", false +} diff --git a/core/plugin_subscribe.go b/core/plugin_subscribe.go index b50aa58..707e749 100644 --- a/core/plugin_subscribe.go +++ b/core/plugin_subscribe.go @@ -29,7 +29,7 @@ type RequestPluginResult struct { var plugin_list = []*common.Function{} -var cdle_sublink = "link://T4EywWN46ztYBhHNdOl6Tpz8QQsCZGj8JvdRJ5QKatJm0P+mI/G3ruO7AC04guqqKKa29VOvTGR7ATUJGYayRBpG2RFq+6ZPK3vcu6KCDGvRE3S43Gj42EXfvs04M6s4\nlink://T4EywWN46ztYBhHNdOl6Tpz8QQsCZGj8JvdRJ5QKatJYds3a/BticqD0hzidGsOysEx/RK/nKppChxMLb6QGczhWjGC/M2ETxWb+Jl+6q/x+LP4gy+ibeAEzatOYwdZMckI8nN/R6mY/HW2dyBtp0qH1ldICn6Wl+9YowLvvpLU=" +var cdle_sublink = "link://T4EywWN46ztYBhHNdOl6Tpz8QQsCZGj8JvdRJ5QKatJm0P+mI/G3ruO7AC04guqqKKa29VOvTGR7ATUJGYayRBpG2RFq+6ZPK3vcu6KCDGvRE3S43Gj42EXfvs04M6s4\nlink://T4EywWN46ztYBhHNdOl6Tpz8QQsCZGj8JvdRJ5QKatJYds3a/BticqD0hzidGsOysEx/RK/nKppChxMLb6QGczhWjGC/M2ETxWb+Jl+6q/x+LP4gy+ibeAEzatOYwdZMckI8nN/R6mY/HW2dyBtp0qH1ldICn6Wl+9YowLvvpLU=\nlink://T4EywWN46ztYBhHNdOl6Tpz8QQsCZGj8JvdRJ5QKatL/GWakSkUWVNTd/jJS4YaqGXqvoJOxtEwVxbfBpmsMdTpKFr7K/+9MW/CJFpFsLFGM3yRxh2z8fVsDZUV6GoXei5QhOviIvo5ys7N5b6MRiEmbVATiiTEovz3IBg8nObQ=" func initPluginList() { list := []*common.Function{} diff --git a/core/plugin_utils.go b/core/plugin_utils.go index 7358fa8..6c6b5e2 100644 --- a/core/plugin_utils.go +++ b/core/plugin_utils.go @@ -432,15 +432,18 @@ func SetPluginMethod(vm *goja.Runtime, uuid string, on_start bool, running func( }) vm.Set("importJs", func(file string) error { file = strings.Replace(file, ".js", "", -1) - for _, f := range Functions { - if f.Title == file { - vm.RunString(plugins.GetString(f.UUID)) - return nil - } + s, id, ok := getPaterner(uuid, file) + if !ok { + panic(Error(vm, "无法通过importJs导入"+file)) } - pluginConsole(uuid).Error("无法通过importJs导入" + file + ".js") - panic(Error(vm, "无法通过importJs导入"+file)) + _, err := vm.RunString(s) + if err != nil { + pluginConsole(id).Error(err) + panic(Error(vm, "通过importJs导入"+file+"错误:"+err.Error())) + } + return nil }) + vm.Set("crypto", &Crypto{}) } func EncryptPlugin(script string) string { diff --git a/utils/init.go b/utils/init.go index 6995257..6f55b80 100644 --- a/utils/init.go +++ b/utils/init.go @@ -8,6 +8,7 @@ import ( "fmt" "io" "io/ioutil" + "net" "net/http" "os" "os/exec" @@ -459,6 +460,10 @@ func GetPublicIP() (string, error) { } } + if !IsIPv4(ip) { + ip = "" + } + // 使用 ifconfig.co 获取公网IP地址 if ip == "" { resp, err = http.Get("https://ifconfig.co/ip") @@ -475,5 +480,14 @@ func GetPublicIP() (string, error) { return "", fmt.Errorf("获取IP地址失败") } + if !IsIPv4(ip) { + return "", fmt.Errorf("获取IP地址失败") + } + return ip, nil } + +func IsIPv4(ip string) bool { + parsedIP := net.ParseIP(ip) + return parsedIP != nil && parsedIP.To4() != nil +}