haraka-net-utils
Advanced tools
Comparing version 1.4.1 to 1.5.0
#### N.N.N - YYYY-MM-DD | ||
### [1.5.0] - 2022-12-20 | ||
- feat: add async support for get_public_ip | ||
- dep: replace vs-stun with stun | ||
- doc: use async/await syntax in examples (#74) | ||
### [1.4.1] - 2022-07-22 | ||
@@ -181,1 +189,2 @@ | ||
[1.4.1]: https://github.com/haraka/haraka-net-utils/releases/tag/1.4.1 | ||
[1.5.0]: https://github.com/haraka/haraka-net-utils/releases/tag/1.5.0 |
71
index.js
@@ -242,3 +242,41 @@ 'use strict'; | ||
exports.get_public_ip = function (cb) { | ||
exports.get_public_ip_async = async function () { | ||
if (this.public_ip !== undefined) return this.public_ip; // cache | ||
// manual config override, for the cases where we can't figure it out | ||
const smtpIni = exports.config.get('smtp.ini').main; | ||
if (smtpIni.public_ip) { | ||
this.public_ip = smtpIni.public_ip; | ||
return this.public_ip; | ||
} | ||
// Initialise cache value to null to prevent running | ||
// should we hit a timeout or the module isn't installed. | ||
this.public_ip = null; | ||
try { | ||
this.stun = require('stun'); | ||
} | ||
catch (e) { | ||
e.install = 'Please install stun: "npm install -g stun"'; | ||
console.error(`${e.msg}\n${e.install}`); | ||
return | ||
} | ||
const timeout = 10; | ||
const timer = setTimeout(() => { | ||
return new Error('STUN timeout') | ||
}, timeout * 1000); | ||
// Connect to STUN Server | ||
const res = await this.stun.request(get_stun_server()) | ||
this.public_ip = res.getXorAddress().address | ||
clearTimeout(timer) | ||
return this.public_ip | ||
} | ||
exports.get_public_ip = async function (cb) { | ||
if (!cb) return exports.get_public_ip_async() | ||
const nu = this; | ||
@@ -259,6 +297,6 @@ if (nu.public_ip !== undefined) return cb(null, nu.public_ip); // cache | ||
try { | ||
nu.stun = require('vs-stun'); | ||
nu.stun = require('stun'); | ||
} | ||
catch (e) { | ||
e.install = 'Please install stun: "npm install -g vs-stun"'; | ||
e.install = 'Please install stun: "npm install -g stun"'; | ||
console.error(`${e.msg}\n${e.install}`); | ||
@@ -274,19 +312,8 @@ return cb(e); | ||
// Connect to STUN Server | ||
nu.stun.connect({ host: get_stun_server(), port: 19302 }, (error, socket) => { | ||
nu.stun.request(get_stun_server(), (error, res) => { | ||
if (timer) clearTimeout(timer); | ||
if (error) return cb(error); | ||
socket.close(); | ||
/* sample socket.stun response | ||
* | ||
* { local: { host: '127.0.0.30', port: 26163 }, | ||
* public: { host: '50.115.0.94', port: 57345, family: 'IPv4' }, | ||
* type: 'Full Cone NAT' | ||
* } | ||
*/ | ||
if (!socket.stun.public) return cb(new Error('invalid STUN result')); | ||
nu.public_ip = socket.stun.public.host; | ||
cb(null, socket.stun.public.host); | ||
nu.public_ip = res.getXorAddress().address | ||
cb(null, nu.public_ip); | ||
}) | ||
@@ -298,7 +325,7 @@ } | ||
const servers = [ | ||
'stun.l.google.com', | ||
'stun1.l.google.com', | ||
'stun2.l.google.com', | ||
'stun3.l.google.com', | ||
'stun4.l.google.com', | ||
'stun.l.google.com:19302', | ||
'stun1.l.google.com:19302', | ||
'stun2.l.google.com:19302', | ||
'stun3.l.google.com:19302', | ||
'stun4.l.google.com:19302', | ||
]; | ||
@@ -305,0 +332,0 @@ return servers[Math.floor(Math.random()*servers.length)]; |
{ | ||
"name": "haraka-net-utils", | ||
"version": "1.4.1", | ||
"version": "1.5.0", | ||
"description": "haraka network utilities", | ||
@@ -45,4 +45,4 @@ "main": "index.js", | ||
"optionalDependencies": { | ||
"vs-stun": "~0.0.7" | ||
"stun": "2.1.0" | ||
} | ||
} |
@@ -242,3 +242,3 @@ | ||
try { | ||
require('vs-stun'); | ||
require('stun'); | ||
} | ||
@@ -285,2 +285,44 @@ catch (e) { | ||
describe('get_public_ip_async', function () { | ||
beforeEach(() => { | ||
this.net_utils = require('../index'); | ||
this.net_utils.config = this.net_utils.config.module_config(path.resolve('test')); | ||
}) | ||
function has_stun () { | ||
try { | ||
require('stun'); | ||
} | ||
catch (e) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
it('cached', async () => { | ||
this.net_utils.public_ip='1.1.1.1'; | ||
const ip = await this.net_utils.get_public_ip() | ||
assert.equal('1.1.1.1', ip); | ||
}) | ||
it('normal', async () => { | ||
this.net_utils.public_ip=undefined; | ||
if (!has_stun()) { | ||
console.log("stun skipped"); | ||
return | ||
} | ||
try { | ||
const ip = await this.net_utils.get_public_ip() | ||
console.log(`stun success: ${ip}`); | ||
assert.ok(ip, ip); | ||
} | ||
catch (e) { | ||
console.error(e); | ||
} | ||
}) | ||
}) | ||
describe('octets_in_string', function () { | ||
@@ -1017,2 +1059,3 @@ it('c-24-18-98-14.hsd1.wa.comcast.net', function (done) { | ||
it(`get_ips_by_host, ${t}`, function (done) { | ||
this.timeout(4000) | ||
net_utils.get_ips_by_host(t, function (err, res) { | ||
@@ -1019,0 +1062,0 @@ if (err && err.length) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
82950
1540