-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
147 lines (128 loc) · 4.46 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
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
import { Config } from "./configs.ts";
import { getConfig } from "./configs.ts";
import { getSymbol, stringifySymbol } from "./symbols.ts";
import {
compress,
init,
} from "https://deno.land/x/[email protected]/deno/zstd.ts";
await init();
const HEADER = 0xbb8ce7a278bb40f6n;
Deno.serve({
port: 31112,
}, (req) => {
if (req.method !== "GET") {
return new Response(null, {
status: 405,
});
}
const url = new URL(req.url);
if (req.headers.get("upgrade") !== "websocket") {
const path = url.pathname.split("/").slice(1);
if (path.length !== 3) {
return new Response(null, {
status: 400,
});
}
const [platform, type, id] = path;
const config = getConfig(platform, type, id);
if (!config) {
return new Response(null, {
status: 404,
});
}
return new Response(JSON.stringify(config), {
headers: {
"content-type": "application/json",
},
});
}
const platform = url.searchParams.get("platform") || "generic";
const { socket, response } = Deno.upgradeWebSocket(req);
const send = (symbol: bigint, data: Uint8Array): void => {
const length = BigInt(data.byteLength);
const buffer = new Uint8Array(24 + data.byteLength);
const dataView = new DataView(buffer.buffer);
dataView.setBigUint64(0, HEADER, true);
dataView.setBigInt64(8, symbol, true);
dataView.setBigUint64(16, length, true);
buffer.set(data, 24);
socket.send(buffer);
};
const sendSuccess = async (
typeSymbol: bigint,
idSymbol: bigint,
config: Config,
): Promise<void> => {
const configData = new TextEncoder().encode(JSON.stringify(config));
const uncompressed = new Uint8Array(configData.byteLength + 1);
uncompressed.set(configData, 0);
const compressed = compress(uncompressed);
const data = new Uint8Array(20 + compressed.byteLength);
const dataView = new DataView(data.buffer);
dataView.setBigInt64(0, typeSymbol, true);
dataView.setBigInt64(8, idSymbol, true);
dataView.setUint32(16, uncompressed.byteLength, true);
data.set(compressed, 20);
send(await getSymbol("SNSConfigSuccessv2"), data);
send(await getSymbol("STcpConnectionUnrequireEvent"), new Uint8Array([0]));
};
const sendFailure = async (
typeSymbol: bigint,
idSymbol: bigint,
errorInfo: {
type: string;
id: string;
errorcode: number;
error: string;
},
): Promise<void> => {
const errorInfoData = new TextEncoder().encode(JSON.stringify(errorInfo));
const data = new Uint8Array(16 + errorInfoData.byteLength + 1);
const dataView = new DataView(data.buffer);
dataView.setBigInt64(0, typeSymbol, true);
dataView.setBigInt64(8, idSymbol, true);
data.set(errorInfoData, 16);
send(await getSymbol("SNSConfigFailurev2"), data);
send(await getSymbol("STcpConnectionUnrequireEvent"), new Uint8Array([0]));
};
const recieve = async (symbol: bigint, data: Uint8Array) => {
if (symbol != await getSymbol("SNSConfigRequestv2")) return;
const _typeTail = data[0];
const infoData = data.slice(1, data.length - 1);
const infoString = new TextDecoder().decode(infoData);
const info: unknown = JSON.parse(infoString);
if (typeof info !== "object" || info === null) return;
if (!("type" in info) || typeof info.type !== "string") return;
if (!("id" in info) || typeof info.id !== "string") return;
const type = info.type;
const id = info.id;
const typeSymbol = await getSymbol(type);
const idSymbol = await getSymbol(id);
const config = getConfig(platform, type, id);
if (config) await sendSuccess(typeSymbol, idSymbol, config);
else {await sendFailure(typeSymbol, idSymbol, {
type,
id,
errorcode: 1,
error:
`Could not find specified config data with the provided identifier (type = ${type}, id = ${id})`,
});}
};
socket.onmessage = async (event) => {
if (!(event.data instanceof ArrayBuffer)) return;
const dataView = new DataView(event.data);
let offset = 0;
while (offset < event.data.byteLength) {
const header = dataView.getBigUint64(offset, true);
if (header !== HEADER) return;
offset += 8;
const symbol = dataView.getBigInt64(offset, true);
offset += 8;
const length = Number(dataView.getBigUint64(offset, true));
offset += 8;
await recieve(symbol, new Uint8Array(event.data, offset, length));
offset += length;
}
};
return response;
});