socket-function 0.59.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "socket-function",
3
- "version": "0.59.0",
3
+ "version": "0.60.0",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "note1": "note on node-forge fork, see https://github.com/digitalbazaar/forge/issues/744 for details",
@@ -188,57 +188,56 @@ export async function startSocketServer(
188
188
  });
189
189
 
190
190
  let realServer = net.createServer(socket => {
191
- let server = sniServers.get("querysub.com") || mainHTTPSServer;
192
- server.emit("connection", socket);
193
-
194
- // //console.log("Received TCP connection from " + socket.remoteAddress);
195
- // const debug = socket.remoteAddress + ":" + socket.remotePort;
196
- // function handleTLSHello(buffer: Buffer, packetCount: number): void | "more" {
197
- // if (!SocketFunction.silent) {
198
- // console.log(`Received TCP header packet from ${debug}, have ${buffer.length} bytes so far, ${packetCount} packets`);
199
- // }
200
- // // All HTTPS requests start with 22, and no HTTP requests start with 22,
201
- // // so we just need to read the first byte.
202
- // let server: https.Server | http.Server;
203
- // if (buffer[0] !== 22) {
204
- // server = httpServer;
205
- // } else {
206
- // let data = parseTLSHello(buffer);
207
- // if (data.missingBytes > 0) {
208
- // return "more";
209
- // }
210
- // let sni = data.extensions.filter(x => x.type === SNIType).flatMap(x => parseSNIExtension(x.data))[0];
211
- // if (!SocketFunction.silent) {
212
- // console.log(`Received TCP connection with SNI ${JSON.stringify(sni)}`);
213
- // }
214
- // if (!sni) {
215
- // console.warn(`No SNI found in TLS hello from ${debug}, using main server. Packets ${packetCount}`);
216
- // console.log(buffer.toString("base64"));
217
- // }
218
- // server = sniServers.get(sni) || mainHTTPSServer;
219
- // }
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
+ }
220
219
 
221
- // // NOTE: Messages aren't dequeued until the current handler finishes, so we don't need to pause the socket or anything.
222
- // server.emit("connection", socket);
223
- // socket.unshift(buffer);
224
- // }
225
- // let buffers: Buffer[] = [];
226
- // function getNextData() {
227
- // // NOTE: ONCE is used, so we only look at the first buffer, and then after that
228
- // // we pipe. This should be very efficient, as pipe has insane throughput
229
- // // (100s of MB/s, easily, even on a terrible machine).
230
- // socket.once("data", buffer => {
231
- // buffers.push(buffer);
232
- // let result = handleTLSHello(Buffer.concat(buffers), buffers.length);
233
- // if (result === "more") {
234
- // getNextData();
235
- // }
236
- // });
237
- // }
238
- // getNextData();
239
- // socket.on("error", (e) => {
240
- // console.error(`Socket error for ${debug}, ${e.stack}`);
241
- // });
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
+ });
242
241
  });
243
242
 
244
243