socket-function 0.56.0 → 0.58.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.56.0",
3
+ "version": "0.58.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",
@@ -58,6 +58,8 @@ export async function httpCallHandler(request: http.IncomingMessage, response: h
58
58
  try {
59
59
  // Always set x-frame-options, to prevent iframe embedding click hijacking
60
60
  response.setHeader("X-Frame-Options", "SAMEORIGIN");
61
+ // Don't keep alive, to prevent issues with zombie sockets.
62
+ response.setHeader("Connection", "close");
61
63
 
62
64
  let urlBase = request.url;
63
65
  if (!urlBase) {
@@ -188,54 +188,56 @@ export async function startSocketServer(
188
188
  });
189
189
 
190
190
  let realServer = net.createServer(socket => {
191
- //console.log("Received TCP connection from " + socket.remoteAddress);
192
- const debug = socket.remoteAddress + ":" + socket.remotePort;
193
- function handleTLSHello(buffer: Buffer, packetCount: number): void | "more" {
194
- if (!SocketFunction.silent) {
195
- console.log(`Received TCP header packet from ${debug}, have ${buffer.length} bytes so far, ${packetCount} packets`);
196
- }
197
- // All HTTPS requests start with 22, and no HTTP requests start with 22,
198
- // so we just need to read the first byte.
199
- let server: https.Server | http.Server;
200
- if (buffer[0] !== 22) {
201
- server = httpServer;
202
- } else {
203
- let data = parseTLSHello(buffer);
204
- if (data.missingBytes > 0) {
205
- return "more";
206
- }
207
- let sni = data.extensions.filter(x => x.type === SNIType).flatMap(x => parseSNIExtension(x.data))[0];
208
- if (!SocketFunction.silent) {
209
- console.log(`Received TCP connection with SNI ${JSON.stringify(sni)}`);
210
- }
211
- if (!sni) {
212
- console.warn(`No SNI found in TLS hello from ${debug}, using main server. Packets ${packetCount}`);
213
- console.log(buffer.toString("base64"));
214
- }
215
- server = sniServers.get(sni) || mainHTTPSServer;
216
- }
191
+ mainHTTPSServer.emit("connection", socket);
217
192
 
218
- // NOTE: Messages aren't dequeued until the current handler finishes, so we don't need to pause the socket or anything.
219
- server.emit("connection", socket);
220
- socket.unshift(buffer);
221
- }
222
- let buffers: Buffer[] = [];
223
- function getNextData() {
224
- // NOTE: ONCE is used, so we only look at the first buffer, and then after that
225
- // we pipe. This should be very efficient, as pipe has insane throughput
226
- // (100s of MB/s, easily, even on a terrible machine).
227
- socket.once("data", buffer => {
228
- buffers.push(buffer);
229
- let result = handleTLSHello(Buffer.concat(buffers), buffers.length);
230
- if (result === "more") {
231
- getNextData();
232
- }
233
- });
234
- }
235
- getNextData();
236
- socket.on("error", (e) => {
237
- console.error(`Socket error for ${debug}, ${e.stack}`);
238
- });
193
+ // //console.log("Received TCP connection from " + socket.remoteAddress);
194
+ // const debug = socket.remoteAddress + ":" + socket.remotePort;
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
+
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
+ // });
239
241
  });
240
242
 
241
243