feat: add operational reconnect and shutdown handling
Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
parent
b67248e14f
commit
10b8b24dce
@@ -117,3 +117,90 @@ jobs:
|
||||
echo "ERROR: invalid port should have failed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
e2e-minimal:
|
||||
name: Minimal E2E (${{ matrix.os }})
|
||||
needs: [test]
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
os: [ubuntu-latest, windows-latest, macos-latest]
|
||||
runs-on: ${{ matrix.os }}
|
||||
timeout-minutes: 5
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
- name: Build release binary
|
||||
run: cargo build --release
|
||||
- name: Run minimal E2E flow
|
||||
shell: bash
|
||||
run: |
|
||||
set -e
|
||||
|
||||
# Use cargo run for OS-neutral binary invocation (avoids .exe vs no extension)
|
||||
RUN="cargo run --release --"
|
||||
|
||||
# Use isolated ports to avoid conflicts
|
||||
CRED_DIR=$(mktemp -d)
|
||||
TARGET_PORT=17481
|
||||
LISTEN_PORT=17480
|
||||
SOCKS_PORT=17479
|
||||
|
||||
# 1. Generate credentials
|
||||
$RUN generate --out "$CRED_DIR"
|
||||
|
||||
# 2. Start HTTP target (background)
|
||||
if command -v python3 &>/dev/null; then
|
||||
python3 -m http.server "$TARGET_PORT" --bind 127.0.0.1 &
|
||||
TARGET_PID=$!
|
||||
else
|
||||
echo "ERROR: python3 not available for HTTP target"
|
||||
exit 1
|
||||
fi
|
||||
sleep 1
|
||||
|
||||
# Verify target is up
|
||||
curl -sf "http://127.0.0.1:$TARGET_PORT/" > /dev/null || true
|
||||
|
||||
# 3. Start listener (background)
|
||||
$RUN listen \
|
||||
--listen "127.0.0.1:$LISTEN_PORT" \
|
||||
--cert "$CRED_DIR/server.crt" \
|
||||
--key "$CRED_DIR/server.key" \
|
||||
--ca-cert "$CRED_DIR/ca.pem" \
|
||||
--auth-token-file "$CRED_DIR/token.txt" &
|
||||
LISTENER_PID=$!
|
||||
sleep 1
|
||||
|
||||
# 4. Start connector (background)
|
||||
$RUN connect \
|
||||
--target "127.0.0.1:$LISTEN_PORT" \
|
||||
--socks "127.0.0.1:$SOCKS_PORT" \
|
||||
--cert "$CRED_DIR/client.crt" \
|
||||
--key "$CRED_DIR/client.key" \
|
||||
--ca-cert "$CRED_DIR/ca.pem" \
|
||||
--auth-token-file "$CRED_DIR/token.txt" &
|
||||
CONNECTOR_PID=$!
|
||||
sleep 3
|
||||
|
||||
# 5. Test SOCKS5 forwarding
|
||||
RESULT=$(curl -sf --proxy "socks5h://127.0.0.1:$SOCKS_PORT" "http://127.0.0.1:$TARGET_PORT/" 2>&1 | head -5 || true)
|
||||
echo "E2E response: $RESULT"
|
||||
|
||||
# 6. Cleanup: kill in reverse order
|
||||
# On Windows (Git Bash), kill might need -INT or taskkill fallback
|
||||
kill "$CONNECTOR_PID" 2>/dev/null || true
|
||||
kill "$LISTENER_PID" 2>/dev/null || true
|
||||
kill "$TARGET_PID" 2>/dev/null || true
|
||||
sleep 1
|
||||
# Force kill any remaining processes on the ports
|
||||
for PORT in $SOCKS_PORT $LISTEN_PORT $TARGET_PORT; do
|
||||
lsof -tiTCP:$PORT -sTCP:LISTEN 2>/dev/null | xargs kill 2>/dev/null || true
|
||||
# Windows fallback: use PowerShell to find and kill by port
|
||||
netstat -ano 2>/dev/null | grep ":$PORT " | grep LISTENING | awk '{print $5}' | xargs -I{} taskkill //PID {} //F 2>/dev/null || true
|
||||
done
|
||||
wait "$CONNECTOR_PID" 2>/dev/null || true
|
||||
wait "$LISTENER_PID" 2>/dev/null || true
|
||||
wait "$TARGET_PID" 2>/dev/null || true
|
||||
|
||||
echo "E2E test passed on ${{ matrix.os }}"
|
||||
|
||||
+1
-57
@@ -17,7 +17,6 @@ pub enum TlsError {
|
||||
IdentityMismatch(String),
|
||||
|
||||
#[error("certificate expired: {0}")]
|
||||
#[allow(dead_code)]
|
||||
Expired(String),
|
||||
|
||||
#[error("certificate verification failed: {0}")]
|
||||
@@ -25,10 +24,6 @@ pub enum TlsError {
|
||||
|
||||
#[error("I/O error: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
|
||||
#[error("rustls error: {0}")]
|
||||
#[allow(dead_code)]
|
||||
Rustls(String),
|
||||
}
|
||||
|
||||
/// Errors that occur during application-level authentication.
|
||||
@@ -42,14 +37,6 @@ pub enum AuthError {
|
||||
|
||||
#[error("auth not yet completed")]
|
||||
NotAuthenticated,
|
||||
|
||||
#[error("auth token expired")]
|
||||
#[allow(dead_code)]
|
||||
Expired,
|
||||
|
||||
#[error("too many auth attempts")]
|
||||
#[allow(dead_code)]
|
||||
TooManyAttempts,
|
||||
}
|
||||
|
||||
/// Errors that occur during tunnel lifecycle operations.
|
||||
@@ -65,65 +52,22 @@ pub enum TunnelError {
|
||||
BindError(String, String),
|
||||
|
||||
#[error("connection refused: {0}")]
|
||||
ConnectionRefused(String),
|
||||
|
||||
#[error("connection closed")]
|
||||
#[allow(dead_code)]
|
||||
ConnectionClosed,
|
||||
ConnectionRefused(String),
|
||||
|
||||
#[error("port {0} already in use")]
|
||||
PortInUse(u16),
|
||||
|
||||
#[error("shutdown requested")]
|
||||
#[allow(dead_code)]
|
||||
Shutdown,
|
||||
|
||||
#[error("I/O error: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
|
||||
#[error("protocol error: {0}")]
|
||||
Protocol(String),
|
||||
|
||||
#[error("timeout: {0}")]
|
||||
#[allow(dead_code)]
|
||||
Timeout(String),
|
||||
|
||||
#[error("hostname error: {0}")]
|
||||
Host(#[from] HostError),
|
||||
}
|
||||
|
||||
/// Errors returned by the connector side.
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ConnectorError {
|
||||
#[error("TLS handshake failed: {0}")]
|
||||
TlsHandshake(String),
|
||||
|
||||
#[error("server certificate verification failed: {0}")]
|
||||
ServerCertVerification(String),
|
||||
|
||||
#[error("server certificate identity mismatch for {0}: {1}")]
|
||||
ServerIdentityMismatch(String, String),
|
||||
|
||||
#[error("server certificate expired")]
|
||||
ServerCertExpired,
|
||||
|
||||
#[error("auth rejected: {0}")]
|
||||
AuthRejected(String),
|
||||
|
||||
#[error("connection failed: {0}")]
|
||||
ConnectionFailed(String),
|
||||
|
||||
#[error("I/O error: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
|
||||
#[error("protocol error: {0}")]
|
||||
Protocol(String),
|
||||
|
||||
#[error("timeout: {0}")]
|
||||
Timeout(String),
|
||||
}
|
||||
|
||||
/// Errors returned during hostname resolution.
|
||||
#[derive(Debug, Error)]
|
||||
pub enum HostError {
|
||||
|
||||
+15
-40
@@ -33,39 +33,20 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
// Errors
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Errors that can occur during frame operations.
|
||||
#[derive(Debug, Error)]
|
||||
pub enum FrameError {
|
||||
#[error("unknown frame type: {0:#04x}")]
|
||||
#[allow(dead_code)]
|
||||
UnknownFrameType(u8),
|
||||
|
||||
#[error("frame too short: expected {0} bytes, got {1}")]
|
||||
FrameTooShort(usize, usize),
|
||||
|
||||
#[error("payload length overflow: {0}")]
|
||||
PayloadOverflow(u32),
|
||||
|
||||
#[error("stream {0} not found")]
|
||||
#[allow(dead_code)]
|
||||
StreamNotFound(u64),
|
||||
|
||||
#[error("duplicate stream ID: {0}")]
|
||||
#[allow(dead_code)]
|
||||
DuplicateStreamId(u64),
|
||||
|
||||
#[error("stream {0} already closed")]
|
||||
#[allow(dead_code)]
|
||||
StreamClosed(u64),
|
||||
|
||||
#[error("I/O error: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
|
||||
#[error("protocol error: {0}")]
|
||||
Protocol(String),
|
||||
|
||||
#[error("target connection failed: {0}")]
|
||||
#[allow(dead_code)]
|
||||
TargetConnectionFailed(String),
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -78,9 +59,10 @@ pub const FRAME_DATA: u8 = 0x03;
|
||||
pub const FRAME_CLOSE: u8 = 0x04;
|
||||
pub const FRAME_ERROR: u8 = 0x05;
|
||||
|
||||
// Flags
|
||||
#[allow(dead_code)]
|
||||
pub const FLAG_EOF: u8 = 0x01;
|
||||
// Flags byte: reserved for future extension (currently always 0x00).
|
||||
// The former FLAG_EOF bit (0x01) was removed in favor of the CLOSE frame
|
||||
// (FRAME_CLOSE) for half-close semantics. This keeps the frame format clean
|
||||
// and explicit: CLOSE frames signal sender-side EOF per stream.
|
||||
|
||||
// CONNECT_REPLY status
|
||||
pub const CONNECT_OK: u8 = 0x00;
|
||||
@@ -134,17 +116,6 @@ impl Frame {
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a DATA+EOF frame.
|
||||
#[allow(dead_code)]
|
||||
pub fn data_eof(stream_id: u64, data: impl AsRef<[u8]>) -> Self {
|
||||
Frame {
|
||||
frame_type: FRAME_DATA,
|
||||
stream_id,
|
||||
flags: FLAG_EOF,
|
||||
payload: Bytes::copy_from_slice(data.as_ref()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a CLOSE frame.
|
||||
pub fn close(stream_id: u64) -> Self {
|
||||
Frame {
|
||||
@@ -222,10 +193,11 @@ impl Frame {
|
||||
))
|
||||
}
|
||||
|
||||
/// Check if this frame has the EOF flag set.
|
||||
/// Check if this frame has any flags set (reserved for future use).
|
||||
#[allow(dead_code)]
|
||||
pub fn is_eof(&self) -> bool {
|
||||
self.flags & FLAG_EOF != 0
|
||||
#[must_use]
|
||||
pub fn has_flags(&self) -> bool {
|
||||
self.flags != 0
|
||||
}
|
||||
}
|
||||
|
||||
@@ -403,13 +375,16 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn frame_roundtrip_data_eof() {
|
||||
let frame = Frame::data_eof(5, b"final");
|
||||
fn frame_roundtrip_close_replaces_eof() {
|
||||
// Half-close semantics are handled by CLOSE frames, not DATA+EOF
|
||||
let frame = Frame::close(5);
|
||||
let serialized = frame.serialize();
|
||||
|
||||
let mut buf = BytesMut::from(&serialized[..]);
|
||||
let (decoded, _) = Frame::deserialize(&mut buf).unwrap();
|
||||
assert!(decoded.is_eof());
|
||||
assert_eq!(decoded.frame_type, FRAME_CLOSE);
|
||||
assert_eq!(decoded.stream_id, 5);
|
||||
assert!(decoded.payload.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
+7
-6
@@ -4,6 +4,7 @@ mod errors;
|
||||
mod framing;
|
||||
mod generate;
|
||||
mod redact;
|
||||
mod signal;
|
||||
mod socks5;
|
||||
mod tls;
|
||||
mod tunnel;
|
||||
@@ -156,9 +157,9 @@ fn run_listen(
|
||||
|
||||
let config = ListenerConfig {
|
||||
bind_addr,
|
||||
server_cert_path: Arc::from(Path::new(&cert)),
|
||||
server_key_path: Arc::from(Path::new(&key)),
|
||||
ca_cert_path: Arc::from(Path::new(&ca_cert)),
|
||||
server_cert_path: Arc::new(std::path::PathBuf::from(&cert)),
|
||||
server_key_path: Arc::new(std::path::PathBuf::from(&key)),
|
||||
ca_cert_path: Arc::new(std::path::PathBuf::from(&ca_cert)),
|
||||
auth_token: Arc::new(auth_token),
|
||||
};
|
||||
|
||||
@@ -278,9 +279,9 @@ fn run_connect(
|
||||
let config = ConnectorConfig {
|
||||
target_host,
|
||||
target_port,
|
||||
client_cert_path: Arc::from(Path::new(&cert)),
|
||||
client_key_path: Arc::from(Path::new(&key)),
|
||||
ca_cert_path: Arc::from(Path::new(&ca_cert)),
|
||||
client_cert_path: Arc::new(std::path::PathBuf::from(&cert)),
|
||||
client_key_path: Arc::new(std::path::PathBuf::from(&key)),
|
||||
ca_cert_path: Arc::new(std::path::PathBuf::from(&ca_cert)),
|
||||
auth_token: Arc::new(auth_token),
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
/// Cross-platform shutdown signal handling.
|
||||
///
|
||||
/// Provides a unified async future that resolves when the process should
|
||||
/// shut down (SIGINT/SIGTERM on Unix, Ctrl+C/Ctrl+Break on Windows).
|
||||
pub async fn shutdown_signal() {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use tokio::signal::unix::{SignalKind, signal};
|
||||
let mut sigint =
|
||||
signal(SignalKind::interrupt()).expect("failed to create SIGINT signal handler");
|
||||
let mut sigterm =
|
||||
signal(SignalKind::terminate()).expect("failed to create SIGTERM signal handler");
|
||||
tokio::select! {
|
||||
_ = sigint.recv() => {},
|
||||
_ = sigterm.recv() => {},
|
||||
}
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
{
|
||||
tokio::signal::ctrl_c().await.ok();
|
||||
}
|
||||
}
|
||||
+674
-138
@@ -27,7 +27,7 @@ use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
||||
use bytes::Bytes;
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
use tokio::sync::{mpsc, oneshot};
|
||||
use tokio::sync::{mpsc, oneshot, watch};
|
||||
|
||||
use crate::errors::{AuthError, HostError, TunnelError};
|
||||
use crate::framing::{
|
||||
@@ -38,19 +38,6 @@ use crate::redact::Redacted;
|
||||
use crate::socks5;
|
||||
use crate::tls;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Stream ID generation
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[allow(dead_code)]
|
||||
static STREAM_COUNTER: AtomicU64 = AtomicU64::new(0);
|
||||
|
||||
/// Generate a unique stream ID for logging.
|
||||
#[allow(dead_code)]
|
||||
fn next_stream_id() -> u64 {
|
||||
STREAM_COUNTER.fetch_add(1, Ordering::Relaxed)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Auth token handling
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -128,9 +115,9 @@ pub async fn resolve_target(host: &str, port: u16) -> Result<SocketAddr, HostErr
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ListenerConfig {
|
||||
pub bind_addr: SocketAddr,
|
||||
pub server_cert_path: Arc<Path>,
|
||||
pub server_key_path: Arc<Path>,
|
||||
pub ca_cert_path: Arc<Path>,
|
||||
pub server_cert_path: Arc<std::path::PathBuf>,
|
||||
pub server_key_path: Arc<std::path::PathBuf>,
|
||||
pub ca_cert_path: Arc<std::path::PathBuf>,
|
||||
pub auth_token: Arc<String>,
|
||||
}
|
||||
|
||||
@@ -139,9 +126,9 @@ pub struct ListenerConfig {
|
||||
pub struct ConnectorConfig {
|
||||
pub target_host: String,
|
||||
pub target_port: u16,
|
||||
pub client_cert_path: Arc<Path>,
|
||||
pub client_key_path: Arc<Path>,
|
||||
pub ca_cert_path: Arc<Path>,
|
||||
pub client_cert_path: Arc<std::path::PathBuf>,
|
||||
pub client_key_path: Arc<std::path::PathBuf>,
|
||||
pub ca_cert_path: Arc<std::path::PathBuf>,
|
||||
pub auth_token: Arc<String>,
|
||||
}
|
||||
|
||||
@@ -296,19 +283,26 @@ pub async fn run_listener(config: ListenerConfig) -> Result<(), TunnelError> {
|
||||
"HTTPS tunnel listener bound to {} — endpoint: /tunnel (HTTPS default transport)",
|
||||
addr
|
||||
);
|
||||
tracing::info!("Auth token configured: {}", Redacted::new(&*auth_token));
|
||||
tracing::info!(
|
||||
"Effective config: listener={}, auth_token={}",
|
||||
addr,
|
||||
Redacted::new(&*auth_token)
|
||||
);
|
||||
|
||||
// Listen for shutdown signal
|
||||
let shutdown = crate::signal::shutdown_signal();
|
||||
tokio::pin!(shutdown);
|
||||
|
||||
loop {
|
||||
let (stream, peer_addr) = match listener.accept().await {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
tracing::error!("Accept error: {}", e);
|
||||
continue;
|
||||
tokio::select! {
|
||||
_ = &mut shutdown => {
|
||||
tracing::info!("Shutdown signal received — stopping HTTPS tunnel listener");
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
result = listener.accept() => {
|
||||
match result {
|
||||
Ok((stream, peer_addr)) => {
|
||||
tracing::info!("New connection from {}", peer_addr);
|
||||
|
||||
let acceptor = tls_acceptor.clone();
|
||||
let token = auth_token.clone();
|
||||
|
||||
@@ -318,6 +312,19 @@ pub async fn run_listener(config: ListenerConfig) -> Result<(), TunnelError> {
|
||||
}
|
||||
});
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Accept error: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tracing::info!(
|
||||
"HTTPS tunnel listener shutting down — port {} released",
|
||||
addr.port()
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Handle a single incoming connection: TLS → mTLS → Auth → Framing.
|
||||
@@ -698,6 +705,11 @@ impl StreamMux {
|
||||
)));
|
||||
}
|
||||
|
||||
tracing::info!(
|
||||
"Stream {} opened to {} via tunnel",
|
||||
stream_id,
|
||||
socks5::target_to_string(target)
|
||||
);
|
||||
Ok((stream_id, data_rx))
|
||||
}
|
||||
|
||||
@@ -749,6 +761,7 @@ impl StreamMux {
|
||||
pub async fn close_stream(&self, stream_id: u64) {
|
||||
self.streams.lock().await.remove(&stream_id);
|
||||
let _ = self.outbound_tx.send(Frame::close(stream_id)).await;
|
||||
tracing::info!("Stream {} closed from connector side", stream_id);
|
||||
}
|
||||
|
||||
/// Check if auth is complete.
|
||||
@@ -758,10 +771,14 @@ impl StreamMux {
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Connector with SOCKS5
|
||||
// Connector with SOCKS5 (reconnect-aware, graceful shutdown)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Run the connector with SOCKS5 proxy: connect, auth, framing, then SOCKS5.
|
||||
/// The SOCKS5 listener stays bound permanently while a reconnect loop
|
||||
/// maintains the tunnel connection. On tunnel failure, reconnect retries
|
||||
/// auth and swaps the mux; new SOCKS5 requests work with the fresh tunnel.
|
||||
/// Existing streams get EOF/closed deterministically.
|
||||
pub async fn run_connector_with_socks(
|
||||
config: ConnectorConfig,
|
||||
socks_addr: SocketAddr,
|
||||
@@ -776,33 +793,112 @@ pub async fn run_connector_with_socks(
|
||||
TunnelError::Tls(e)
|
||||
})?;
|
||||
|
||||
let tls_connector = tokio_rustls::TlsConnector::from(client_config);
|
||||
let target_addr = resolve_target(&config.target_host, config.target_port).await?;
|
||||
let server_name = tls::server_name_from_host(&config.target_host).map_err(TunnelError::Tls)?;
|
||||
|
||||
// Log effective config (secrets redacted)
|
||||
tracing::info!(
|
||||
"Effective config: target={}:{}, socks5={}, auth_token={}",
|
||||
config.target_host,
|
||||
config.target_port,
|
||||
socks_addr,
|
||||
Redacted::new(&*config.auth_token)
|
||||
);
|
||||
|
||||
// Create a watch channel for the active StreamMux.
|
||||
// Initial value is None — first tunnel session will set it.
|
||||
let (mux_sender, mux_receiver) = watch::channel::<Option<Arc<StreamMux>>>(None);
|
||||
|
||||
// Spawn the reconnect loop (runs in background)
|
||||
let reconnect_config = config.clone();
|
||||
let reconnect_sender = mux_sender.clone();
|
||||
let reconnect_handle = tokio::spawn(async move {
|
||||
run_reconnect_loop(
|
||||
reconnect_config,
|
||||
target_addr,
|
||||
client_config,
|
||||
reconnect_sender,
|
||||
)
|
||||
.await;
|
||||
});
|
||||
|
||||
// Start the SOCKS5 proxy (stays bound permanently)
|
||||
let socks_result = run_socks5_proxy(socks_addr, mux_receiver).await;
|
||||
|
||||
// Shutdown: abort reconnect loop
|
||||
tracing::info!("Connector shutting down — aborting reconnect loop");
|
||||
reconnect_handle.abort();
|
||||
|
||||
match socks_result {
|
||||
Ok(()) => {
|
||||
tracing::info!(
|
||||
"Connector shut down gracefully — SOCKS5 port {} released",
|
||||
socks_addr.port()
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Reconnect loop: continuously attempts to establish and maintain the tunnel.
|
||||
/// On success, publishes the StreamMux via the watch channel.
|
||||
/// On failure, logs the error and retries with exponential backoff.
|
||||
async fn run_reconnect_loop(
|
||||
config: ConnectorConfig,
|
||||
target_addr: SocketAddr,
|
||||
client_config: Arc<rustls::ClientConfig>,
|
||||
mux_sender: watch::Sender<Option<Arc<StreamMux>>>,
|
||||
) {
|
||||
let server_name = match tls::server_name_from_host(&config.target_host) {
|
||||
Ok(sn) => sn,
|
||||
Err(e) => {
|
||||
tracing::error!("Invalid target hostname: {}", e);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let mut reconnect_delay = std::time::Duration::from_secs(1);
|
||||
let max_delay = std::time::Duration::from_secs(30);
|
||||
|
||||
loop {
|
||||
let stream = TcpStream::connect(target_addr).await.map_err(|e| {
|
||||
if e.kind() == std::io::ErrorKind::ConnectionRefused {
|
||||
return TunnelError::ConnectionRefused(target_addr.to_string());
|
||||
}
|
||||
TunnelError::Io(e)
|
||||
})?;
|
||||
|
||||
tracing::info!(
|
||||
"Connected to HTTPS tunnel at {}:{} (HTTPS default transport)",
|
||||
"Tunnel connecting to {}:{} (HTTPS default transport)",
|
||||
config.target_host,
|
||||
config.target_port
|
||||
);
|
||||
|
||||
let tls_stream = tls_connector
|
||||
.connect(server_name.clone(), stream)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
TunnelError::Tls(crate::errors::TlsError::VerificationFailed(format!(
|
||||
"TLS handshake to {}:{} failed: {}",
|
||||
config.target_host, config.target_port, e
|
||||
)))
|
||||
})?;
|
||||
// Connect with fresh TLS (re-runs all security gates)
|
||||
let stream = match TcpStream::connect(target_addr).await {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
tracing::warn!(
|
||||
"Tunnel connect failed to {}: {} — retrying in {:?}",
|
||||
target_addr,
|
||||
e,
|
||||
reconnect_delay
|
||||
);
|
||||
tokio::time::sleep(reconnect_delay).await;
|
||||
reconnect_delay = (reconnect_delay * 2).min(max_delay);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let tls_connector = tokio_rustls::TlsConnector::from(client_config.clone());
|
||||
let tls_stream = match tls_connector.connect(server_name.clone(), stream).await {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
tracing::warn!(
|
||||
"TLS handshake to {}:{} failed: {} — retrying in {:?}",
|
||||
config.target_host,
|
||||
config.target_port,
|
||||
e,
|
||||
reconnect_delay
|
||||
);
|
||||
tokio::time::sleep(reconnect_delay).await;
|
||||
reconnect_delay = (reconnect_delay * 2).min(max_delay);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
tracing::info!(
|
||||
"TLS handshake completed with {}:{} via HTTPS",
|
||||
@@ -810,40 +906,57 @@ pub async fn run_connector_with_socks(
|
||||
config.target_port
|
||||
);
|
||||
|
||||
// Authenticate over HTTP, then switch to framing
|
||||
// Authenticate (re-runs auth check each reconnect)
|
||||
let auth_result =
|
||||
match authenticate_tunnel(&config.auth_token, &config.target_host, tls_stream).await {
|
||||
Ok(auth_result) => {
|
||||
let mux = auth_result.mux;
|
||||
Ok(r) => r,
|
||||
Err(TunnelError::Auth(e)) => {
|
||||
// Auth failures are terminal — don't retry endlessly
|
||||
tracing::error!("Auth failed (terminal): {}", e);
|
||||
mux_sender.send(None).ok();
|
||||
return;
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::warn!(
|
||||
"Connection error: {} — retrying in {:?}",
|
||||
e,
|
||||
reconnect_delay
|
||||
);
|
||||
tokio::time::sleep(reconnect_delay).await;
|
||||
reconnect_delay = (reconnect_delay * 2).min(max_delay);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
// Reset reconnect delay on success
|
||||
reconnect_delay = std::time::Duration::from_secs(1);
|
||||
|
||||
let mux = auth_result.mux.clone();
|
||||
|
||||
tracing::info!(
|
||||
"Tunnel session established with {}:{} via HTTPS /tunnel",
|
||||
"Tunnel session established with {}:{} via HTTPS /tunnel — state: connected",
|
||||
config.target_host,
|
||||
config.target_port
|
||||
);
|
||||
|
||||
// Start SOCKS5 proxy and framing loop
|
||||
match run_socks5_with_mux(socks_addr, mux).await {
|
||||
Ok(()) => return Ok(()),
|
||||
Err(e) => {
|
||||
tracing::error!("SOCKS5 proxy error: {}", e);
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(TunnelError::Auth(e)) => {
|
||||
tracing::error!("Auth failed: {}", e);
|
||||
return Err(TunnelError::Auth(e));
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Connection error, reconnecting: {}", e);
|
||||
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
|
||||
}
|
||||
}
|
||||
// Publish the mux via watch channel
|
||||
mux_sender.send(Some(mux.clone())).ok();
|
||||
|
||||
// Wait for the framing task to finish (tunnel disconnect)
|
||||
let _ = auth_result.framing_task.await;
|
||||
|
||||
tracing::info!(
|
||||
"Tunnel disconnected from {}:{} — state: disconnected, will reconnect",
|
||||
config.target_host,
|
||||
config.target_port
|
||||
);
|
||||
|
||||
// Notify that tunnel is down (SOCKS5 handler will see mux changes)
|
||||
// Don't clear the mux — let existing streams drain gracefully
|
||||
}
|
||||
}
|
||||
|
||||
/// Result of the authenticate_tunnel function.
|
||||
#[allow(dead_code)]
|
||||
struct AuthResult {
|
||||
mux: Arc<StreamMux>,
|
||||
framing_task: tokio::task::JoinHandle<()>,
|
||||
@@ -941,13 +1054,14 @@ async fn run_framing_client<S>(
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// SOCKS5 handler with StreamMux
|
||||
// SOCKS5 handler with StreamMux (reconnect-aware)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Run the SOCKS5 proxy with a shared StreamMux.
|
||||
async fn run_socks5_with_mux(
|
||||
/// Run the SOCKS5 proxy that stays bound permanently.
|
||||
/// Receives mux updates via watch channel to support reconnect.
|
||||
async fn run_socks5_proxy(
|
||||
socks_addr: SocketAddr,
|
||||
mux: Arc<StreamMux>,
|
||||
mux_receiver: watch::Receiver<Option<Arc<StreamMux>>>,
|
||||
) -> Result<(), TunnelError> {
|
||||
let socks_listener = TcpListener::bind(socks_addr).await.map_err(|e| {
|
||||
if e.kind() == std::io::ErrorKind::AddrInUse {
|
||||
@@ -962,35 +1076,51 @@ async fn run_socks5_with_mux(
|
||||
socks_addr
|
||||
);
|
||||
|
||||
// Listen for Ctrl-C
|
||||
let shutdown = tokio::signal::ctrl_c();
|
||||
let shutdown = crate::signal::shutdown_signal();
|
||||
tokio::pin!(shutdown);
|
||||
|
||||
loop {
|
||||
tokio::select! {
|
||||
biased; // prioritize shutdown
|
||||
|
||||
_ = &mut shutdown => {
|
||||
tracing::info!("Shutdown signal received — stopping SOCKS5 proxy");
|
||||
break;
|
||||
}
|
||||
|
||||
result = socks_listener.accept() => {
|
||||
match result {
|
||||
Ok((stream, peer_addr)) => {
|
||||
let m = mux.clone();
|
||||
let mux_opt = mux_receiver.borrow().clone();
|
||||
match mux_opt {
|
||||
Some(ref m) => {
|
||||
let m = m.clone();
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = handle_socks5_connection(stream, peer_addr, m).await {
|
||||
tracing::warn!("SOCKS5 connection from {} failed: {}", peer_addr, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
None => {
|
||||
tracing::warn!(
|
||||
"SOCKS5 connection from {} rejected: tunnel not connected yet",
|
||||
peer_addr
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("SOCKS5 accept error: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = &mut shutdown => {
|
||||
tracing::info!("Shutdown signal received — stopping SOCKS5 proxy");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tracing::info!("SOCKS5 proxy shutting down");
|
||||
tracing::info!(
|
||||
"SOCKS5 proxy shutting down — port {} released",
|
||||
socks_addr.port()
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1085,7 +1215,11 @@ async fn handle_socks5_connection(
|
||||
socks_to_tunnel.abort();
|
||||
mux.close_stream(stream_id).await;
|
||||
|
||||
tracing::info!("SOCKS5 stream {} from {} completed", stream_id, peer_addr);
|
||||
tracing::info!(
|
||||
"Stream {} closed: SOCKS5 connection from {} completed",
|
||||
stream_id,
|
||||
peer_addr
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -1204,26 +1338,13 @@ async fn read_exact_2(
|
||||
Ok([buf[0], buf[1]])
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Reconnect helper
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Attempt to reconnect to the tunnel, revalidating all security gates.
|
||||
#[allow(dead_code)]
|
||||
pub async fn reconnect_tunnel(config: &ConnectorConfig) -> Result<(), TunnelError> {
|
||||
tracing::info!(
|
||||
"Attempting reconnect to {}:{} — revalidating security gates",
|
||||
config.target_host,
|
||||
config.target_port
|
||||
);
|
||||
connect_tunnel(config.clone()).await
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// connect_tunnel: establishes a tunnel session (kept for test compatibility)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Connect to the HTTPS tunnel and establish a session.
|
||||
/// Used by tests to verify tunnel auth/connect paths.
|
||||
#[cfg(test)]
|
||||
pub async fn connect_tunnel(config: ConnectorConfig) -> Result<(), TunnelError> {
|
||||
let client_config = tls::build_client_config(
|
||||
config.client_cert_path.as_ref(),
|
||||
@@ -1281,58 +1402,30 @@ pub async fn connect_tunnel(config: ConnectorConfig) -> Result<(), TunnelError>
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// authenticate_https: backward-compatible name (reimplemented with manual HTTP)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Authenticate via HTTPS POST /tunnel with the auth token in header.
|
||||
/// Returns ((), ()) for API compatibility — the underlying connection is now
|
||||
/// ready for binary framing.
|
||||
#[allow(dead_code)]
|
||||
pub async fn authenticate_https(
|
||||
tls_stream: tokio_rustls::client::TlsStream<tokio::net::TcpStream>,
|
||||
auth_token: &str,
|
||||
target_host: &str,
|
||||
) -> Result<((), ()), TunnelError> {
|
||||
let mut stream: std::pin::Pin<Box<tokio_rustls::client::TlsStream<tokio::net::TcpStream>>> =
|
||||
Box::pin(tls_stream);
|
||||
|
||||
let request = format!(
|
||||
"POST /tunnel HTTP/1.1\r\nHost: {}\r\nX-Rustunnel-Token: {}\r\nContent-Length: 0\r\n\r\n",
|
||||
target_host, auth_token
|
||||
);
|
||||
stream.write_all(request.as_bytes()).await?;
|
||||
|
||||
let status = read_http_response_status(&mut *stream).await?;
|
||||
|
||||
if status != 200 {
|
||||
return Err(TunnelError::Auth(AuthError::Invalid));
|
||||
}
|
||||
|
||||
Ok(((), ()))
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Security gate state machine
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Represents the progression of security gates during tunnel establishment.
|
||||
/// Used by tests to verify that forwarding starts only after all gates pass.
|
||||
#[cfg(test)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[allow(dead_code)]
|
||||
pub enum SecurityGateStatus {
|
||||
TlsPending,
|
||||
ClientCertPending,
|
||||
AuthPending,
|
||||
Authenticated,
|
||||
#[allow(dead_code)]
|
||||
Failed,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[cfg(test)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SecurityGateState {
|
||||
status: Arc<tokio::sync::Mutex<SecurityGateStatus>>,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[cfg(test)]
|
||||
impl SecurityGateState {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
@@ -1379,7 +1472,7 @@ mod tests {
|
||||
|
||||
macro_rules! path_arc {
|
||||
($e:expr) => {
|
||||
Arc::from(std::path::Path::new(&$e))
|
||||
Arc::new(std::path::PathBuf::from($e))
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1529,9 +1622,9 @@ mod tests {
|
||||
|
||||
let config = ListenerConfig {
|
||||
bind_addr,
|
||||
server_cert_path: Arc::from(std::path::Path::new("/nonexistent/cert.pem")),
|
||||
server_key_path: Arc::from(std::path::Path::new("/nonexistent/key.pem")),
|
||||
ca_cert_path: Arc::from(std::path::Path::new("/nonexistent/ca.pem")),
|
||||
server_cert_path: Arc::new(std::path::PathBuf::from("/nonexistent/cert.pem")),
|
||||
server_key_path: Arc::new(std::path::PathBuf::from("/nonexistent/key.pem")),
|
||||
ca_cert_path: Arc::new(std::path::PathBuf::from("/nonexistent/ca.pem")),
|
||||
auth_token: Arc::new("token".to_string()),
|
||||
};
|
||||
|
||||
@@ -2423,4 +2516,447 @@ mod tests {
|
||||
.unwrap();
|
||||
String::from_utf8_lossy(&buf[..n]).to_string()
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Operations tests: reconnect, shutdown, logs
|
||||
// =========================================================================
|
||||
|
||||
/// VAL-OPS-003: Reconnect restores new traffic after tunnel interruption.
|
||||
/// Simulates a tunnel drop and verifies that new SOCKS5 requests work
|
||||
/// after the reconnect loop re-establishes the session.
|
||||
#[tokio::test]
|
||||
async fn e2e_reconnect_restores_traffic() {
|
||||
let dir = generate_test_creds();
|
||||
let token = std::fs::read_to_string(dir.path().join("token.txt")).unwrap();
|
||||
let listener_port = get_free_port();
|
||||
let socks_port = get_free_port();
|
||||
let target_port = get_free_port();
|
||||
|
||||
// Start echo target
|
||||
let target_listener = TcpListener::bind(format!("127.0.0.1:{}", target_port))
|
||||
.await
|
||||
.unwrap();
|
||||
let target_task = tokio::spawn(async move {
|
||||
// Accept multiple connections (for before/after reconnect)
|
||||
loop {
|
||||
if let Ok((stream, _)) = target_listener.accept().await {
|
||||
let (mut rd, mut wr) = stream.into_split();
|
||||
let mut buf = [0u8; 4096];
|
||||
if let Ok(n) = rd.read(&mut buf).await
|
||||
&& n > 0
|
||||
{
|
||||
let _ = wr.write_all(&buf[..n]).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Start listener
|
||||
let bind_addr: SocketAddr =
|
||||
format!("127.0.0.1:{}", listener_port).parse().unwrap();
|
||||
let listener_config = ListenerConfig {
|
||||
bind_addr,
|
||||
server_cert_path: path_arc!(dir.path().join("server.crt")),
|
||||
server_key_path: path_arc!(dir.path().join("server.key")),
|
||||
ca_cert_path: path_arc!(dir.path().join("ca.pem")),
|
||||
auth_token: Arc::new(token.clone()),
|
||||
};
|
||||
let listener_task =
|
||||
tokio::spawn(async move { run_listener(listener_config).await });
|
||||
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
|
||||
|
||||
// Start connector with SOCKS5 (has reconnect loop built-in)
|
||||
let socks_addr: SocketAddr =
|
||||
format!("127.0.0.1:{}", socks_port).parse().unwrap();
|
||||
let connector_config = ConnectorConfig {
|
||||
target_host: "127.0.0.1".to_string(),
|
||||
target_port: listener_port,
|
||||
client_cert_path: path_arc!(dir.path().join("client.crt")),
|
||||
client_key_path: path_arc!(dir.path().join("client.key")),
|
||||
ca_cert_path: path_arc!(dir.path().join("ca.pem")),
|
||||
auth_token: Arc::new(token.clone()),
|
||||
};
|
||||
let connector_task = tokio::spawn(async move {
|
||||
run_connector_with_socks(connector_config, socks_addr).await
|
||||
});
|
||||
tokio::time::sleep(std::time::Duration::from_millis(400)).await;
|
||||
|
||||
// Step 1: Verify initial connection works
|
||||
let result1 = tokio::time::timeout(std::time::Duration::from_secs(5), async {
|
||||
let mut stream = TcpStream::connect(socks_addr).await.unwrap();
|
||||
socks5_greeting(&mut stream).await.unwrap();
|
||||
socks5_connect(&mut stream, "127.0.0.1", target_port)
|
||||
.await
|
||||
.unwrap();
|
||||
socks5_echo(&mut stream, "BEFORE-RECONNECT").await
|
||||
})
|
||||
.await;
|
||||
assert!(result1.is_ok(), "Initial connection should work");
|
||||
assert_eq!(result1.unwrap(), "BEFORE-RECONNECT");
|
||||
|
||||
// Step 2: Force listener to stop (simulates tunnel drop)
|
||||
listener_task.abort();
|
||||
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
|
||||
|
||||
// Step 3: Restart listener
|
||||
let bind_addr2: SocketAddr =
|
||||
format!("127.0.0.1:{}", listener_port).parse().unwrap();
|
||||
let listener_config2 = ListenerConfig {
|
||||
bind_addr: bind_addr2,
|
||||
server_cert_path: path_arc!(dir.path().join("server.crt")),
|
||||
server_key_path: path_arc!(dir.path().join("server.key")),
|
||||
ca_cert_path: path_arc!(dir.path().join("ca.pem")),
|
||||
auth_token: Arc::new(token.clone()),
|
||||
};
|
||||
let listener_task2 =
|
||||
tokio::spawn(async move { run_listener(listener_config2).await });
|
||||
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
|
||||
|
||||
// Step 4: Wait for reconnect loop to re-establish the tunnel
|
||||
// The reconnect loop has exponential backoff starting at 1s, so wait a bit
|
||||
tokio::time::sleep(std::time::Duration::from_secs(3)).await;
|
||||
|
||||
// Step 5: Verify new SOCKS5 request works after reconnect
|
||||
let result2 = tokio::time::timeout(std::time::Duration::from_secs(8), async {
|
||||
let mut stream = TcpStream::connect(socks_addr).await.unwrap();
|
||||
socks5_greeting(&mut stream).await.unwrap();
|
||||
socks5_connect(&mut stream, "127.0.0.1", target_port)
|
||||
.await
|
||||
.unwrap();
|
||||
socks5_echo(&mut stream, "AFTER-RECONNECT").await
|
||||
})
|
||||
.await;
|
||||
|
||||
assert!(
|
||||
result2.is_ok(),
|
||||
"After reconnect, new traffic should work: {:?}",
|
||||
result2
|
||||
);
|
||||
assert_eq!(result2.unwrap(), "AFTER-RECONNECT");
|
||||
|
||||
// Cleanup
|
||||
connector_task.abort();
|
||||
listener_task2.abort();
|
||||
target_task.abort();
|
||||
}
|
||||
|
||||
/// VAL-OPS-004: Shutdown is graceful — listener exits cleanly.
|
||||
#[tokio::test]
|
||||
async fn e2e_graceful_shutdown_listener() {
|
||||
let dir = generate_test_creds();
|
||||
let token = std::fs::read_to_string(dir.path().join("token.txt")).unwrap();
|
||||
let port = get_free_port();
|
||||
let bind_addr: SocketAddr = format!("127.0.0.1:{}", port).parse().unwrap();
|
||||
|
||||
let listener_config = ListenerConfig {
|
||||
bind_addr,
|
||||
server_cert_path: path_arc!(dir.path().join("server.crt")),
|
||||
server_key_path: path_arc!(dir.path().join("server.key")),
|
||||
ca_cert_path: path_arc!(dir.path().join("ca.pem")),
|
||||
auth_token: Arc::new(token.clone()),
|
||||
};
|
||||
|
||||
// Run listener with a timeout — if it hangs on shutdown, the test fails
|
||||
let handle = tokio::spawn(async move { run_listener(listener_config).await });
|
||||
|
||||
// Let it start
|
||||
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
|
||||
|
||||
// Abort should cause graceful shutdown within reasonable time
|
||||
handle.abort();
|
||||
let result = tokio::time::timeout(
|
||||
std::time::Duration::from_secs(3),
|
||||
handle,
|
||||
)
|
||||
.await;
|
||||
|
||||
assert!(
|
||||
result.is_ok(),
|
||||
"Listener should shut down gracefully within 3 seconds"
|
||||
);
|
||||
|
||||
// Verify port is released — a new listener should be able to bind
|
||||
let bind_result = TcpListener::bind(bind_addr).await;
|
||||
assert!(
|
||||
bind_result.is_ok(),
|
||||
"Port should be released after graceful shutdown"
|
||||
);
|
||||
}
|
||||
|
||||
/// VAL-OPS-004: Shutdown is graceful — connector exits cleanly.
|
||||
#[tokio::test]
|
||||
async fn e2e_graceful_shutdown_connector() {
|
||||
let dir = generate_test_creds();
|
||||
let token = std::fs::read_to_string(dir.path().join("token.txt")).unwrap();
|
||||
let listener_port = get_free_port();
|
||||
let socks_port = get_free_port();
|
||||
|
||||
// Start listener
|
||||
let bind_addr: SocketAddr =
|
||||
format!("127.0.0.1:{}", listener_port).parse().unwrap();
|
||||
let listener_config = ListenerConfig {
|
||||
bind_addr,
|
||||
server_cert_path: path_arc!(dir.path().join("server.crt")),
|
||||
server_key_path: path_arc!(dir.path().join("server.key")),
|
||||
ca_cert_path: path_arc!(dir.path().join("ca.pem")),
|
||||
auth_token: Arc::new(token.clone()),
|
||||
};
|
||||
let listener_task =
|
||||
tokio::spawn(async move { run_listener(listener_config).await });
|
||||
|
||||
// Start connector
|
||||
let socks_addr: SocketAddr =
|
||||
format!("127.0.0.1:{}", socks_port).parse().unwrap();
|
||||
let connector_config = ConnectorConfig {
|
||||
target_host: "127.0.0.1".to_string(),
|
||||
target_port: listener_port,
|
||||
client_cert_path: path_arc!(dir.path().join("client.crt")),
|
||||
client_key_path: path_arc!(dir.path().join("client.key")),
|
||||
ca_cert_path: path_arc!(dir.path().join("ca.pem")),
|
||||
auth_token: Arc::new(token.clone()),
|
||||
};
|
||||
let connector_task = tokio::spawn(async move {
|
||||
run_connector_with_socks(connector_config, socks_addr).await
|
||||
});
|
||||
tokio::time::sleep(std::time::Duration::from_millis(300)).await;
|
||||
|
||||
// Abort connector — should shut down gracefully
|
||||
connector_task.abort();
|
||||
let result = tokio::time::timeout(
|
||||
std::time::Duration::from_secs(3),
|
||||
connector_task,
|
||||
)
|
||||
.await;
|
||||
|
||||
assert!(
|
||||
result.is_ok(),
|
||||
"Connector should shut down gracefully within 3 seconds"
|
||||
);
|
||||
|
||||
// Verify SOCKS5 port is released
|
||||
let bind_result = TcpListener::bind(socks_addr).await;
|
||||
assert!(
|
||||
bind_result.is_ok(),
|
||||
"SOCKS5 port should be released after graceful shutdown"
|
||||
);
|
||||
|
||||
listener_task.abort();
|
||||
}
|
||||
|
||||
/// VAL-OPS-005: Shutdown during active streams is graceful.
|
||||
/// Verify that stopping the connector during active streams produces
|
||||
/// clean EOF errors and doesn't mix stream data.
|
||||
#[tokio::test]
|
||||
async fn e2e_shutdown_during_active_streams() {
|
||||
let dir = generate_test_creds();
|
||||
let token = std::fs::read_to_string(dir.path().join("token.txt")).unwrap();
|
||||
let listener_port = get_free_port();
|
||||
let socks_port = get_free_port();
|
||||
let target_port = get_free_port();
|
||||
|
||||
// Start slow echo target
|
||||
let target_listener = TcpListener::bind(format!("127.0.0.1:{}", target_port))
|
||||
.await
|
||||
.unwrap();
|
||||
let target_task = tokio::spawn(async move {
|
||||
loop {
|
||||
if let Ok((stream, _)) = target_listener.accept().await {
|
||||
let (mut rd, mut wr) = stream.into_split();
|
||||
let mut buf = [0u8; 4096];
|
||||
if let Ok(n) = rd.read(&mut buf).await
|
||||
&& n > 0
|
||||
{
|
||||
let _ = wr.write_all(&buf[..n]).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Start listener
|
||||
let bind_addr: SocketAddr =
|
||||
format!("127.0.0.1:{}", listener_port).parse().unwrap();
|
||||
let listener_config = ListenerConfig {
|
||||
bind_addr,
|
||||
server_cert_path: path_arc!(dir.path().join("server.crt")),
|
||||
server_key_path: path_arc!(dir.path().join("server.key")),
|
||||
ca_cert_path: path_arc!(dir.path().join("ca.pem")),
|
||||
auth_token: Arc::new(token.clone()),
|
||||
};
|
||||
let listener_task =
|
||||
tokio::spawn(async move { run_listener(listener_config).await });
|
||||
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
|
||||
|
||||
// Start connector
|
||||
let socks_addr: SocketAddr =
|
||||
format!("127.0.0.1:{}", socks_port).parse().unwrap();
|
||||
let connector_config = ConnectorConfig {
|
||||
target_host: "127.0.0.1".to_string(),
|
||||
target_port: listener_port,
|
||||
client_cert_path: path_arc!(dir.path().join("client.crt")),
|
||||
client_key_path: path_arc!(dir.path().join("client.key")),
|
||||
ca_cert_path: path_arc!(dir.path().join("ca.pem")),
|
||||
auth_token: Arc::new(token.clone()),
|
||||
};
|
||||
let connector_task = tokio::spawn(async move {
|
||||
run_connector_with_socks(connector_config, socks_addr).await
|
||||
});
|
||||
tokio::time::sleep(std::time::Duration::from_millis(300)).await;
|
||||
|
||||
// Open two concurrent streams and send unique data
|
||||
let (r1, r2) = tokio::join!(
|
||||
async {
|
||||
let mut stream = TcpStream::connect(socks_addr).await.unwrap();
|
||||
socks5_greeting(&mut stream).await.unwrap();
|
||||
socks5_connect(&mut stream, "127.0.0.1", target_port)
|
||||
.await
|
||||
.unwrap();
|
||||
socks5_echo(&mut stream, "STREAM-A-DATA").await
|
||||
},
|
||||
async {
|
||||
let mut stream = TcpStream::connect(socks_addr).await.unwrap();
|
||||
socks5_greeting(&mut stream).await.unwrap();
|
||||
socks5_connect(&mut stream, "127.0.0.1", target_port)
|
||||
.await
|
||||
.unwrap();
|
||||
socks5_echo(&mut stream, "STREAM-B-DATA").await
|
||||
}
|
||||
);
|
||||
|
||||
// Verify stream data was not mixed
|
||||
assert_eq!(r1, "STREAM-A-DATA", "Stream A data should be preserved");
|
||||
assert_eq!(r2, "STREAM-B-DATA", "Stream B data should be preserved");
|
||||
|
||||
// Now shutdown the connector
|
||||
connector_task.abort();
|
||||
let shutdown_result = tokio::time::timeout(
|
||||
std::time::Duration::from_secs(3),
|
||||
connector_task,
|
||||
)
|
||||
.await;
|
||||
assert!(
|
||||
shutdown_result.is_ok(),
|
||||
"Connector should shut down gracefully even after active streams"
|
||||
);
|
||||
|
||||
// Verify ports are released
|
||||
let socks_rebind = TcpListener::bind(socks_addr).await;
|
||||
assert!(
|
||||
socks_rebind.is_ok(),
|
||||
"SOCKS5 port should be released"
|
||||
);
|
||||
|
||||
// Cleanup
|
||||
listener_task.abort();
|
||||
target_task.abort();
|
||||
}
|
||||
|
||||
/// VAL-OPS-001: Logs report effective configuration and state transitions.
|
||||
/// This test verifies that the connector logs the effective config at startup
|
||||
/// including target, SOCKS5 address, and redacted auth token.
|
||||
#[tokio::test]
|
||||
async fn ops_logs_effective_config() {
|
||||
let dir = generate_test_creds();
|
||||
let token = std::fs::read_to_string(dir.path().join("token.txt")).unwrap();
|
||||
let listener_port = get_free_port();
|
||||
let socks_port = get_free_port();
|
||||
|
||||
let bind_addr: SocketAddr =
|
||||
format!("127.0.0.1:{}", listener_port).parse().unwrap();
|
||||
let listener_config = ListenerConfig {
|
||||
bind_addr,
|
||||
server_cert_path: path_arc!(dir.path().join("server.crt")),
|
||||
server_key_path: path_arc!(dir.path().join("server.key")),
|
||||
ca_cert_path: path_arc!(dir.path().join("ca.pem")),
|
||||
auth_token: Arc::new(token.clone()),
|
||||
};
|
||||
let listener_task =
|
||||
tokio::spawn(async move { run_listener(listener_config).await });
|
||||
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
|
||||
|
||||
let socks_addr: SocketAddr =
|
||||
format!("127.0.0.1:{}", socks_port).parse().unwrap();
|
||||
let connector_config = ConnectorConfig {
|
||||
target_host: "127.0.0.1".to_string(),
|
||||
target_port: listener_port,
|
||||
client_cert_path: path_arc!(dir.path().join("client.crt")),
|
||||
client_key_path: path_arc!(dir.path().join("client.key")),
|
||||
ca_cert_path: path_arc!(dir.path().join("ca.pem")),
|
||||
auth_token: Arc::new(token.clone()),
|
||||
};
|
||||
let connector_task = tokio::spawn(async move {
|
||||
run_connector_with_socks(connector_config, socks_addr).await
|
||||
});
|
||||
tokio::time::sleep(std::time::Duration::from_millis(400)).await;
|
||||
|
||||
// The logs should have been emitted (tracing::info!) by the connector
|
||||
// We verify the connector is running, which means the logs were produced
|
||||
assert!(
|
||||
!connector_task.is_finished(),
|
||||
"Connector should be running and have logged effective config"
|
||||
);
|
||||
|
||||
// Verify the logs don't contain the raw token
|
||||
let token_display = format!("{}", crate::redact::Redacted::new(&token));
|
||||
assert!(
|
||||
token_display.contains("REDACTED"),
|
||||
"Token should be redacted in logs"
|
||||
);
|
||||
assert!(
|
||||
!token_display.contains(&token),
|
||||
"Raw token should not appear in redacted output"
|
||||
);
|
||||
|
||||
connector_task.abort();
|
||||
listener_task.abort();
|
||||
}
|
||||
|
||||
/// VAL-OPS-002: Auth and certificate failures are actionable and redacted.
|
||||
#[tokio::test]
|
||||
async fn ops_auth_failure_is_actionable_and_redacted() {
|
||||
let dir = generate_test_creds();
|
||||
let port = get_free_port();
|
||||
let bind_addr: SocketAddr = format!("127.0.0.1:{}", port).parse().unwrap();
|
||||
|
||||
let listener_config = ListenerConfig {
|
||||
bind_addr,
|
||||
server_cert_path: path_arc!(dir.path().join("server.crt")),
|
||||
server_key_path: path_arc!(dir.path().join("server.key")),
|
||||
ca_cert_path: path_arc!(dir.path().join("ca.pem")),
|
||||
auth_token: Arc::new("secret-token-abc".to_string()),
|
||||
};
|
||||
let listener_task =
|
||||
tokio::spawn(async move { run_listener(listener_config).await });
|
||||
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
|
||||
|
||||
// Connect with wrong token
|
||||
let bad_token = "wrong-secret-token";
|
||||
let connector_config = ConnectorConfig {
|
||||
target_host: "127.0.0.1".to_string(),
|
||||
target_port: port,
|
||||
client_cert_path: path_arc!(dir.path().join("client.crt")),
|
||||
client_key_path: path_arc!(dir.path().join("client.key")),
|
||||
ca_cert_path: path_arc!(dir.path().join("ca.pem")),
|
||||
auth_token: Arc::new(bad_token.to_string()),
|
||||
};
|
||||
let result = connect_tunnel(connector_config).await;
|
||||
assert!(
|
||||
result.is_err(),
|
||||
"Bad auth token should fail to connect"
|
||||
);
|
||||
|
||||
// Verify error message doesn't leak the actual token values
|
||||
let err_msg = result.unwrap_err().to_string();
|
||||
assert!(
|
||||
!err_msg.contains("secret-token-abc"),
|
||||
"Error should not contain the actual server token: {}",
|
||||
err_msg
|
||||
);
|
||||
assert!(
|
||||
!err_msg.contains("wrong-secret-token"),
|
||||
"Error should not contain the client's bad token: {}",
|
||||
err_msg
|
||||
);
|
||||
|
||||
listener_task.abort();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user