-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
57 lines (51 loc) · 1.44 KB
/
main.ts
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
// deno-lint-ignore-file require-await
import config from "./config.ts";
import { WServer } from "./network/socket.ts";
import User from "./models/users.ts";
import TTYList from "./tty.ts";
import handlers from "./network/handlers/mod.ts";
const ttyManager = new TTYList();
const _server = new WServer(
config.WEBSOCKET_HOST,
config.WEBSOCKET_PORT,
config.WEBSOCKET_SSL_CERT,
config.WEBSOCKET_SSL_KEY,
);
_server.on("connect", async (socket, context) => {
await socket.send("Please authenticate.");
});
_server.on("message", async (socket, context, ev) => {
try {
const packet: { [key: string]: any } = JSON.parse(ev);
ev = JSON.stringify(packet, (k, v) => {
if (k == "password") return "*".repeat(10);
else return v;
}, 2);
console.log(context.uuid, ev);
if (packet.event != undefined) {
const handler = handlers[packet.event as string];
if (handler != undefined) {
await handler(socket, context, packet, ttyManager);
}
}
} catch (_e) {
console.error(_e);
await socket.send(_e.toString());
}
});
_server.on("disconnect", async (client) => {
if (client.uid != null) {
// Cleanup DB
const user = await User.findUser({ uuid: client.uid });
if (user != null) {
user.is_online = false;
await user.update();
}
client.uid = null;
if (client.tty != null) {
client.tty.end();
client.tty = null;
}
}
});
_server.on("error", console.error);