Skip to content

Commit 43da066

Browse files
authored
More errors (#1)
* All the errors * Handle common socket errors * More log * Handle common socket errors pt 2 * Bump version
1 parent 056b690 commit 43da066

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ All available options:
2020
```js
2121
require('proxyproto').createServer(server, {
2222
setNoDelay: true, // diable nagle algorithm
23+
handleCommonErrors: false, // handle common socket errors (default: true)
2324
onError: err => log.error(err) // error handler for servers and sockets
2425
});
2526
```

index.js

+33-14
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,30 @@ const createServer = (server, options) => {
1010
throw new Error('Missing server argument - http.createServer(), https, net, tls, etc');
1111
}
1212
options = options || {};
13+
if (!options.hasOwnProperty('handleCommonErrors')) {
14+
options.handleCommonErrors = true;
15+
}
1316

14-
function onError(err) {
15-
if (err && err.code === 'ECONNRESET') {
16-
console.log('Connection interrupted');
17-
} else if (options.onError) {
18-
options.onError(err);
19-
} else {
20-
throw err;
17+
function onError(err, source) {
18+
// handle common socket errors
19+
if (options.handleCommonErrors) {
20+
const error = String(err);
21+
if (err && err.code === 'ECONNRESET') {
22+
return console.log(`${source} Connection interrupted`);
23+
} else if (error.includes('peer did not return a certificate')) {
24+
return console.log(`${source} Connection dropped - Client certificate required but not presented`);
25+
} else if (error.includes('inappropriate fallback') ||
26+
error.includes('version too low') ||
27+
error.includes('no shared cipher')) {
28+
return console.log(`${source} Connection dropped - Client used insecure cipher`);
29+
} else if (error.includes('unknown protocol')) {
30+
return console.log(`${source} Connection dropped - Client used unknown protocol`);
31+
}
2132
}
33+
if (options.onError) {
34+
return options.onError(err, source);
35+
}
36+
throw err;
2237
}
2338

2439
// create proxy protocol processing server
@@ -31,7 +46,7 @@ const createServer = (server, options) => {
3146
if (options.setNoDelay) {
3247
connection.setNoDelay(true); // disable nagle algorithm
3348
}
34-
connection.addListener('error', onError);
49+
connection.addListener('error', err => onError(err, 'proxyproto socket'));
3550
connection.addListener('data', onData);
3651
function onData(buffer) {
3752
connection.pause();
@@ -65,10 +80,10 @@ const createServer = (server, options) => {
6580
}
6681
});
6782

68-
proxied.on('clientError', onError);
69-
proxied.on('error', onError);
70-
server.on('clientError', onError);
71-
server.on('error', onError);
83+
proxied.on('clientError', err => onError(err, 'proxyproto client'));
84+
proxied.on('error', err => onError(err, 'proxyproto'));
85+
server.on('clientError', err => onError(err, 'server client'));
86+
server.on('error', err => onError(err, 'server'));
7287

7388
// if server is tls, prepare child connection
7489
if (server._sharedCreds) {
@@ -78,14 +93,18 @@ const createServer = (server, options) => {
7893
get: () => connection._parent[property]
7994
});
8095
});
81-
connection.addListener('error', onError);
96+
connection.addListener('error', err => onError(err, 'secure socket'));
8297
connection.setKeepAlive(true); // prevent idle timeout ECONNRESET
8398
if (options.setNoDelay) {
8499
connection.setNoDelay(true); // disable nagle algorithm
85100
}
86101
});
102+
} else {
103+
server.on('connection', connection => {
104+
connection.addListener('error', err => onError(err, 'socket'));
105+
});
87106
}
88-
107+
89108
// if server is already listening, use that port
90109
if (server.listening) {
91110
const port = server.address().port;

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "proxyproto",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Pre-process PROXY protocol headers from node tcp connections",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)