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 that opens secure tunnels from public URLs to localhost. It's perfect
for rapid
development (test webhooks, demo local websites, enable SSH access), establishing ingress to external
networks and devices, building production APIs (traffic policies, OAuth, load balancing), and more. And
it's made even more powerful with native Java integration through the java-ngrok client.
java-ngrok is available
on Maven Central.
If you want ngrok to be available from the command
line, pyngrok
can be installed using pip to manage that for you.
To open a tunnel, use the NgrokClient's 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: "https://<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);
// Open a named tunnel from the config file
final CreateTunnel createNamedTunnel = new CreateTunnel.Builder()
.withName("my-config-file-tunnel")
.build();
final Tunnel namedTunnel = ngrokClient.connect(createNamedTunnel);
// Open an Internal Endpoint that's load balanced
// <Tunnel: "https://some-endpoint.internal" -> "http://localhost:9000">
final CreateTunnel createInternalEndpoint = new CreateTunnel.Builder()
.withAddr("9000")
.withDomain("some-endpoint.internal")
.withPoolingEnabled(true)
.build();
final Tunnel internalEndpoint = ngrokClient.connect(createInternalEndpoint);The connect()
method can also take
a CreateTunnel
(which can be built through its Builder)
that allows you to pass additional properties that
are supported by ngrok (or withName()
to use a tunnel defined in ngrok's config
file), as documented here.
The api() method allows you to use the local
ngrok agent to make requests against the ngrok API, if you
have set an API key.
For example, here's how you would reserve a ngrok domain, then create a Cloud Endpoint with an associated traffic
policy:
final NgrokClient ngrokClient = new NgrokClient.Builder().build();
final String domain = "some-domain.ngrok.dev";
final ApiResponse domainResponse = ngrokClient.api(
List.of("reserved-domains", "create",
"--domain", domain));
final ApiResponse endpointResponse = ngrokClient.api(
List.of("endpoints", "create",
"--bindings", "public",
"--url", String.format("https://%s", domain),
"--traffic-policy-file", "policy.yml"));Assuming you have also installed pyngrok, all features of
ngrok are available
on the command line.
ngrok http 80For details on how to fully leverage ngrok from the command line,
see ngrok's official documentation.
For more advanced usage, java-ngrok's official documentation is available
here on GitHub, with additional API documentation on javadoc.io.
A Java 8-compatible build was previously maintained in the 1.4.x
branch. While it is no longer supported, it is available through the java8-ngrok artifact instead on Maven Central.
For more details on what differs in the java8-ngrok dependency,
see the "Java 8" section of the docs.
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, sponsorship
would also be greatly appreciated!
