This content originally appeared on DEV Community and was authored by Alex Laird
java-ngrok
is a Java wrapper for ngrok
that manages its own binary, making ngrok
available via a convenient Java
API.
ngrok is a reverse proxy tool that opens secure tunnels from public URLs to localhost, perfect for exposing local web servers, building webhook integrations, enabling SSH access, testing chatbots, demoing from your own machine, and more, and its made even more powerful with native Java integration through java-ngrok
.
Installation
java-ngrok
is available on Maven Central.
Maven
<dependency>
<groupId>com.github.alexdlaird</groupId>
<artifactId>java-ngrok</artifactId>
<version>1.5.3</version>
</dependency>
Gradle
implementation "com.github.alexdlaird:java-ngrok:1.5.3"
If we want ngrok
to be available from the command line, pyngrok can be
installed using pip
to manage that for us.
Basic Usage
All ngrok
functionality is available through the NgrokClient
. To open a tunnel, use the connect
method, which returns a Tunnel
, and this returned object has a reference to the public URL generated by ngrok
, which can be retrieved with getPublicUrl()
.
final NgrokClient ngrokClient = new NgrokClient.Builder().build();
// Open a HTTP tunnel on the default port 80
// <Tunnel: "http://<public_sub>.ngrok.io" -> "http://localhost:80">
final Tunnel httpTunnel = ngrokClient.connect();
// Open a SSH tunnel
// <Tunnel: "tcp://0.tcp.ngrok.io:12345" -> "localhost:22">
final CreateTunnel sshCreateTunnel = new CreateTunnel.Builder()
.withProto(Proto.TCP)
.withAddr(22)
.build();
final Tunnel sshTunnel = ngrokClient.connect(sshCreateTunnel);
The connect
method can also take a CreateTunnel
(which can be built through its Builder) that allows us to pass additional properties that are supported by ngrok.
Assuming we have also installed pyngrok, all features of ngrok
are available on the command line.
ngrok http 80
For details on how to fully leverage ngrok
from the command line, see ngrok's official documentation.
Documentation
For more advanced usage, java-ngrok
's official documentation is available at https://javadoc.io/doc/com.github.alexdlaird/java-ngrok.
Java 8
Java 8 support is not actively maintained, but a compatible build of this project does exist for Java 8. To use it,
include the java8-ngrok
dependency from Maven Central instead.
<dependency>
<groupId>com.github.alexdlaird</groupId>
<artifactId>java8-ngrok</artifactId>
<version>1.4.3</version>
</dependency>
The Process API was introduced in Java 9, so certain convenience methods around managing the ngrok
process (for instance, tearing it down) are not available in the Java 8 build.
Contributing
If you would like to get involved, be sure to review
the Contribution Guide.
Want to contribute financially? If you've found java-ngrok
useful, a donation
would also be greatly appreciated!
This content originally appeared on DEV Community and was authored by Alex Laird
Alex Laird | Sciencx (2021-08-27T19:19:54+00:00) java-ngrok – a Java wrapper for ngrok. Retrieved from https://www.scien.cx/2021/08/27/java-ngrok-a-java-wrapper-for-ngrok/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.