Add native dynamic port forwarding
This commit is contained in:
@@ -182,6 +182,25 @@ fn connect_with_retry(port: u16, timeout: Duration, child: &mut Child) -> TcpStr
|
||||
}
|
||||
}
|
||||
|
||||
fn socks5_connect(port: u16, target_port: u16, child: &mut Child) -> TcpStream {
|
||||
let mut stream = connect_with_retry(port, Duration::from_secs(5), child);
|
||||
stream
|
||||
.set_read_timeout(Some(Duration::from_secs(3)))
|
||||
.unwrap();
|
||||
stream.write_all(&[5, 1, 0]).unwrap();
|
||||
let mut response = [0u8; 2];
|
||||
stream.read_exact(&mut response).unwrap();
|
||||
assert_eq!(response, [5, 0]);
|
||||
let mut request = vec![5, 1, 0, 1, 127, 0, 0, 1];
|
||||
request.extend_from_slice(&target_port.to_be_bytes());
|
||||
stream.write_all(&request).unwrap();
|
||||
let mut reply = [0u8; 10];
|
||||
stream.read_exact(&mut reply).unwrap();
|
||||
assert_eq!(reply[0], 5);
|
||||
assert_eq!(reply[1], 0);
|
||||
stream
|
||||
}
|
||||
|
||||
fn start_echo_server() -> u16 {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let port = listener.local_addr().unwrap().port();
|
||||
@@ -659,6 +678,55 @@ fn native_remote_forward_echo_smoke() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn native_dynamic_forward_socks_echo_smoke() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let port = free_udp_port();
|
||||
let config = write_server_config(&dir, port);
|
||||
write_native_client_auth(&dir, &config);
|
||||
let echo_port = start_echo_server();
|
||||
let socks_port = free_tcp_port();
|
||||
let mut server = start_server(&dir, &config);
|
||||
let client_bin = env!("CARGO_BIN_EXE_dosh-client");
|
||||
let mut client = Command::new(client_bin)
|
||||
.arg("--auth")
|
||||
.arg("native")
|
||||
.arg("--no-cache")
|
||||
.arg("-N")
|
||||
.arg("-D")
|
||||
.arg(socks_port.to_string())
|
||||
.arg("--session")
|
||||
.arg("default")
|
||||
.arg("--dosh-host")
|
||||
.arg("127.0.0.1")
|
||||
.arg("--dosh-port")
|
||||
.arg(port.to_string())
|
||||
.arg("local")
|
||||
.env("HOME", dir.path())
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()
|
||||
.unwrap();
|
||||
|
||||
let mut stream = socks5_connect(socks_port, echo_port, &mut client);
|
||||
stream.write_all(b"dosh-dynamic-forward-ping").unwrap();
|
||||
let mut buf = [0u8; 64];
|
||||
let n = stream.read(&mut buf).unwrap();
|
||||
|
||||
let _ = client.kill();
|
||||
let client_output = client.wait_with_output().unwrap();
|
||||
let _ = server.kill();
|
||||
let _ = server.wait();
|
||||
|
||||
assert_eq!(&buf[..n], b"dosh-dynamic-forward-ping");
|
||||
assert!(
|
||||
client_output.status.success() || client_output.status.code().is_none(),
|
||||
"stderr={}",
|
||||
String::from_utf8_lossy(&client_output.stderr)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ticket_attach_after_server_restart_smoke() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user