43 releases

0.14.0-pre.14 Jul 22, 2024
0.14.0-pre.12 Mar 11, 2024
0.14.0-pre.9 Nov 27, 2023
0.12.4 Jun 21, 2023
0.5.0 Nov 23, 2020

#144 in Web programming

Download history 2285/week @ 2024-07-21 2154/week @ 2024-07-28 1578/week @ 2024-08-04 1712/week @ 2024-08-11 1720/week @ 2024-08-18 1536/week @ 2024-08-25 1175/week @ 2024-09-01 1664/week @ 2024-09-08 1513/week @ 2024-09-15 1523/week @ 2024-09-22 1536/week @ 2024-09-29 1450/week @ 2024-10-06 1446/week @ 2024-10-13 1665/week @ 2024-10-20 1078/week @ 2024-10-27 1112/week @ 2024-11-03

5,365 downloads per month
Used in cargo-doc-ngrok

MIT/Apache

245KB
6K SLoC

ngrok-rust

Crates.io docs.rs MIT licensed Apache-2.0 licensed Continuous integration

API Docs (main)

ngrok is a simplified API-first ingress-as-a-service that adds connectivity, security, and observability to your apps.

ngrok-rust, our native and idiomatic crate for adding a public internet address with secure ingress traffic directly into your Rust apps 🦀. If you’ve used ngrok in the past, you can think of ngrok-rust as the ngrok agent packaged as a Rust crate.

ngrok-rust lets developers serve Rust services on the internet in a single statement without setting up low-level network primitives like IPs, NAT, certificates, load balancers, and even ports! Applications using ngrok-rust listen on ngrok’s global ingress network for TCP and HTTP traffic. ngrok-rust listeners are usable with hyper Servers, and connections implement tokio’s AsyncRead and AsyncWrite traits. This makes it easy to add ngrok-rust into any application that’s built on hyper, such as the popular axum HTTP framework.

See /ngrok/examples/ for example usage, or the tests in /ngrok/src/online_tests.rs.

For working with the ngrok API, check out the ngrok Rust API Client Library.

If you're looking for the agent wrapper, it's over here. See UPGRADING.md for tips on migrating.

For additional information, be sure to also check out the ngrok-rust launch announcement!

Installation

Add ngrok to the [dependencies] section of your Cargo.toml:

...

[dependencies]
ngrok = "0.13"

...

Alternatively, with cargo add:

$ cargo add ngrok

Quickstart

Create a simple HTTP server using ngrok and axum:

Cargo.toml:

[package]
name = "ngrok-axum-example"
version = "0.1.0"
edition = "2021"

[dependencies]
ngrok = { version="0.13", features=["axum"] }
tokio = { version = "1.26", features = ["full"] }
axum = "0.6"
anyhow = "1.0"

src/main.rs:

use std::net::SocketAddr;

use axum::{
    extract::ConnectInfo,
    routing::get,
    Router,
};
use ngrok::prelude::*;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // build our application with a single route
    let app = Router::new().route(
        "/",
        get(
            |ConnectInfo(remote_addr): ConnectInfo<SocketAddr>| async move {
                format!("Hello, {remote_addr:?}!\r\n")
            },
        ),
    );

    let tun = ngrok::Session::builder()
        // Read the token from the NGROK_AUTHTOKEN environment variable
        .authtoken_from_env()
        // Connect the ngrok session
        .connect()
        .await?
        // Start a tunnel with an HTTP edge
        .http_endpoint()
        .listen()
        .await?;

    println!("Tunnel started on URL: {:?}", tun.url());

    // Instead of binding a local port like so:
    // axum::Server::bind(&"0.0.0.0:8000".parse().unwrap())
    // Run it with an ngrok tunnel instead!
    axum::Server::builder(tun)
        .serve(app.into_make_service_with_connect_info::<SocketAddr>())
        .await
        .unwrap();

    Ok(())
}

Changelog

Changes to ngrok-rust are tracked under CHANGELOG.md.

Join the ngrok Community

License

This project is licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in ngrok by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~20–39MB
~633K SLoC