16
16
* See the License for the specific language governing permissions and
17
17
* limitations under the License.
18
18
*/
19
- import net from " net" ;
20
- import tls from " tls" ;
21
- import fs from "fs" ;
22
- import path from " path" ;
23
- import { EOL } from "os" ;
24
- import { NodeBuffer } from " ./buf" ;
25
- import { ENCRYPTION_OFF , isEmptyObjectOrNull } from " ./util" ;
26
- import { newError , SESSION_EXPIRED } from " ./../error" ;
19
+ import net from ' net' ;
20
+ import tls from ' tls' ;
21
+ import fs from 'fs' ;
22
+ import path from ' path' ;
23
+ import { EOL } from 'os' ;
24
+ import { NodeBuffer } from ' ./buf' ;
25
+ import { ENCRYPTION_OFF , isEmptyObjectOrNull } from ' ./util' ;
26
+ import { newError } from ' ./../error' ;
27
27
28
28
let _CONNECTION_IDGEN = 0 ;
29
29
@@ -107,13 +107,13 @@ const TrustStrategy = {
107
107
/**
108
108
* @deprecated Since version 1.0. Will be deleted in a future version. {@link #TRUST_CUSTOM_CA_SIGNED_CERTIFICATES}.
109
109
*/
110
- TRUST_SIGNED_CERTIFICATES : function ( opts , onSuccess , onFailure ) {
110
+ TRUST_SIGNED_CERTIFICATES : function ( config , onSuccess , onFailure ) {
111
111
console . log ( "`TRUST_SIGNED_CERTIFICATES` has been deprecated as option and will be removed in a future version of " +
112
112
"the driver. Please use `TRUST_CUSTOM_CA_SIGNED_CERTIFICATES` instead." ) ;
113
- return TrustStrategy . TRUST_CUSTOM_CA_SIGNED_CERTIFICATES ( opts , onSuccess , onFailure ) ;
113
+ return TrustStrategy . TRUST_CUSTOM_CA_SIGNED_CERTIFICATES ( config , onSuccess , onFailure ) ;
114
114
} ,
115
- TRUST_CUSTOM_CA_SIGNED_CERTIFICATES : function ( opts , onSuccess , onFailure ) {
116
- if ( ! opts . trustedCertificates || opts . trustedCertificates . length == 0 ) {
115
+ TRUST_CUSTOM_CA_SIGNED_CERTIFICATES : function ( config , onSuccess , onFailure ) {
116
+ if ( ! config . trustedCertificates || config . trustedCertificates . length = == 0 ) {
117
117
onFailure ( newError ( "You are using TRUST_CUSTOM_CA_SIGNED_CERTIFICATES as the method " +
118
118
"to verify trust for encrypted connections, but have not configured any " +
119
119
"trustedCertificates. You must specify the path to at least one trusted " +
@@ -124,13 +124,13 @@ const TrustStrategy = {
124
124
}
125
125
126
126
let tlsOpts = {
127
- ca : opts . trustedCertificates . map ( ( f ) => fs . readFileSync ( f ) ) ,
127
+ ca : config . trustedCertificates . map ( ( f ) => fs . readFileSync ( f ) ) ,
128
128
// Because we manually check for this in the connect callback, to give
129
129
// a more helpful error to the user
130
130
rejectUnauthorized : false
131
131
} ;
132
132
133
- let socket = tls . connect ( opts . port , opts . host , tlsOpts , function ( ) {
133
+ let socket = tls . connect ( config . port , config . host , tlsOpts , function ( ) {
134
134
if ( ! socket . authorized ) {
135
135
onFailure ( newError ( "Server certificate is not trusted. If you trust the database you are connecting to, add" +
136
136
" the signing certificate, or the server certificate, to the list of certificates trusted by this driver" +
@@ -145,14 +145,14 @@ const TrustStrategy = {
145
145
socket . on ( 'error' , onFailure ) ;
146
146
return socket ;
147
147
} ,
148
- TRUST_SYSTEM_CA_SIGNED_CERTIFICATES : function ( opts , onSuccess , onFailure ) {
148
+ TRUST_SYSTEM_CA_SIGNED_CERTIFICATES : function ( config , onSuccess , onFailure ) {
149
149
150
150
let tlsOpts = {
151
151
// Because we manually check for this in the connect callback, to give
152
152
// a more helpful error to the user
153
153
rejectUnauthorized : false
154
154
} ;
155
- let socket = tls . connect ( opts . port , opts . host , tlsOpts , function ( ) {
155
+ let socket = tls . connect ( config . port , config . host , tlsOpts , function ( ) {
156
156
if ( ! socket . authorized ) {
157
157
onFailure ( newError ( "Server certificate is not trusted. If you trust the database you are connecting to, use " +
158
158
"TRUST_CUSTOM_CA_SIGNED_CERTIFICATES and add" +
@@ -171,7 +171,7 @@ const TrustStrategy = {
171
171
/**
172
172
* @deprecated in 1.1 in favour of {@link #TRUST_ALL_CERTIFICATES}. Will be deleted in a future version.
173
173
*/
174
- TRUST_ON_FIRST_USE : function ( opts , onSuccess , onFailure ) {
174
+ TRUST_ON_FIRST_USE : function ( config , onSuccess , onFailure ) {
175
175
console . log ( "`TRUST_ON_FIRST_USE` has been deprecated as option and will be removed in a future version of " +
176
176
"the driver. Please use `TRUST_ALL_CERTIFICATES` instead." ) ;
177
177
@@ -180,7 +180,7 @@ const TrustStrategy = {
180
180
rejectUnauthorized : false
181
181
} ;
182
182
183
- let socket = tls . connect ( opts . port , opts . host , tlsOpts , function ( ) {
183
+ let socket = tls . connect ( config . port , config . host , tlsOpts , function ( ) {
184
184
var serverCert = socket . getPeerCertificate ( /*raw=*/ true ) ;
185
185
186
186
if ( ! serverCert . raw ) {
@@ -195,9 +195,9 @@ const TrustStrategy = {
195
195
return ;
196
196
}
197
197
198
- var serverFingerprint = require ( 'crypto' ) . createHash ( 'sha512' ) . update ( serverCert . raw ) . digest ( "hex" ) ;
199
- let knownHostsPath = opts . knownHosts || path . join ( userHome ( ) , ".neo4j" , "known_hosts" ) ;
200
- let serverId = opts . host + ":" + opts . port ;
198
+ const serverFingerprint = require ( 'crypto' ) . createHash ( 'sha512' ) . update ( serverCert . raw ) . digest ( "hex" ) ;
199
+ const knownHostsPath = config . knownHostsPath || path . join ( userHome ( ) , ".neo4j" , "known_hosts" ) ;
200
+ const serverId = config . host + ":" + config . port ;
201
201
202
202
loadFingerprint ( serverId , knownHostsPath , ( knownFingerprint ) => {
203
203
if ( knownFingerprint === serverFingerprint ) {
@@ -228,11 +228,11 @@ const TrustStrategy = {
228
228
return socket ;
229
229
} ,
230
230
231
- TRUST_ALL_CERTIFICATES : function ( opts , onSuccess , onFailure ) {
231
+ TRUST_ALL_CERTIFICATES : function ( config , onSuccess , onFailure ) {
232
232
const tlsOpts = {
233
233
rejectUnauthorized : false
234
234
} ;
235
- const socket = tls . connect ( opts . port , opts . host , tlsOpts , function ( ) {
235
+ const socket = tls . connect ( config . port , config . host , tlsOpts , function ( ) {
236
236
const certificate = socket . getPeerCertificate ( ) ;
237
237
if ( isEmptyObjectOrNull ( certificate ) ) {
238
238
onFailure ( newError ( "Secure connection was successful but server did not return any valid " +
@@ -249,16 +249,23 @@ const TrustStrategy = {
249
249
}
250
250
} ;
251
251
252
- function connect ( opts , onSuccess , onFailure = ( ( ) => null ) ) {
252
+ /**
253
+ * Connect using node socket.
254
+ * @param {ChannelConfig } config - configuration of this channel.
255
+ * @param {function } onSuccess - callback to execute on connection success.
256
+ * @param {function } onFailure - callback to execute on connection failure.
257
+ * @return {* } socket connection.
258
+ */
259
+ function connect ( config , onSuccess , onFailure = ( ( ) => null ) ) {
253
260
//still allow boolean for backwards compatibility
254
- if ( opts . encrypted === false || opts . encrypted === ENCRYPTION_OFF ) {
255
- var conn = net . connect ( opts . port , opts . host , onSuccess ) ;
261
+ if ( config . encrypted === false || config . encrypted === ENCRYPTION_OFF ) {
262
+ var conn = net . connect ( config . port , config . host , onSuccess ) ;
256
263
conn . on ( 'error' , onFailure ) ;
257
264
return conn ;
258
- } else if ( TrustStrategy [ opts . trust ] ) {
259
- return TrustStrategy [ opts . trust ] ( opts , onSuccess , onFailure ) ;
265
+ } else if ( TrustStrategy [ config . trust ] ) {
266
+ return TrustStrategy [ config . trust ] ( config , onSuccess , onFailure ) ;
260
267
} else {
261
- onFailure ( newError ( "Unknown trust strategy: " + opts . trust + ". Please use either " +
268
+ onFailure ( newError ( "Unknown trust strategy: " + config . trust + ". Please use either " +
262
269
"trust:'TRUST_CUSTOM_CA_SIGNED_CERTIFICATES' or trust:'TRUST_ALL_CERTIFICATES' in your driver " +
263
270
"configuration. Alternatively, you can disable encryption by setting " +
264
271
"`encrypted:\"" + ENCRYPTION_OFF + "\"`. There is no mechanism to use encryption without trust verification, " +
@@ -277,11 +284,9 @@ class NodeChannel {
277
284
278
285
/**
279
286
* Create new instance
280
- * @param {Object } opts - Options object
281
- * @param {string } opts.host - The host, including protocol to connect to.
282
- * @param {Integer } opts.port - The port to use.
287
+ * @param {ChannelConfig } config - configuration for this channel.
283
288
*/
284
- constructor ( opts ) {
289
+ constructor ( config ) {
285
290
let self = this ;
286
291
287
292
this . id = _CONNECTION_IDGEN ++ ;
@@ -291,9 +296,10 @@ class NodeChannel {
291
296
this . _error = null ;
292
297
this . _handleConnectionError = this . _handleConnectionError . bind ( this ) ;
293
298
this . _handleConnectionTerminated = this . _handleConnectionTerminated . bind ( this ) ;
299
+ this . _connectionErrorCode = config . connectionErrorCode ;
294
300
295
- this . _encrypted = opts . encrypted ;
296
- this . _conn = connect ( opts , ( ) => {
301
+ this . _encrypted = config . encrypted ;
302
+ this . _conn = connect ( config , ( ) => {
297
303
if ( ! self . _open ) {
298
304
return ;
299
305
}
@@ -318,14 +324,14 @@ class NodeChannel {
318
324
319
325
_handleConnectionError ( err ) {
320
326
let msg = err . message || 'Failed to connect to server' ;
321
- this . _error = newError ( msg , SESSION_EXPIRED ) ;
327
+ this . _error = newError ( msg , this . _connectionErrorCode ) ;
322
328
if ( this . onerror ) {
323
329
this . onerror ( this . _error ) ;
324
330
}
325
331
}
326
332
327
333
_handleConnectionTerminated ( ) {
328
- this . _error = newError ( 'Connection was closed by server' , SESSION_EXPIRED ) ;
334
+ this . _error = newError ( 'Connection was closed by server' , this . _connectionErrorCode ) ;
329
335
if ( this . onerror ) {
330
336
this . onerror ( this . _error ) ;
331
337
}
0 commit comments