@@ -239,7 +239,45 @@ exports.same_ipv4_network = function (ip, ipList) {
239
239
return false ;
240
240
}
241
241
242
- exports . get_public_ip = function ( cb ) {
242
+ exports . get_public_ip_async = async function ( ) {
243
+
244
+ if ( this . public_ip !== undefined ) return this . public_ip ; // cache
245
+
246
+ // manual config override, for the cases where we can't figure it out
247
+ const smtpIni = exports . config . get ( 'smtp.ini' ) . main ;
248
+ if ( smtpIni . public_ip ) {
249
+ this . public_ip = smtpIni . public_ip ;
250
+ return this . public_ip ;
251
+ }
252
+
253
+ // Initialise cache value to null to prevent running
254
+ // should we hit a timeout or the module isn't installed.
255
+ this . public_ip = null ;
256
+
257
+ try {
258
+ this . stun = require ( 'stun' ) ;
259
+ }
260
+ catch ( e ) {
261
+ e . install = 'Please install stun: "npm install -g stun"' ;
262
+ console . error ( `${ e . msg } \n${ e . install } ` ) ;
263
+ return
264
+ }
265
+
266
+ const timeout = 10 ;
267
+ const timer = setTimeout ( ( ) => {
268
+ return new Error ( 'STUN timeout' )
269
+ } , timeout * 1000 ) ;
270
+
271
+ // Connect to STUN Server
272
+ const res = await this . stun . request ( get_stun_server ( ) )
273
+ this . public_ip = res . getXorAddress ( ) . address
274
+ clearTimeout ( timer )
275
+ return this . public_ip
276
+ }
277
+
278
+ exports . get_public_ip = async function ( cb ) {
279
+ if ( ! cb ) return exports . get_public_ip_async ( )
280
+
243
281
const nu = this ;
244
282
if ( nu . public_ip !== undefined ) return cb ( null , nu . public_ip ) ; // cache
245
283
@@ -255,10 +293,10 @@ exports.get_public_ip = function (cb) {
255
293
nu . public_ip = null ;
256
294
257
295
try {
258
- nu . stun = require ( 'vs- stun' ) ;
296
+ nu . stun = require ( 'stun' ) ;
259
297
}
260
298
catch ( e ) {
261
- e . install = 'Please install stun: "npm install -g vs- stun"' ;
299
+ e . install = 'Please install stun: "npm install -g stun"' ;
262
300
console . error ( `${ e . msg } \n${ e . install } ` ) ;
263
301
return cb ( e ) ;
264
302
}
@@ -269,34 +307,23 @@ exports.get_public_ip = function (cb) {
269
307
} , timeout * 1000 ) ;
270
308
271
309
// Connect to STUN Server
272
- nu . stun . connect ( { host : get_stun_server ( ) , port : 19302 } , ( error , socket ) => {
310
+ nu . stun . request ( get_stun_server ( ) , ( error , res ) => {
273
311
if ( timer ) clearTimeout ( timer ) ;
274
312
if ( error ) return cb ( error ) ;
275
313
276
- socket . close ( ) ;
277
-
278
- /* sample socket.stun response
279
- *
280
- * { local: { host: '127.0.0.30', port: 26163 },
281
- * public: { host: '50.115.0.94', port: 57345, family: 'IPv4' },
282
- * type: 'Full Cone NAT'
283
- * }
284
- */
285
- if ( ! socket . stun . public ) return cb ( new Error ( 'invalid STUN result' ) ) ;
286
-
287
- nu . public_ip = socket . stun . public . host ;
288
- cb ( null , socket . stun . public . host ) ;
314
+ nu . public_ip = res . getXorAddress ( ) . address
315
+ cb ( null , nu . public_ip ) ;
289
316
} )
290
317
}
291
318
292
319
function get_stun_server ( ) {
293
320
// STUN servers by Google
294
321
const servers = [
295
- 'stun.l.google.com' ,
296
- 'stun1.l.google.com' ,
297
- 'stun2.l.google.com' ,
298
- 'stun3.l.google.com' ,
299
- 'stun4.l.google.com' ,
322
+ 'stun.l.google.com:19302 ' ,
323
+ 'stun1.l.google.com:19302 ' ,
324
+ 'stun2.l.google.com:19302 ' ,
325
+ 'stun3.l.google.com:19302 ' ,
326
+ 'stun4.l.google.com:19302 ' ,
300
327
] ;
301
328
return servers [ Math . floor ( Math . random ( ) * servers . length ) ] ;
302
329
}
0 commit comments