Skip to content
rust-proxyPublic

About

Chromium Network Stack (cronet) bindings

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

46 Commits

Folders and files

Repository files navigation

libcronet

English · 简体中文

Use Chromium's Cronet network stack from Rust to open NaiveProxy CONNECT tunnels over HTTP/2 or HTTP/3. NaiveClient handles the handshake and NaiveProxy padding. The resulting NaiveConn implements blocking std::io::Read and Write. The bindings are ported from SagerNet/cronet-go and target its Chromium 154.0.8037.49 dialer ABI.

Wind and ClashRS use libcronet in production.

Crate Use it for Reference
libcronet Opening tunnels with NaiveClient or configuring Cronet directly with Engine Rust API
libcronet-sys Calling the unsafe C API and resolving the native library FFI API

Open a tunnel

On Linux or Windows, run this in a Rust binary project. The download feature fetches the matching native library at build time and verifies its SHA-256 checksum.

cargo add libcronet@0.2.0 --features download

Save the following as src/main.rs. Set NAIVE_PROXY to your proxy's TLS address in host:port form and run cargo run. If the proxy requires authentication, set NAIVE_USERNAME and NAIVE_PASSWORD. If it uses a private CA, set NAIVE_CA_FILE to the path of its PEM certificate. The example opens a tunnel to example.com:80, sends an HTTP request, and prints the destination's status line.

use libcronet::{NaiveClient, NaiveClientConfig};
use std::env;
use std::io::{BufRead, BufReader, Write};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // SAFETY: Load the verified library once, before the first Cronet call.
    unsafe { libcronet::sys::load_downloaded_library()? };

    let proxy = env::var("NAIVE_PROXY")?;
    let mut client = NaiveClient::new(NaiveClientConfig {
        server_name: Some(proxy.clone()), // Keep the port in the proxy URL.
        server_address: proxy,
        username: env::var("NAIVE_USERNAME").ok(),
        password: env::var("NAIVE_PASSWORD").ok(),
        trusted_root_certificates: env::var_os("NAIVE_CA_FILE")
            .map(std::fs::read_to_string)
            .transpose()?,
        ..Default::default()
    })?;
    client.start()?;

    {
        let mut tunnel = client.dial_and_handshake("example.com:80")?;
        tunnel.write_all(b"HEAD / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n")?;
        tunnel.flush()?;
        let mut status = String::new();
        BufReader::new(tunnel).read_line(&mut status)?;
        print!("{status}");
    }
    client.close()?;
    Ok(())
}

The client uses HTTP/2 by default. Set quic_enabled: true in NaiveClientConfig to use HTTP/3 with a proxy that supports it.

Tokio I/O

With version 0.2, enable libcronet's tokio feature and Tokio's io-util feature in your application. Inside an async function, dial with a started NaiveClient:

use tokio::io::{AsyncReadExt, AsyncWriteExt};

let mut tunnel = client.dial_async("example.com:80").await?;
tunnel.write_all(b"GET / HTTP/1.0\r\nHost: example.com\r\n\r\n").await?;
tunnel.shutdown().await?;
let mut response = Vec::new();
tunnel.read_to_end(&mut response).await?;

The tunnel borrows the client. Cronet callbacks wake the waiting task directly. Use tokio::io::split for independent reads and writes. shutdown().await finishes sending and leaves the reader open. To cancel both directions and wait for Cronet to finish, use tunnel.close().await.

Choose how to link Cronet

Linux, Windows, and Android load a shared library by default. Apple targets link statically by default. To embed Cronet using a verified prebuilt archive, use this dependency configuration:

cargo add libcronet@0.2.0 --features static-link,download-static

If both dynamic and static-link are enabled, dynamic wins. Windows static builds require MSVC, and Android static builds require the NDK target linker. The iOS and tvOS prebuilts are static archives. For manual setup, see the native library guide and the matching prebuilt libraries.

About

Chromium Network Stack (cronet) bindings

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages