6 changed files with 53 additions and 42 deletions
@ -0,0 +1,7 @@ |
|||
import fs from 'fs' |
|||
import { promisify } from 'util' |
|||
import untildify from 'untildify' |
|||
|
|||
const fsReadFile = promisify(fs.readFile) |
|||
|
|||
export const fileExists = async path => fsReadFile(untildify(path)) |
@ -0,0 +1,41 @@ |
|||
import dns from 'dns' |
|||
import { promisify } from 'util' |
|||
import isFQDN from 'validator/lib/isFQDN' |
|||
import isIP from 'validator/lib/isIP' |
|||
import isPort from 'validator/lib/isPort' |
|||
|
|||
const dnsLookup = promisify(dns.lookup) |
|||
|
|||
/** |
|||
* Helper function to check a hostname in the format hostname:port is valid for passing to node-grpc. |
|||
* @param {string} host A hostname + optional port in the format [hostname]:[port?] |
|||
* @returns {Promise<Boolean>} |
|||
*/ |
|||
export const validateHost = async host => { |
|||
var splits = host.split(':') |
|||
const lndHost = splits[0] |
|||
const lndPort = splits[1] |
|||
|
|||
// If the hostname starts with a number, ensure that it is a valid IP address.
|
|||
if (!isFQDN(lndHost, { require_tld: false }) && !isIP(lndHost)) { |
|||
const error = new Error(`${lndHost} is not a valid IP address or hostname`) |
|||
error.code = 'LND_GRPC_HOST_ERROR' |
|||
return Promise.reject(error) |
|||
} |
|||
|
|||
// If the host includes a port, ensure that it is a valid.
|
|||
if (lndPort && !isPort(lndPort)) { |
|||
const error = new Error(`${lndPort} is not a valid port`) |
|||
error.code = 'LND_GRPC_HOST_ERROR' |
|||
return Promise.reject(error) |
|||
} |
|||
|
|||
// Do a DNS lookup to ensure that the host is reachable.
|
|||
return dnsLookup(lndHost) |
|||
.then(() => true) |
|||
.catch(e => { |
|||
const error = new Error(`${lndHost} is not accessible: ${e.message}`) |
|||
error.code = 'LND_GRPC_HOST_ERROR' |
|||
return Promise.reject(error) |
|||
}) |
|||
} |
Loading…
Reference in new issue