@@ -18,6 +18,7 @@ module.exports = (config, http) => {
18
18
const log = config . log
19
19
const io = new SocketIO ( http . listener )
20
20
const proto = new util . Protocol ( log )
21
+ const getConfig = ( ) => config
21
22
22
23
proto . addRequest ( 'ss-join' , [ 'multiaddr' , 'string' , 'function' ] , join )
23
24
proto . addRequest ( 'ss-leave' , [ 'multiaddr' ] , leave )
@@ -31,9 +32,12 @@ module.exports = (config, http) => {
31
32
const nonces = { }
32
33
33
34
const peersMetric = config . metrics ? new client . Gauge ( { name : 'rendezvous_peers' , help : 'peers online now' } ) : fake . gauge
34
- const dialsSucessTotal = config . metrics ? new client . Counter ( { name : 'rendezvous_dials_total_success' , help : 'sucessfully completed dials since server started' } ) : fake . counter
35
+ const dialsSuccessTotal = config . metrics ? new client . Counter ( { name : 'rendezvous_dials_total_success' , help : 'sucessfully completed dials since server started' } ) : fake . counter
35
36
const dialsFailureTotal = config . metrics ? new client . Counter ( { name : 'rendezvous_dials_total_failure' , help : 'failed dials since server started' } ) : fake . counter
36
37
const dialsTotal = config . metrics ? new client . Counter ( { name : 'rendezvous_dials_total' , help : 'all dials since server started' } ) : fake . counter
38
+ const joinsSuccessTotal = config . metrics ? new client . Counter ( { name : 'rendezvous_joins_total_success' , help : 'sucessfully completed joins since server started' } ) : fake . counter
39
+ const joinsFailureTotal = config . metrics ? new client . Counter ( { name : 'rendezvous_joins_total_failure' , help : 'failed joins since server started' } ) : fake . counter
40
+ const joinsTotal = config . metrics ? new client . Counter ( { name : 'rendezvous_joins_total' , help : 'all joins since server started' } ) : fake . counter
37
41
38
42
const getPeers = ( ) => this . _peers
39
43
const refreshMetrics = ( ) => peersMetric . set ( Object . keys ( getPeers ( ) ) . length )
@@ -63,12 +67,16 @@ module.exports = (config, http) => {
63
67
function join ( socket , multiaddr , pub , cb ) {
64
68
const log = config . log . bind ( config . log , '[' + socket . id + ']' )
65
69
66
- if ( config . strictMultiaddr && ! util . validateMa ( multiaddr ) ) {
70
+ if ( getConfig ( ) . strictMultiaddr && ! util . validateMa ( multiaddr ) ) {
71
+ joinsTotal . inc ( )
72
+ joinsFailureTotal . inc ( )
67
73
return cb ( 'Invalid multiaddr' )
68
74
}
69
75
70
- if ( config . cryptoChallenge ) {
76
+ if ( getConfig ( ) . cryptoChallenge ) {
71
77
if ( ! pub . length ) {
78
+ joinsTotal . inc ( )
79
+ joinsFailureTotal . inc ( )
72
80
return cb ( 'Crypto Challenge required but no Id provided' )
73
81
}
74
82
@@ -80,18 +88,23 @@ module.exports = (config, http) => {
80
88
log ( 'response cryptoChallenge' , multiaddr )
81
89
82
90
nonces [ socket . id ] [ multiaddr ] . key . verify ( nonces [ socket . id ] [ multiaddr ] . nonce , Buffer . from ( pub , 'hex' ) , ( err , ok ) => {
83
- if ( err ) { return cb ( 'Crypto error' ) } // the errors NEED to be strings otherwise JSON.stringify() turns them into {}
91
+ if ( err || ! ok ) {
92
+ joinsTotal . inc ( )
93
+ joinsFailureTotal . inc ( )
94
+ }
95
+ if ( err ) { return cb ( 'Crypto error' ) } // the errors NEED to be a string otherwise JSON.stringify() turns them into {}
84
96
if ( ! ok ) { return cb ( 'Signature Invalid' ) }
85
97
86
98
joinFinalize ( socket , multiaddr , cb )
87
99
} )
88
100
} else {
101
+ joinsTotal . inc ( )
89
102
const addr = multiaddr . split ( 'ipfs/' ) . pop ( )
90
103
91
104
log ( 'do cryptoChallenge' , multiaddr , addr )
92
105
93
106
util . getIdAndValidate ( pub , addr , ( err , key ) => {
94
- if ( err ) return cb ( err )
107
+ if ( err ) { joinsFailureTotal . inc ( ) ; return cb ( err ) }
95
108
const nonce = uuid ( ) + uuid ( )
96
109
97
110
socket . once ( 'disconnect' , ( ) => {
@@ -102,19 +115,23 @@ module.exports = (config, http) => {
102
115
cb ( null , nonce )
103
116
} )
104
117
}
105
- } else joinFinalize ( socket , multiaddr , cb )
118
+ } else {
119
+ joinsTotal . inc ( )
120
+ joinFinalize ( socket , multiaddr , cb )
121
+ }
106
122
}
107
123
108
124
function joinFinalize ( socket , multiaddr , cb ) {
109
- const log = config . log . bind ( config . log , '[' + socket . id + ']' )
125
+ const log = getConfig ( ) . log . bind ( getConfig ( ) . log , '[' + socket . id + ']' )
110
126
getPeers ( ) [ multiaddr ] = socket
127
+ joinsSuccessTotal . inc ( )
111
128
refreshMetrics ( )
112
129
socket . addrs . push ( multiaddr )
113
130
log ( 'registered as' , multiaddr )
114
131
115
132
// discovery
116
133
117
- let refreshInterval = setInterval ( sendPeers , config . refreshPeerListIntervalMS )
134
+ let refreshInterval = setInterval ( sendPeers , getConfig ( ) . refreshPeerListIntervalMS )
118
135
119
136
socket . once ( 'ss-leave' , function handleLeave ( ma ) {
120
137
if ( ma === multiaddr ) {
@@ -166,7 +183,7 @@ module.exports = (config, http) => {
166
183
}
167
184
168
185
function dial ( socket , from , to , dialId , cb ) {
169
- const log = config . log . bind ( config . log , '[' + dialId + ']' )
186
+ const log = getConfig ( ) . log . bind ( getConfig ( ) . log , '[' + dialId + ']' )
170
187
const s = socket . addrs . filter ( ( a ) => a === from ) [ 0 ]
171
188
172
189
dialsTotal . inc ( )
@@ -192,7 +209,7 @@ module.exports = (config, http) => {
192
209
return cb ( err )
193
210
}
194
211
195
- dialsSucessTotal . inc ( )
212
+ dialsSuccessTotal . inc ( )
196
213
peer . createProxy ( dialId + '.listener' , socket )
197
214
cb ( )
198
215
} )
0 commit comments