socket-function 0.58.0 → 0.60.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/webSocketServer.ts +49 -49
package/package.json
CHANGED
package/src/webSocketServer.ts
CHANGED
|
@@ -188,56 +188,56 @@ export async function startSocketServer(
|
|
|
188
188
|
});
|
|
189
189
|
|
|
190
190
|
let realServer = net.createServer(socket => {
|
|
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
|
-
|
|
191
|
+
const debug = socket.remoteAddress + ":" + socket.remotePort;
|
|
192
|
+
if (!SocketFunction.silent) {
|
|
193
|
+
console.log(`Received TCP connection from ${debug}`);
|
|
194
|
+
}
|
|
195
|
+
function handleTLSHello(buffer: Buffer, packetCount: number): void | "more" {
|
|
196
|
+
if (!SocketFunction.silent) {
|
|
197
|
+
console.log(`Received TCP header packet from ${debug}, have ${buffer.length} bytes so far, ${packetCount} packets`);
|
|
198
|
+
}
|
|
199
|
+
// All HTTPS requests start with 22, and no HTTP requests start with 22,
|
|
200
|
+
// so we just need to read the first byte.
|
|
201
|
+
let server: https.Server | http.Server;
|
|
202
|
+
if (buffer[0] !== 22) {
|
|
203
|
+
server = httpServer;
|
|
204
|
+
} else {
|
|
205
|
+
let data = parseTLSHello(buffer);
|
|
206
|
+
if (data.missingBytes > 0) {
|
|
207
|
+
return "more";
|
|
208
|
+
}
|
|
209
|
+
let sni = data.extensions.filter(x => x.type === SNIType).flatMap(x => parseSNIExtension(x.data))[0];
|
|
210
|
+
if (!SocketFunction.silent) {
|
|
211
|
+
console.log(`Received TCP connection with SNI ${JSON.stringify(sni)}`);
|
|
212
|
+
}
|
|
213
|
+
if (!sni) {
|
|
214
|
+
console.warn(`No SNI found in TLS hello from ${debug}, using main server. Packets ${packetCount}`);
|
|
215
|
+
console.log(buffer.toString("base64"));
|
|
216
|
+
}
|
|
217
|
+
server = sniServers.get(sni) || mainHTTPSServer;
|
|
218
|
+
}
|
|
219
219
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
220
|
+
// NOTE: Messages aren't dequeued until the current handler finishes, so we don't need to pause the socket or anything.
|
|
221
|
+
server.emit("connection", socket);
|
|
222
|
+
socket.unshift(buffer);
|
|
223
|
+
}
|
|
224
|
+
let buffers: Buffer[] = [];
|
|
225
|
+
function getNextData() {
|
|
226
|
+
// NOTE: ONCE is used, so we only look at the first buffer, and then after that
|
|
227
|
+
// we pipe. This should be very efficient, as pipe has insane throughput
|
|
228
|
+
// (100s of MB/s, easily, even on a terrible machine).
|
|
229
|
+
socket.once("data", buffer => {
|
|
230
|
+
buffers.push(buffer);
|
|
231
|
+
let result = handleTLSHello(Buffer.concat(buffers), buffers.length);
|
|
232
|
+
if (result === "more") {
|
|
233
|
+
getNextData();
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
getNextData();
|
|
238
|
+
socket.on("error", (e) => {
|
|
239
|
+
console.error(`Socket error for ${debug}, ${e.stack}`);
|
|
240
|
+
});
|
|
241
241
|
});
|
|
242
242
|
|
|
243
243
|
|