@push.rocks/smartproxy 3.0.58 → 3.0.61

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.
@@ -129,16 +129,20 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
129
129
  originResponse.writeHead(statusArg, messageArg);
130
130
  originResponse.end(messageArg);
131
131
  if (originRequest.socket !== originResponse.socket) {
132
- console.log('hey, something is strange.')
132
+ console.log('hey, something is strange.');
133
133
  }
134
134
  originResponse.destroy();
135
135
  };
136
136
 
137
- console.log(`got request: ${originRequest.headers.host}${plugins.url.parse(originRequest.url).path}`);
137
+ console.log(
138
+ `got request: ${originRequest.headers.host}${plugins.url.parse(originRequest.url).path}`
139
+ );
138
140
  const destinationConfig = this.router.routeReq(originRequest);
139
141
 
140
142
  if (!destinationConfig) {
141
- console.log(`${originRequest.headers.host} can't be routed properly. Terminating request.`);
143
+ console.log(
144
+ `${originRequest.headers.host} can't be routed properly. Terminating request.`
145
+ );
142
146
  endOriginReqRes();
143
147
  return;
144
148
  }
@@ -182,114 +186,140 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
182
186
  return endOriginReqRes();
183
187
  }
184
188
  console.log(destinationUrl);
185
- const proxyResponse = await plugins.smartrequest.request(
186
- destinationUrl,
187
- {
188
- method: originRequest.method,
189
- headers: {
190
- ...originRequest.headers,
191
- 'X-Forwarded-Host': originRequest.headers.host,
192
- 'X-Forwarded-Proto': 'https'
189
+ try {
190
+ const proxyResponse = await plugins.smartrequest.request(
191
+ destinationUrl,
192
+ {
193
+ method: originRequest.method,
194
+ headers: {
195
+ ...originRequest.headers,
196
+ 'X-Forwarded-Host': originRequest.headers.host,
197
+ 'X-Forwarded-Proto': 'https',
198
+ },
199
+ keepAlive: true,
193
200
  },
194
- keepAlive: true,
195
- },
196
- true, // lets make this streaming
197
- (proxyRequest) => {
198
- originRequest.on('data', (data) => {
199
- proxyRequest.write(data);
200
- });
201
- originRequest.on('end', (data) => {
202
- proxyRequest.end();
203
- });
204
- originRequest.on('error', () => {
205
- proxyRequest.end();
206
- });
207
- originRequest.on('close', () => {
208
- proxyRequest.end();
209
- });
210
- originRequest.on('timeout', () => {
211
- proxyRequest.end();
212
- originRequest.destroy();
213
- });
214
- proxyRequest.on('error', () => {
215
- endOriginReqRes();
216
- })
201
+ true, // lets make this streaming
202
+ (proxyRequest) => {
203
+ originRequest.on('data', (data) => {
204
+ proxyRequest.write(data);
205
+ });
206
+ originRequest.on('end', () => {
207
+ proxyRequest.end();
208
+ });
209
+ originRequest.on('error', () => {
210
+ proxyRequest.end();
211
+ });
212
+ originRequest.on('close', () => {
213
+ proxyRequest.end();
214
+ });
215
+ originRequest.on('timeout', () => {
216
+ proxyRequest.end();
217
+ originRequest.destroy();
218
+ });
219
+ proxyRequest.on('error', () => {
220
+ endOriginReqRes();
221
+ });
222
+ }
223
+ );
224
+ originResponse.statusCode = proxyResponse.statusCode;
225
+ console.log(proxyResponse.statusCode);
226
+ for (const defaultHeader of Object.keys(this.defaultHeaders)) {
227
+ originResponse.setHeader(defaultHeader, this.defaultHeaders[defaultHeader]);
217
228
  }
218
- );
219
- originResponse.statusCode = proxyResponse.statusCode;
220
- console.log(proxyResponse.statusCode);
221
- for (const defaultHeader of Object.keys(this.defaultHeaders)) {
222
- originResponse.setHeader(defaultHeader, this.defaultHeaders[defaultHeader]);
223
- }
224
- for (const header of Object.keys(proxyResponse.headers)) {
225
- originResponse.setHeader(header, proxyResponse.headers[header]);
229
+ for (const header of Object.keys(proxyResponse.headers)) {
230
+ originResponse.setHeader(header, proxyResponse.headers[header]);
231
+ }
232
+ proxyResponse.on('data', (data) => {
233
+ originResponse.write(data);
234
+ });
235
+ proxyResponse.on('end', () => {
236
+ originResponse.end();
237
+ });
238
+ proxyResponse.on('error', () => {
239
+ originResponse.destroy();
240
+ });
241
+ proxyResponse.on('close', () => {
242
+ originResponse.end();
243
+ });
244
+ proxyResponse.on('timeout', () => {
245
+ originResponse.end();
246
+ originResponse.destroy();
247
+ });
248
+ } catch (error) {
249
+ console.error('Error while processing request:', error);
250
+ endOriginReqRes(502, 'Bad Gateway: Error processing the request');
226
251
  }
227
- proxyResponse.on('data', (data) => {
228
- originResponse.write(data);
229
- });
230
- proxyResponse.on('end', () => {
231
- originResponse.end();
232
- });
233
- proxyResponse.on('error', () => {
234
- originResponse.destroy();
235
- });
236
- proxyResponse.on('close', () => {
237
- originResponse.end();
238
- });
239
- proxyResponse.on('timeout', () => {
240
- originResponse.end();
241
- originResponse.destroy()
242
- });
243
-
244
252
  }
245
253
  );
246
254
 
247
255
  // Enable websockets
248
- const wsServer = new plugins.ws.WebSocketServer({ server: this.httpsServer });
249
- wsServer.on('connection', async (wsIncoming: plugins.wsDefault, reqArg: plugins.http.IncomingMessage) => {
250
- console.log(`wss proxy: got connection for wsc for https://${reqArg.headers.host}${reqArg.url}`);
251
-
252
- let wsOutgoing: plugins.wsDefault;
256
+ const wsServer = new plugins.ws.WebSocketServer({ server: this.httpsServer });
257
+ wsServer.on(
258
+ 'connection',
259
+ async (wsIncoming: plugins.wsDefault, reqArg: plugins.http.IncomingMessage) => {
260
+ console.log(
261
+ `wss proxy: got connection for wsc for https://${reqArg.headers.host}${reqArg.url}`
262
+ );
253
263
 
254
- const outGoingDeferred = plugins.smartpromise.defer();
264
+ let wsOutgoing: plugins.wsDefault;
255
265
 
256
- try {
257
- wsOutgoing = new plugins.wsDefault(`ws://${this.router.routeReq(reqArg).destinationIp}:${this.router.routeReq(reqArg).destinationPort}${reqArg.url}`);
258
- console.log('wss proxy: initiated outgoing proxy');
259
- wsOutgoing.on('open', async () => {
260
- outGoingDeferred.resolve();
261
- })
262
- } catch (err) {
263
- console.log(err);
264
- wsIncoming.terminate();
265
- return;
266
- }
266
+ const outGoingDeferred = plugins.smartpromise.defer();
267
267
 
268
- wsIncoming.on("message", async (message, isBinary) => {
269
- await outGoingDeferred.promise;
270
- // console.log("client to upstream", message);
271
- wsOutgoing.send(message, { binary: isBinary });
272
- });
273
-
274
- wsOutgoing.on("message", async (message, isBinary) => {
275
- // console.log("upstream to client", message);
276
- wsIncoming.send(message, { binary: isBinary });
277
- });
278
- const terminateWsOutgoing = () => {
279
- wsOutgoing.terminate();
280
- console.log('terminated outgoing ws.');
281
- }
282
- wsIncoming.on("error", () => terminateWsOutgoing());
283
- wsIncoming.on("close", () => terminateWsOutgoing());
268
+ try {
269
+ wsOutgoing = new plugins.wsDefault(
270
+ `ws://${this.router.routeReq(reqArg).destinationIp}:${
271
+ this.router.routeReq(reqArg).destinationPort
272
+ }${reqArg.url}`
273
+ );
274
+ console.log('wss proxy: initiated outgoing proxy');
275
+ wsOutgoing.on('open', async () => {
276
+ outGoingDeferred.resolve();
277
+ });
278
+ } catch (err) {
279
+ console.log(err);
280
+ wsIncoming.terminate();
281
+ return;
282
+ }
284
283
 
285
- const terminateWsIncoming = () => {
286
- wsIncoming.terminate();
287
- console.log('terminated incoming ws.');
284
+ wsIncoming.on('message', async (message, isBinary) => {
285
+ try {
286
+ await outGoingDeferred.promise;
287
+ wsOutgoing.send(message, { binary: isBinary });
288
+ } catch (error) {
289
+ console.error('Error sending message to wsOutgoing:', error);
290
+ }
291
+ });
292
+
293
+ wsOutgoing.on('message', async (message, isBinary) => {
294
+ try {
295
+ wsIncoming.send(message, { binary: isBinary });
296
+ } catch (error) {
297
+ console.error('Error sending message to wsIncoming:', error);
298
+ }
299
+ });
300
+
301
+ const terminateWsOutgoing = () => {
302
+ if (wsOutgoing) {
303
+ wsOutgoing.terminate();
304
+ console.log('terminated outgoing ws.');
305
+ }
306
+ };
307
+ wsIncoming.on('error', () => terminateWsOutgoing());
308
+ wsIncoming.on('close', () => terminateWsOutgoing());
309
+
310
+ const terminateWsIncoming = () => {
311
+ if (wsIncoming) {
312
+ wsIncoming.terminate();
313
+ console.log('terminated incoming ws.');
314
+ }
315
+ };
316
+ wsOutgoing.on('error', () => terminateWsIncoming());
317
+ wsOutgoing.on('close', () => terminateWsIncoming());
318
+
319
+
288
320
  }
289
- wsOutgoing.on("error", () => terminateWsIncoming());
290
- wsOutgoing.on("close", () => terminateWsIncoming());
321
+ );
291
322
 
292
- });
293
323
  this.httpsServer.keepAliveTimeout = 600 * 1000;
294
324
  this.httpsServer.headersTimeout = 600 * 1000;
295
325
 
@@ -314,7 +344,7 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
314
344
  });
