mirror of https://gitlab.com/ecentrics/concordia
Ezerous
3 years ago
5 changed files with 67 additions and 26 deletions
@ -1,21 +1,50 @@ |
|||||
import dns from 'dns'; |
import dns from 'dns'; |
||||
import util from 'util'; |
import util from 'util'; |
||||
|
import { Multiaddr } from 'multiaddr'; |
||||
import { rendezvousHost, rendezvousPort } from 'concordia-shared/src/environment/interpolated/rendezvous'; |
import { rendezvousHost, rendezvousPort } from 'concordia-shared/src/environment/interpolated/rendezvous'; |
||||
import { logger } from './logger'; |
import { logger } from './logger'; |
||||
|
|
||||
const dnsLookup = util.promisify(dns.lookup); |
const dnsLookup = util.promisify(dns.lookup); |
||||
|
|
||||
export const getResolvedRendezvousUrl = async () => { |
const isMultiaddr = (input) => { |
||||
if (rendezvousHost.startsWith('/docker/')) { |
try { |
||||
return dnsLookup(rendezvousHost.substring('/docker/'.length), { family: 4 }) |
// eslint-disable-next-line no-new
|
||||
.then((resolvedDomain) => `/ip4/${resolvedDomain.address}`) |
new Multiaddr(input); |
||||
.catch((error) => logger.error(new Error(`DNS lookup of ${rendezvousHost} failed.\n${error}`))); |
return true; |
||||
|
} catch (e) { |
||||
|
logger.error(new Error(`Invalid rendezvous multiaddress ${input}!`)); |
||||
|
return false; |
||||
} |
} |
||||
|
}; |
||||
|
|
||||
|
// eslint-disable-next-line consistent-return
|
||||
|
const getRendezvousMultiaddress = () => { |
||||
|
const host = rendezvousHost.startsWith('/docker/') ? rendezvousHost.substring('/docker/'.length) : rendezvousHost; |
||||
|
|
||||
|
const multiaddress = `${host}/tcp/${rendezvousPort}/wss/p2p-webrtc-star`; |
||||
|
if (isMultiaddr(multiaddress)) return new Multiaddr(multiaddress); |
||||
|
process.exit(1); // Invalid multiaddress - exiting with error
|
||||
|
}; |
||||
|
|
||||
|
export const getResolvedRendezvousMultiaddress = async () => { |
||||
|
const rendezvousMultiaddress = getRendezvousMultiaddress(); |
||||
|
const { address } = rendezvousMultiaddress.nodeAddress(); |
||||
|
|
||||
return Promise.resolve(rendezvousHost); |
// Address is a domain to be resolved to IP
|
||||
|
if (rendezvousMultiaddress.toString().includes('/dns4/')) { |
||||
|
try { |
||||
|
const resolvedDomain = await dnsLookup(address); |
||||
|
return Promise.resolve(new Multiaddr(`/ip4/${resolvedDomain.address}/tcp/${rendezvousPort}/wss/p2p-webrtc-star`)); |
||||
|
} catch (error) { |
||||
|
logger.error(new Error(`DNS lookup of ${address} failed.\n${error}`)); |
||||
|
process.exit(2); |
||||
|
} |
||||
|
} |
||||
|
return Promise.resolve(rendezvousMultiaddress); |
||||
}; |
}; |
||||
|
|
||||
export const getSwarmAddresses = async () => getResolvedRendezvousUrl() |
// TODO: currently this only works for a single rendezvous
|
||||
.then((resolvedRendezvousHost) => [ |
export const getSwarmAddresses = async () => getResolvedRendezvousMultiaddress() |
||||
`${resolvedRendezvousHost}/tcp/${rendezvousPort}/wss/p2p-webrtc-star`, |
.then((resolvedRendezvousMultiaddress) => [ |
||||
]); |
resolvedRendezvousMultiaddress.toString(), |
||||
|
]).catch((error) => logger.error(new Error(`getSwarmAddresses failed.\n${error}`))); |
||||
|
Loading…
Reference in new issue