Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_proxy_connection: make test more robust #390

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 47 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ rstest = "0.23.0"
serial_test = "3.2.0"
derive_more = { version = "1.0.0", features = ["from"] }

[target.'cfg(target_os = "macos")'.dev-dependencies]
get_if_addrs = "0.5.3"


[profile.release]
lto = "fat"
panic = "abort"
Expand Down
29 changes: 22 additions & 7 deletions src/protocols/tcp/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use tracing::{debug, instrument};
use url::{Host, Url};

pub fn configure_socket(socket: SockRef, so_mark: &Option<u32>) -> Result<(), anyhow::Error> {

Check warning on line 21 in src/protocols/tcp/server.rs

View workflow job for this annotation

GitHub Actions / Build - MacOS x86_64

unused variable: `so_mark`

Check warning on line 21 in src/protocols/tcp/server.rs

View workflow job for this annotation

GitHub Actions / Build - MacOS aarch64

unused variable: `so_mark`

Check warning on line 21 in src/protocols/tcp/server.rs

View workflow job for this annotation

GitHub Actions / Build - Freebsd x86_64

unused variable: `so_mark`

Check warning on line 21 in src/protocols/tcp/server.rs

View workflow job for this annotation

GitHub Actions / Build - Freebsd x86

unused variable: `so_mark`

Check warning on line 21 in src/protocols/tcp/server.rs

View workflow job for this annotation

GitHub Actions / Build - Android aarch64

unused variable: `so_mark`

Check warning on line 21 in src/protocols/tcp/server.rs

View workflow job for this annotation

GitHub Actions / Build - Android armv7

unused variable: `so_mark`

Check warning on line 21 in src/protocols/tcp/server.rs

View workflow job for this annotation

GitHub Actions / Build - Windows x86_64

unused variable: `so_mark`

Check warning on line 21 in src/protocols/tcp/server.rs

View workflow job for this annotation

GitHub Actions / Build - Windows x86

unused variable: `so_mark`
socket
.set_nodelay(true)
.with_context(|| format!("cannot set no_delay on socket: {:?}", io::Error::last_os_error()))?;
Expand Down Expand Up @@ -212,7 +212,7 @@
Ok(socket)
}

pub async fn run_server(bind: SocketAddr, ip_transparent: bool) -> Result<TcpListenerStream, anyhow::Error> {

Check warning on line 215 in src/protocols/tcp/server.rs

View workflow job for this annotation

GitHub Actions / Build - MacOS x86_64

unused variable: `ip_transparent`

Check warning on line 215 in src/protocols/tcp/server.rs

View workflow job for this annotation

GitHub Actions / Build - MacOS aarch64

unused variable: `ip_transparent`

Check warning on line 215 in src/protocols/tcp/server.rs

View workflow job for this annotation

GitHub Actions / Build - Freebsd x86_64

unused variable: `ip_transparent`

Check warning on line 215 in src/protocols/tcp/server.rs

View workflow job for this annotation

GitHub Actions / Build - Freebsd x86

unused variable: `ip_transparent`

Check warning on line 215 in src/protocols/tcp/server.rs

View workflow job for this annotation

GitHub Actions / Build - Android aarch64

unused variable: `ip_transparent`

Check warning on line 215 in src/protocols/tcp/server.rs

View workflow job for this annotation

GitHub Actions / Build - Android armv7

unused variable: `ip_transparent`

Check warning on line 215 in src/protocols/tcp/server.rs

View workflow job for this annotation

GitHub Actions / Build - Windows x86_64

unused variable: `ip_transparent`

Check warning on line 215 in src/protocols/tcp/server.rs

View workflow job for this annotation

GitHub Actions / Build - Windows x86

unused variable: `ip_transparent`
info!("Starting TCP server listening cnx on {}", bind);

let listener = TcpListener::bind(bind)
Expand All @@ -233,7 +233,7 @@
use super::*;
use futures_util::pin_mut;
use std::borrow::Cow;
use std::net::SocketAddr;
use std::net::IpAddr;
use testcontainers::core::WaitFor;
use testcontainers::runners::AsyncRunner;
use testcontainers::{ContainerAsync, Image, ImageExt};
Expand Down Expand Up @@ -263,15 +263,30 @@

#[tokio::test]
async fn test_proxy_connection() {
let server_addr: SocketAddr = "[::1]:1236".parse().unwrap();
let server = TcpListener::bind(server_addr).await.unwrap();
let (network_name, host) = if cfg!(not(target_os = "macos")) {
("host", "127.0.0.1".parse::<IpAddr>().unwrap())
} else {
let host = get_if_addrs::get_if_addrs()
.unwrap()
.into_iter()
.map(|iface| iface.addr.ip())
.find(|ip| ip.is_ipv4() && !ip.is_loopback())
.unwrap();
("wstunnel_test_proxy_connection", host)
};

let mitm_proxy: ContainerAsync<MitmProxy> = MitmProxy.with_network(network_name).start().await.unwrap();

let proxy_port = mitm_proxy.get_host_port_ipv4(8080).await.unwrap();

let _mitm_proxy: ContainerAsync<MitmProxy> = MitmProxy.with_network("host".to_string()).start().await.unwrap();
// bind to a dynamic port - avoid conflicts
let server = TcpListener::bind((host, 0)).await.unwrap();
let server_port = server.local_addr().unwrap().port();

let mut client = connect_with_http_proxy(
&"http://localhost:8080".parse().unwrap(),
&Host::Domain("[::1]".to_string()),
1236,
&Url::parse(&format!("http://127.0.0.1:{proxy_port}")).unwrap(),
&Host::Domain(host.to_string()),
server_port,
None,
Duration::from_secs(1),
&DnsResolver::System,
Expand Down
Loading