From f61e928139b9b97c4b7afcb773d222691d2df2f6 Mon Sep 17 00:00:00 2001 From: Erik Arvstedt Date: Fri, 1 Oct 2021 11:51:57 +0200 Subject: [PATCH] services: support 0.0.0.0/:: in `address` options Previously, client services didn't decode these special INADDR_ANY addresses and failed to connect. --- modules/btcpayserver.nix | 8 ++++---- modules/charge-lnd.nix | 5 +++-- modules/clightning.nix | 2 +- modules/electrs.nix | 2 +- modules/joinmarket.nix | 2 +- modules/lightning-loop.nix | 7 +++---- modules/lightning-pool.nix | 5 ++--- modules/liquid.nix | 2 +- modules/lnd-rest-onion-service.nix | 2 +- modules/lnd.nix | 6 +++--- modules/nodeinfo.nix | 3 ++- modules/onion-services.nix | 2 +- pkgs/lib.nix | 13 +++++++++++++ 13 files changed, 36 insertions(+), 23 deletions(-) diff --git a/modules/btcpayserver.nix b/modules/btcpayserver.nix index 786aaf9..e44208f 100644 --- a/modules/btcpayserver.nix +++ b/modules/btcpayserver.nix @@ -143,15 +143,15 @@ in { configFile = builtins.toFile "config" '' network=${bitcoind.network} btcrpcuser=${cfg.bitcoind.rpc.users.btcpayserver.name} - btcrpcurl=http://${bitcoind.rpc.address}:${toString cfg.bitcoind.rpc.port} - btcnodeendpoint=${bitcoind.address}:${toString bitcoind.port} + btcrpcurl=http://${nbLib.addressWithPort bitcoind.rpc.address cfg.bitcoind.rpc.port} + btcnodeendpoint=${nbLib.addressWithPort bitcoind.address bitcoind.port} bind=${cfg.nbxplorer.address} port=${toString cfg.nbxplorer.port} ${optionalString cfg.btcpayserver.lbtc '' chains=btc,lbtc lbtcrpcuser=${liquidd.rpcuser} - lbtcrpcurl=http://${liquidd.rpc.address}:${toString liquidd.rpc.port} - lbtcnodeendpoint=${liquidd.address}:${toString liquidd.port} + lbtcrpcurl=http://${nbLib.addressWithPort liquidd.rpc.address liquidd.rpc.port} + lbtcnodeendpoint=${nbLib.addressWithPort liquidd.address liquidd.port} ''} ''; in { diff --git a/modules/charge-lnd.nix b/modules/charge-lnd.nix index f6579c1..6f564dc 100644 --- a/modules/charge-lnd.nix +++ b/modules/charge-lnd.nix @@ -112,9 +112,10 @@ in ExecStart = '' ${config.nix-bitcoin.pkgs.charge-lnd}/bin/charge-lnd \ --lnddir ${dataDir}/lnddir-proxy \ - --grpc ${lnd.rpcAddress}:${toString lnd.rpcPort} \ + --grpc ${nbLib.addressWithPort lnd.rpcAddress lnd.rpcPort} \ --config ${checkedConfig} \ - ${optionalString (electrs != null) "--electrum-server ${electrs.address}:${toString electrs.port}"} \ + ${optionalString (electrs != null) + "--electrum-server ${nbLib.addressWithPort electrs.address electrs.port}"} \ ${escapeShellArgs cfg.extraFlags} ''; Type = "oneshot"; diff --git a/modules/clightning.nix b/modules/clightning.nix index 070f73d..19d8b31 100644 --- a/modules/clightning.nix +++ b/modules/clightning.nix @@ -84,7 +84,7 @@ let ${optionalString (cfg.proxy != null) "proxy=${cfg.proxy}"} always-use-proxy=${boolToString cfg.always-use-proxy} bind-addr=${cfg.address}:${toString cfg.port} - bitcoin-rpcconnect=${config.services.bitcoind.rpc.address} + bitcoin-rpcconnect=${nbLib.address config.services.bitcoind.rpc.address} bitcoin-rpcport=${toString config.services.bitcoind.rpc.port} bitcoin-rpcuser=${config.services.bitcoind.rpc.users.public.name} rpc-file-mode=0660 diff --git a/modules/electrs.nix b/modules/electrs.nix index 095f6a0..15cad4c 100644 --- a/modules/electrs.nix +++ b/modules/electrs.nix @@ -96,7 +96,7 @@ in { --daemon-dir='${bitcoind.dataDir}' \ --electrum-rpc-addr=${cfg.address}:${toString cfg.port} \ --monitoring-addr=${cfg.address}:${toString cfg.monitoringPort} \ - --daemon-rpc-addr=${bitcoind.rpc.address}:${toString bitcoind.rpc.port} \ + --daemon-rpc-addr=${nbLib.addressWithPort bitcoind.rpc.address bitcoind.rpc.port} \ ${cfg.extraArgs} ''; User = cfg.user; diff --git a/modules/joinmarket.nix b/modules/joinmarket.nix index f2e92d9..9ac4d8a 100644 --- a/modules/joinmarket.nix +++ b/modules/joinmarket.nix @@ -124,7 +124,7 @@ let [BLOCKCHAIN] blockchain_source = bitcoin-rpc network = ${bitcoind.network} - rpc_host = ${bitcoind.rpc.address} + rpc_host = ${nbLib.address bitcoind.rpc.address} rpc_port = ${toString bitcoind.rpc.port} rpc_user = ${bitcoind.rpc.users.privileged.name} ${optionalString (cfg.rpcWalletFile != null) "rpc_wallet_file = ${cfg.rpcWalletFile}"} diff --git a/modules/lightning-loop.nix b/modules/lightning-loop.nix index b7b388e..1249a3a 100644 --- a/modules/lightning-loop.nix +++ b/modules/lightning-loop.nix @@ -50,7 +50,7 @@ let cli = mkOption { default = pkgs.writeScriptBin "loop" '' ${cfg.package}/bin/loop \ - --rpcserver ${rpclisten} \ + --rpcserver ${nbLib.addressWithPort cfg.rpcAddress cfg.rpcPort} \ --macaroonpath '${cfg.dataDir}/${network}/loop.macaroon' \ --tlscertpath '${secretsDir}/loop-cert' "$@" ''; @@ -66,17 +66,16 @@ let lnd = config.services.lnd; network = config.services.bitcoind.network; - rpclisten = "${cfg.rpcAddress}:${toString cfg.rpcPort}"; configFile = builtins.toFile "loop.conf" '' datadir=${cfg.dataDir} network=${network} - rpclisten=${rpclisten} + rpclisten=${cfg.rpcAddress}:${toString cfg.rpcPort} restlisten=${cfg.restAddress}:${toString cfg.restPort} logdir=${cfg.dataDir}/logs tlscertpath=${secretsDir}/loop-cert tlskeypath=${secretsDir}/loop-key - lnd.host=${lnd.rpcAddress}:${toString lnd.rpcPort} + lnd.host=${nbLib.addressWithPort lnd.rpcAddress lnd.rpcPort} lnd.macaroonpath=${lnd.networkDir}/admin.macaroon lnd.tlspath=${lnd.certPath} diff --git a/modules/lightning-pool.nix b/modules/lightning-pool.nix index 4a9c9ca..0c69d32 100644 --- a/modules/lightning-pool.nix +++ b/modules/lightning-pool.nix @@ -50,7 +50,7 @@ let cli = mkOption { default = pkgs.writeScriptBin "pool" '' exec ${cfg.package}/bin/pool \ - --rpcserver ${rpclisten} \ + --rpcserver ${nbLib.addressWithPort cfg.rpcAddress cfg.rpcPort} \ --network ${network} \ --basedir '${cfg.dataDir}' "$@" ''; @@ -65,9 +65,8 @@ let lnd = config.services.lnd; network = config.services.bitcoind.network; - rpclisten = "${cfg.rpcAddress}:${toString cfg.rpcPort}"; configFile = builtins.toFile "pool.conf" '' - rpclisten=${rpclisten} + rpclisten=${cfg.rpcAddress}:${toString cfg.rpcPort} restlisten=${cfg.restAddress}:${toString cfg.restPort} ${optionalString (cfg.proxy != null) "proxy=${cfg.proxy}"} diff --git a/modules/liquid.nix b/modules/liquid.nix index 9a311fc..aaf7059 100644 --- a/modules/liquid.nix +++ b/modules/liquid.nix @@ -169,7 +169,7 @@ let rpcconnect=${cfg.rpc.address} ${lib.concatMapStrings (rpcallowip: "rpcallowip=${rpcallowip}\n") cfg.rpcallowip} rpcuser=${cfg.rpcuser} - mainchainrpchost=${bitcoind.rpc.address} + mainchainrpchost=${nbLib.address bitcoind.rpc.address} mainchainrpcport=${toString bitcoind.rpc.port} mainchainrpcuser=${bitcoind.rpc.users.public.name} diff --git a/modules/lnd-rest-onion-service.nix b/modules/lnd-rest-onion-service.nix index 8b2ea25..3d845c1 100644 --- a/modules/lnd-rest-onion-service.nix +++ b/modules/lnd-rest-onion-service.nix @@ -41,7 +41,7 @@ in { services.tor = { enable = true; relay.onionServices.lnd-rest = nbLib.mkOnionService { - target.addr = lnd.restAddress; + target.addr = nbLib.address lnd.restAddress; target.port = lnd.restPort; port = lnd.restPort; }; diff --git a/modules/lnd.nix b/modules/lnd.nix index 8805622..6da0f18 100644 --- a/modules/lnd.nix +++ b/modules/lnd.nix @@ -127,7 +127,7 @@ let bitcoind = config.services.bitcoind; - bitcoindRpcAddress = bitcoind.rpc.address; + bitcoindRpcAddress = nbLib.address bitcoind.rpc.address; networkDir = cfg.networkDir; configFile = pkgs.writeText "lnd.conf" '' datadir=${cfg.dataDir} @@ -217,12 +217,12 @@ in { # existing, but the RPC service isn't yet, which results in error # "waiting to start, RPC services not available". curl = "${pkgs.curl}/bin/curl -s --show-error --retry 10 --cacert ${cfg.certPath}"; - restUrl = "https://${cfg.restAddress}:${toString cfg.restPort}/v1"; + restUrl = "https://${nbLib.addressWithPort cfg.restAddress cfg.restPort}/v1"; in [ (nbLib.script "lnd-create-wallet" '' attempts=250 while ! { - exec 3>/dev/tcp/${cfg.restAddress}/${toString cfg.restPort} && exec 3>&- + exec 3>/dev/tcp/${nbLib.address cfg.restAddress}/${toString cfg.restPort} && exec 3>&- } &>/dev/null; do ((attempts-- == 0)) && { echo "lnd REST service unreachable"; exit 1; } sleep 0.1 diff --git a/modules/nodeinfo.nix b/modules/nodeinfo.nix index 5a9599c..8bba8e0 100644 --- a/modules/nodeinfo.nix +++ b/modules/nodeinfo.nix @@ -13,6 +13,7 @@ let }; cfg = config.nix-bitcoin.nodeinfo; + nbLib = config.nix-bitcoin.lib; # Services included in the output services = { @@ -96,7 +97,7 @@ let mkInfo = extraCode: name: cfg: '' add_service("${name}", """ - info["local_address"] = "${cfg.address}:${toString cfg.port}" + info["local_address"] = "${nbLib.addressWithPort cfg.address cfg.port}" '' + mkIfOnionPort name (onionPort: '' set_onion_address(info, "${name}", ${onionPort}) '') + extraCode + '' diff --git a/modules/onion-services.nix b/modules/onion-services.nix index 25c9f45..192ac87 100644 --- a/modules/onion-services.nix +++ b/modules/onion-services.nix @@ -65,7 +65,7 @@ in { in nbLib.mkOnionService { port = if externalPort != null then externalPort else service.port; target.port = service.port; - target.addr = if service.address == "0.0.0.0" then "127.0.0.1" else service.address; + target.addr = nbLib.address service.address; } ); }; diff --git a/pkgs/lib.nix b/pkgs/lib.nix index c7a7480..61db1b0 100644 --- a/pkgs/lib.nix +++ b/pkgs/lib.nix @@ -83,4 +83,17 @@ let self = { map = [ map ]; version = 3; }; + + # Convert a bind address, which may be a special INADDR_ANY address, + # to an actual IP address + address = addr: + if addr == "0.0.0.0" then + "127.0.0.1" + else if addr == "::" then + "::1" + else + addr; + + addressWithPort = addr: port: "${self.address addr}:${toString port}"; + }; in self