From 87e6f98876ae1ab5e299c5dfd86cbe4fc2ef84a7 Mon Sep 17 00:00:00 2001 From: cdle Date: Sat, 8 Jul 2023 12:57:11 +0800 Subject: [PATCH] x --- core/adapter.go | 2 +- core/bucket.go | 19 ++++++++ core/cdle.go | 4 ++ core/init.go | 5 +- core/node_bucket.go | 26 ++++++++++ core/plugin_utils.go | 5 +- core/proxy.go | 111 ++++++++++++++++++++++++------------------- core/reply.go | 6 ++- core/web.go | 2 +- core/websocket.go | 6 +-- install.sh | 18 +++---- 11 files changed, 133 insertions(+), 71 deletions(-) diff --git a/core/adapter.go b/core/adapter.go index ba99825..70d0098 100644 --- a/core/adapter.go +++ b/core/adapter.go @@ -683,7 +683,7 @@ func (sender *CustomSender) Reply(msgs ...interface{}) (string, error) { if one != nil { message_id := "" one.Handle(&Faker{ - Type: "*", + Type: "message", }, func(vm *goja.Runtime) { obj := vm.NewObject() for k, v := range msg { diff --git a/core/bucket.go b/core/bucket.go index c446436..7bc69c3 100644 --- a/core/bucket.go +++ b/core/bucket.go @@ -199,3 +199,22 @@ func SetBucketKeyValue(bucket storage.Bucket, key interface{}, value interface{} } return bucket.Set(key, new) } + +func SetBucketKeyValue2(bucket storage.Bucket, key interface{}, value interface{}) (string, error) { + new := "" + switch value := value.(type) { + case int, int64, int32, uint: + new = fmt.Sprintf("d:%d", value) + case float32, float64: + new = fmt.Sprintf("f:%f", value) + case string, []byte: + new = fmt.Sprintf("%s", value) + case bool: + new = fmt.Sprintf("b:%t", value) + case nil: + new = "" + default: + new = fmt.Sprintf("o:%s", utils.JsonMarshal(value)) + } + return bucket.Set2(key, new) +} diff --git a/core/cdle.go b/core/cdle.go index 93d9a0f..c1adbdb 100644 --- a/core/cdle.go +++ b/core/cdle.go @@ -21,6 +21,10 @@ func init() { { Admin: true, Rules: []string{"identify sublink [地址] [组织]"}, + ImType: &common.Filter{ + BlackMode: true, + Items: []string{"pgm"}, + }, Handle: func(s common.Sender, _ func(vm *goja.Runtime)) interface{} { address := s.Get(0) organization := s.Get(1) diff --git a/core/init.go b/core/init.go index dd6647c..c103353 100644 --- a/core/init.go +++ b/core/init.go @@ -130,7 +130,9 @@ func Init() { Message: fmt.Sprintf("当前版本 %s 已是最新,无需升级", compiled_at), } } - client = &http.Client{} + client = &http.Client{ + Timeout: 30 * time.Second, + } if transport != nil { client.Transport = transport } @@ -151,7 +153,6 @@ func Init() { defer resp.Body.Close() body = resp.Body goto CREATE - PROXY: //使用免费代理下载 proxy = true diff --git a/core/node_bucket.go b/core/node_bucket.go index 09c579a..f977a7b 100644 --- a/core/node_bucket.go +++ b/core/node_bucket.go @@ -25,6 +25,13 @@ func MakeBucketObject(vm *goja.Runtime, uuid string, on_start bool, bucket stora } return msg }) + obj.Set("set2", func(key, value interface{}) interface{} { + msg, err := SetBucketKeyValue2(bucket, key, value) + if err != nil { + panic(Error(vm, err)) + } + return msg + }) obj.Set("delete", func(key interface{}) error { _, err := bucket.Set(key, "") return err @@ -94,3 +101,22 @@ func JsBucket(vm *goja.Runtime, name string, uuid string, on_start bool) goja.Pr }, }) } + +func JsBucket2(vm *goja.Runtime, name string, uuid string, on_start bool) goja.Proxy { + return vm.NewProxy(MakeBucketObject(vm, uuid, on_start, MakeBucket(name)), &goja.ProxyTrapConfig{ + Get: func(target *goja.Object, property string, receiver goja.Value) (value goja.Value) { + obj := target.Get(property) + if obj != nil { + return obj + } + result := target.Get("get").Export().(func(...interface{}) interface{})(property) + return vm.ToValue(result) + }, + Set: func(target *goja.Object, property string, value, receiver goja.Value) (success bool) { + target.Get("set2").Export().(func(interface{}, interface{}) interface{})( + property, value.Export(), + ) + return true + }, + }) +} diff --git a/core/plugin_utils.go b/core/plugin_utils.go index 6b9d1d9..577fcf2 100644 --- a/core/plugin_utils.go +++ b/core/plugin_utils.go @@ -114,7 +114,8 @@ func SetPluginMethod(vm *goja.Runtime, uuid string, on_start bool, running func( return JsBucket(vm, name, uuid, on_start) }) registry := require.NewRegistry(require.WithLoader(mapFileSystemSourceLoader(uuid))) - + registry.RegisterNativeModule("crypto", cryptoModule) + registry.RegisterNativeModule("buffer", bufferModule) if on_start { var ids = &[]cron.EntryID{} crons.Store(uuid, ids) @@ -157,8 +158,6 @@ func SetPluginMethod(vm *goja.Runtime, uuid string, on_start bool, running func( return o }) - registry.RegisterNativeModule("crypto", cryptoModule) - registry.RegisterNativeModule("buffer", bufferModule) vm.Set("gofor", func(running func() bool, handle func()) { go func() { for { diff --git a/core/proxy.go b/core/proxy.go index b44794e..0ea8e77 100644 --- a/core/proxy.go +++ b/core/proxy.go @@ -5,10 +5,12 @@ import ( "encoding/json" "errors" "fmt" + "net" + "net/http" "net/url" - "reflect" "strings" "sync" + "time" "github.com/Dreamacro/clash/adapter" C "github.com/Dreamacro/clash/constant" @@ -21,6 +23,7 @@ import ( // } type ProxyConfig struct { + ID string `json:"id"` Type string `json:"type"` Server string `json:"server"` Port int `json:"port"` @@ -47,7 +50,7 @@ type ProxyConfig struct { // Enable bool `json:"enable,omitempty"` // ExcludeIPs []string `json:"exclude-ip,omitempty"` // ProxyGroups []string `json:"proxy-groups,omitempty"` - + Google []int `json:"google"` } var Proxies sync.Map @@ -124,6 +127,7 @@ func GetProxyTransport(rawURL string, uuid string, params map[string]interface{} func init() { proxies.Foreach(func(b1, b2 []byte) error { + key := string(b1) new := string(b2) var ncfg = ProxyConfig{} var params = map[string]interface{}{} @@ -144,7 +148,7 @@ func init() { // fmt.Println("===", string(utils.JsonMarshal(ncfg))) // fmt.Println(ncfg.RuleMatcher.Match("106.52.87.206")) // fmt.Println(ncfg.RuleMatcher.Match("api.telegram.org")) - Proxies.Store(string(b1), &ncfg) + Proxies.Store(key, &ncfg) } return nil }) @@ -215,49 +219,60 @@ func init() { }) } -func structToMap(s interface{}) map[string]interface{} { - result := make(map[string]interface{}) - d, _ := json.Marshal(s) - json.Unmarshal(d, &result) - return result -} - -func IsDifferent(a, b interface{}, ignoreFields []string) bool { - aVal := reflect.ValueOf(a) - bVal := reflect.ValueOf(b) - - if aVal.Kind() != reflect.Struct || bVal.Kind() != reflect.Struct { - // 如果不是 struct 类型,则返回 true(不同) - return true - } - - // 获取 struct 的字段数 - numFields := aVal.NumField() - - // 遍历 struct 的所有字段,检查是否有不同 - for i := 0; i < numFields; i++ { - aField := aVal.Field(i) - bField := bVal.Field(i) - - // 获取字段名 - fieldName := aVal.Type().Field(i).Name - - // 如果字段名在 ignoreFields 中,则跳过比较 - if Contains(ignoreFields, fieldName) { - continue - } - - // 如果字段类型不同,则返回 true(不同) - if aField.Type() != bField.Type() { - return true - } - - // 如果字段值不同,则返回 true(不同) - if !reflect.DeepEqual(aField.Interface(), bField.Interface()) { - return true - } - } - - // 如果所有字段都相同,则返回 false(相同) - return false +func checkProxy() { + proxies.Foreach(func(b1, b2 []byte) error { + go func(b1, b2 []byte) { + key := string(b1) + new := string(b2) + var ncfg = ProxyConfig{} + var params = map[string]interface{}{} + if strings.HasPrefix(new, "o:") { + var data = []byte(strings.Replace(new, "o:", "", 1)) + err := json.Unmarshal(data, &ncfg) + json.Unmarshal(data, ¶ms) + if err != nil { + return + } + var check = map[string]int64{} + for _, site := range []string{"https://google.com", "https://github.com"} { + func(site string) { + instance, err := GetProxyTransport(site, "", params) + if err != nil { + return + } + defer instance.Close() + var client = &http.Client{ + Transport: &http.Transport{ + Dial: func(string, string) (net.Conn, error) { + return instance, nil + }, + MaxIdleConns: 100, + IdleConnTimeout: 90 * time.Second, + TLSHandshakeTimeout: 10 * time.Second, + ExpectContinueTimeout: 1 * time.Second, + }, + } + req, err := http.NewRequest("GET", site, strings.NewReader("")) + if err != nil { + return + } + startTime := time.Now().UnixMilli() + resp, err := client.Do(req) + if err != nil { + return + } + endTime := time.Now().UnixMilli() + resp.Body.Close() + spend := endTime - startTime + check[site] = spend + + console.Log(site, "resp.StatusCode", resp.StatusCode, "time", spend) + }(site) + } + params["check"] = check + proxies.Set2(key, "o:"+string(utils.JsonMarshal(params))) + } + }(b1, b2) + return nil + }) } diff --git a/core/reply.go b/core/reply.go index a686513..d14adcc 100644 --- a/core/reply.go +++ b/core/reply.go @@ -244,6 +244,7 @@ func parseReply2(str string) string { }) } +// rule 专用 func parseReply3(str string, f func(string, string)) string { ks := map[string]bool{} re := regexp.MustCompile(`\$\{\s*([^{}]+)\s*\}`) @@ -259,8 +260,11 @@ func parseReply3(str string, f func(string, string)) string { return fmt.Sprintf(`Bucket("%s")["%s"]`, parts[0], parts[1]) }) vm := goja.New() + // vm.Set("Bucket", func(name string) interface{} { + // return JsBucket(vm, name, "", false) + // }) vm.Set("Bucket", func(name string) interface{} { - return JsBucket(vm, name, "", false) + return JsBucket2(vm, name, "", false) }) v, err := vm.RunString(script) if err == nil { diff --git a/core/web.go b/core/web.go index 0e4e108..cc777aa 100644 --- a/core/web.go +++ b/core/web.go @@ -141,7 +141,7 @@ func initWeb() { if c.Request.URL.Path == path && c.Request.Method == method { req.handled = true function.Handle(&Faker{ - Type: "*", + Type: "http", }, func(vm *goja.Runtime) { vm.Set("res", res) vm.Set("req", req) diff --git a/core/websocket.go b/core/websocket.go index 2a2a7e0..af5d0b6 100644 --- a/core/websocket.go +++ b/core/websocket.go @@ -36,7 +36,7 @@ func handleWebsocket(c *gin.Context) { } req._event = "connect" function.Handle(&Faker{ - Type: "*", + Type: "websocket", }, func(vm *goja.Runtime) { vm.Set("res", res) vm.Set("req", req) @@ -63,7 +63,7 @@ func handleWebsocket(c *gin.Context) { } } function.Handle(&Faker{ - Type: "*", + Type: "websocket", }, func(vm *goja.Runtime) { vm.Set("res", res) vm.Set("req", req) @@ -79,7 +79,7 @@ func handleWebsocket(c *gin.Context) { } } function.Handle(&Faker{ - Type: "*", + Type: "websocket", }, func(vm *goja.Runtime) { vm.Set("res", res) vm.Set("req", req) diff --git a/install.sh b/install.sh index 2526257..807eb43 100644 --- a/install.sh +++ b/install.sh @@ -1,4 +1,4 @@ -n="sillyplus" +n="sillyGirl" s="/usr/local/$n" a=arm64 if [[ $(uname -a | grep "x86_64") != "" ]]; then @@ -9,21 +9,15 @@ if [ ! -d $s ]; then fi ; cd $s; rm -rf $n; -v=`curl https://gitlab.com/cdle/$a/-/raw/main/compile_time.go --silent | tr -cd "[0-9]"` -d="" +v=`curl https://raw.githubusercontent.com/cdle/binary/main/compile_time.go --silent | tr -cd "[0-9]"` if [ ${#v} == 13 ]; then - d="https://gitlab.com/cdle/${a}/-/raw/main/sillyplus_linux_${a}_${v}" + d="https://raw.githubusercontent.com/cdle/binary/main/sillyGirl_linux_${a}_${v}" else - v=`curl https://raw.githubusercontent.com/cdle/binary/main/compile_time.go --silent | tr -cd "[0-9]"` - if [ ${#v} == 13 ]; then - d="https://raw.githubusercontent.com/cdle/binary/main/sillyplus_linux_${a}_${v}" - else - echo "Sorry,你网不好!" - exit - fi + echo "Sorry,你网不好,请使用其他方式下载!" + exit fi echo "检测到版本 $v" echo "正在从 $d 下载..." curl -o $n $d && chmod 777 $n echo "傻妞已安装到 $s" -echo "请手动运行 $s/$n -t" \ No newline at end of file +echo "请手动运行 $s/$n 带 -t 进入交互模式"