1
1
import { createConnection , Socket } from 'net'
2
+ import { NatMap } from '../../cluster/ClusterOptions' ;
2
3
import { CONNECTION_CLOSED_ERROR_MSG , packObject , sample } from '../../utils'
3
4
import { connect as createTLSConnection , TLSSocket , SecureContextOptions } from 'tls'
4
5
import { ITcpConnectionOptions , isIIpcConnectionOptions } from '../StandaloneConnector'
@@ -30,6 +31,7 @@ interface ISentinelConnectionOptions extends ITcpConnectionOptions {
30
31
connectTimeout ?: number
31
32
enableTLSForSentinelMode ?: boolean
32
33
sentinelTLS ?: SecureContextOptions
34
+ natMap : NatMap
33
35
updateSentinels ?: boolean
34
36
}
35
37
@@ -152,7 +154,7 @@ export default class SentinelConnector extends AbstractConnector {
152
154
result . map < IAddressFromResponse > ( packObject as ( value : any ) => IAddressFromResponse ) . forEach ( sentinel => {
153
155
const flags = sentinel . flags ? sentinel . flags . split ( ',' ) : [ ]
154
156
if ( flags . indexOf ( 'disconnected' ) === - 1 && sentinel . ip && sentinel . port ) {
155
- const endpoint = addressResponseToAddress ( sentinel )
157
+ const endpoint = this . sentinelNatResolve ( addressResponseToAddress ( sentinel ) )
156
158
if ( this . sentinelIterator . add ( endpoint ) ) {
157
159
debug ( 'adding sentinel %s:%s' , endpoint . host , endpoint . port )
158
160
}
@@ -174,7 +176,10 @@ export default class SentinelConnector extends AbstractConnector {
174
176
if ( err ) {
175
177
return callback ( err )
176
178
}
177
- callback ( null , Array . isArray ( result ) ? { host : result [ 0 ] , port : Number ( result [ 1 ] ) } : null )
179
+
180
+ callback ( null , this . sentinelNatResolve (
181
+ Array . isArray ( result ) ? { host : result [ 0 ] , port : Number ( result [ 1 ] ) } : null
182
+ ) )
178
183
} )
179
184
} )
180
185
}
@@ -194,10 +199,19 @@ export default class SentinelConnector extends AbstractConnector {
194
199
slave . flags && ! slave . flags . match ( / ( d i s c o n n e c t e d | s _ d o w n | o _ d o w n ) / )
195
200
) )
196
201
197
- callback ( null , selectPreferredSentinel ( availableSlaves , this . options . preferredSlaves ) )
202
+ callback ( null , this . sentinelNatResolve (
203
+ selectPreferredSentinel ( availableSlaves , this . options . preferredSlaves )
204
+ ) )
198
205
} )
199
206
}
200
207
208
+ sentinelNatResolve ( item : ISentinelAddress ) {
209
+ if ( ! item || ! this . options . natMap )
210
+ return item ;
211
+
212
+ return this . options . natMap [ `${ item . host } :${ item . port } ` ] || item
213
+ }
214
+
201
215
private resolve ( endpoint , callback : NodeCallback < ITcpConnectionOptions > ) : void {
202
216
if ( typeof Redis === 'undefined' ) {
203
217
Redis = require ( '../../redis' )
0 commit comments