x
This commit is contained in:
@@ -10,12 +10,15 @@ from pagermaid.enums import Message
|
|||||||
from pagermaid.services import bot
|
from pagermaid.services import bot
|
||||||
from pagermaid.single_utils import sqlite
|
from pagermaid.single_utils import sqlite
|
||||||
from pagermaid.utils import pip_install
|
from pagermaid.utils import pip_install
|
||||||
|
from pyrogram.enums.chat_type import ChatType
|
||||||
|
|
||||||
|
|
||||||
pip_install("aiohttp")
|
pip_install("aiohttp")
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
|
||||||
uri=""
|
uri = "ws://106.52.87.206:8080/bot/pagermaid?secure_token=1a14c723-2150-11ee-af60-5254001a4920"
|
||||||
|
|
||||||
|
|
||||||
class WebSocket:
|
class WebSocket:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@@ -25,6 +28,7 @@ class WebSocket:
|
|||||||
self.need_stop = False
|
self.need_stop = False
|
||||||
self.ws = None
|
self.ws = None
|
||||||
self.connection = None
|
self.connection = None
|
||||||
|
self.whitelist = []
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def database_have_uri():
|
def database_have_uri():
|
||||||
@@ -46,7 +50,10 @@ class WebSocket:
|
|||||||
await self.disconnect()
|
await self.disconnect()
|
||||||
if self.uri:
|
if self.uri:
|
||||||
self.ws = self.client.ws_connect(
|
self.ws = self.client.ws_connect(
|
||||||
self.uri+"&user_id="+str(bot.me.id), autoclose=False, autoping=False, timeout=5
|
self.uri + "&user_id=" + str(bot.me.id),
|
||||||
|
autoclose=False,
|
||||||
|
autoping=False,
|
||||||
|
timeout=5,
|
||||||
)
|
)
|
||||||
self.connection = await self.ws._coro
|
self.connection = await self.ws._coro
|
||||||
|
|
||||||
@@ -57,7 +64,6 @@ class WebSocket:
|
|||||||
self.ws = None
|
self.ws = None
|
||||||
self.connection = None
|
self.connection = None
|
||||||
|
|
||||||
|
|
||||||
async def keep_alive(self):
|
async def keep_alive(self):
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
@@ -106,6 +112,9 @@ class WebSocket:
|
|||||||
data = json.loads(text)
|
data = json.loads(text)
|
||||||
except Exception:
|
except Exception:
|
||||||
return
|
return
|
||||||
|
if data['action'] == "set_whitelist":
|
||||||
|
ws.whitelist = data['data']
|
||||||
|
return
|
||||||
echo = data.get("echo", "")
|
echo = data.get("echo", "")
|
||||||
action = data.get("action", None)
|
action = data.get("action", None)
|
||||||
action_data = data.get("data", None)
|
action_data = data.get("data", None)
|
||||||
@@ -113,7 +122,7 @@ class WebSocket:
|
|||||||
if bot_action and action_data:
|
if bot_action and action_data:
|
||||||
message = await bot_action(**action_data)
|
message = await bot_action(**action_data)
|
||||||
message = str(message.__str__())
|
message = str(message.__str__())
|
||||||
message = message.replace("{", '{"echo": "'+str(echo)+'",', 1)
|
message = message.replace("{", '{"echo": "' + str(echo) + '",', 1)
|
||||||
await ws.push(message)
|
await ws.push(message)
|
||||||
|
|
||||||
|
|
||||||
@@ -132,6 +141,21 @@ async def connect_ws():
|
|||||||
@listener(incoming=True, outgoing=True)
|
@listener(incoming=True, outgoing=True)
|
||||||
async def websocket_push(message: Message):
|
async def websocket_push(message: Message):
|
||||||
with contextlib.suppress(Exception):
|
with contextlib.suppress(Exception):
|
||||||
|
if message.chat and message.chat.type in [
|
||||||
|
ChatType.GROUP,
|
||||||
|
ChatType.SUPERGROUP,
|
||||||
|
ChatType.CHANNEL,
|
||||||
|
]:
|
||||||
|
if not ws.whitelist or str(message.chat.id) not in ws.whitelist:
|
||||||
|
if message.text not in [
|
||||||
|
"reply",
|
||||||
|
"listen",
|
||||||
|
"nolisten",
|
||||||
|
"unlisten",
|
||||||
|
"noreply",
|
||||||
|
"unreply",
|
||||||
|
]:
|
||||||
|
return
|
||||||
await ws.push(message.__str__())
|
await ws.push(message.__str__())
|
||||||
|
|
||||||
|
|
||||||
@@ -141,4 +165,3 @@ async def websocket_to_connect(message: Message):
|
|||||||
return await message.edit("傻+ 已连接")
|
return await message.edit("傻+ 已连接")
|
||||||
else:
|
else:
|
||||||
return await message.edit("傻+ 已离线")
|
return await message.edit("傻+ 已离线")
|
||||||
|
|
||||||
|
|||||||
@@ -182,6 +182,13 @@ func initListenReply() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func initToHandleMessage() {
|
func initToHandleMessage() {
|
||||||
|
listen_admin := sillyGirl.GetBool("listen_admin", true)
|
||||||
|
storage.Watch(sillyGirl, "listen_admin", func(old, new, key string) *storage.Final {
|
||||||
|
if new == "false" {
|
||||||
|
listen_admin = false
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
Messages = make(chan common.Sender)
|
Messages = make(chan common.Sender)
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
@@ -261,11 +268,13 @@ func initToHandleMessage() {
|
|||||||
}
|
}
|
||||||
_, ok1 := ListenOnGroups.Load(cid)
|
_, ok1 := ListenOnGroups.Load(cid)
|
||||||
if !ok1 {
|
if !ok1 {
|
||||||
|
if !listen_admin || !isAdmin {
|
||||||
_, ok2 := StaticListenOnGroups.Load(cid)
|
_, ok2 := StaticListenOnGroups.Load(cid)
|
||||||
if !ok2 {
|
if !ok2 {
|
||||||
ignore = true
|
ignore = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if isAdmin {
|
if isAdmin {
|
||||||
switch ctt {
|
switch ctt {
|
||||||
|
|||||||
@@ -166,6 +166,7 @@ func fetch(vm *goja.Runtime, uuid string, wts ...interface{}) (interface{}, erro
|
|||||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
} else {
|
} else {
|
||||||
req, err = http.NewRequest(method, url, bytes.NewBuffer(body))
|
req, err = http.NewRequest(method, url, bytes.NewBuffer(body))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -184,7 +185,6 @@ func fetch(vm *goja.Runtime, uuid string, wts ...interface{}) (interface{}, erro
|
|||||||
var rspObj goja.Proxy
|
var rspObj goja.Proxy
|
||||||
var rsp *http.Response
|
var rsp *http.Response
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
var client = &http.Client{
|
var client = &http.Client{
|
||||||
Timeout: timeout,
|
Timeout: timeout,
|
||||||
}
|
}
|
||||||
|
|||||||
+93
-18
@@ -1,7 +1,9 @@
|
|||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"errors"
|
"errors"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -12,12 +14,101 @@ import (
|
|||||||
|
|
||||||
// type Reason map[string]interface{}
|
// type Reason map[string]interface{}
|
||||||
|
|
||||||
|
func readEvent(body io.Reader) (string, error) {
|
||||||
|
var event string
|
||||||
|
|
||||||
|
// 读取一行数据
|
||||||
|
reader := bufio.NewReader(body)
|
||||||
|
line, err := reader.ReadString('\n')
|
||||||
|
if err != nil {
|
||||||
|
return event, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 去掉换行符
|
||||||
|
line = strings.TrimSuffix(line, "\n")
|
||||||
|
|
||||||
|
// 判断是否是事件行
|
||||||
|
if strings.HasPrefix(line, "data: ") {
|
||||||
|
event = strings.TrimPrefix(line, "data: ")
|
||||||
|
}
|
||||||
|
|
||||||
|
return event, nil
|
||||||
|
}
|
||||||
|
|
||||||
func MakeResponseObject(vm *goja.Runtime, resp *http.Response, responseType string) (*goja.Object, error) {
|
func MakeResponseObject(vm *goja.Runtime, resp *http.Response, responseType string) (*goja.Object, error) {
|
||||||
obj := vm.NewObject()
|
obj := vm.NewObject()
|
||||||
data, err := ioutil.ReadAll(resp.Body)
|
|
||||||
|
cjson := false
|
||||||
|
|
||||||
|
var data []byte
|
||||||
|
var err error
|
||||||
|
|
||||||
|
obj.Set("status", resp.StatusCode)
|
||||||
|
obj.Set("headers", vm.NewProxy(MakeHeadersObject(vm, resp.Header), &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(name string) string)(property)
|
||||||
|
return vm.ToValue(result)
|
||||||
|
},
|
||||||
|
Set: func(target *goja.Object, property string, value, receiver goja.Value) (success bool) {
|
||||||
|
target.Get("set").Export().(func(name, value string))(
|
||||||
|
property, value.String(),
|
||||||
|
)
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
|
||||||
|
var dataCallback func(chunk interface{})
|
||||||
|
var endCallback func(chunk interface{})
|
||||||
|
|
||||||
|
obj.Set("on", func(event string, callback func(chunk interface{})) {
|
||||||
|
switch event {
|
||||||
|
case "data":
|
||||||
|
dataCallback = callback
|
||||||
|
case "json":
|
||||||
|
cjson = true
|
||||||
|
dataCallback = callback
|
||||||
|
case "end":
|
||||||
|
endCallback = callback
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if resp.Header.Get("Content-Type") == "text/event-stream" {
|
||||||
|
events := []string{}
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
event, err := readEvent(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
if endCallback != nil {
|
||||||
|
endCallback(nil)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
events = append(events, event)
|
||||||
|
if dataCallback != nil {
|
||||||
|
for i := range events {
|
||||||
|
if cjson {
|
||||||
|
var v interface{}
|
||||||
|
json.Unmarshal([]byte(events[i]), &v)
|
||||||
|
dataCallback(v)
|
||||||
|
} else {
|
||||||
|
dataCallback(events[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
events = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return obj, err
|
||||||
|
} else {
|
||||||
|
data, err = ioutil.ReadAll(resp.Body)
|
||||||
if err != nil { ///////
|
if err != nil { ///////
|
||||||
return obj, err
|
return obj, err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
var body interface{}
|
var body interface{}
|
||||||
if Contains([]string{"blob", "arraybuffer"}, responseType) {
|
if Contains([]string{"blob", "arraybuffer"}, responseType) {
|
||||||
body = data
|
body = data
|
||||||
@@ -69,22 +160,6 @@ func MakeResponseObject(vm *goja.Runtime, resp *http.Response, responseType stri
|
|||||||
obj.Set("text", func() string {
|
obj.Set("text", func() string {
|
||||||
return string(data)
|
return string(data)
|
||||||
})
|
})
|
||||||
obj.Set("status", resp.StatusCode)
|
|
||||||
obj.Set("headers", vm.NewProxy(MakeHeadersObject(vm, resp.Header), &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(name string) string)(property)
|
|
||||||
return vm.ToValue(result)
|
|
||||||
},
|
|
||||||
Set: func(target *goja.Object, property string, value, receiver goja.Value) (success bool) {
|
|
||||||
target.Get("set").Export().(func(name, value string))(
|
|
||||||
property, value.String(),
|
|
||||||
)
|
|
||||||
return true
|
|
||||||
},
|
|
||||||
}))
|
|
||||||
return obj, nil
|
return obj, nil
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-1
@@ -482,6 +482,7 @@ func initPlugin(data string, uuid string) (*common.Function, []func(), error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
script := ""
|
script := ""
|
||||||
if encrypt {
|
if encrypt {
|
||||||
script = DecryptPlugin(string(data))
|
script = DecryptPlugin(string(data))
|
||||||
@@ -493,7 +494,9 @@ func initPlugin(data string, uuid string) (*common.Function, []func(), error) {
|
|||||||
script = strings.ReplaceAll(script, "new Bucket", "Bucket")
|
script = strings.ReplaceAll(script, "new Bucket", "Bucket")
|
||||||
// script = regexp.MustCompile(`import\s+\{\s*([^\}]+)\s*\}\s*from\s*['"]([^'"]+)['"]\s*;`).ReplaceAllString(script, "const {$1} = require('$2');")
|
// script = regexp.MustCompile(`import\s+\{\s*([^\}]+)\s*\}\s*from\s*['"]([^'"]+)['"]\s*;`).ReplaceAllString(script, "const {$1} = require('$2');")
|
||||||
// script = regexp.MustCompile(`import\s+\s*([^\}]+)\s*\s*from\s*['"]([^'"]+)['"]\s*;`).ReplaceAllString(script, "const $1 = require('$2');")
|
// script = regexp.MustCompile(`import\s+\s*([^\}]+)\s*\s*from\s*['"]([^'"]+)['"]\s*;`).ReplaceAllString(script, "const $1 = require('$2');")
|
||||||
|
if !hasForm {
|
||||||
|
hasForm = strings.Contains(script, "Form(")
|
||||||
|
}
|
||||||
prg, err2 := goja.Compile(title+".js", script, false)
|
prg, err2 := goja.Compile(title+".js", script, false)
|
||||||
if err == nil && err2 != nil {
|
if err == nil && err2 != nil {
|
||||||
err = err2
|
err = err2
|
||||||
|
|||||||
Reference in New Issue
Block a user