Skip to content

[Node] Gracefully handle connection errors in the outbound network proxy #2370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
'use strict';

import * as dns from 'dns';
import * as util from 'node:util';
import * as net from 'net';
import * as http from 'http';
import * as net from 'net';
import * as util from 'node:util';
import { WebSocketServer } from 'ws';
import { debugLog } from './utils';

Expand Down Expand Up @@ -146,6 +146,15 @@ async function onWsConnect(client: any, request: http.IncomingMessage) {
return;
}

// Validate port range
if (reqTargetPort < 0 || reqTargetPort > 65535) {
clientLog('Invalid port number: ' + reqTargetPort);
// Send empty binary data to notify requester that connection failed
client.send([]);
client.close(3000);
return;
}

// eslint-disable-next-line prefer-const
let target: any;
const recvQueue: Buffer[] = [];
Expand Down Expand Up @@ -207,7 +216,11 @@ async function onWsConnect(client: any, request: http.IncomingMessage) {
// Send empty binary data to notify requester that connection was
// initiated
client.send([]);
client.close(3000);
// Without this random timeout, PHP sometimes doesn't notice the socket
// disconnected. TODO: figure out why.
setTimeout(() => {
client.close(3000);
});
return;
}
} else {
Expand Down Expand Up @@ -238,7 +251,16 @@ async function onWsConnect(client: any, request: http.IncomingMessage) {
});
target.on('error', function (e: any) {
clientLog('target connection error', e);
target.end();
client.close(3000);
client.send([]);
// Without this random timeout, PHP sometimes doesn't notice the socket
// disconnected. TODO: figure out why.
setTimeout(() => {
client.close(3000);
try {
target.end();
} catch {
// Ignore
}
});
});
}
Loading