-
Notifications
You must be signed in to change notification settings - Fork 78
ETCM-541 attempt UPnP port mapping to aid in peer discovery & connection #929
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1666bf7
eb60681
d6598c8
187c4c0
be832ae
64766ba
f58231f
3d36ce2
5095b34
7b84cfe
5991141
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,9 @@ mantis { | |
| port = 9076 | ||
| } | ||
|
|
||
| # Try automatic port forwarding via UPnP | ||
| automatic-port-forwarding = true | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be on by default?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For it to be useful, I think so. Someone who wouldn't map the ports themselves probably won't be inclined to enable this manually either, right? |
||
|
|
||
| discovery { | ||
|
|
||
| # Turn discovery of/off | ||
|
|
@@ -577,7 +580,7 @@ mantis { | |
| # otherwise mantis VM will be run in the same process, but acting as an external VM (listening at `host` and `port`) | ||
| # - none: doesn't run anything, expect the VM to be started by other means | ||
| vm-type = "mantis" | ||
|
|
||
| # path to the executable - optional depending on the `vm-type` setting | ||
| executable-path = "./bin/mantis-vm" | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package io.iohk.ethereum.network | ||
|
|
||
| import io.iohk.ethereum.utils.Logger | ||
| import java.net.InetAddress | ||
| import java.util.concurrent.ExecutorService | ||
| import monix.eval.Task | ||
| import org.jupnp.DefaultUpnpServiceConfiguration | ||
| import org.jupnp.support.igd.PortMappingListener | ||
| import org.jupnp.support.model.PortMapping | ||
| import org.jupnp.support.model.PortMapping.Protocol.{TCP, UDP} | ||
| import org.jupnp.tool.transport.JDKTransportConfiguration | ||
| import org.jupnp.transport.Router | ||
| import org.jupnp.transport.spi.NetworkAddressFactory | ||
| import org.jupnp.transport.spi.StreamClient | ||
| import org.jupnp.transport.spi.StreamClientConfiguration | ||
| import org.jupnp.transport.spi.StreamServer | ||
| import org.jupnp.transport.spi.StreamServerConfiguration | ||
| import org.jupnp.UpnpServiceImpl | ||
| import scala.jdk.CollectionConverters._ | ||
| import scala.util.chaining._ | ||
| import org.jupnp.QueueingThreadPoolExecutor | ||
| import cats.effect.Resource | ||
| import org.jupnp.UpnpService | ||
| import cats.implicits._ | ||
|
|
||
| private class ClientOnlyUpnpServiceConfiguration extends DefaultUpnpServiceConfiguration() { | ||
| private final val THREAD_POOL_SIZE = 4 // seemingly the minimum required to perform port mapping | ||
|
|
||
| override def createDefaultExecutorService(): ExecutorService = | ||
| QueueingThreadPoolExecutor.createInstance("mantis-jupnp", THREAD_POOL_SIZE); | ||
|
|
||
| override def createStreamClient(): StreamClient[_ <: StreamClientConfiguration] = | ||
| JDKTransportConfiguration.INSTANCE.createStreamClient(getSyncProtocolExecutorService()) | ||
|
|
||
| override def createStreamServer(networkAddressFactory: NetworkAddressFactory): NoStreamServer.type = | ||
| NoStreamServer // prevent a StreamServer from running needlessly | ||
| } | ||
|
|
||
| private object NoStreamServer extends StreamServer[StreamServerConfiguration] { | ||
| def run(): Unit = () | ||
| def init(_1: InetAddress, _2: Router): Unit = () | ||
| def getPort(): Int = 0 | ||
| def stop(): Unit = () | ||
| def getConfiguration(): StreamServerConfiguration = new StreamServerConfiguration { | ||
| def getListenPort(): Int = 0 | ||
| } | ||
| } | ||
|
|
||
| object PortForwarder extends Logger { | ||
| private final val description = "Mantis" | ||
|
|
||
| def openPorts(tcpPorts: Seq[Int], udpPorts: Seq[Int]): Resource[Task, Unit] = | ||
| Resource.make(startForwarding(tcpPorts, udpPorts))(stopForwarding).void | ||
|
|
||
| private def startForwarding(tcpPorts: Seq[Int], udpPorts: Seq[Int]): Task[UpnpService] = Task { | ||
| log.info("Attempting port forwarding for TCP ports {} and UDP ports {}", tcpPorts, udpPorts) | ||
| new UpnpServiceImpl(new ClientOnlyUpnpServiceConfiguration()).tap { service => | ||
| service.startup() | ||
|
|
||
| val bindAddresses = | ||
| service | ||
| .getConfiguration() | ||
| .createNetworkAddressFactory() | ||
| .getBindAddresses() | ||
| .asScala | ||
| .map(_.getHostAddress()) | ||
| .toArray | ||
|
|
||
| val portMappings = for { | ||
| address <- bindAddresses | ||
| (port, protocol) <- tcpPorts.map(_ -> TCP) ++ udpPorts.map(_ -> UDP) | ||
| } yield new PortMapping(port, address, protocol).tap(_.setDescription(description)) | ||
|
|
||
| service.getRegistry().addListener(new PortMappingListener(portMappings)) | ||
| } | ||
| } | ||
|
|
||
| private def stopForwarding(service: UpnpService) = Task { | ||
| service.shutdown() | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.