Implement rpcbind to allow for binding to a different interface (#2409)

This commit is contained in:
Chris Stewart 2020-12-22 07:25:50 -06:00 committed by GitHub
parent de5041ee27
commit eaecc1c377
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 55 additions and 14 deletions

View file

@ -22,13 +22,19 @@ class OracleServerMain(override val args: Array[String])
routes = Seq(OracleRoutes(oracle))
server = rpcPortOpt match {
case Some(rpcport) =>
Server(conf, routes, rpcport = rpcport)
Server(conf = conf,
handlers = routes,
rpcbindOpt = rpcBindOpt,
rpcport = rpcport)
case None =>
conf.rpcPortOpt match {
case Some(rpcport) =>
Server(conf, routes, rpcport)
Server(conf = conf,
handlers = routes,
rpcbindOpt = rpcBindOpt,
rpcport = rpcport)
case None =>
Server(conf, routes)
Server(conf = conf, handlers = routes, rpcbindOpt = rpcBindOpt)
}
}

View file

@ -26,6 +26,15 @@ trait BitcoinSRunner extends BitcoinSLogger {
lazy val argsWithIndex: Vector[(String, Int)] = args.toVector.zipWithIndex
/** The ip address we are binding the server to */
lazy val rpcBindOpt: Option[String] = {
val rpcbindOpt = argsWithIndex.find(_._1.toLowerCase == "--rpcbind")
rpcbindOpt.map {
case (_, idx) =>
args(idx + 1)
}
}
lazy val rpcPortOpt: Option[Int] = {
val portOpt = argsWithIndex.find(_._1.toLowerCase == "--rpcport")
portOpt.map {

View file

@ -94,7 +94,11 @@ class BitcoinSServerMain(override val args: Array[String])
_ <- node.start()
_ <- wallet.start()
chainApi <- node.chainApiFromDb()
binding <- startHttpServer(node, chainApi, wallet, rpcPortOpt)
binding <- startHttpServer(nodeApi = node,
chainApi = chainApi,
wallet = wallet,
rpcbindOpt = rpcBindOpt,
rpcPortOpt = rpcPortOpt)
_ = {
logger.info(s"Starting ${nodeConf.nodeType.shortName} node sync")
}
@ -151,7 +155,11 @@ class BitcoinSServerMain(override val args: Array[String])
blockCount)
}
binding <- startHttpServer(bitcoind, bitcoind, wallet, rpcPortOpt)
binding <- startHttpServer(nodeApi = bitcoind,
chainApi = bitcoind,
wallet = wallet,
rpcbindOpt = rpcBindOpt,
rpcPortOpt = rpcPortOpt)
_ = BitcoinSServer.startedFP.success(Future.successful(binding))
} yield {
logger.info(s"Done starting Main!")
@ -266,6 +274,7 @@ class BitcoinSServerMain(override val args: Array[String])
nodeApi: NodeApi,
chainApi: ChainApi,
wallet: Wallet,
rpcbindOpt: Option[String],
rpcPortOpt: Option[Int])(implicit
system: ActorSystem,
conf: BitcoinSAppConfig): Future[Http.ServerBinding] = {
@ -279,18 +288,26 @@ class BitcoinSServerMain(override val args: Array[String])
val server = {
rpcPortOpt match {
case Some(rpcport) =>
Server(nodeConf,
Seq(walletRoutes, nodeRoutes, chainRoutes, coreRoutes),
rpcport = rpcport)
Server.apply(conf = nodeConf,
handlers =
Seq(walletRoutes, nodeRoutes, chainRoutes, coreRoutes),
rpcbindOpt = rpcbindOpt,
rpcport = rpcport)
case None =>
conf.rpcPortOpt match {
case Some(rpcport) =>
Server(nodeConf,
Seq(walletRoutes, nodeRoutes, chainRoutes, coreRoutes),
rpcport)
Server.apply(
conf = nodeConf,
handlers =
Seq(walletRoutes, nodeRoutes, chainRoutes, coreRoutes),
rpcbindOpt = rpcbindOpt,
rpcport = rpcport)
case None =>
Server(nodeConf,
Seq(walletRoutes, nodeRoutes, chainRoutes, coreRoutes))
Server.apply(
conf = nodeConf,
handlers =
Seq(walletRoutes, nodeRoutes, chainRoutes, coreRoutes),
rpcbindOpt = rpcbindOpt)
}
}
}

View file

@ -16,6 +16,7 @@ import scala.concurrent.Future
case class Server(
conf: AppConfig,
handlers: Seq[ServerRoute],
rpcbindOpt: Option[String],
rpcport: Int = 9999)(implicit system: ActorSystem)
extends HttpLogger {
@ -78,7 +79,7 @@ case class Server(
def start(): Future[Http.ServerBinding] = {
val httpFut =
Http().bindAndHandle(route, "localhost", rpcport)
Http().bindAndHandle(route, rpcbindOpt.getOrElse("localhost"), rpcport)
httpFut.foreach { http =>
logger.info(s"Started Bitcoin-S HTTP server at ${http.localAddress}")
}

View file

@ -108,6 +108,7 @@ bitcoin-s {
server {
# The port we bind our rpc server on
rpcport = 9999
rpcbind = "127.0.0.1"
}
oracle = ${bitcoin-s.dbDefault}

View file

@ -51,6 +51,10 @@ There are a few command line options available that take precedence over configu
`datadir` sets the data directory instead of using the default `$HOME/.bitcoin-s`
- `--rpcbind <ip>`
`rpcbind` sets the interface the rpc server binds to instead of using the default `127.0.0.1`
- `--rpcport <port>`
`rpcport` sets the port the rpc server binds to instead of using the default `9999`
@ -203,6 +207,9 @@ bitcoin-s {
server {
# The port we bind our rpc server on
rpcport = 9999
# The ip address we bind our server too
rpcbind = "127.0.0.1"
}
}