mirror of
https://github.com/fluencelabs/js-libp2p-tcp
synced 2025-04-24 22:52:34 +00:00
47 lines
1.1 KiB
JavaScript
47 lines
1.1 KiB
JavaScript
'use strict'
|
|
|
|
const multiaddr = require('multiaddr')
|
|
const os = require('os')
|
|
const { resolve } = require('path')
|
|
const ProtoFamily = { ip4: 'IPv4', ip6: 'IPv6' }
|
|
|
|
function multiaddrToNetConfig (addr) {
|
|
const listenPath = addr.getPath()
|
|
// unix socket listening
|
|
if (listenPath) {
|
|
return resolve(listenPath)
|
|
}
|
|
// tcp listening
|
|
return addr.toOptions()
|
|
}
|
|
|
|
function getMultiaddrs (proto, ip, port) {
|
|
const toMa = ip => multiaddr(`/${proto}/${ip}/tcp/${port}`)
|
|
return (isAnyAddr(ip) ? getNetworkAddrs(ProtoFamily[proto]) : [ip]).map(toMa)
|
|
}
|
|
|
|
function isAnyAddr (ip) {
|
|
return ['0.0.0.0', '::'].includes(ip)
|
|
}
|
|
|
|
/**
|
|
* @private
|
|
* @param {string} family One of ['IPv6', 'IPv4']
|
|
* @returns {string[]} an array of ip address strings
|
|
*/
|
|
function getNetworkAddrs (family) {
|
|
return Object.values(os.networkInterfaces()).reduce((addresses, netAddrs) => {
|
|
netAddrs.forEach(netAddr => {
|
|
// Add the ip of each matching network interface
|
|
if (netAddr.family === family) addresses.push(netAddr.address)
|
|
})
|
|
return addresses
|
|
}, [])
|
|
}
|
|
|
|
module.exports = {
|
|
multiaddrToNetConfig,
|
|
isAnyAddr,
|
|
getMultiaddrs
|
|
}
|