Deploy ttstd relay and configure secure public WSS clients
This commit is contained in:
1 parent
82adce545d
commit
c38f067bea
16 files changed
+384
-24
No files matched your search
@@ -14,3 +14,4 @@ tokio.workspace = true
|
||||
tokio-tungstenite.workspace = true
|
||||
tracing-subscriber.workspace = true
|
||||
url.workspace = true
|
||||
rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12"] }
|
||||
+71
-14
@@ -9,7 +9,7 @@ use tokio::{
|
||||
time::timeout,
|
||||
};
|
||||
use tokio_tungstenite::{
|
||||
connect_async_with_config,
|
||||
client_async_tls_with_config, connect_async_with_config,
|
||||
tungstenite::{client::IntoClientRequest, http::HeaderValue, Message},
|
||||
MaybeTlsStream, WebSocketStream,
|
||||
};
|
||||
@@ -18,12 +18,16 @@ use url::Url;
|
||||
pub type Ws = WebSocketStream<MaybeTlsStream<TcpStream>>;
|
||||
pub const CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
|
||||
pub const IO_TIMEOUT: Duration = Duration::from_secs(30);
|
||||
pub const DEFAULT_RELAY: &str = "wss://ttstd.com/ads";
|
||||
|
||||
#[derive(Args, Clone, Debug)]
|
||||
pub struct NetworkArgs {
|
||||
/// Relay URL, e.g. wss://devices.example.com
|
||||
#[arg(long)]
|
||||
/// Relay base URL; an optional reverse-proxy path prefix is supported
|
||||
#[arg(long, default_value = DEFAULT_RELAY)]
|
||||
pub server: String,
|
||||
/// Optional origin host:port override; TLS still verifies the relay URL hostname
|
||||
#[arg(long, env = "ADS_CONNECT_ADDRESS")]
|
||||
pub connect_address: Option<String>,
|
||||
/// Allow unencrypted ws:// for isolated development only
|
||||
#[arg(long, default_value_t = false)]
|
||||
pub allow_insecure: bool,
|
||||
@@ -44,10 +48,12 @@ impl NetworkArgs {
|
||||
{
|
||||
bail!("relay URL must not contain credentials, query, or fragment");
|
||||
}
|
||||
if url.path() != "/" && !url.path().is_empty() {
|
||||
bail!("relay URL must use the root path (no URL prefix)");
|
||||
}
|
||||
url.set_path(path);
|
||||
let endpoint = format!(
|
||||
"{}/{}",
|
||||
url.path().trim_end_matches('/'),
|
||||
path.trim_start_matches('/')
|
||||
);
|
||||
url.set_path(&endpoint);
|
||||
url.query_pairs_mut().extend_pairs(params.iter().copied());
|
||||
Ok(url)
|
||||
}
|
||||
@@ -69,7 +75,13 @@ pub fn validate_token(token: &str) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn dial(url: Url, token: &str) -> Result<Ws> {
|
||||
pub async fn dial(url: Url, token: &str, connect_address: Option<&str>) -> Result<Ws> {
|
||||
// WSS needs an explicit provider; relying on transitive feature defaults
|
||||
// leaves pure WebSocket builds without one and panics at the first TLS dial.
|
||||
static TLS_PROVIDER: std::sync::Once = std::sync::Once::new();
|
||||
TLS_PROVIDER.call_once(|| {
|
||||
let _ = rustls::crypto::ring::default_provider().install_default();
|
||||
});
|
||||
let mut request = url.as_str().into_client_request()?;
|
||||
request.headers_mut().insert(
|
||||
"Authorization",
|
||||
@@ -79,12 +91,18 @@ pub async fn dial(url: Url, token: &str) -> Result<Ws> {
|
||||
let config = tokio_tungstenite::tungstenite::protocol::WebSocketConfig::default()
|
||||
.max_message_size(Some(128 * 1024))
|
||||
.max_frame_size(Some(128 * 1024));
|
||||
let result = timeout(
|
||||
CONNECT_TIMEOUT,
|
||||
connect_async_with_config(request, Some(config), true),
|
||||
)
|
||||
.await
|
||||
.context("relay handshake timed out")?;
|
||||
let handshake = async {
|
||||
if let Some(address) = connect_address {
|
||||
let tcp = TcpStream::connect(address).await?;
|
||||
tcp.set_nodelay(true)?;
|
||||
client_async_tls_with_config(request, tcp, Some(config), None).await
|
||||
} else {
|
||||
connect_async_with_config(request, Some(config), true).await
|
||||
}
|
||||
};
|
||||
let result = timeout(CONNECT_TIMEOUT, handshake)
|
||||
.await
|
||||
.context("relay handshake timed out")?;
|
||||
match result {
|
||||
Ok((socket, _)) => Ok(socket),
|
||||
Err(tokio_tungstenite::tungstenite::Error::Http(response)) => {
|
||||
@@ -221,6 +239,7 @@ mod tests {
|
||||
fn rejects_unsafe_configuration() {
|
||||
let network = NetworkArgs {
|
||||
server: "ws://example.com".into(),
|
||||
connect_address: None,
|
||||
allow_insecure: false,
|
||||
};
|
||||
assert!(network.url("/v1/agent", &[]).is_err());
|
||||
@@ -229,6 +248,44 @@ mod tests {
|
||||
assert!(valid_device_id("home-phone_1"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn relay_prefix_and_query_parameters_are_preserved() {
|
||||
for base in ["wss://ttstd.com/ads", "wss://ttstd.com/ads/"] {
|
||||
let network = NetworkArgs {
|
||||
server: base.into(),
|
||||
connect_address: None,
|
||||
allow_insecure: false,
|
||||
};
|
||||
let url = network
|
||||
.url(
|
||||
"/v1/connect",
|
||||
&[("device", "home-phone"), ("lease", "secret-ticket")],
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(url.path(), "/ads/v1/connect");
|
||||
assert_eq!(url.query(), Some("device=home-phone&lease=secret-ticket"));
|
||||
}
|
||||
assert_eq!(DEFAULT_RELAY, "wss://ttstd.com/ads");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn tls_failure_is_reported_without_a_provider_panic() {
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let address = listener.local_addr().unwrap().to_string();
|
||||
let peer = tokio::spawn(async move {
|
||||
let (mut sock, _) = listener.accept().await.unwrap();
|
||||
sock.write_all(b"not a TLS record").await.unwrap();
|
||||
});
|
||||
let result = dial(
|
||||
Url::parse("wss://relay.invalid/v1/agent").unwrap(),
|
||||
"test-token-at-least-thirty-two-characters",
|
||||
Some(&address),
|
||||
)
|
||||
.await;
|
||||
assert!(result.is_err());
|
||||
peer.await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn adb_handshake_selects_transport_and_phone_port() {
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
|
||||
Reference in new issue
Block a user