From dbcc6ce78b27885dc871c0ddbe04684422576352 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Wed, 12 Jun 2019 21:18:08 +0300 Subject: [PATCH] Add support for specifying the listening address of the Daemon --- libp2p/daemon/daemonapi.nim | 21 +++++++++++++++++++++ libp2p/multiaddress.nim | 4 ++++ 2 files changed, 25 insertions(+) diff --git a/libp2p/daemon/daemonapi.nim b/libp2p/daemon/daemonapi.nim index 38886d4..074c191 100644 --- a/libp2p/daemon/daemonapi.nim +++ b/libp2p/daemon/daemonapi.nim @@ -556,6 +556,8 @@ proc listPeers*(api: DaemonAPI): Future[seq[PeerInfo]] {.async.} proc newDaemonApi*(flags: set[P2PDaemonFlags] = {}, bootstrapNodes: seq[string] = @[], id: string = "", + hostAddresses: seq[MultiAddress] = @[], + announcedAddresses: seq[MultiAddress] = @[], daemon = DefaultDaemonFile, sockpath = "", patternSock = DefaultUnixSocketPattern, @@ -575,6 +577,13 @@ proc newDaemonApi*(flags: set[P2PDaemonFlags] = {}, ## ``id`` - path to file with identification information (default: "" which ## means - generate new random identity). ## + ## ``hostAddresses`` - list of multiaddrs the host should listen on. + ## (default: @[], the daemon will pick a listening port at random). + ## + ## ``announcedAddresses`` - list of multiaddrs the host should announce to + ## the network (default: @[], the daemon will announce its own listening + ## address). + ## ## ``daemon`` - name of ``go-libp2p-daemon`` executable (default: "p2pd"). ## ## ``sockpath`` - default control socket MultiAddress @@ -654,6 +663,18 @@ proc newDaemonApi*(flags: set[P2PDaemonFlags] = {}, args.add("-bootstrapPeers=" & bootstrapNodes.join(",")) if len(id) != 0: args.add("-id=" & id) + if len(hostAddresses) > 0: + var opt = "-hostAddrs=" + for i, address in hostAddresses: + if i > 0: opt.add "," + opt.add $address + args.add(opt) + if len(announcedAddresses) > 0: + var opt = "-announceAddrs=" + for i, address in announcedAddresses: + if i > 0: opt.add "," + opt.add $address + args.add(opt) args.add("-listen=" & $api.address) # We are trying to get absolute daemon path. diff --git a/libp2p/multiaddress.nim b/libp2p/multiaddress.nim index 5960a40..4aa2c70 100644 --- a/libp2p/multiaddress.nim +++ b/libp2p/multiaddress.nim @@ -727,6 +727,10 @@ proc init*(mtype: typedesc[MultiAddress]): MultiAddress = ## Initialize empty MultiAddress. result.data = initVBuffer() +proc tcpEndPoint*(address: IpAddress, port: Port): MultiAddress = + # TODO: this can be more efficient + MultiAddress.init("/ip4/" & $address & "/tcp/" & $port) + proc isEmpty*(ma: MultiAddress): bool = ## Returns ``true``, if MultiAddress ``ma`` is empty or non initialized. result = len(ma.data) == 0