diff --git a/Cargo.lock b/Cargo.lock index 10e9598cc9..29bcab6479 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2415,6 +2415,20 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8eb564c5c7423d25c886fb561d1e4ee69f72354d16918afa32c08811f6b6a55" +[[package]] +name = "fast-socks5" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d09fe4a491909a716088083eeb5bcc25427330fdbcd4ecd3dfa5469b3da795df" +dependencies = [ + "anyhow", + "async-trait", + "log", + "thiserror 1.0.69", + "tokio", + "tokio-stream", +] + [[package]] name = "fastrand" version = "2.5.0" @@ -4465,6 +4479,7 @@ dependencies = [ "bson", "derive-where", "derive_more", + "fast-socks5", "futures-core", "futures-io", "futures-util", diff --git a/crates/dbx-core/Cargo.toml b/crates/dbx-core/Cargo.toml index aa6a7b8610..b6974bacba 100644 --- a/crates/dbx-core/Cargo.toml +++ b/crates/dbx-core/Cargo.toml @@ -49,7 +49,7 @@ tiberius = { version = "0.12.3", default-features = false, features = ["tds73", reqwest = { version = "0.12", default-features = false, features = ["json", "stream", "rustls-tls", "socks"] } prometheus-parse = "=0.2.5" futures = "0.3" -mongodb = "3.2.5" +mongodb = { version = "3.2.5", features = ["socks5-proxy"] } rumqttc = { version = "0.24", features = ["websocket"], optional = true } russh = "0.60" pageant = "0.2" diff --git a/crates/dbx-core/tests/mongodb_socks5_proxy.rs b/crates/dbx-core/tests/mongodb_socks5_proxy.rs new file mode 100644 index 0000000000..5bdf96023c --- /dev/null +++ b/crates/dbx-core/tests/mongodb_socks5_proxy.rs @@ -0,0 +1,68 @@ +use mongodb::options::ClientOptions; +use std::time::Duration; + +use dbx_core::db::mongo_driver; + +#[tokio::test] +async fn parses_socks5_proxy_host_and_port() { + let options = ClientOptions::parse("mongodb://localhost/?proxyHost=127.0.0.1&proxyPort=1080") + .await + .expect("MongoDB connection strings should accept SOCKS5 proxy options"); + + let proxy = options.socks5_proxy.expect("SOCKS5 proxy options"); + assert_eq!(proxy.host, "127.0.0.1"); + assert_eq!(proxy.port, Some(1080)); + assert_eq!(proxy.authentication, None); +} + +#[tokio::test] +async fn parses_socks5_proxy_credentials() { + let options = + ClientOptions::parse("mongodb://localhost/?proxyHost=proxy.example.com&proxyUsername=dbx&proxyPassword=secret") + .await + .expect("MongoDB connection strings should accept SOCKS5 proxy credentials"); + + let proxy = options.socks5_proxy.expect("SOCKS5 proxy options"); + assert_eq!(proxy.host, "proxy.example.com"); + assert_eq!(proxy.port, None); + assert_eq!(proxy.authentication, Some(("dbx".to_string(), "secret".to_string()))); +} + +#[tokio::test] +async fn rejects_socks5_proxy_port_without_host() { + let error = ClientOptions::parse("mongodb://localhost/?proxyPort=1080") + .await + .expect_err("proxyPort without proxyHost should remain invalid"); + + let message = error.to_string(); + assert!( + message.to_ascii_lowercase().contains("proxyport cannot be set if proxyhost is unspecified"), + "unexpected error: {message}" + ); +} + +#[tokio::test] +async fn leaves_direct_connections_without_a_proxy() { + let options = ClientOptions::parse("mongodb://localhost:27017/dbx") + .await + .expect("ordinary MongoDB connection strings should remain valid"); + + assert!(options.socks5_proxy.is_none()); +} + +#[tokio::test] +#[ignore = "requires DBX_LIVE_MONGODB_SOCKS5_URL routed through a SOCKS5 proxy"] +async fn connects_to_mongodb_through_socks5_proxy() { + let url = std::env::var("DBX_LIVE_MONGODB_SOCKS5_URL").expect("DBX_LIVE_MONGODB_SOCKS5_URL"); + let client = mongo_driver::connect(&url, Duration::from_secs(5), Duration::from_secs(60)) + .await + .expect("connect to MongoDB through SOCKS5"); + + mongo_driver::test_connection(&client, Duration::from_secs(5), Some("admin")) + .await + .expect("ping MongoDB through SOCKS5"); + let version = mongo_driver::server_version(&client, "admin").await.expect("MongoDB buildInfo through SOCKS5"); + if let Ok(expected) = std::env::var("DBX_LIVE_MONGODB_EXPECTED_VERSION") { + assert_eq!(version, expected); + } +} diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 44fc8b94bb..9da0789735 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -52,7 +52,7 @@ tiberius = { version = "0.12.3", default-features = false, features = ["tds73", tokio-util = { version = "0.7", features = ["compat"] } reqwest = { version = "0.12", features = ["json", "stream"] } futures = "0.3" -mongodb = "3.2.5" +mongodb = { version = "3.2.5", features = ["socks5-proxy"] } percent-encoding = "2" minisign-verify = "0.2.5" semver = "1"