This repository was archived by the owner on Jul 22, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathworker.ts
More file actions
278 lines (258 loc) · 7.53 KB
/
Copy pathworker.ts
File metadata and controls
278 lines (258 loc) · 7.53 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
/**
* MTKruto Server
* Copyright (C) 2024 Roj <https://roj.im/>
*
* This file is part of MTKruto Server.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
///<reference lib="webworker"/>
///<reference lib="dom" />
import * as log from "std/log/mod.ts";
import * as path from "std/path/mod.ts";
import { existsSync } from "std/fs/mod.ts";
import { InputError } from "mtkruto/0_errors.ts";
import { setLogVerbosity } from "mtkruto/1_utilities.ts";
import { errors, isValidType, setLoggingProvider } from "mtkruto/mod.ts";
import { transform } from "./transform.ts";
import { fileLogger } from "./file_logger.ts";
import { isFunctionDisallowed } from "./disallowed_functions.ts";
import { ClientManager, ClientStats } from "./client_manager.ts";
import { ALLOWED_METHODS, AllowedMethod } from "./allowed_methods.ts";
const WORKER_LOG_PATH = path.join(Deno.cwd(), ".logs", "workers");
const CLIENT_LOG_PATH = path.join(Deno.cwd(), ".logs", "clients");
if (!existsSync(CLIENT_LOG_PATH)) {
Deno.mkdirSync(CLIENT_LOG_PATH, { recursive: true });
}
if (!existsSync(WORKER_LOG_PATH)) {
Deno.mkdirSync(WORKER_LOG_PATH, { recursive: true });
}
let id = -1;
let clientManager = new ClientManager(0, "");
addEventListener("message", async (e) => {
const [_id, { _, args }] = e.data;
if (id != -1) {
log.info("in", args, _, _id);
}
let result;
try {
result = await (handlers as any)[_](...args as any[]);
} catch (err) {
if (err instanceof InputError) {
result = [err.message, {
status: 400,
headers: { "x-error-type": "input" },
}];
} else if (err instanceof errors.TelegramError) {
result = [err.errorMessage, {
status: err.errorCode,
headers: { "x-error-type": "rpc" },
}];
} else {
log.error(
`[${_id}]\nAn unexpected error occurred.`,
Deno.inspect(err, { colors: false }),
);
result = [null, { status: 500 }];
}
}
log.info("out", result ?? null, _, _id);
postMessage([_id, result ?? null]);
});
const handlers = {
init,
clientCount,
serve,
next,
stats,
getUpdates,
invoke,
setWebhook,
deleteWebhook,
startWebhookLoop,
unload,
dropPendingUpdates,
clearCache,
};
export type Handler = typeof handlers;
function init(id_: number, apiId: number, apiHash: string) {
if (id != -1) {
return;
}
id = id_;
clientManager = new ClientManager(apiId, apiHash);
const workerLogFile = path.join(WORKER_LOG_PATH, id + "");
const clientLogFile = path.join(CLIENT_LOG_PATH, id + "");
setLogVerbosity(Infinity);
setLoggingProvider(fileLogger(clientLogFile));
const ENTRY_SEPARATOR = "-".repeat(25);
log.setup({
loggers: {
default: {
level: "INFO",
handlers: ["file"],
},
},
handlers: {
file: new log.FileHandler("NOTSET", {
filename: workerLogFile,
formatter(record) {
const time = record.datetime.toISOString();
const payload = record.args.length >= 1
? (typeof record.args[0] === "string" ? record.args[0] : JSON.stringify(record.args[0], null, 2))
.trim()
.split("\n")
.map((v) => ` ${v}`)
.join("\n")
: "";
if (record.msg == "out" || record.msg == "in") {
const A = record.msg == "in" ? ">>>>>>>>>>" : "<<<<<<<<<<";
const name = record.args[1];
const id = (record.args[2] as string).toUpperCase();
return `[${time}]\n [${id}]\n ${A} ${name}\n${payload}\n\n${ENTRY_SEPARATOR}\n`;
} else {
const msg = record.msg
.split("\n")
.map((v) => ` ${v}`)
.join("\n");
const maybePayload = payload.length ? `\n${payload}` : "";
return `[${time}]\n${msg}${maybePayload}\n\n${ENTRY_SEPARATOR}\n`;
}
},
}),
},
});
}
function clientCount() {
return clientManager.count();
}
async function serve(
id: string,
method: AllowedMethod,
args: any[],
): Promise<
"DROP" | Parameters<typeof Response["json"]> | { streamId: string }
> {
if (!id.trim() || !method.trim()) {
return "DROP";
}
if (!(ALLOWED_METHODS.includes(method))) {
return "DROP";
}
let result;
if (method == "download") {
result = await clientManager.download(id, args[0]);
} else {
const client = await clientManager.getClient(id);
// deno-lint-ignore ban-ts-comment
// @ts-ignore
result = transform(await client[method](...args));
}
if (result !== undefined) {
if (
typeof result === "object" && result != null &&
Symbol.asyncIterator in result
) {
return { streamId: getStreamId(result) };
} else {
return [result];
}
} else {
return [null];
}
}
const streams = new Map<string, AsyncIterator<Uint8Array>>();
function getStreamId(iterable: AsyncIterable<Uint8Array>) {
const id = crypto.randomUUID();
streams.set(id, iterable[Symbol.asyncIterator]());
return id;
}
async function next(streamId: string) {
const result = await streams.get(streamId)?.next();
if (result === undefined) {
return null;
} else {
if (result.done) {
streams.delete(streamId);
}
return result;
}
}
async function invoke(
id: string,
function_: any,
): Promise<Parameters<typeof Response["json"]>> {
function_ = transform(function_);
if (!isValidType(function_)) {
throw new InputError("Invalid function");
}
if (isFunctionDisallowed(function_)) {
throw new InputError("Unallowed function");
}
const client = await clientManager.getClient(id);
const result = transform(await client.invoke(function_));
if (result !== undefined) {
return [result];
} else {
return [null];
}
}
export interface WorkerStats {
clientCount: number;
clients: ClientStats[];
}
async function stats(): Promise<WorkerStats> {
return {
clientCount: clientCount(),
clients: await clientManager.getStats(),
};
}
async function getUpdates(
id: string,
timeout: number,
): Promise<Parameters<typeof Response["json"]>> {
if (timeout < 0) {
throw new InputError(`Invalid timeout: ${timeout}`);
}
return [await clientManager.getUpdates(id, timeout)];
}
async function setWebhook(
id: string,
url: string,
): Promise<Parameters<typeof Response["json"]>> {
await clientManager.setWebhook(id, url);
return ["Webhook was set."];
}
async function deleteWebhook(
id: string,
): Promise<Parameters<typeof Response["json"]>> {
await clientManager.deleteWebhook(id);
return ["Webhook was deleted."];
}
async function startWebhookLoop(id: string) {
await clientManager.startWebhookLoop(id);
}
function unload() {
dispatchEvent(new Event("unload"));
}
async function dropPendingUpdates(
id: string,
): Promise<Parameters<typeof Response["json"]>> {
await clientManager.dropPendingUpdates(id);
return [null];
}
async function clearCache(): Promise<Parameters<typeof Response["json"]>> {
await clientManager.clearCache();
return [null];
}