Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More errors #1

Merged
merged 5 commits into from
Mar 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
```
Expand Down
47 changes: 33 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,30 @@ 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) {
if (err && err.code === 'ECONNRESET') {
console.log('Connection interrupted');
} else if (options.onError) {
options.onError(err);
} else {
throw err;
function onError(err, source) {
// handle common socket errors
if (options.handleCommonErrors) {
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(`${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(`${source} Connection dropped - Client used insecure cipher`);
} else if (error.includes('unknown protocol')) {
return console.log(`${source} Connection dropped - Client used unknown protocol`);
}
}
if (options.onError) {
return options.onError(err, source);
}
throw err;
}

// create proxy protocol processing server
Expand All @@ -31,7 +46,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();
Expand Down Expand Up @@ -65,10 +80,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) {
Expand All @@ -78,14 +93,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;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down