arti_ureq_builder_and_configs/
arti-ureq-builder-and-configs.rs

1// Configure arti-ureq using custom configurations and the ConnectorBuilder.
2
3use anyhow::Context;
4use arti_ureq::ureq::tls::RootCerts;
5
6const TEST_URL: &str = "https://check.torproject.org/api/ip";
7
8fn main() -> anyhow::Result<()> {
9    // Build tor client config.
10    let tor_client_config = arti_ureq::arti_client::config::TorClientConfig::default();
11
12    // Create tor client.
13    let tor_client = arti_ureq::arti_client::TorClient::with_runtime(
14        arti_ureq::tor_rtcompat::PreferredRuntime::create().context("Failed to create runtime.")?,
15    )
16    .config(tor_client_config)
17    .create_unbootstrapped()
18    .context("Error creating Tor Client.")?;
19
20    // Define the TLS provider.
21    // This method returns the default TLS provider based on the feature flags.
22    let tls_provider = arti_ureq::get_default_tls_provider();
23
24    // You can also manually set the TLS provider.
25    // let tls_provider = arti_ureq::ureq::tls::TlsProvider::Rustls; // To use Rustls.
26    // let tls_provider = arti_ureq::ureq::tls::TlsProvider::Native; // To use NativeTls.
27
28    // Build arti_ureq::Connector.
29    let connector_builder =
30        arti_ureq::Connector::<arti_ureq::tor_rtcompat::PreferredRuntime>::builder()
31            .context("Failed to create ConnectorBuilder")?
32            .tor_client(tor_client) // Set Tor client.
33            .tls_provider(tls_provider); // Set TLS provider.
34
35    // Build ureq TLS config.
36    let ureq_tls_config = arti_ureq::ureq::tls::TlsConfig::builder()
37        .root_certs(RootCerts::PlatformVerifier)
38        .provider(tls_provider) // TLS provider in ureq config must match the one in Connector.
39        .build();
40
41    // Build ureq config.
42    let ureq_config = arti_ureq::ureq::config::Config::builder()
43        .user_agent("arti-ureq-custom-user-agent")
44        .tls_config(ureq_tls_config)
45        .build();
46
47    // Get ureq agent with ureq_config.
48    let ureq_agent = connector_builder
49        .build()
50        .context("Failed to build Connector")?
51        .agent_with_ureq_config(ureq_config)
52        .context("Failed to create ureq agent.")?;
53
54    // Make request.
55    let mut request = ureq_agent
56        .get(TEST_URL)
57        .call()
58        .context("Failed to make request.")?;
59
60    // Get response body.
61    let response = request
62        .body_mut()
63        .read_to_string()
64        .context("Failed to read body.")?;
65
66    // Will output if request was made using Tor.
67    println!("Response: {}", response);
68
69    Ok(())
70}