- java.lang.Object
-
- com.github.alexdlaird.ngrok.NgrokClient
-
public class NgrokClient extends java.lang.ObjectA client for interacting with ngrok, its binary, and its APIs. Can be configured withJavaNgrokConfig.Open a Tunnel
To open a tunnel, use theNgrokClient.connect()method, which returns aTunnel, and this returned object has a reference to the public URL generated byngrokin itsTunnel.getPublicUrl()method.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_tunnel_name") .build(); final Tunnel namedTunnel = ngrokClient.connect(createNamedTunnel);The
NgrokClient.connect()method can also take aCreateTunnel(which can be built throughits Builder) that allows us to pass additional properties that are supported by ngrok.java-ngrokis compatible withngrokv2 and v3, but by default it will install v3. To install v2 instead, set the version withJavaNgrokConfig.Builder.withNgrokVersion(NgrokVersion)andCreateTunnel.Builder.withNgrokVersion(NgrokVersion).Note:
ngrokv2's default behavior forhttpwhen no additional properties are passed is to open two tunnels, onehttpand onehttps. This method will return a reference to thehttptunnel in this case. If only a single tunnel is needed, callCreateTunnel.Builder.withBindTls(BindTls)withBindTls.TRUEand a reference to thehttpstunnel will be returned.
To usengrok's Cloud Edgengrok's Cloud Edge withjava-ngrok, first configure an Edge onngrok's dashboard (with at least one Endpoint mapped to the Edge), and define a labeled tunnel in thengrokconfig file. that points to the Edge.tunnels: some-edge-tunnel: labels: - edge=my_edge_id addr: http://localhost:80To start a labeled tunnel injava-ngrok, setCreateTunnel.Builder.withName(String).final NgrokClient ngrokClient = new NgrokClient.Builder().build(); // Open a named tunnel from the config file final CreateTunnel createNamedTunnel = new CreateTunnel.Builder() .withName("some-edge-tunnel") .build(); final Tunnel namedTunnel = ngrokClient.connect(createNamedTunnel);Once a Cloud Edge tunnel is started, it can be managed throughngrok's dashboard.Get Active Tunnels
It can be useful to ask thengrokclient what tunnels are currently open. This can be accomplished with thegetTunnels()method, which returns a list ofTunnelobjects.[<Tunnel: "https://<public_sub>.ngrok.io" -> "http://localhost:80">] final List<Tunnel> tunnels = ngrokClient.getTunnels();
Close a Tunnel
All open tunnels will automatically be closed when the Java process terminates, but we can also close them manually withdisconnect(String).// The Tunnel returned from methods like connect(), getTunnels(), etc. contains the public URL ngrokClient.disconnect(publicUrl);
Expose Other Services
Usingngrokwe can expose any number of non-HTTP services, for instances databases, game servers, etc. This can be accomplished by usingjava-ngrokto open atcptunnel to the desired service.// Open a tunnel to MySQL with a Reserved TCP Address // <NgrokTunnel: "tcp://1.tcp.ngrok.io:12345" -> "localhost:3306"> final CreateTunnel mysqlCreateTunnel = new CreateTunnel.Builder() .withProto(Proto.TCP) .withAddr(3306) .withRemoteAddr("1.tcp.ngrok.io:12345") .build(); final Tunnel mysqlTunnel = ngrokClient.connect(mysqlCreateTunnel);We can also serve up local directories via ngrok’s built-in fileserver.
// Open a tunnel to a local file server // <NgrokTunnel: "https://<public_sub>.ngrok.io" -> "file:///"> final CreateTunnel fileserverCreateTunnel = new CreateTunnel.Builder() .withAddr("file:///) .build(); final Tunnel fileserverTunnel = ngrokClient.connect(fileserverCreateTunnel);Integration Examples
java-ngrokis useful in any number of integrations, for instance to test locally without having to deploy or configure. Here are some common usage examples.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classNgrokClient.BuilderBuilder for aNgrokClient, see docs for that class for example usage.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Tunnelconnect()Tunnelconnect(CreateTunnel createTunnel)Establish a newngroktunnel for the Tunnel creation request, returning an object representing the connected tunnel.voiddisconnect(java.lang.String publicUrl)Disconnect thengroktunnel for the given URL, if open.HttpClientgetHttpClient()Get the class used to make HTTP requests tongrok's APIs.JavaNgrokConfiggetJavaNgrokConfig()Get thejava-ngrokto use when interacting with thengrokbinary.NgrokProcessgetNgrokProcess()Get the class used to manage thengrokbinary.java.util.List<Tunnel>getTunnels()Get a list of activengroktunnels.VersiongetVersion()Get thengrokandjava-ngrokversion.voidkill()Terminate thengrokprocesses, if running.voidrefreshMetrics(Tunnel tunnel)Get the latest metrics for the givenTunneland update itsmetricsattribute.voidsetAuthToken(java.lang.String authToken)Set thengrokauth token in the config file, enabling authenticated features (for instance, more concurrent tunnels, custom subdomains, etc.).voidupdate()Updatengrok, if an update is available.
-
-
-
Method Detail
-
connect
public Tunnel connect(CreateTunnel createTunnel)
Establish a newngroktunnel for the Tunnel creation request, returning an object representing the connected tunnel.If a tunnel definition in ngrok's config file matches the given
CreateTunnel.Builder.withName(String), it will be loaded and used to start the tunnel. WhenCreateTunnel.Builder.withName(String)is not set and a "java-ngrok-default" tunnel definition exists inngrok's config, it will be loaded and use. Any properties defined onCreateTunnelwill override properties from the loaded tunnel definition.If
ngrokis not installed atJavaNgrokConfig'sngrokPath, calling this method will first download and installngrok.java-ngrokis compatible withngrokv2 and v3, but by default it will install v2. To install v3 instead, set the version withJavaNgrokConfig.Builder.withNgrokVersion(NgrokVersion)andCreateTunnel.Builder.withNgrokVersion(NgrokVersion).If
ngrokis not running, calling this method will first start a process withJavaNgrokConfig.Note:
ngrokv2's default behavior forhttpwhen no additional properties are passed is to open two tunnels, onehttpand onehttps. This method will return a reference to thehttptunnel in this case. If only a single tunnel is needed, callCreateTunnel.Builder.withBindTls(BindTls)withBindTls.TRUEand a reference to thehttpstunnel will be returned.- Parameters:
createTunnel- The tunnel definition.- Returns:
- The created Tunnel.
-
connect
public Tunnel connect()
-
disconnect
public void disconnect(java.lang.String publicUrl)
Disconnect thengroktunnel for the given URL, if open.- Parameters:
publicUrl- The public URL of the tunnel to disconnect.
-
getTunnels
public java.util.List<Tunnel> getTunnels()
Get a list of activengroktunnels.If
ngrokis not running, calling this method will first start a process withJavaNgrokConfig.- Returns:
- The active
ngroktunnels.
-
refreshMetrics
public void refreshMetrics(Tunnel tunnel)
Get the latest metrics for the givenTunneland update itsmetricsattribute.- Parameters:
tunnel- The Tunnel to update.
-
kill
public void kill()
Terminate thengrokprocesses, if running. This method will not block, it will just issue a kill request.
-
setAuthToken
public void setAuthToken(java.lang.String authToken)
Set thengrokauth token in the config file, enabling authenticated features (for instance, more concurrent tunnels, custom subdomains, etc.).- Parameters:
authToken- The auth token.
-
update
public void update()
Updatengrok, if an update is available.
-
getVersion
public Version getVersion()
Get thengrokandjava-ngrokversion.- Returns:
- The versions.
-
getJavaNgrokConfig
public JavaNgrokConfig getJavaNgrokConfig()
Get thejava-ngrokto use when interacting with thengrokbinary.
-
getNgrokProcess
public NgrokProcess getNgrokProcess()
Get the class used to manage thengrokbinary.
-
getHttpClient
public HttpClient getHttpClient()
Get the class used to make HTTP requests tongrok's APIs.
-
-