Class NgrokClient


  • public class NgrokClient
    extends java.lang.Object
    A client for interacting with ngrok, its binary, and its APIs. Can be configured with JavaNgrokConfig.

    Open a Tunnel

    To open a tunnel, use the NgrokClient.connect() method, which returns a Tunnel, and this returned object has a reference to the public URL generated by ngrok in its Tunnel.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 a CreateTunnel (which can be built through its Builder) that allows us to pass additional properties that are supported by ngrok.

    java-ngrok is compatible with ngrok v2 and v3, but by default it will install v3. To install v2 instead, set the version with JavaNgrokConfig.Builder.withNgrokVersion(NgrokVersion) and CreateTunnel.Builder.withNgrokVersion(NgrokVersion).

    Note: ngrok v2's default behavior for http when no additional properties are passed is to open two tunnels, one http and one https. This method will return a reference to the http tunnel in this case. If only a single tunnel is needed, call CreateTunnel.Builder.withBindTls(BindTls) with BindTls.TRUE and a reference to the https tunnel will be returned.

    ngrok's Edge

    To use ngrok's Edges with java-ngrok, first configure an Edge on ngrok's dashboard (with at least one Endpoint mapped to the Edge), and define a labeled tunnel in the ngrok config file that points to the Edge.

     tunnels:
       some-edge-tunnel:
         labels:
           - edge=my_edge_id
         addr: http://localhost:80
     
    To start a labeled tunnel in java-ngrok, set CreateTunnel.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 an Edge tunnel is started, it can be managed through ngrok's dashboard.

    Get Active Tunnels

    It can be useful to ask the ngrok client what tunnels are currently open. This can be accomplished with the getTunnels() method, which returns a list of Tunnel objects.

     [<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 with disconnect(String).

     // The Tunnel returned from methods like connect(), getTunnels(), etc. contains the public URL
     ngrokClient.disconnect(publicUrl);
     

    Expose Other Services

    Using ngrok we can expose any number of non-HTTP services, for instances databases, game servers, etc. This can be accomplished by using java-ngrok to open a tcp tunnel to the desired service.

     final NgrokClient ngrokClient = new NgrokClient.Builder().build();
    
     // 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.

     final NgrokClient ngrokClient = new NgrokClient.Builder().build();
    
     // 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-ngrok is 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 class  NgrokClient.Builder
      Builder for a NgrokClient, see docs for that class for example usage.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Tunnel connect()
      Tunnel connect​(CreateTunnel createTunnel)
      Establish a new ngrok tunnel for the Tunnel creation request, returning an object representing the connected tunnel.
      void disconnect​(java.lang.String publicUrl)
      Disconnect the ngrok tunnel for the given URL, if open.
      HttpClient getHttpClient()
      Get the class used to make HTTP requests to ngrok's APIs.
      JavaNgrokConfig getJavaNgrokConfig()
      Get the java-ngrok to use when interacting with the ngrok binary.
      NgrokProcess getNgrokProcess()
      Get the class used to manage the ngrok binary.
      java.util.List<Tunnel> getTunnels()
      Get a list of active ngrok tunnels.
      Version getVersion()
      Get the ngrok and java-ngrok version.
      void kill()
      Terminate the ngrok processes, if running.
      void refreshMetrics​(Tunnel tunnel)
      Get the latest metrics for the given Tunnel and update its metrics attribute.
      void setAuthToken​(java.lang.String authToken)
      Set the ngrok auth token in the config file, enabling authenticated features (for instance, more concurrent tunnels, custom subdomains, etc.).
      void update()
      Update ngrok, if an update is available.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • disconnect

        public void disconnect​(java.lang.String publicUrl)
        Disconnect the ngrok tunnel for the given URL, if open.
        Parameters:
        publicUrl - The public URL of the tunnel to disconnect.
        Throws:
        JavaNgrokHTTPException - An HTTP error occurred communicating with the ngrok API.
        JavaNgrokSecurityException - The URL was not supported.
      • getTunnels

        public java.util.List<Tunnel> getTunnels()
        Get a list of active ngrok tunnels.

        If ngrok is not running, calling this method will first start a process with JavaNgrokConfig.

        Returns:
        The active ngrok tunnels.
        Throws:
        JavaNgrokException - The response was invalid or not compatible with java-ngrok.
        JavaNgrokHTTPException - An HTTP error occurred communicating with the ngrok API.
        JavaNgrokSecurityException - The URL was not supported.
      • refreshMetrics

        public void refreshMetrics​(Tunnel tunnel)
        Get the latest metrics for the given Tunnel and update its metrics attribute.
        Parameters:
        tunnel - The Tunnel to update.
        Throws:
        JavaNgrokException - The API did not return metrics.
        JavaNgrokSecurityException - The URL was not supported.
      • kill

        public void kill()
        Terminate the ngrok processes, if running. This method will not block, it will just issue a kill request.
      • setAuthToken

        public void setAuthToken​(java.lang.String authToken)
        Set the ngrok auth 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()
        Update ngrok, if an update is available.
      • getVersion

        public Version getVersion()
        Get the ngrok and java-ngrok version.
        Returns:
        The versions.
      • getJavaNgrokConfig

        public JavaNgrokConfig getJavaNgrokConfig()
        Get the java-ngrok to use when interacting with the ngrok binary.
      • getNgrokProcess

        public NgrokProcess getNgrokProcess()
        Get the class used to manage the ngrok binary.
      • getHttpClient

        public HttpClient getHttpClient()
        Get the class used to make HTTP requests to ngrok's APIs.