Overridable Tor config (#3780)

This commit is contained in:
rorp 2021-10-27 10:57:10 -07:00 committed by GitHub
parent 6001da3d59
commit e02c9bba12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 52 additions and 19 deletions

View File

@ -38,7 +38,7 @@ case class BitcoinSAppConfig(
lazy val nodeConf: NodeAppConfig = NodeAppConfig(directory, confs: _*)
lazy val chainConf: ChainAppConfig = ChainAppConfig(directory, confs: _*)
lazy val dlcConf: DLCAppConfig = DLCAppConfig(directory, confs: _*)
lazy val torConf: TorAppConfig = TorAppConfig(directory, confs: _*)
lazy val torConf: TorAppConfig = TorAppConfig(directory, None, confs: _*)
lazy val dlcNodeConf: DLCNodeAppConfig =
DLCNodeAppConfig(directory, confs: _*)

View File

@ -20,5 +20,11 @@ bitcoin-s.keymanager.entropy = ${?BITCOIN_S_KEYMANAGER_ENTROPY}
bitcoin-s.proxy.enabled = ${?BITCOIN_S_PROXY_ENABLED}
bitcoin-s.bitcoind-rpc.proxy.enabled = ${?BITCOIN_S_BITCOIND_RPC_PROXY_ENABLED}
bitcoin-s.node.proxy.enabled = ${?BITCOIN_S_NODE_PROXY_ENABLED}
bitcoin-s.node.tor.enabled = ${?BITCOIN_S_NODE_TOR_ENABLED}
bitcoin-s.dlcnode.proxy.enabled = ${?BITCOIN_S_DLCNODE_PROXY_ENABLED}
bitcoin-s.dlcnode.tor.enabled = ${?BITCOIN_S_DLCNODE_TOR_ENABLED}
bitcoin-s.tor.enabled = ${?BITCOIN_S_TOR_ENABLED}
bitcoin-s.tor.provided = ${?BITCOIN_S_TOR_PROVIDED}

View File

@ -104,7 +104,7 @@ case class BitcoindRpcAppConfig(
config.getStringOrNone("bitcoin-s.bitcoind-rpc.rpcpassword")
lazy val torConf: TorAppConfig =
TorAppConfig(directory, confs: _*)
TorAppConfig(directory, Some(moduleName), confs: _*)
lazy val socks5ProxyParams: Option[Socks5ProxyParams] =
torConf.socks5ProxyParams

View File

@ -42,7 +42,7 @@ case class DLCNodeAppConfig(
override def stop(): Future[Unit] = Future.unit
lazy val torConf: TorAppConfig =
TorAppConfig(directory, conf: _*)
TorAppConfig(directory, Some(moduleName), conf: _*)
lazy val socks5ProxyParams: Option[Socks5ProxyParams] =
torConf.socks5ProxyParams

View File

@ -77,6 +77,9 @@ bitcoin-s {
}
```
You can override global proxy settings in subprojects, for example `bitcoin-s.dlcnode.proxy.enabled = true`
will enable SOCKS5 proxy for `dlcnode`.
## Creating our own Tor hidden service
Enabling the tor hidden services allows for inbound connections over tor.
@ -150,6 +153,8 @@ bitcoin-s {
}
}
```
Similarly with proxy settings you can override global Tor settings in subprojects,
for example `bitcoin-s.dlcnode.tor.enabled = true` will enable Tor for `dlcnode`.
### Manually Creating a Tor Hidden Service

View File

@ -112,7 +112,7 @@ case class NodeAppConfig(
}
lazy val torConf: TorAppConfig =
TorAppConfig(directory, confs: _*)
TorAppConfig(directory, Some(moduleName), confs: _*)
lazy val socks5ProxyParams: Option[Socks5ProxyParams] =
torConf.socks5ProxyParams

View File

@ -21,6 +21,7 @@ import scala.concurrent.{Await, ExecutionContext, Future}
*/
case class TorAppConfig(
private val directory: Path,
private val subModuleNameOpt: Option[String],
private val confs: Config*)(implicit ec: ExecutionContext)
extends AppConfig {
override protected[bitcoins] def configOverrides: List[Config] = confs.toList
@ -29,7 +30,7 @@ case class TorAppConfig(
override protected[bitcoins] def newConfigOfType(
configs: Seq[Config]): TorAppConfig =
TorAppConfig(directory, configs: _*)
TorAppConfig(directory, subModuleNameOpt, configs: _*)
protected[bitcoins] def baseDatadir: Path = directory
@ -39,16 +40,15 @@ case class TorAppConfig(
lazy val torLogFile: Path = torDir.resolve("TorLogs.txt")
lazy val torProvided = config.getBoolean("bitcoin-s.tor.provided")
lazy val torProvided = getBoolean("tor.provided")
lazy val useRandomPorts = config.getBoolean("bitcoin-s.tor.use-random-ports")
lazy val useRandomPorts = getBoolean("tor.use-random-ports")
lazy val socks5ProxyParams: Option[Socks5ProxyParams] = {
if (config.getBoolean("bitcoin-s.proxy.enabled")) {
if (getBoolean("proxy.enabled")) {
val address = if (torProvided) {
NetworkUtil.parseInetSocketAddress(
config.getString("bitcoin-s.proxy.socks5"),
TorParams.DefaultProxyPort)
NetworkUtil.parseInetSocketAddress(getString("proxy.socks5"),
TorParams.DefaultProxyPort)
} else {
new InetSocketAddress(InetAddress.getLoopbackAddress,
if (useRandomPorts)
@ -68,11 +68,10 @@ case class TorAppConfig(
}
lazy val torParams: Option[TorParams] = {
if (config.getBoolean("bitcoin-s.tor.enabled")) {
if (getBoolean("tor.enabled")) {
val address = if (torProvided) {
NetworkUtil.parseInetSocketAddress(
config.getString("bitcoin-s.tor.control"),
TorParams.DefaultControlPort)
NetworkUtil.parseInetSocketAddress(getString("tor.control"),
TorParams.DefaultControlPort)
} else {
new InetSocketAddress(InetAddress.getLoopbackAddress,
if (useRandomPorts)
@ -80,13 +79,13 @@ case class TorAppConfig(
else TorParams.DefaultControlPort)
}
val auth = config.getStringOrNone("bitcoin-s.tor.password") match {
val auth = getStringOrNone("tor.password") match {
case Some(pass) => Password(pass)
case None => SafeCookie()
}
val privKeyPath =
config.getStringOrNone("bitcoin-s.tor.privateKeyPath") match {
getStringOrNone("tor.privateKeyPath") match {
case Some(path) => new File(path).toPath
case None => datadir.resolve("tor_priv_key")
}
@ -198,6 +197,29 @@ case class TorAppConfig(
NetworkUtil.portIsBound(toCheck)
}
private def getBoolean(key: String): Boolean =
getConfigValue(config.getBoolean)(key)
private def getString(key: String): String =
getConfigValue(config.getString)(key)
private def getStringOrNone(key: String): Option[String] =
getConfigValue(config.getStringOrNone)(key)
private def getConfigValue[V](getValue: String => V)(key: String): V = {
subModuleNameOpt match {
case Some(subModuleName) =>
val path = s"bitcoin-s.$subModuleName.$key"
if (config.hasPath(path)) {
getValue(path)
} else {
getValue(s"bitcoin-s.$key")
}
case None =>
getValue(s"bitcoin-s.$key")
}
}
}
object TorAppConfig extends AppConfigFactory[TorAppConfig] {
@ -209,7 +231,7 @@ object TorAppConfig extends AppConfigFactory[TorAppConfig] {
*/
override def fromDatadir(datadir: Path, confs: Vector[Config])(implicit
ec: ExecutionContext): TorAppConfig =
TorAppConfig(datadir, confs: _*)
TorAppConfig(datadir, None, confs: _*)
lazy val randomSocks5Port: Int = ports.proxyPort

View File

@ -54,7 +54,7 @@ case class WalletAppConfig(
override def appConfig: WalletAppConfig = this
lazy val torConf: TorAppConfig =
TorAppConfig(directory, conf: _*)
TorAppConfig(directory, Some(moduleName), conf: _*)
private[wallet] lazy val scheduler: ScheduledExecutorService = {
Executors.newScheduledThreadPool(