+10
-10
@@ -29,7 +29,6 @@ function debugLog(...args) {
|
|||||||
let proxyServer = null;
|
let proxyServer = null;
|
||||||
let proxyPort = null;
|
let proxyPort = null;
|
||||||
let proxyCertsDirOrCA = null; // saved so restart() can regenerate the cert
|
let proxyCertsDirOrCA = null; // saved so restart() can regenerate the cert
|
||||||
const proxyConnections = new Set(); // track live sockets so stop() can force-close them
|
|
||||||
/** Resolver: hostname -> { host, port } or port number (then host defaults to 127.0.0.1) or null */
|
/** Resolver: hostname -> { host, port } or port number (then host defaults to 127.0.0.1) or null */
|
||||||
let getBackendForHostname = null;
|
let getBackendForHostname = null;
|
||||||
|
|
||||||
@@ -99,11 +98,6 @@ function start(port, certsDirOrCA, callback, baseDomains) {
|
|||||||
proxyServer = https.createServer(opts, onRequest);
|
proxyServer = https.createServer(opts, onRequest);
|
||||||
// Forward WebSocket upgrade requests to the backend tunnel
|
// Forward WebSocket upgrade requests to the backend tunnel
|
||||||
proxyServer.on('upgrade', onUpgrade);
|
proxyServer.on('upgrade', onUpgrade);
|
||||||
// Track live connections so stop() can force-close them for instant restart
|
|
||||||
proxyServer.on('connection', (socket) => {
|
|
||||||
proxyConnections.add(socket);
|
|
||||||
socket.once('close', () => proxyConnections.delete(socket));
|
|
||||||
});
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (process.stderr) process.stderr.write('[https-proxy] createServer threw: ' + err.message + '\n');
|
if (process.stderr) process.stderr.write('[https-proxy] createServer threw: ' + err.message + '\n');
|
||||||
done(err);
|
done(err);
|
||||||
@@ -332,11 +326,17 @@ function stop(callback) {
|
|||||||
const s = proxyServer;
|
const s = proxyServer;
|
||||||
proxyServer = null;
|
proxyServer = null;
|
||||||
proxyPort = null;
|
proxyPort = null;
|
||||||
// Destroy all live connections so s.close() completes immediately
|
// Destroy the raw TCP connections tracked by bare-tcp's internal _connections
|
||||||
for (const socket of proxyConnections) {
|
// set. This is necessary because bare-tcp's _closeMaybe() only fires the
|
||||||
try { socket.destroy(); } catch (_) {}
|
// 'close' event once _connections is empty — destroying only the TLS-wrapped
|
||||||
|
// sockets (which we no longer track) would leave the raw sockets open and
|
||||||
|
// s.close() would never call its callback.
|
||||||
|
const rawConns = s._connections;
|
||||||
|
if (rawConns && typeof rawConns[Symbol.iterator] === 'function') {
|
||||||
|
for (const socket of rawConns) {
|
||||||
|
try { socket.destroy(); } catch (_) {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
proxyConnections.clear();
|
|
||||||
s.close(() => {
|
s.close(() => {
|
||||||
if (callback) callback();
|
if (callback) callback();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user