From 975790eadf07abb8c59928f473a32a480c230488 Mon Sep 17 00:00:00 2001 From: Harrison Powers Date: Mon, 25 Mar 2019 09:44:36 -0400 Subject: [PATCH 1/5] All the errors --- index.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index cb3e3be..d65c364 100644 --- a/index.js +++ b/index.js @@ -11,11 +11,11 @@ const createServer = (server, options) => { } options = options || {}; - function onError(err) { + function onError(err, source) { if (err && err.code === 'ECONNRESET') { - console.log('Connection interrupted'); + console.log(`${source} Connection interrupted`); } else if (options.onError) { - options.onError(err); + options.onError(err, source); } else { throw err; } @@ -31,7 +31,7 @@ const createServer = (server, options) => { if (options.setNoDelay) { connection.setNoDelay(true); // disable nagle algorithm } - connection.addListener('error', onError); + connection.addListener('error', err => onError(err, 'proxyproto socket')); connection.addListener('data', onData); function onData(buffer) { connection.pause(); @@ -65,10 +65,10 @@ const createServer = (server, options) => { } }); - proxied.on('clientError', onError); - proxied.on('error', onError); - server.on('clientError', onError); - server.on('error', onError); + proxied.on('clientError', err => onError(err, 'proxyproto client')); + proxied.on('error', err => onError(err, 'proxyproto')); + server.on('clientError', err => onError(err, 'server client')); + server.on('error', err => onError(err, 'server')); // if server is tls, prepare child connection if (server._sharedCreds) { @@ -78,14 +78,18 @@ const createServer = (server, options) => { get: () => connection._parent[property] }); }); - connection.addListener('error', onError); + connection.addListener('error', err => onError(err, 'secure socket')); connection.setKeepAlive(true); // prevent idle timeout ECONNRESET if (options.setNoDelay) { connection.setNoDelay(true); // disable nagle algorithm } }); + } else { + server.on('connection', connection => { + connection.addListener('error', err => onError(err, 'socket')); + }); } - + // if server is already listening, use that port if (server.listening) { const port = server.address().port; From e9e9514e4fcd4e2929bd2bbca0c02a40b8cb1405 Mon Sep 17 00:00:00 2001 From: Harrison Powers Date: Mon, 25 Mar 2019 11:26:31 -0400 Subject: [PATCH 2/5] Handle common socket errors --- index.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index d65c364..3c86b97 100644 --- a/index.js +++ b/index.js @@ -12,13 +12,25 @@ const createServer = (server, options) => { options = options || {}; function onError(err, source) { - if (err && err.code === 'ECONNRESET') { - console.log(`${source} Connection interrupted`); - } else if (options.onError) { - options.onError(err, source); - } else { - throw err; + // handle common socket errors + if (!options.allErrors) { + const error = String(err); + if (err && err.code === 'ECONNRESET') { + return console.log(`${source} Connection interrupted`); + } else if (error.includes('peer did not return a certificate')) { + return console.log('Connection dropped - Client certificate required but not presented'); + } else if (error.includes('inappropriate fallback') || + error.includes('version too low') || + error.includes('no shared cipher')) { + return console.log('Connection dropped - Client used insecure cipher'); + } else if (error.includes('unknown protocol')) { + return console.log('Connection dropped - Client used unknown protocol'); + } + } + if (options.onError) { + return options.onError(err, source); } + throw err; } // create proxy protocol processing server From 1e1fc1b67fae2854b7f3f055c9d51d42630a1e98 Mon Sep 17 00:00:00 2001 From: Harrison Powers Date: Mon, 25 Mar 2019 11:27:59 -0400 Subject: [PATCH 3/5] More log --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 3c86b97..2656104 100644 --- a/index.js +++ b/index.js @@ -18,13 +18,13 @@ const createServer = (server, options) => { if (err && err.code === 'ECONNRESET') { return console.log(`${source} Connection interrupted`); } else if (error.includes('peer did not return a certificate')) { - return console.log('Connection dropped - Client certificate required but not presented'); + return console.log(`${source} Connection dropped - Client certificate required but not presented`); } else if (error.includes('inappropriate fallback') || error.includes('version too low') || error.includes('no shared cipher')) { - return console.log('Connection dropped - Client used insecure cipher'); + return console.log(`${source} Connection dropped - Client used insecure cipher`); } else if (error.includes('unknown protocol')) { - return console.log('Connection dropped - Client used unknown protocol'); + return console.log(`${source} Connection dropped - Client used unknown protocol`); } } if (options.onError) { From dcc2cdc6665489553408205729df9c6bbd99a5cc Mon Sep 17 00:00:00 2001 From: Harrison Powers Date: Mon, 25 Mar 2019 11:58:02 -0400 Subject: [PATCH 4/5] Handle common socket errors pt 2 --- README.md | 1 + index.js | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 02cb57c..c94fc64 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ All available options: ```js require('proxyproto').createServer(server, { setNoDelay: true, // diable nagle algorithm + handleCommonErrors: false, // handle common socket errors (default: true) onError: err => log.error(err) // error handler for servers and sockets }); ``` diff --git a/index.js b/index.js index 2656104..9565923 100644 --- a/index.js +++ b/index.js @@ -10,10 +10,13 @@ const createServer = (server, options) => { throw new Error('Missing server argument - http.createServer(), https, net, tls, etc'); } options = options || {}; + if (!options.hasOwnProperty('handleCommonErrors')) { + options.handleCommonErrors = true; + } function onError(err, source) { // handle common socket errors - if (!options.allErrors) { + if (options.handleCommonErrors) { const error = String(err); if (err && err.code === 'ECONNRESET') { return console.log(`${source} Connection interrupted`); From 6af3b6d59b787e748fcbf62a06f322d82ffcef8b Mon Sep 17 00:00:00 2001 From: Harrison Powers Date: Mon, 25 Mar 2019 12:00:55 -0400 Subject: [PATCH 5/5] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3d5b3ad..f8001be 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "proxyproto", - "version": "1.0.0", + "version": "1.0.1", "description": "Pre-process PROXY protocol headers from node tcp connections", "main": "index.js", "scripts": {