public class NgrokProcess
extends java.lang.Object
ngrok
process. Can be configured with
JavaNgrokConfig
.
ngrok
process. This process will remain alive, and the tunnels open,
until stop()
is invoked, or until the Java process terminates.
ngrok
emits logs, java-ngrok
can surface them to a callback function. To register
this callback, use JavaNgrokConfig.Builder.withLogEventCallback(java.util.function.Function<com.github.alexdlaird.ngrok.process.NgrokLog, java.lang.Void>)
.
If these events aren't necessary for our use case, some resources can be freed up by turning them off.
JavaNgrokConfig.Builder.withoutMonitoring()
will disable logging, or we can
call
NgrokProcess.ProcessMonitor.stop()
to stop monitoring on a running process.
Modifier and Type | Class and Description |
---|---|
static class |
NgrokProcess.ProcessMonitor
A Runnable that monitors the
ngrok thread. |
Constructor and Description |
---|
NgrokProcess(JavaNgrokConfig javaNgrokConfig,
NgrokInstaller ngrokInstaller)
If
ngrok is not already installed at JavaNgrokConfig.getNgrokPath() , the given
NgrokInstaller will install it. |
Modifier and Type | Method and Description |
---|---|
java.lang.String |
getApiUrl()
Get the API URL for the
ngrok web interface. |
NgrokInstaller |
getNgrokInstaller()
Get the class used to download and install
ngrok . |
NgrokProcess.ProcessMonitor |
getProcessMonitor()
Get the Runnable that is monitoring the
ngrok thread. |
java.lang.String |
getVersion()
Get the
ngrok version. |
boolean |
isRunning()
Check if this object is currently managing a running
ngrok process. |
void |
setApiKey(java.lang.String apiKey)
Set the
ngrok API key in the config file, enabling more features (for instance, labeled tunnels). |
void |
setAuthToken(java.lang.String authToken)
Set the
ngrok auth token in the config file, enabling authenticated features (for instance, opening
multiple concurrent tunnels, custom domains, etc.). |
void |
start()
If not already running, start a
ngrok process with no tunnels. |
void |
stop()
Terminate the
ngrok processes, if running. |
void |
update()
Update
ngrok , if an update is available. |
public NgrokProcess(JavaNgrokConfig javaNgrokConfig, NgrokInstaller ngrokInstaller)
ngrok
is not already installed at JavaNgrokConfig.getNgrokPath()
, the given
NgrokInstaller
will install it. This will also provision a default ngrok
config at
JavaNgrokConfig.getConfigPath()
, if none exists.javaNgrokConfig
- The config to use when interacting with the ngrok
binary.ngrokInstaller
- The class used to download and install ngrok
.public NgrokInstaller getNgrokInstaller()
ngrok
.public NgrokProcess.ProcessMonitor getProcessMonitor()
ngrok
thread.public void start()
ngrok
process with no tunnels. This will start the
ngrok
web interface, against which HTTP requests can be made to create, interact with, and
destroy tunnels.NgrokException
- ngrok
could not start.JavaNgrokSecurityException
- The URL was not supported.public boolean isRunning()
ngrok
process.public void stop()
ngrok
processes, if running. This method will not block, it will just issue a kill
request.public void setAuthToken(java.lang.String authToken)
ngrok
auth token in the config file, enabling authenticated features (for instance, opening
multiple concurrent tunnels, custom domains, etc.).
// Setting an auth token allows us to do things like open multiple tunnels at the same time final NgrokClient ngrokClient = new NgrokClient.Builder().build(); ngrokClient.setAuthToken("<NGROK_AUTHTOKEN>") // <NgrokTunnel: "http://<public_sub1>.ngrok.io" -> "http://localhost:80"> final Tunnel ngrokTunnel1 = ngrokClient.connect(); // <NgrokTunnel: "http://<public_sub2>.ngrok.io" -> "http://localhost:8000"> final CreateTunnel sshCreateTunnel = new CreateTunnel.Builder() .withAddr(8000) .build(); final Tunnel ngrokTunnel2 = ngrokClient.connect(createTunnel);
The auth token can also be set in the JavaNgrokConfig
that is passed to the
NgrokClient.Builder
.
authToken
- The auth token.NgrokException
- ngrok
could not start.public void setApiKey(java.lang.String apiKey)
ngrok
API key in the config file, enabling more features (for instance, labeled tunnels).
The API key can also be set in the JavaNgrokConfig
that is passed to the
NgrokClient.Builder
.
apiKey
- The API key.NgrokException
- ngrok
could not start.public void update()
ngrok
, if an update is available.NgrokException
- ngrok
could not start.public java.lang.String getVersion()
ngrok
version.NgrokException
- ngrok
could not start.public java.lang.String getApiUrl()
ngrok
web interface.JavaNgrokSecurityException
- The URL was not supported.