-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
164 lines (143 loc) · 4.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
logger.info(logger.yellow("- 正在加载 路由插件"))
import makeConfig from "../../lib/plugins/config.js"
import httpProxy from "express-http-proxy"
import { WebSocket, WebSocketServer } from "ws"
const { config, configSave } = await makeConfig("Route", {
tips: "",
permission: "master",
blackWord: "^$",
token: [],
}, {
tips: [
"欢迎使用 TRSS-Yunzai Route Plugin ! 作者:时雨🌌星空",
"参考:https://github.com/TimeRainStarSky/Yunzai-Route-Plugin",
],
})
const adapter = new class RouteAdapter {
id = "Route"
name = "Route"
version = "express-http-proxy v2.0.0"
blackWord = new RegExp(config.blackWord)
wsUrl = {}
httpProxy(token) {
const opts = {}
const path = `/${token.shift()}`
token = token.join(":")
opts.https = token.startsWith("https://")
token = token.replace(/^https?:\/\//, "").split("/")
let url = token.shift()
if (token.length) {
token = token.join("/").split("?")
token = [`/${token.shift()}`, token.join("?")]
} else token = ["", ""]
opts.proxyReqPathResolver = token[1] ?
req => `${token[0]}${req.url.replace(/^\//, "")}${req.url.includes("?")?"&":"?"}${token[1]}` :
req => `${token[0]}${req.url.replace(/^\//, "")}`
const fnc = httpProxy(url, opts)
url = `http${opts.https?"s":""}://${url}`
Bot.express.use(path, (...args) => {
Bot.makeLog("info", "", `${args[0].rid} => ${url}${opts.proxyReqPathResolver(args[0])}`, true)
return fnc(...args)
})
Bot.makeLog("mark", `${path} => ${url}${opts.proxyReqPathResolver({ url: "/" })}`, "Route")
}
wsClose(conn) {
if (conn.closed) return
conn.closed = true
Bot.makeLog("info", "断开连接", `${conn.ws.rid} <≠> ${this.wsUrl[conn.path]}`, true)
conn.ws.terminate()
for (const i of conn.wsp) i.terminate()
}
wsConnect(conn) {
Bot.makeLog("info", ["建立连接", conn.req.headers], `${conn.ws.rid} <=> ${this.wsUrl[conn.path]}`, true)
conn.wsp = []
conn.ws.on("error", error => {
Bot.makeLog("error", error, `${conn.ws.rid} <=> ${this.wsUrl[conn.path]}`, true)
this.wsClose(conn)
})
conn.ws.on("close", () => this.wsClose(conn))
conn.ws.on("message", msg => {
const data = String(msg).trim()
for (const i of conn.wsp) i.send(data)
})
for (const i of this.wsUrl[conn.path]) {
const wsp = new WebSocket(i, { headers: conn.req.headers })
wsp.onopen = () => conn.wsp.push(wsp)
wsp.onerror = error => {
Bot.makeLog("error", error, `${conn.ws.rid} <=> ${i}`, true)
this.wsClose(conn)
}
wsp.onclose = () => this.wsClose(conn)
wsp.onmessage = msg => {
const data = String(msg.data).trim()
if (!data.match(this.blackWord))
Bot.makeLog("debug", ["消息", data], `${conn.ws.rid} <= ${i}`, true)
conn.ws.send(data)
}
}
}
wsProxy(token) {
const path = token.shift()
const url = `ws://${token.join(":")}`
Bot.makeLog("mark", `/${path} => ${url}`, "Route")
if (Array.isArray(this.wsUrl[path]))
return this.wsUrl[path].push(url)
this.wsUrl[path] = [url]
if (!Array.isArray(Bot.wsf[path]))
Bot.wsf[path] = []
Bot.wsf[path].push((ws, req, ...args) => this.wsConnect({ path, url: this.wsUrl[path], ws, req, args }))
}
connect(token) {
token = token.split(":")
const scheme = token.shift().toLowerCase()
switch (scheme) {
case "http":
this.httpProxy(token)
break
case "ws":
this.wsProxy(token)
break
}
}
load() {
for (const token of config.token)
adapter.connect(token)
}
}
Bot.adapter.push(adapter)
export class RouteAdapter extends plugin {
constructor() {
super({
name: "RouteAdapter",
dsc: "路由设置",
event: "message",
rule: [
{
reg: "^#路由列表$",
fnc: "List",
permission: config.permission,
},
{
reg: "^#路由设置.+:.+:.+$",
fnc: "Token",
permission: config.permission,
}
]
})
}
List() {
this.reply(`共${config.token.length}个路由:\n${config.token.join("\n")}`, true)
}
async Token() {
const token = this.e.msg.replace(/^#路由设置/, "").trim()
if (config.token.includes(token)) {
config.token = config.token.filter(item => item !== token)
this.reply(`路由已删除,重启后生效,共${config.token.length}个路由`, true)
} else {
config.token.push(token)
this.reply(`路由已设置,重启后生效,共${config.token.length}个路由`, true)
}
await configSave()
}
}
logger.info(logger.green("- 路由插件 加载完成"))