From 7ea4710d1638d1dc8c67217074c0f8560b613706 Mon Sep 17 00:00:00 2001 From: cdle Date: Thu, 29 Jun 2023 13:49:23 +0800 Subject: [PATCH] x --- adapters/qq/main.go | 1 + core/adapter.go | 7 +-- core/carry.go | 2 +- core/common/function.go | 12 ++--- core/plugin_core.go | 8 +-- core/plugin_web_request.go | 1 + core/plugin_web_response.go | 6 +++ core/web.go | 26 +++++---- core/websocket.go | 102 ++++++++++++++++++++---------------- main.go | 2 +- 10 files changed, 97 insertions(+), 70 deletions(-) diff --git a/adapters/qq/main.go b/adapters/qq/main.go index a7043e0..233e63d 100644 --- a/adapters/qq/main.go +++ b/adapters/qq/main.go @@ -206,6 +206,7 @@ func init() { } return id } else { + // {"params":{"user_id":"798731886","message":"2023-06-29 13:44:05","group_id":"178583761"},"action":"send_group_msg"} params := map[string]interface{}{ "group_id": msg[core.CHAT_ID], "user_id": msg[core.USER_ID], diff --git a/core/adapter.go b/core/adapter.go index 0917555..449a6b1 100644 --- a/core/adapter.go +++ b/core/adapter.go @@ -58,6 +58,7 @@ type Factory struct { cancel context.CancelFunc destroid bool errorTimes int + Res *Response } type Bot [2]string //botplt botid @@ -493,13 +494,13 @@ func (f *Factory) Receive(wt interface{}) *CustomSender { sender.details.Content = fmt.Sprint(props[i]) h = true case "message_id", "messageId": - sender.details.MessageID = fmt.Sprint(props[i]) + sender.details.MessageID = utils.Itoa(props[i]) h = true case "user_id", "userId": - sender.details.UserID = fmt.Sprint(props[i]) + sender.details.UserID = utils.Itoa(props[i]) h = true case "chat_id", "chatId", "group_id", "groupId", "group_code", "groupCode": - sender.details.ChatID = fmt.Sprint(props[i]) + sender.details.ChatID = ChatID(props[i]) h = true case "user_name", "userName": sender.details.Username = fmt.Sprint(props[i]) diff --git a/core/carry.go b/core/carry.go index 13e7f28..0c91b88 100644 --- a/core/carry.go +++ b/core/carry.go @@ -395,7 +395,7 @@ func init() { var scripts = map[string]string{} functions := Functions for _, function := range functions { - if function.UUID != "" && ((len(function.Rules) == 0 && !function.OnStart && !function.Module && function.Http == nil && function.Reply == nil) || function.Carry) { + if function.UUID != "" && ((len(function.Rules) == 0 && !function.OnStart && !function.Module && len(function.Https) == 0 && function.Reply == nil) || function.Carry) { scripts[function.UUID] = function.Title + ".js" } } diff --git a/core/common/function.go b/core/common/function.go index 944561d..e05c754 100644 --- a/core/common/function.go +++ b/core/common/function.go @@ -32,12 +32,12 @@ type Function struct { Encrypt bool `json:"encrypt"` OnStart bool `json:"on_start"` PluginPublisher - Running bool `json:"running"` - Http *Http `json:"-"` - Reply *Reply `json:"-"` - Downloads int `json:"downloads"` - HasForm bool `json:"has_form"` - Carry bool `json:"carry"` + Running bool `json:"running"` + Https []*Http `json:"-"` + Reply *Reply `json:"-"` + Downloads int `json:"downloads"` + HasForm bool `json:"has_form"` + Carry bool `json:"carry"` } type Filter struct { BlackMode bool diff --git a/core/plugin_core.go b/core/plugin_core.go index 06e516a..bade052 100644 --- a/core/plugin_core.go +++ b/core/plugin_core.go @@ -272,7 +272,7 @@ func initPlugin(data string, uuid string) (*common.Function, []func(), error) { var encrypt bool var onStart bool var origin = "自定义" - var http *common.Http + var https = []*common.Http{} var message *common.Reply var FindAll bool var hasForm bool @@ -416,10 +416,10 @@ func initPlugin(data string, uuid string) (*common.Function, []func(), error) { case "http": ss := regexp.MustCompile(`[\S]+`).FindAllString(strings.TrimSpace(res[2]), -1) if len(ss) == 2 { - http = &common.Http{ + https = append(https, &common.Http{ Path: ss[1], Method: strings.ToUpper(ss[0]), - } + }) } else { console.Warn("http param is not 2") } @@ -608,7 +608,7 @@ func initPlugin(data string, uuid string) (*common.Function, []func(), error) { Origin: origin, Running: onStart, Reply: message, - Http: http, + Https: https, FindAll: FindAll, HasForm: hasForm, Carry: carry, diff --git a/core/plugin_web_request.go b/core/plugin_web_request.go index 84b809e..5ed64c0 100644 --- a/core/plugin_web_request.go +++ b/core/plugin_web_request.go @@ -36,6 +36,7 @@ type Request struct { uuid string bodyData []byte _event string + Mark interface{} } func (r *Request) Body() string { diff --git a/core/plugin_web_response.go b/core/plugin_web_response.go index d729b80..9d1d860 100644 --- a/core/plugin_web_response.go +++ b/core/plugin_web_response.go @@ -84,6 +84,12 @@ func (r *Response) Json(ps ...interface{}) *Response { return r } +func (r *Response) Close() { + if r.conn != nil { + r.conn.Close() + } +} + func (r *Response) Header(str, value string) *Response { r.c.Header(str, value) return r diff --git a/core/web.go b/core/web.go index 2d83789..8c9fee1 100644 --- a/core/web.go +++ b/core/web.go @@ -134,20 +134,24 @@ func initWeb() { c: c, } for _, function := range Functions { - if function.Http != nil { - path := function.Http.Path - method := function.Http.Method - if c.Request.URL.Path == path && c.Request.Method == method { - req.handled = true - function.Handle(&Faker{ - Type: "*", - }, func(vm *goja.Runtime) { - vm.Set("res", res) - vm.Set("req", req) - }) + if len(function.Https) != 0 { + for _, http := range function.Https { + path := http.Path + method := http.Method + if c.Request.URL.Path == path && c.Request.Method == method { + req.handled = true + function.Handle(&Faker{ + Type: "*", + }, func(vm *goja.Runtime) { + vm.Set("res", res) + vm.Set("req", req) + }) + goto HELL + } } } } + HELL: if req.handled { if res.isRedirect { diff --git a/core/websocket.go b/core/websocket.go index c576268..2a2a7e0 100644 --- a/core/websocket.go +++ b/core/websocket.go @@ -21,57 +21,71 @@ func IsWebSocketRequest(req *http.Request) bool { func handleWebsocket(c *gin.Context) { for _, function := range Functions { - if function.Http != nil { - path := function.Http.Path - method := function.Http.Method - if c.Request.URL.Path == path && strings.HasPrefix(method, "W") { - // connect - var req = &Request{ - c: c, - // uuid: uuid, - } - var res = &Response{ - c: c, - } - req._event = "connect" - function.Handle(&Faker{ - Type: "*", - }, func(vm *goja.Runtime) { - vm.Set("res", res) - vm.Set("req", req) - }) - // message - var upGrader = websocket.Upgrader{ - CheckOrigin: func(r *http.Request) bool { - return true - }, - } - ws, err := upGrader.Upgrade(c.Writer, c.Request, nil) - if err != nil { - c.Writer.Write([]byte(err.Error())) - return - } - for { - _, data, err := ws.ReadMessage() - if err != nil { // disconnect - req._event = "disconnect" - function.Handle(&Faker{ - Type: "*", - }, func(vm *goja.Runtime) { - vm.Set("res", res) - vm.Set("req", req) - }) - ws.Close() - break + if len(function.Https) != 0 { + for _, h := range function.Https { + path := h.Path + method := h.Method + if c.Request.URL.Path == path && strings.HasPrefix(method, "W") { + // connect + var req = &Request{ + c: c, + // uuid: uuid, } - req.bodyData = data - req._event = "message" + var res = &Response{ + c: c, + } + req._event = "connect" function.Handle(&Faker{ Type: "*", }, func(vm *goja.Runtime) { vm.Set("res", res) vm.Set("req", req) }) + // message + var upGrader = websocket.Upgrader{ + CheckOrigin: func(r *http.Request) bool { + return true + }, + } + ws, err := upGrader.Upgrade(c.Writer, c.Request, nil) + if err != nil { + c.Writer.Write([]byte(err.Error())) + return + } + res.conn = ws + for { + _, data, err := ws.ReadMessage() + if err != nil { // disconnect + req._event = "disconnect" + for _, f2 := range Functions { + if f2.UUID == function.UUID { + function = f2 + } + } + function.Handle(&Faker{ + Type: "*", + }, func(vm *goja.Runtime) { + vm.Set("res", res) + vm.Set("req", req) + }) + ws.Close() + break + } + req.bodyData = data + req._event = "message" + for _, f2 := range Functions { + if f2.UUID == function.UUID { + function = f2 + } + } + function.Handle(&Faker{ + Type: "*", + }, func(vm *goja.Runtime) { + vm.Set("res", res) + vm.Set("req", req) + }) + } + return } } } diff --git a/main.go b/main.go index 52fb09d..9064449 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,7 @@ import ( "runtime" "time" - _ "github.com/cdle/sillyplus/adapters/qq" + // _ "github.com/cdle/sillyplus/adapters/qq" "github.com/cdle/sillyplus/adapters/web" "github.com/cdle/sillyplus/core"