diff --git a/openapi.yaml b/openapi.yaml index 150986d8..041127ee 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -240,7 +240,9 @@ paths: description: | If supplied, it will be used to dial the peer. The address has to target the listening address of the peer, - which is specified with the `--listen-addrs` CLI flag. + which is /ip4//tcp/, where `listen-port` is + specified with the `--listen-port` CLI flag and `listen-ip` with the + `--listen-ip` CLI flag. responses: "200": diff --git a/storage/conf.nim b/storage/conf.nim index 50d191cf..db9dbc27 100644 --- a/storage/conf.nim +++ b/storage/conf.nim @@ -135,19 +135,29 @@ type name: "data-dir" .}: OutDir - listenAddrs* {. - desc: "Multi Addresses to listen on", - defaultValue: - @[MultiAddress.init("/ip4/0.0.0.0/tcp/0").expect("Should init multiaddress")], - defaultValueDesc: "/ip4/0.0.0.0/tcp/0", + listenIp* {. + desc: + "IP address to listen on for remote peer connections, can be ipv4 or ipv6", + defaultValue: "0.0.0.0".parseIpAddress, + defaultValueDesc: "Listens on all addresses.", abbr: "i", - name: "listen-addrs" - .}: seq[MultiAddress] + name: "listen-ip" + .}: IpAddress + + listenPort* {. + desc: + "TCP port to listen on for remote peer connections. Selects a random port if none is specified.", + defaultValue: 0, + defaultValueDesc: "Listens on a random free port.", + abbr: "l", + name: "listen-port" + .}: Port nat* {. desc: "Specify method to use for determining public address. " & - "Must be one of: any, none, upnp, pmp, extip:", + "Must be one of: any, none, upnp, pmp, extip:. " & + "If connecting to peers on a local network only, use 'none'.", defaultValue: defaultNatConfig(), defaultValueDesc: "any", name: "nat" diff --git a/storage/rest/api.nim b/storage/rest/api.nim index 0c4a9c06..dbc308e1 100644 --- a/storage/rest/api.nim +++ b/storage/rest/api.nim @@ -440,7 +440,9 @@ proc initNodeApi(node: StorageNodeRef, conf: StorageConf, router: var RestRouter ## to invoke peer discovery, if it succeeds ## the returned addresses will be used to dial ## - ## `addrs` the listening addresses of the peers to dial, eg the one specified with `--listen-addrs` + ## `addrs` the listening addresses of the peers to dial, which is + ## /ip4/0.0.0.0/tcp/, where port is specified with the + ## `--listen-port` CLI flag. ## var headers = buildCorsHeaders("GET", allowedOrigin) @@ -475,7 +477,6 @@ proc initDebugApi(node: StorageNodeRef, conf: StorageConf, router: var RestRoute try: let table = RestRoutingTable.init(node.discovery.protocol.routingTable) - let json = %*{ "id": $node.switch.peerInfo.peerId, "addrs": node.switch.peerInfo.addrs.mapIt($it), diff --git a/storage/storage.nim b/storage/storage.nim index e85b3959..16a2cb20 100644 --- a/storage/storage.nim +++ b/storage/storage.nim @@ -139,10 +139,12 @@ proc new*( T: type StorageServer, config: StorageConf, privateKey: StoragePrivateKey ): StorageServer = ## create StorageServer including setting up datastore, repostore, etc + let listenMultiAddr = getMultiAddrWithIpAndTcpPort(config.listenIp, config.listenPort) + let switch = SwitchBuilder .new() .withPrivateKey(privateKey) - .withAddresses(config.listenAddrs) + .withAddresses(@[listenMultiAddr]) .withRng(random.Rng.instance()) .withNoise() .withMplex(5.minutes, 5.minutes) @@ -190,7 +192,7 @@ proc new*( discovery = Discovery.new( switch.peerInfo.privateKey, - announceAddrs = config.listenAddrs, + announceAddrs = @[listenMultiAddr], bindPort = config.discoveryPort, bootstrapNodes = config.bootstrapNodes, store = discoveryStore, diff --git a/storage/utils/addrutils.nim b/storage/utils/addrutils.nim index 1b7a556a..31570e06 100644 --- a/storage/utils/addrutils.nim +++ b/storage/utils/addrutils.nim @@ -53,6 +53,21 @@ proc getMultiAddrWithIPAndUDPPort*(ip: IpAddress, port: Port): MultiAddress = let ipFamily = if ip.family == IpAddressFamily.IPv4: "/ip4/" else: "/ip6/" return MultiAddress.init(ipFamily & $ip & "/udp/" & $port).expect("valid multiaddr") +proc getMultiAddrWithIpAndTcpPort*(ip: IpAddress, port: Port): MultiAddress = + ## Creates a MultiAddress with the specified IP address and TCP port + ## + ## Parameters: + ## - ip: A valid IP address (IPv4 or IPv6) + ## - port: The TCP port number + ## + ## Returns: + ## A MultiAddress in the format "/ip4/
/tcp/" or "/ip6/
/tcp/" + + let ipFamily = if ip.family == IpAddressFamily.IPv4: "/ip4/" else: "/ip6/" + return MultiAddress.init(ipFamily & $ip & "/tcp/" & $port).expect( + "Failed to construct multiaddress with IP and TCP port" + ) + proc getAddressAndPort*( ma: MultiAddress ): tuple[ip: Option[IpAddress], port: Option[Port]] = diff --git a/tests/integration/multinodes.nim b/tests/integration/multinodes.nim index f6f8135f..5ee7d202 100644 --- a/tests/integration/multinodes.nim +++ b/tests/integration/multinodes.nim @@ -135,7 +135,6 @@ template multinodesuite*(suiteName: string, body: untyped) = config.addCliOption("--data-dir", datadir) config.addCliOption("--nat", "none") - config.addCliOption("--listen-addrs", "/ip4/127.0.0.1/tcp/0") except StorageConfigError as e: raiseMultiNodeSuiteError "invalid cli option, error: " & e.msg