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,6 +186,7 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
return endOriginReqRes(); return endOriginReqRes();
} }
console.log(destinationUrl); console.log(destinationUrl);
try {
const proxyResponse = await plugins.smartrequest.request( const proxyResponse = await plugins.smartrequest.request(
destinationUrl, destinationUrl,
{ {
@ -202,7 +203,7 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
originRequest.on('data', (data) => { originRequest.on('data', (data) => {
proxyRequest.write(data); proxyRequest.write(data);
}); });
originRequest.on('end', (data) => { originRequest.on('end', () => {
proxyRequest.end(); proxyRequest.end();
}); });
originRequest.on('error', () => { originRequest.on('error', () => {
@ -244,6 +245,10 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
originResponse.end(); originResponse.end();
originResponse.destroy(); originResponse.destroy();
}); });
} catch (error) {
console.error('Error while processing request:', error);
endOriginReqRes(502, 'Bad Gateway: Error processing the request');
}
} }
); );
@ -277,30 +282,44 @@ JNj2Dr5H0XoLFFnvuvzcRbhlJ9J67JzR+7g=
} }
wsIncoming.on('message', async (message, isBinary) => { wsIncoming.on('message', async (message, isBinary) => {
try {
await outGoingDeferred.promise; await outGoingDeferred.promise;
// console.log("client to upstream", message);
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 = () => {
if (wsOutgoing) {
wsOutgoing.terminate(); wsOutgoing.terminate();
console.log('terminated outgoing ws.'); 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 = () => {
if (wsIncoming) {
wsIncoming.terminate(); wsIncoming.terminate();
console.log('terminated incoming ws.'); 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.');
} }
} }