315
345
  connection.on('timeout', () => {
316
346
  cleanupConnection();
317
- })
347
+ });
318
348
  });
319
349
 
320
350
  this.httpsServer.listen(this.options.port);
@@ -349,7 +379,6 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
349
379
  cert: hostCandidate.publicKey,
350
380
  key: hostCandidate.privateKey,
351
381
  });
352
- this.httpsServer;
353
382
  }
354
383
  }
355
384
 
@@ -368,5 +397,6 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
368
397
  socket.destroy();
369
398
  });
370
399
  await done.promise;
400
+ console.log('NetworkProxy -> OK: Server has been stopped and all connections closed.');
371
401
  }
372
- }
402
+ }
@@ -29,4 +29,4 @@ export class SslRedirect {
29
29
  });
30
30
  await done.promise;
31
31
  }
32
- }
32
+ }
@@ -12,11 +12,11 @@ import * as tsclass from '@tsclass/tsclass';
12
12
  export { tsclass };
13
13
 
14
14
  // pushrocks scope
15
- import * as lik from '@pushrocks/lik';
16
- import * as smartdelay from '@pushrocks/smartdelay';
17
- import * as smartpromise from '@pushrocks/smartpromise';
18
- import * as smartrequest from '@pushrocks/smartrequest';
19
- import * as smartstring from '@pushrocks/smartstring';
15
+ import * as lik from '@push.rocks/lik';
16
+ import * as smartdelay from '@push.rocks/smartdelay';
17
+ import * as smartpromise from '@push.rocks/smartpromise';
18
+ import * as smartrequest from '@push.rocks/smartrequest';
19
+ import * as smartstring from '@push.rocks/smartstring';
20
20
 
21
21
  export { lik, smartdelay, smartrequest, smartpromise, smartstring };
22
22