diff --git a/storage/conf.nim b/storage/conf.nim index 7b71a7f5..68f67eae 100644 --- a/storage/conf.nim +++ b/storage/conf.nim @@ -135,20 +135,29 @@ type name: "data-dir" .}: OutDir + listenIp* {. + desc: + "IP address to listen on for remote peer connections. Announced in the DHT as a multiaddress, eg /ip4//tcp/. IP address can be v4 or v6", + defaultValue: "0.0.0.0".parseIpAddress, + defaultValueDesc: "Listens on all addresses.", + abbr: "i", + name: "listen-ip" + .}: IpAddress + listenPort* {. desc: - "Port to listen on for remote peer connections. Announced in the DHT as /ip4/0.0.0.0/tcp/", + "Port to listen on for remote peer connections. Announced in the DHT as a multiaddress, eg /ip4//tcp/. Port must be in the range 0-65535.", defaultValue: 0, - defaultValueDesc: - "Chooses a random port for the MultiAddress, eg /ip4/0.0.0.0/tcp/0", + defaultValueDesc: "Listens on a random free port.", abbr: "l", name: "listen-port" - .}: int + .}: 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/storage.nim b/storage/storage.nim index 5eabb433..16a2cb20 100644 --- a/storage/storage.nim +++ b/storage/storage.nim @@ -139,14 +139,12 @@ proc new*( T: type StorageServer, config: StorageConf, privateKey: StoragePrivateKey ): StorageServer = ## create StorageServer including setting up datastore, repostore, etc - let listenAddr = MultiAddress.init(DefaultListenAddress & $config.listenPort).expect( - "Default multiaddress and provied port not valid" - ) + let listenMultiAddr = getMultiAddrWithIpAndTcpPort(config.listenIp, config.listenPort) let switch = SwitchBuilder .new() .withPrivateKey(privateKey) - .withAddresses(@[listenAddr]) + .withAddresses(@[listenMultiAddr]) .withRng(random.Rng.instance()) .withNoise() .withMplex(5.minutes, 5.minutes) @@ -194,7 +192,7 @@ proc new*( discovery = Discovery.new( switch.peerInfo.privateKey, - announceAddrs = @[listenAddr], + 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]] =