fix(networkproxy): Improve error handling for proxy requests

This commit is contained in:
Philipp Kunz 2024-10-07 12:29:49 +02:00
parent 3eb4b576a7
commit 70bdf074a1
3 changed files with 114 additions and 68 deletions

27
changelog.md Normal file
View File

@ -0,0 +1,27 @@
# Changelog
## 2024-10-07 - 3.0.61 - fix(networkproxy)
Improve error handling for proxy requests
- Wrapped proxy request logic in a try-catch block to handle errors gracefully.
- Improved error handling for WebSocket communication by checking errors before attempting to send messages.
- Added logging for error cases to aid in debugging.
## 2024-05-29 - 3.0.60 - various updates
Maintenance updates and adjustments.
- Updated project description
- Updated tsconfig settings
- Updated npmextra.json with new githost info
## 2023-07-27 - 3.0.58 to 3.0.59 - core improvements
Improvements and internal restructuring.
- Switch to a new organizational scheme
- Core updates and adjustments
## 2022-07-29 - 2.0.16 to 3.0.0 - major transition
This release marks a major transition with several breaking changes.
- BREAKING CHANGE: switched core to ESM (EcmaScript Module)
- Major core updates

View File

@ -1,8 +1,8 @@
/** /**
* autocreated commitinfo by @pushrocks/commitinfo * autocreated commitinfo by @push.rocks/commitinfo
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartproxy', name: '@push.rocks/smartproxy',
version: '3.0.60', version: '3.0.61',
description: 'a proxy for handling high workloads of proxying' description: 'a proxy for handling high workloads of proxying'
} }

View File

@ -186,64 +186,69 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
return endOriginReqRes(); return endOriginReqRes();
} }
console.log(destinationUrl); console.log(destinationUrl);
const proxyResponse = await plugins.smartrequest.request( try {
destinationUrl, const proxyResponse = await plugins.smartrequest.request(
{ destinationUrl,
method: originRequest.method, {
headers: { method: originRequest.method,
...originRequest.headers, headers: {
'X-Forwarded-Host': originRequest.headers.host, ...originRequest.headers,
'X-Forwarded-Proto': 'https', 'X-Forwarded-Host': originRequest.headers.host,
'X-Forwarded-Proto': 'https',
},
keepAlive: true,
}, },
keepAlive: true, true, // lets make this streaming
}, (proxyRequest) => {
true, // lets make this streaming originRequest.on('data', (data) => {
(proxyRequest) => { proxyRequest.write(data);
originRequest.on('data', (data) => { });
proxyRequest.write(data); originRequest.on('end', () => {
}); proxyRequest.end();
originRequest.on('end', (data) => { });
proxyRequest.end(); originRequest.on('error', () => {
}); proxyRequest.end();
originRequest.on('error', () => { });
proxyRequest.end(); originRequest.on('close', () => {
}); proxyRequest.end();
originRequest.on('close', () => { });
proxyRequest.end(); originRequest.on('timeout', () => {
}); proxyRequest.end();
originRequest.on('timeout', () => { originRequest.destroy();
proxyRequest.end(); });
originRequest.destroy(); proxyRequest.on('error', () => {
}); endOriginReqRes();
proxyRequest.on('error', () => { });
endOriginReqRes(); }
}); );
originResponse.statusCode = proxyResponse.statusCode;
console.log(proxyResponse.statusCode);
for (const defaultHeader of Object.keys(this.defaultHeaders)) {
originResponse.setHeader(defaultHeader, this.defaultHeaders[defaultHeader]);
} }
); for (const header of Object.keys(proxyResponse.headers)) {
originResponse.statusCode = proxyResponse.statusCode; originResponse.setHeader(header, proxyResponse.headers[header]);
console.log(proxyResponse.statusCode); }
for (const defaultHeader of Object.keys(this.defaultHeaders)) { proxyResponse.on('data', (data) => {
originResponse.setHeader(defaultHeader, this.defaultHeaders[defaultHeader]); originResponse.write(data);
});
proxyResponse.on('end', () => {
originResponse.end();
});
proxyResponse.on('error', () => {
originResponse.destroy();
});
proxyResponse.on('close', () => {
originResponse.end();
});
proxyResponse.on('timeout', () => {
originResponse.end();
originResponse.destroy();
});
} catch (error) {
console.error('Error while processing request:', error);
endOriginReqRes(502, 'Bad Gateway: Error processing the request');
} }
for (const header of Object.keys(proxyResponse.headers)) {
originResponse.setHeader(header, proxyResponse.headers[header]);
}
proxyResponse.on('data', (data) => {
originResponse.write(data);
});
proxyResponse.on('end', () => {
originResponse.end();
});
proxyResponse.on('error', () => {
originResponse.destroy();
});
proxyResponse.on('close', () => {
originResponse.end();
});
proxyResponse.on('timeout', () => {
originResponse.end();
originResponse.destroy();
});
} }
); );
@ -277,30 +282,44 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
} }
wsIncoming.on('message', async (message, isBinary) => { wsIncoming.on('message', async (message, isBinary) => {
await outGoingDeferred.promise; try {
// console.log("client to upstream", message); await outGoingDeferred.promise;
wsOutgoing.send(message, { binary: isBinary }); wsOutgoing.send(message, { binary: isBinary });
} catch (error) {
console.error('Error sending message to wsOutgoing:', error);
}
}); });
wsOutgoing.on('message', async (message, isBinary) => { wsOutgoing.on('message', async (message, isBinary) => {
// console.log("upstream to client", message); try {
wsIncoming.send(message, { binary: isBinary }); wsIncoming.send(message, { binary: isBinary });
} catch (error) {
console.error('Error sending message to wsIncoming:', error);
}
}); });
const terminateWsOutgoing = () => { const terminateWsOutgoing = () => {
wsOutgoing.terminate(); if (wsOutgoing) {
console.log('terminated outgoing ws.'); wsOutgoing.terminate();
console.log('terminated outgoing ws.');
}
}; };
wsIncoming.on('error', () => terminateWsOutgoing()); wsIncoming.on('error', () => terminateWsOutgoing());
wsIncoming.on('close', () => terminateWsOutgoing()); wsIncoming.on('close', () => terminateWsOutgoing());
const terminateWsIncoming = () => { const terminateWsIncoming = () => {
wsIncoming.terminate(); if (wsIncoming) {
console.log('terminated incoming ws.'); wsIncoming.terminate();
console.log('terminated incoming ws.');
}
}; };
wsOutgoing.on('error', () => terminateWsIncoming()); wsOutgoing.on('error', () => terminateWsIncoming());
wsOutgoing.on('close', () => terminateWsIncoming()); wsOutgoing.on('close', () => terminateWsIncoming());
} }
); );
this.httpsServer.keepAliveTimeout = 600 * 1000; this.httpsServer.keepAliveTimeout = 600 * 1000;
this.httpsServer.headersTimeout = 600 * 1000; this.httpsServer.headersTimeout = 600 * 1000;
@ -360,7 +379,6 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
cert: hostCandidate.publicKey, cert: hostCandidate.publicKey,
key: hostCandidate.privateKey, key: hostCandidate.privateKey,
}); });
this.httpsServer;
} }
} }
@ -379,5 +397,6 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
socket.destroy(); socket.destroy();
}); });
await done.promise; await done.promise;
console.log('NetworkProxy -> OK: Server has been stopped and all connections closed.');
} }
} }