@@ -10,15 +10,30 @@ const createServer = (server, options) => {
10
10
throw new Error ( 'Missing server argument - http.createServer(), https, net, tls, etc' ) ;
11
11
}
12
12
options = options || { } ;
13
+ if ( ! options . hasOwnProperty ( 'handleCommonErrors' ) ) {
14
+ options . handleCommonErrors = true ;
15
+ }
13
16
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
+ }
21
32
}
33
+ if ( options . onError ) {
34
+ return options . onError ( err , source ) ;
35
+ }
36
+ throw err ;
22
37
}
23
38
24
39
// create proxy protocol processing server
@@ -31,7 +46,7 @@ const createServer = (server, options) => {
31
46
if ( options . setNoDelay ) {
32
47
connection . setNoDelay ( true ) ; // disable nagle algorithm
33
48
}
34
- connection . addListener ( 'error' , onError ) ;
49
+ connection . addListener ( 'error' , err => onError ( err , 'proxyproto socket' ) ) ;
35
50
connection . addListener ( 'data' , onData ) ;
36
51
function onData ( buffer ) {
37
52
connection . pause ( ) ;
@@ -65,10 +80,10 @@ const createServer = (server, options) => {
65
80
}
66
81
} ) ;
67
82
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' ) ) ;
72
87
73
88
// if server is tls, prepare child connection
74
89
if ( server . _sharedCreds ) {
@@ -78,14 +93,18 @@ const createServer = (server, options) => {
78
93
get : ( ) => connection . _parent [ property ]
79
94
} ) ;
80
95
} ) ;
81
- connection . addListener ( 'error' , onError ) ;
96
+ connection . addListener ( 'error' , err => onError ( err , 'secure socket' ) ) ;
82
97
connection . setKeepAlive ( true ) ; // prevent idle timeout ECONNRESET
83
98
if ( options . setNoDelay ) {
84
99
connection . setNoDelay ( true ) ; // disable nagle algorithm
85
100
}
86
101
} ) ;
102
+ } else {
103
+ server . on ( 'connection' , connection => {
104
+ connection . addListener ( 'error' , err => onError ( err , 'socket' ) ) ;
105
+ } ) ;
87
106
}
88
-
107
+
89
108
// if server is already listening, use that port
90
109
if ( server . listening ) {
91
110
const port = server . address ( ) . port ;
0 commit comments