mirror of
https://github.com/tsightler/ring-mqtt.git
synced 2025-10-17 06:10:41 +08:00
Release 4.8.1
This commit is contained in:
20
lib/utils.js
20
lib/utils.js
@@ -1,4 +1,7 @@
|
||||
const fs = require('fs')
|
||||
const dns = require('dns')
|
||||
const os = require('os')
|
||||
const { promisify } = require('util')
|
||||
|
||||
class Utils
|
||||
{
|
||||
@@ -30,19 +33,24 @@ class Utils
|
||||
}
|
||||
|
||||
async getHostFqdn() {
|
||||
const dns = require('dns')
|
||||
const os = require('os')
|
||||
const { promisify } = require('util')
|
||||
const pLookup = promisify(dns.lookup)
|
||||
const pLookupService = promisify(dns.lookupService)
|
||||
|
||||
try {
|
||||
return (await pLookupService((await pLookup(os.hostname())).address, 0)).hostname
|
||||
return await pLookupService(await this.getHostIp(), 0).hostname
|
||||
} catch {
|
||||
console.log('Failed to resolve FQDN, using os.hostname() instead')
|
||||
return os.hostname()
|
||||
}
|
||||
}
|
||||
|
||||
async getHostIp() {
|
||||
const pLookup = promisify(dns.lookup)
|
||||
try {
|
||||
return await pLookup(os.hostname()).address
|
||||
} catch {
|
||||
console.log('Failed to resolve hostname IP address, returning localhost instead')
|
||||
return 'localhost'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new Utils()
|
||||
|
Reference in New Issue
Block a user