Skip to content

Latest commit

 

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

porter

Tiny Java library that uses UPnP to map external WAN ports to internal LAN ports for both TCP and UDP on a supporting router.

Maven Central javadoc JPMS

Features

  • Discover UPnP routers on the network.
  • Discover external IP address.
  • Map external TCP and UDP ports to internal ones.
  • Automatically unmap on JVM shutdown.
  • Requires Java 11 or above.
  • Zero runtime dependencies.
  • Graal Native Image compatible.

Installation

Available on Maven Central, so just add the following dependency to your project's pom.xml. Adjust for other build systems.

<dependency>
    <groupId>com.sshtools</groupId>
    <artifactId>porter</artifactId>
    <version>1.0.1</version>
</dependency> 

JPMS

If you are using JPMS, add com.sshtools.porter to your module-info.java.

Usage

Usage is very simple. A port mapping can be achieved with a single line of code.

UPnP.gateway().ifPresent(gw -> gw.map(80, Protocol.TCP));

Or to map different ports.

gw.map(8080, 80, Protocol.TCP);

Or to unmap.

gw.unmap(80, Protocol.TCP);

Or to test if mapped.

var mapped = gw.mapped(80, Protocol.TCP);
if(mapped) {
    System.out.println("Mapped!");
}
else {
    System.out.println("Not Mapped!");
}

For more control over the discovery process, instead of usage, UPnP.gateway(), use DiscoveryBuilder. You can use this to configure, monitor discovery, list all gateways and more.

try(var discovery = new UPnP.DiscoveryBuilder().
    withoutShutdownHooks().
    onGateway(gw -> {
        System.out.format("Gateway found %s%n", gw.ip());
    }).
    build()) {
    
    /* Not strictly needed, here from demonstrations purposes */
    discovery.awaitCompletion();
    
    /* gateways() will wait till complete */
    var count = discovery.gateways().size();
    System.out.format("Found %d gateways%n", count);
}

About

Tiny Java library that uses UPnP to map external WAN ports to internal LAN ports for both TCP and UDP on a supporting router.

Topics

Resources

Stars

2 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages