This repository was archived by the owner on Jan 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwebSystem.js
More file actions
405 lines (391 loc) · 10.5 KB
/
webSystem.js
File metadata and controls
405 lines (391 loc) · 10.5 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import { creatorX } from "./handlers/page/webhook.js";
import { createDiscordListener } from "./handlers/discord/discordLogin.js";
import { tphHandler } from "./handlers/talkersPH/tphHandler.js";
const http = require("http");
const WebSocket = require("ws");
export class Listener {
constructor({ api, app }) {
this.api = api;
this.app = app;
this.callback = () => {};
if (api?.sendMessage) {
const e = api?.listenMqtt?.((err, event) => {
this.#callListener(err, event);
});
}
app.post("/listenMsg", (req, res) => {
try {
const event = req.body;
this.#callListener(undefined, event);
res.json(event);
} catch (err) {
res.send({
error: err.message,
});
}
});
const { handleEvents, handleGetEvents, pageApi } = creatorX(this.callback);
app.post("/webhook", handleEvents);
app.get("/webhook", handleGetEvents);
this.pageApi = pageApi;
const httpServer = http.createServer(app);
const wss = new WebSocket.Server({
server: httpServer,
path: "/ws",
});
global.logger("Server created.", "Websocket");
this.httpServer = httpServer;
this.wss = wss;
app.get("/ws-url", (req, res) => {
res.json({
url: `wss://${req.headers.host}/ws`,
});
});
}
async startListen(callback = () => {}) {
this.callback = callback;
try {
handleWebSocket(this.wss, this.callback);
await createDiscordListener(this.callback);
// await tphHandler(this.callback);
} catch (error) {
console.log(error);
}
}
async #callListener(err, data, willEvent) {
try {
this.callback(err, willEvent ? new Event(data) : data);
} catch (error) {
console.log(error);
}
}
_call = this.#callListener;
}
import axios from "axios";
export const pref = "web:";
export async function postEvent(event) {
try {
const response = await axios.post("http://localhost:8000/listenMsg", event);
return response.data;
} catch (err) {
throw err;
}
}
export function formatIP(ip) {
try {
const encodedIP = Buffer.from(ip)
.toString("base64")
.replace(/[+/=]/g, (match) => ({ "+": "0", "/": "1", "=": "" })[match]);
return `${pref}${encodedIP}`;
} catch (error) {
return ip;
}
}
export function generateWssMessageID() {
const ID =
"wss-mid_" + Date.now() + "_" + Math.random().toString(36).substring(7);
return ID;
}
export function formatWssEvent(event) {
const { WEB_PASSWORD } = global.Cassidy.config;
return {
...event,
body: String(event.body || ""),
senderID: event.senderID
? formatIP(`custom_${event.senderID}`)
: event.password === WEB_PASSWORD
? "wss:admin"
: "wss:main",
threadID: "wss:main",
type: event.type,
timestamp: event.timestamp || Date.now().toString(),
attachments: [],
messageID: event.messageID || generateWssMessageID(),
isWss: true,
isGroup: true,
messageReply: event.messageReply || null,
...(event.type === "message_reaction"
? {
userID: event.password === WEB_PASSWORD ? "wss:admin" : "wss:main",
senderID: "wss:bot",
}
: {}),
};
}
export class Event {
constructor({ ...info } = {}) {
let defaults = {
body: "",
senderID: "0",
threadID: "0",
messageID: "0",
type: "message",
timestamp: Date.now().toString(),
isGroup: false,
participantIDs: [],
attachments: [],
mentions: {},
isWeb: true,
};
Object.assign(this, defaults, info);
if (this.userID && this.isWeb) {
this.userID = formatIP(this.senderID);
}
this.senderID = formatIP(this.senderID);
this.threadID = formatIP(this.threadID);
if (this.messageReply) {
this.messageReply.senderID = formatIP(
"custom_" + this.messageReply.senderID,
);
}
if (Array.isArray(this.participantIDs)) {
this.participantIDs = this.participantIDs.map((id) =>
formatIP("custom_" + id),
);
}
}
}
import fs from "fs";
export function genericPage(...replacer) {
return pageParse("public/generic.html", ...replacer);
}
export function pageParse(filepath, ...replacer) {
let content = fs.readFileSync(filepath, "utf-8");
replacer.forEach((replacerItem) => {
if (typeof replacerItem !== "object" || replacerItem === null) {
return;
}
for (const key in replacerItem) {
const data = replacerItem[key];
const regex = new RegExp(`\\{\\{ ${key} \\}\\}`, "g");
if (data?.startsWith("fs:")) {
try {
content = content.replace(regex, () =>
fs.readFileSync(data.slice(3), "utf-8"),
);
} catch (error) {
content = content.replace(regex, "Error loading file");
}
} else {
content = content.replace(regex, data);
}
}
});
replacer.forEach((value, index) => {
if (typeof value !== "string") {
return;
}
const placeholder = new RegExp(`\\$${index + 1}(?![0-9])`, "g");
content = content.replace(placeholder, value);
});
return content;
}
export async function aiPage(prompt) {
const whatToDo = `Create a full effort, very satisfying, decent, and a very long HTML page with complete css, use the json below as guide, make sure to make it dark theme and add gradient texts, send it as HTML without comments.
${prompt}`;
try {
const {
data: { message },
} = await axios.get(
"https://lianeapi.onrender.com/ask/gpt?query=" +
encodeURIComponent(whatToDo),
);
fs.writeFileSync(`public/aiResults/ai${Date.now()}.html`, message);
return message;
} catch (error) {
return prompt;
}
}
export async function takeScreenshot(id, url, facebook) {
try {
if (facebook) {
id = formatIP("custom_" + id);
}
const encodedId = encodeURIComponent(id);
const response = await axios.get("https://api.screenshotone.com/take", {
params: {
access_key: "nbBijYDFzYIzSw",
url: `https://${url}/underpic.html?id=${encodedId}`,
full_page: false,
viewport_width: 500,
viewport_height: 250,
device_scale_factor: 1,
format: "png",
image_quality: 100,
omit_background: true,
block_ads: true,
block_cookie_banners: true,
block_banners_by_heuristics: false,
block_trackers: true,
delay: 1,
timeout: 60,
wait_until: "domcontentloaded",
time_zone: "Asia/Shanghai",
},
responseType: "arraybuffer",
});
return response.data;
} catch (error) {
console.error(error);
}
}
export class WssAPI {
constructor(socket) {
this._socket = socket;
this._queue = [];
}
sendMessage(message, _, ...args) {
let body;
if (typeof message === "string") {
body = {
body: message,
};
} else if (typeof message === "object") {
body = {
...message,
body: String(message.body || ""),
};
}
let messageReply = null;
let argg =
typeof args[0] === "string"
? args[0]
: typeof args[1] === "string"
? args[1]
: null;
if (typeof argg === "string") {
messageReply = {
messageID: argg,
senderID: "wss:main",
};
}
const self = this;
return new Promise((resolve) => {
self._queue.push({
resolve(data) {
const callback =
typeof args[0] === "function"
? args[0]
: typeof args[1] === "function"
? args[1]
: () => {};
callback(null, data);
resolve(data);
},
});
handleMessage(
self._socket,
{
body: body.body,
botSend: true,
messageReply,
},
null,
self,
);
});
}
async editMessage(str, messageID, callback) {
console.log(`Editing ${messageID} with: ${str}`);
handleEditMessage(this._socket, { body: str, messageID });
if (callback) {
callback(true, true);
}
}
getCurrentUserID() {
return "wss:bot";
}
}
export function handleWebSocket(ws, funcListen) {
ws.on("connection", (socket) => {
const api = new WssAPI(socket);
socket.on("message", (i) => {
const data = JSON.parse(i);
if (socket._xPassword) {
data.password = socket._xPassword;
}
//console.log(data);
function listenCall({ ...props } = {}) {
const payload = { ...formatWssEvent({ ...data, ...props }) };
funcListen(null, payload, { wssApi: api });
}
if (data.botSend) {
return;
}
switch (data.type) {
case "login":
const { WEB_PASSWORD } = global.Cassidy.config;
if (data.password !== WEB_PASSWORD) {
socket.send(
JSON.stringify({
type: "login_failure",
}),
);
} else {
socket._xPassword = data.password;
}
break;
case "message":
handleMessage(socket, data, listenCall, api);
break;
case "message_reply":
handleMessage(socket, data, listenCall, api);
break;
case "message_reaction":
handleReaction(socket, data, listenCall, api);
}
});
});
}
export function handleReaction(socket, { messageID, reaction }, listenCall) {
const payload = formatWssEvent({
type: "message_reaction",
messageID,
reaction,
});
listenCall(payload);
}
export function handleEditMessage(socket, { body, messageID }) {
if (socket) {
socket.send(
JSON.stringify({
type: "message_edit",
body: String(body),
messageID,
}),
);
}
}
export function handleMessage(socket, data, listenCall, api) {
let { body, messageReply, botSend } = data;
const messageID = generateWssMessageID();
listenCall ??= function () {};
if (socket) {
console.log(`Sending data with messageID: ${messageID}`);
socket.send(
JSON.stringify({
type: messageReply ? "message_reply" : "message",
body: String(body),
messageID,
...(messageReply
? {
messageReply: {
senderID: "wss:bot",
...messageReply,
},
}
: {}),
botSend: !!botSend,
}),
);
}
if (botSend && api._queue.length > 0) {
const { resolve } = api._queue.shift();
if (resolve) {
resolve(formatWssEvent({ ...data, messageID }));
console.log(`Resolved data with messageID: ${messageID}`);
}
return;
}
listenCall();
}