x
This commit is contained in:
+1
-1
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
+3
-2
@@ -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
|
||||
|
||||
@@ -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
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
+63
-48
@@ -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
|
||||
})
|
||||
}
|
||||
|
||||
+5
-1
@@ -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 {
|
||||
|
||||
+1
-1
@@ -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)
|
||||
|
||||
+3
-3
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user