feat: add control center, multiple relay nodes and P2P-first transport
This commit is contained in:
1 parent
0988e9c363
commit
2abfb63da9
44 files changed
+3477
-67
No files matched your search
@@ -15,9 +15,11 @@
|
||||
| --- | --- | --- |
|
||||
| `agent/` | 远程设备守护程序 ads-agent,注册设备、连接手机 ADB、建立转发并重连;源码在 `src/` | Rust、TOML |
|
||||
| `client/` | 本地守护程序 ads-client,维护设备租约、监听本地端口并自动调用 adb connect;源码在 `src/` | Rust、TOML |
|
||||
| `server/` | 公网中继 ads-server,负责认证、设备注册、独占租约、会话配对及数据转发 | Go |
|
||||
| `server/` | ads-server 控制中心、旧协议兼容转发及独立 ads-relay 中继节点 | Go |
|
||||
| `server/cmd/ads-relay/` | 独立中继入口,提供短期票据认证、WSS 数据配对及可选 STUN | Go |
|
||||
| `server/internal/relay/` | 中继票据、数据通道、租约撤销、STUN 与测试 | Go |
|
||||
| `crates/` | Rust 工作区共享库容器 | Rust、TOML |
|
||||
| `crates/ads-core/` | 共用网络与协议代码,包括 WSS、ADB smart socket 和流式转发;源码在 `src/` | Rust、TOML |
|
||||
| `crates/ads-core/` | 共用网络与协议代码,包括 WSS、WebRTC P2P、节点回退、ADB smart socket 和流式转发;源码在 `src/` | Rust、TOML |
|
||||
| `desktop/` | 跨平台桌面客户端及其测试项目、.NET SDK 配置与使用说明 | C#、Avalonia XAML(AXAML)、JSON、Markdown |
|
||||
| `desktop/Ads.Desktop/` | Avalonia 桌面应用,管理配置、设备列表、连接、托盘及 scrcpy 子进程;`Assets/` 保存图标资源 | C#、AXAML、XML(csproj)、PNG |
|
||||
| `desktop/Ads.Desktop.Tests/` | 桌面客户端单元测试 | C#、XML(csproj) |
|
||||
|
||||
Generated
+1397
-9
File diff suppressed because it is too large.
Load diff
@@ -1,15 +1,17 @@
|
||||
# Android Device Streaming
|
||||
|
||||
公网中继 + 远程设备守护程序 + 本地连接守护程序。两端主动出站连接服务器,无需 VPN 或路由器端口映射。连接后,普通本地 `adb devices` 可以显示远程手机,Android Studio / scrcpy 使用本地 ADB 的正常设备通道。
|
||||
一个控制中心 + 多个中继节点 + P2P 优先。两端连接控制中心取得设备租约、交换信令,优先通过 WebRTC DataChannel 直连;直连失败时自动选择独立 WSS 中继。普通本地 `adb devices` 显示本地映射地址,Android Studio / scrcpy 使用同一通道。
|
||||
|
||||
```text
|
||||
本地 ADB → 127.0.0.1:15555 → ads-client (Rust)
|
||||
│ WSS
|
||||
ads-server (Go)
|
||||
│ WSS
|
||||
ads-agent (Rust) → 手机 adbd TCP:5555
|
||||
ads-server 控制中心(注册、租约、信令、节点健康)
|
||||
↙ WSS WSS ↘
|
||||
本地 ADB → ads-client ═══════ WebRTC P2P ═══════ ads-agent → 手机 adbd
|
||||
╲ 失败则 WSS 中继 ╱
|
||||
relay-1 / relay-2 / …
|
||||
```
|
||||
|
||||
已部署的集群和使用方法见 [集群部署与协议](docs/cluster.md)。不配置集群时保留原有 v1 单中继模式,供兼容旧客户端;以下原有单机启动命令也仍可使用。
|
||||
|
||||
## 目录说明
|
||||
|
||||
下表覆盖项目根目录及主要自有子项目;第三方源码的内部目录由其自身文档说明。语言列为主要语言或文件格式,产物目录不视为源码模块。
|
||||
@@ -18,9 +20,11 @@
|
||||
| --- | --- | --- |
|
||||
| `agent/` | 远程设备守护程序 ads-agent,注册设备、连接手机 ADB、建立转发并重连;源码在 `src/` | Rust、TOML |
|
||||
| `client/` | 本地守护程序 ads-client,维护设备租约、监听本地端口并自动调用 adb connect;源码在 `src/` | Rust、TOML |
|
||||
| `server/` | 公网中继 ads-server,负责认证、设备注册、独占租约、会话配对及数据转发 | Go |
|
||||
| `server/` | ads-server 控制中心、旧协议兼容转发及独立 ads-relay 中继节点 | Go |
|
||||
| `server/cmd/ads-relay/` | 独立中继入口,提供短期票据认证、WSS 数据配对及可选 STUN | Go |
|
||||
| `server/internal/relay/` | 中继票据、数据通道、租约撤销、STUN 与测试 | Go |
|
||||
| `crates/` | Rust 工作区共享库容器 | Rust、TOML |
|
||||
| `crates/ads-core/` | 共用网络与协议代码,包括 WSS、ADB smart socket 和流式转发;源码在 `src/` | Rust、TOML |
|
||||
| `crates/ads-core/` | 共用网络与协议代码,包括 WSS、WebRTC P2P、节点回退、ADB smart socket 和流式转发;源码在 `src/` | Rust、TOML |
|
||||
| `desktop/` | 跨平台桌面客户端及其测试项目、.NET SDK 配置与使用说明 | C#、Avalonia XAML(AXAML)、JSON、Markdown |
|
||||
| `desktop/Ads.Desktop/` | Avalonia 桌面应用,管理配置、设备列表、连接、托盘及 scrcpy 子进程;`Assets/` 保存图标资源 | C#、AXAML、XML(csproj)、PNG |
|
||||
| `desktop/Ads.Desktop.Tests/` | 桌面客户端单元测试 | C#、XML(csproj) |
|
||||
@@ -48,13 +52,13 @@
|
||||
|
||||
| 目录 | 输出 | 职责 |
|
||||
|---|---|---|
|
||||
| `server/` | `ads-server` | 角色认证、网关注册、设备独占、会话配对、二进制转发 |
|
||||
| `server/` | `ads-server` / `ads-relay` | 控制中心:认证、注册、设备独占、信令、健康检查;独立中继:数据转发 |
|
||||
| `agent/` | `ads-agent` | 远程电脑后台进程,主动注册设备、接通手机、断线重连 |
|
||||
| `client/` | `ads-client` | 本地后台进程,保持设备租约,提供本地端口,自动调用 `adb connect` |
|
||||
| `crates/ads-core/` | 共用 Rust 库 | WSS、协议、ADB smart socket、流式转发 |
|
||||
| `deploy/` | 部署示例 | Caddy、systemd、launchd 示例 |
|
||||
|
||||
三个程序均为独立可执行文件。没有 UI,不依赖 Python 运行正式程序。两端守护程序当前作为前台长运行进程,启动安装交给各平台进程管理器;尚未实现原生 Windows Service 安装器。
|
||||
四个后端程序均为独立可执行文件;另有 Avalonia 桌面工作台,不依赖 Python 运行正式程序。两端守护程序当前作为前台长运行进程,启动安装交给各平台进程管理器;尚未实现原生 Windows Service 安装器。
|
||||
|
||||
## 编译
|
||||
|
||||
@@ -73,7 +77,7 @@ Linux / macOS:
|
||||
sh scripts/build.sh
|
||||
```
|
||||
|
||||
产物在 `bin/`,Windows 是三个 `.exe`。Go 服务器和 Rust 核心使用跨平台 API;当前实际编译和集成测试平台为 Windows x64,Linux/macOS 仍需在对应平台验证构建、进程托管和 ADB 权限。
|
||||
产物在 `bin/`,Windows 是四个后端 `.exe`。Go 服务器和 Rust 核心使用跨平台 API;Rust 两端和桌面在 Windows x64 验证,Go 控制中心与中继已部署到 Linux amd64;其余平台仍需验证构建、进程托管和 ADB 权限。
|
||||
|
||||
## 1. 公网服务器
|
||||
|
||||
|
||||
@@ -12,3 +12,5 @@ $env:ADS_AGENT_TOKEN = '<设备端令牌>'
|
||||
也可用 `--adb-server 127.0.0.1:5037 --adb-serial <序列号>` 替代 `--target`。该模式仍要求手机开启 TCP adbd(默认 5555,可用 `--device-port` 修改),不是通用的 USB-only 接入。
|
||||
|
||||
Windows 可由任务计划程序在登录后启动;Linux/macOS 可使用 `deploy/` 示例托管。当前二进制不是原生 Windows Service,不能直接用 `sc.exe create` 安装。
|
||||
|
||||
新版注册时协商 v2,按控制中心指令与本地客户端交换 WebRTC SDP,优先直连或连接独立中继;兼容控制中心下发的 v1 转发指令。无需增加手机或远程电脑的公网入站端口。控制连接丢失时会结束所属数据通道。
|
||||
+16
-4
@@ -51,8 +51,18 @@ async fn upstream(args: &Args) -> Result<TcpStream> {
|
||||
}
|
||||
}
|
||||
|
||||
async fn serve_session(args: Arc<Args>, session: String) -> Result<()> {
|
||||
async fn serve_session(args: Arc<Args>, session: String, signal_key: Option<String>) -> Result<()> {
|
||||
let tcp = upstream(&args).await.context("cannot reach phone adbd")?;
|
||||
if let Some(key) = signal_key {
|
||||
let signal = dial(
|
||||
args.network
|
||||
.url("/v2/signal", &[("session", &session), ("key", &key)])?,
|
||||
&args.token,
|
||||
args.network.connect_address.as_deref(),
|
||||
)
|
||||
.await?;
|
||||
return ads_core::transport::agent_session(tcp, signal, &args.network).await;
|
||||
}
|
||||
let ws = dial(
|
||||
args.network.url(
|
||||
"/v1/data",
|
||||
@@ -67,7 +77,8 @@ async fn serve_session(args: Arc<Args>, session: String) -> Result<()> {
|
||||
|
||||
async fn online(args: Arc<Args>) -> Result<()> {
|
||||
let mut ws = dial(
|
||||
args.network.url("/v1/agent", &[("device", &args.device)])?,
|
||||
args.network
|
||||
.url("/v1/agent", &[("device", &args.device), ("v", "2")])?,
|
||||
&args.token,
|
||||
args.network.connect_address.as_deref(),
|
||||
)
|
||||
@@ -82,12 +93,13 @@ async fn online(args: Arc<Args>) -> Result<()> {
|
||||
tokio::select! {
|
||||
message = next_control(&mut ws) => {
|
||||
let message = message?;
|
||||
if message.kind != "open" { bail!("unexpected gateway control message"); }
|
||||
if message.kind != "open" && message.kind != "open_v2" { bail!("unexpected gateway control message"); }
|
||||
let signal_key = if message.kind == "open_v2" { Some(message.signal_key.context("missing signal key")?) } else { None };
|
||||
let session = message.session.context("missing session ticket")?;
|
||||
if tasks.len() >= 32 { warn!("gateway stream limit reached"); continue; }
|
||||
let args = args.clone();
|
||||
tasks.spawn(async move {
|
||||
if let Err(error) = serve_session(args, session).await { warn!(%error, "device stream ended"); }
|
||||
if let Err(error) = serve_session(args, session, signal_key).await { warn!(%error, "device stream ended"); }
|
||||
});
|
||||
},
|
||||
_ = tasks.join_next(), if !tasks.is_empty() => {},
|
||||
|
||||
@@ -15,3 +15,10 @@ $env:ADS_CLIENT_TOKEN = '<本地端令牌>'
|
||||
- Ctrl+C 释放租约,并在启用自动连接时执行该端点的 `adb disconnect`。
|
||||
|
||||
使用本地 ADB 的正常通道,可配合 Android Studio 和 `scrcpy -s 127.0.0.1:15555`。不要将 `ADB_SERVER_SOCKET` 指向远程服务,否则其他终端可能查询到另一台 ADB Server。
|
||||
|
||||
- `--transport auto`(默认):WebRTC P2P 优先,超时后按控制中心节点顺序回退。
|
||||
- `--transport relay`:跳过 P2P,仍自动选择健康中继并在建立失败时尝试下一节点。
|
||||
- `--p2p-public-only`:诊断选项,只交换 STUN 公网候选地址,用于排除 LAN/VPN 直连。
|
||||
- `--status-file <JSON路径>`:输出当前 p2p/relay/mixed/negotiating/idle、通道数和节点 ID,供桌面展示。
|
||||
|
||||
节点断线会中断现有 TCP 流;本地 ADB 在重试后创建新通道,重新选择路线,不支持将原 TCP 流无缝迁移。旧中心或旧 agent 会协商回 v1 模式。
|
||||
+33
-8
@@ -33,11 +33,27 @@ struct Args {
|
||||
/// Do not periodically invoke local adb connect (useful for manual control/tests)
|
||||
#[arg(long)]
|
||||
no_auto_connect: bool,
|
||||
/// Prefer direct WebRTC or force WSS relay nodes
|
||||
#[arg(long, default_value = "auto", value_parser = ["auto", "relay"])]
|
||||
transport: String,
|
||||
/// Exclude LAN/VPN host candidates (diagnostic WAN test)
|
||||
#[arg(long)]
|
||||
p2p_public_only: bool,
|
||||
/// Machine-readable active transport state for desktop clients
|
||||
#[arg(long)]
|
||||
status_file: Option<PathBuf>,
|
||||
}
|
||||
|
||||
async fn lease_once(args: &Args, state: &watch::Sender<Option<String>>) -> Result<()> {
|
||||
#[derive(Clone)]
|
||||
struct LeaseState {
|
||||
id: String,
|
||||
protocol: u8,
|
||||
}
|
||||
|
||||
async fn lease_once(args: &Args, state: &watch::Sender<Option<LeaseState>>) -> Result<()> {
|
||||
let mut ws = dial(
|
||||
args.network.url("/v1/lease", &[("device", &args.device)])?,
|
||||
args.network
|
||||
.url("/v1/lease", &[("device", &args.device), ("v", "2")])?,
|
||||
&args.token,
|
||||
args.network.connect_address.as_deref(),
|
||||
)
|
||||
@@ -47,14 +63,17 @@ async fn lease_once(args: &Args, state: &watch::Sender<Option<String>>) -> Resul
|
||||
bail!("expected leased response");
|
||||
}
|
||||
let id = response.lease.context("missing lease ticket")?;
|
||||
state.send_replace(Some(id));
|
||||
state.send_replace(Some(LeaseState {
|
||||
id,
|
||||
protocol: response.protocol,
|
||||
}));
|
||||
info!(device = %args.device, endpoint = %args.listen, "device leased; local tunnel ready");
|
||||
loop {
|
||||
next_control(&mut ws).await?;
|
||||
}
|
||||
}
|
||||
|
||||
async fn maintain_lease(args: Arc<Args>, state: watch::Sender<Option<String>>) {
|
||||
async fn maintain_lease(args: Arc<Args>, state: watch::Sender<Option<LeaseState>>) {
|
||||
let mut delay = 1;
|
||||
loop {
|
||||
let started = std::time::Instant::now();
|
||||
@@ -96,7 +115,7 @@ async fn adb_command(args: &Args, action: &str) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn maintain_adb(args: Arc<Args>, state: watch::Receiver<Option<String>>) {
|
||||
async fn maintain_adb(args: Arc<Args>, state: watch::Receiver<Option<LeaseState>>) {
|
||||
loop {
|
||||
if state.borrow().is_some() {
|
||||
if let Err(error) = adb_command(&args, "connect").await {
|
||||
@@ -110,8 +129,9 @@ async fn maintain_adb(args: Arc<Args>, state: watch::Receiver<Option<String>>) {
|
||||
async fn accept_connections(
|
||||
args: Arc<Args>,
|
||||
listener: TcpListener,
|
||||
state: watch::Receiver<Option<String>>,
|
||||
state: watch::Receiver<Option<LeaseState>>,
|
||||
) -> Result<()> {
|
||||
let status = ads_core::transport::TransportStatus::new(args.status_file.clone());
|
||||
let mut tasks = JoinSet::new();
|
||||
loop {
|
||||
tokio::select! {
|
||||
@@ -121,11 +141,16 @@ async fn accept_connections(
|
||||
let Some(lease) = lease else { drop(tcp); continue; };
|
||||
if tasks.len() >= 32 { drop(tcp); warn!("local stream limit reached"); continue; }
|
||||
let args = args.clone();
|
||||
let status = status.stream();
|
||||
tasks.spawn(async move {
|
||||
let result = async {
|
||||
let url = args.network.url("/v1/connect", &[("device", &args.device), ("lease", &lease)])?;
|
||||
let path = if lease.protocol >= 2 { "/v2/connect" } else { "/v1/connect" };
|
||||
let public_only = if args.p2p_public_only { "1" } else { "0" };
|
||||
let url = args.network.url(path, &[("device", &args.device), ("lease", &lease.id), ("public_only", public_only)])?;
|
||||
let ws = dial(url, &args.token, args.network.connect_address.as_deref()).await?;
|
||||
relay(tcp, ws).await
|
||||
if lease.protocol >= 2 {
|
||||
ads_core::transport::client_session(tcp, ws, &args.network, &args.transport, &status).await
|
||||
} else { status.set("compat", "controller-v1"); relay(tcp, ws).await }
|
||||
}.await;
|
||||
if let Err(error) = result { warn!(%error, "local device stream ended"); }
|
||||
});
|
||||
|
||||
@@ -15,3 +15,6 @@ tokio-tungstenite.workspace = true
|
||||
tracing-subscriber.workspace = true
|
||||
url.workspace = true
|
||||
rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12"] }
|
||||
webrtc = "0.14"
|
||||
bytes = "1"
|
||||
tracing.workspace = true
|
||||
@@ -0,0 +1,5 @@
|
||||
# ads-core
|
||||
|
||||
Rust 共享库,源码位于 `src/`(Rust)。`lib.rs` 提供 WSS/TLS、控制消息、ADB smart socket 和旧协议流式转发;`transport.rs` 提供 WebRTC DataChannel、SDP 信令、独立中继回退与桌面使用的状态文件。
|
||||
|
||||
数据通道可靠、有序、二进制,单块 16 KiB,接收队列和发送积压有界。WSS 保持正常域名证书验证;指定 origin 地址只覆盖 TCP 连接目标。节点短期票据只在内存与 Authorization 中使用,状态文件不含票据、密钥或 SDP。
|
||||
@@ -14,6 +14,7 @@ use tokio_tungstenite::{
|
||||
MaybeTlsStream, WebSocketStream,
|
||||
};
|
||||
use url::Url;
|
||||
pub mod transport;
|
||||
|
||||
pub type Ws = WebSocketStream<MaybeTlsStream<TcpStream>>;
|
||||
pub const CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
|
||||
@@ -112,7 +113,7 @@ pub async fn dial(url: Url, token: &str, connect_address: Option<&str>) -> Resul
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[derive(Clone, Deserialize, Serialize)]
|
||||
pub struct Control {
|
||||
#[serde(rename = "type")]
|
||||
pub kind: String,
|
||||
@@ -122,13 +123,29 @@ pub struct Control {
|
||||
pub lease: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub session: Option<String>,
|
||||
#[serde(default)]
|
||||
pub protocol: u8,
|
||||
#[serde(default)]
|
||||
pub signal_key: Option<String>,
|
||||
#[serde(default)]
|
||||
pub ice_servers: Vec<String>,
|
||||
#[serde(default)]
|
||||
pub p2p_timeout_ms: u64,
|
||||
#[serde(default)]
|
||||
pub public_only: bool,
|
||||
#[serde(default)]
|
||||
pub routes: Vec<transport::Route>,
|
||||
#[serde(default)]
|
||||
pub sdp: Option<String>,
|
||||
#[serde(default)]
|
||||
pub node: Option<String>,
|
||||
}
|
||||
|
||||
pub async fn next_control(ws: &mut Ws) -> Result<Control> {
|
||||
loop {
|
||||
match ws.next().await.context("control connection closed")?? {
|
||||
Message::Text(text) => {
|
||||
if text.len() > 16 * 1024 {
|
||||
if text.len() > 128 * 1024 {
|
||||
bail!("control message too large");
|
||||
}
|
||||
return Ok(serde_json::from_str(&text)?);
|
||||
|
||||
@@ -0,0 +1,582 @@
|
||||
//! v2 signaling, P2P-first data transport and ordered relay fallback.
|
||||
use crate::{dial, next_control, relay, Control, NetworkArgs, Ws, IO_TIMEOUT};
|
||||
use anyhow::{bail, Context, Result};
|
||||
use bytes::Bytes;
|
||||
use futures_util::SinkExt;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
path::PathBuf,
|
||||
sync::{
|
||||
atomic::{AtomicU64, Ordering},
|
||||
Arc, Mutex,
|
||||
},
|
||||
time::Duration,
|
||||
};
|
||||
use tokio::{
|
||||
io::{AsyncReadExt, AsyncWriteExt},
|
||||
net::TcpStream,
|
||||
sync::{mpsc, watch, Notify},
|
||||
time::{interval, timeout},
|
||||
};
|
||||
use tokio_tungstenite::tungstenite::Message;
|
||||
use webrtc::{
|
||||
api::APIBuilder,
|
||||
data_channel::{data_channel_message::DataChannelMessage, RTCDataChannel},
|
||||
ice_transport::ice_server::RTCIceServer,
|
||||
peer_connection::{
|
||||
configuration::RTCConfiguration, sdp::session_description::RTCSessionDescription,
|
||||
RTCPeerConnection,
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Clone, Deserialize, Serialize)]
|
||||
pub struct Route {
|
||||
pub id: String,
|
||||
pub url: String,
|
||||
#[serde(default)]
|
||||
pub connect_address: Option<String>,
|
||||
pub ticket: String,
|
||||
}
|
||||
impl std::fmt::Debug for Route {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("Route")
|
||||
.field("id", &self.id)
|
||||
.field("url", &self.url)
|
||||
.finish_non_exhaustive()
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn send(ws: &mut Ws, value: serde_json::Value) -> Result<()> {
|
||||
timeout(
|
||||
IO_TIMEOUT,
|
||||
ws.send(Message::Text(serde_json::to_string(&value)?.into())),
|
||||
)
|
||||
.await??;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn monitor_signal(mut ws: Ws) -> Result<()> {
|
||||
let mut heartbeat = interval(Duration::from_secs(20));
|
||||
loop {
|
||||
tokio::select! {
|
||||
result=next_control(&mut ws)=>{result?;},
|
||||
_=heartbeat.tick()=>{timeout(IO_TIMEOUT,ws.send(Message::Ping(Bytes::new()))).await??;}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Channel {
|
||||
dc: Arc<RTCDataChannel>,
|
||||
incoming: mpsc::Receiver<Bytes>,
|
||||
opened: watch::Receiver<bool>,
|
||||
closed: watch::Receiver<bool>,
|
||||
writable: Arc<Notify>,
|
||||
}
|
||||
|
||||
async fn channel(dc: Arc<RTCDataChannel>) -> Channel {
|
||||
let (tx, rx) = mpsc::channel(64);
|
||||
let (opened_tx, opened) = watch::channel(false);
|
||||
let (closed_tx, closed) = watch::channel(false);
|
||||
dc.on_open(Box::new(move || {
|
||||
let tx = opened_tx.clone();
|
||||
Box::pin(async move {
|
||||
let _ = tx.send(true);
|
||||
})
|
||||
}));
|
||||
let close = closed_tx.clone();
|
||||
dc.on_close(Box::new(move || {
|
||||
let tx = close.clone();
|
||||
Box::pin(async move {
|
||||
let _ = tx.send(true);
|
||||
})
|
||||
}));
|
||||
let close = closed_tx.clone();
|
||||
dc.on_error(Box::new(move |_| {
|
||||
let tx = close.clone();
|
||||
Box::pin(async move {
|
||||
let _ = tx.send(true);
|
||||
})
|
||||
}));
|
||||
dc.on_message(Box::new(move |message: DataChannelMessage| {
|
||||
let tx = tx.clone();
|
||||
let closed = closed_tx.clone();
|
||||
Box::pin(async move {
|
||||
if message.is_string || message.data.len() > 16384 {
|
||||
let _ = closed.send(true);
|
||||
return;
|
||||
}
|
||||
if tx.send(message.data).await.is_err() {
|
||||
let _ = closed.send(true);
|
||||
}
|
||||
})
|
||||
}));
|
||||
let writable = Arc::new(Notify::new());
|
||||
let notify = writable.clone();
|
||||
dc.set_buffered_amount_low_threshold(256 * 1024).await;
|
||||
dc.on_buffered_amount_low(Box::new(move || {
|
||||
let notify = notify.clone();
|
||||
Box::pin(async move {
|
||||
notify.notify_one();
|
||||
})
|
||||
}))
|
||||
.await;
|
||||
Channel {
|
||||
dc,
|
||||
incoming: rx,
|
||||
opened,
|
||||
closed,
|
||||
writable,
|
||||
}
|
||||
}
|
||||
|
||||
impl Channel {
|
||||
async fn ready(&mut self) -> Result<()> {
|
||||
loop {
|
||||
if *self.opened.borrow() {
|
||||
return Ok(());
|
||||
}
|
||||
if *self.closed.borrow() {
|
||||
bail!("P2P data channel closed before opening")
|
||||
}
|
||||
tokio::select! {
|
||||
result=self.opened.changed()=>{result.context("P2P open event closed")?;},
|
||||
result=self.closed.changed()=>{result.context("P2P close event closed")?;}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn run(mut self, tcp: TcpStream) -> Result<()> {
|
||||
tcp.set_nodelay(true)?;
|
||||
let (mut read, mut write) = tcp.into_split();
|
||||
let dc = self.dc.clone();
|
||||
let writable = self.writable.clone();
|
||||
let outbound = async {
|
||||
let mut buffer = vec![0; 16384];
|
||||
loop {
|
||||
let size = read.read(&mut buffer).await?;
|
||||
if size == 0 {
|
||||
return Ok::<(), anyhow::Error>(());
|
||||
}
|
||||
while dc.buffered_amount().await > 1024 * 1024 {
|
||||
timeout(IO_TIMEOUT, writable.notified())
|
||||
.await
|
||||
.context("P2P send backpressure timed out")?;
|
||||
}
|
||||
let sent = timeout(
|
||||
IO_TIMEOUT,
|
||||
dc.send(&Bytes::copy_from_slice(&buffer[..size])),
|
||||
)
|
||||
.await??;
|
||||
if sent != size {
|
||||
bail!("incomplete P2P message write")
|
||||
}
|
||||
}
|
||||
};
|
||||
let inbound = async {
|
||||
while let Some(data) = self.incoming.recv().await {
|
||||
timeout(IO_TIMEOUT, write.write_all(&data)).await??;
|
||||
}
|
||||
Ok::<(), anyhow::Error>(())
|
||||
};
|
||||
tokio::select! {result=outbound=>result,result=inbound=>result,_=self.closed.changed()=>Ok(())}
|
||||
}
|
||||
}
|
||||
|
||||
struct Peer {
|
||||
pc: Arc<RTCPeerConnection>,
|
||||
channels: mpsc::Receiver<Channel>,
|
||||
public_only: bool,
|
||||
}
|
||||
impl Drop for Peer {
|
||||
fn drop(&mut self) {
|
||||
let pc = self.pc.clone();
|
||||
tokio::spawn(async move {
|
||||
let _ = pc.close().await;
|
||||
});
|
||||
}
|
||||
}
|
||||
impl Peer {
|
||||
async fn new(offerer: bool, manifest: &Control) -> Result<Self> {
|
||||
let api = APIBuilder::new().build();
|
||||
let ice_servers = manifest
|
||||
.ice_servers
|
||||
.iter()
|
||||
.map(|url| RTCIceServer {
|
||||
urls: vec![url.clone()],
|
||||
..Default::default()
|
||||
})
|
||||
.collect();
|
||||
let pc = Arc::new(
|
||||
api.new_peer_connection(RTCConfiguration {
|
||||
ice_servers,
|
||||
..Default::default()
|
||||
})
|
||||
.await?,
|
||||
);
|
||||
let (tx, rx) = mpsc::channel(1);
|
||||
if offerer {
|
||||
let dc = pc.create_data_channel("ads-adb", None).await?;
|
||||
tx.send(channel(dc).await)
|
||||
.await
|
||||
.map_err(|_| anyhow::anyhow!("P2P channel queue closed"))?;
|
||||
} else {
|
||||
pc.on_data_channel(Box::new(move |dc| {
|
||||
let tx = tx.clone();
|
||||
Box::pin(async move {
|
||||
if dc.label() != "ads-adb" {
|
||||
let _ = dc.close().await;
|
||||
return;
|
||||
}
|
||||
let _ = tx.try_send(channel(dc).await);
|
||||
})
|
||||
}));
|
||||
}
|
||||
Ok(Self {
|
||||
pc,
|
||||
channels: rx,
|
||||
public_only: manifest.public_only,
|
||||
})
|
||||
}
|
||||
|
||||
async fn description(&self, offer: bool) -> Result<String> {
|
||||
let mut complete = self.pc.gathering_complete_promise().await;
|
||||
let sdp = if offer {
|
||||
self.pc.create_offer(None).await?
|
||||
} else {
|
||||
self.pc.create_answer(None).await?
|
||||
};
|
||||
self.pc.set_local_description(sdp).await?;
|
||||
// STUN failures must not stall fallback; a partial SDP still has host candidates.
|
||||
let _ = timeout(Duration::from_millis(2200), complete.recv()).await;
|
||||
let mut sdp = self
|
||||
.pc
|
||||
.local_description()
|
||||
.await
|
||||
.context("missing local SDP")?
|
||||
.sdp;
|
||||
if self.public_only {
|
||||
sdp = sdp
|
||||
.lines()
|
||||
.filter(|line| !line.starts_with("a=candidate:") || line.contains(" typ srflx"))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\r\n")
|
||||
+ "\r\n";
|
||||
}
|
||||
Ok(sdp)
|
||||
}
|
||||
async fn take_channel(&mut self) -> Result<Channel> {
|
||||
let mut channel = self
|
||||
.channels
|
||||
.recv()
|
||||
.await
|
||||
.context("P2P channel not received")?;
|
||||
channel.ready().await?;
|
||||
Ok(channel)
|
||||
}
|
||||
}
|
||||
|
||||
async fn node_dial(network: &NetworkArgs, route: &Route) -> Result<Ws> {
|
||||
let target = NetworkArgs {
|
||||
server: route.url.clone(),
|
||||
connect_address: route.connect_address.clone(),
|
||||
allow_insecure: network.allow_insecure,
|
||||
};
|
||||
timeout(
|
||||
Duration::from_secs(4),
|
||||
dial(
|
||||
target.url("/v2/tunnel", &[])?,
|
||||
&route.ticket,
|
||||
target.connect_address.as_deref(),
|
||||
),
|
||||
)
|
||||
.await
|
||||
.context("relay node connection timed out")?
|
||||
}
|
||||
|
||||
async fn run_ws(tcp: TcpStream, data: Ws, signal: Ws) -> Result<()> {
|
||||
tokio::select! {result=relay(tcp,data)=>result,result=monitor_signal(signal)=>{result?;bail!("controller signaling closed")}}
|
||||
}
|
||||
async fn run_p2p(tcp: TcpStream, data: Channel, signal: Ws, _peer: Peer) -> Result<()> {
|
||||
tokio::select! {result=data.run(tcp)=>result,result=monitor_signal(signal)=>{result?;bail!("controller signaling closed")}}
|
||||
}
|
||||
|
||||
/// The controller carries SDP and route decisions only, never TCP payloads.
|
||||
pub async fn client_session(
|
||||
tcp: TcpStream,
|
||||
mut signal: Ws,
|
||||
network: &NetworkArgs,
|
||||
mode: &str,
|
||||
status: &StreamStatus,
|
||||
) -> Result<()> {
|
||||
let manifest = timeout(Duration::from_secs(15), next_control(&mut signal)).await??;
|
||||
if manifest.kind != "session" {
|
||||
bail!("expected session manifest")
|
||||
}
|
||||
if mode != "relay" {
|
||||
let attempt = async {
|
||||
let mut peer = Peer::new(true, &manifest).await?;
|
||||
let offer = peer.description(true).await?;
|
||||
send(&mut signal, serde_json::json!({"type":"offer","sdp":offer})).await?;
|
||||
let answer = next_control(&mut signal).await?;
|
||||
if answer.kind != "answer" {
|
||||
bail!("peer did not accept P2P offer")
|
||||
}
|
||||
peer.pc
|
||||
.set_remote_description(RTCSessionDescription::answer(
|
||||
answer.sdp.context("missing answer SDP")?,
|
||||
)?)
|
||||
.await?;
|
||||
let channel = peer.take_channel().await?;
|
||||
send(&mut signal, serde_json::json!({"type":"p2p-ready"})).await?;
|
||||
let ready = next_control(&mut signal).await?;
|
||||
if ready.kind != "p2p-ready" {
|
||||
bail!("peer did not commit P2P")
|
||||
}
|
||||
Ok::<_, anyhow::Error>((channel, peer))
|
||||
};
|
||||
match timeout(
|
||||
Duration::from_millis(manifest.p2p_timeout_ms.clamp(1000, 30000)),
|
||||
attempt,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(Ok((channel, peer))) => {
|
||||
status.set("p2p", "");
|
||||
return run_p2p(tcp, channel, signal, peer).await;
|
||||
}
|
||||
_ => {
|
||||
tracing::info!("P2P unavailable; trying relay nodes");
|
||||
}
|
||||
}
|
||||
}
|
||||
for route in &manifest.routes {
|
||||
send(
|
||||
&mut signal,
|
||||
serde_json::json!({"type":"relay","node":route.id}),
|
||||
)
|
||||
.await?;
|
||||
let response = timeout(Duration::from_secs(7), async {
|
||||
loop {
|
||||
let reply = next_control(&mut signal).await?;
|
||||
// A timed-out P2P answer or an earlier node reply can arrive late.
|
||||
if reply.node.as_deref() == Some(route.id.as_str())
|
||||
&& (reply.kind == "route-ready" || reply.kind == "route-error")
|
||||
{
|
||||
return Ok::<_, anyhow::Error>(reply.kind == "route-ready");
|
||||
}
|
||||
}
|
||||
})
|
||||
.await;
|
||||
if !matches!(response, Ok(Ok(true))) {
|
||||
continue;
|
||||
}
|
||||
match node_dial(network, route).await {
|
||||
Ok(data) => {
|
||||
send(
|
||||
&mut signal,
|
||||
serde_json::json!({"type":"route-ready","node":route.id}),
|
||||
)
|
||||
.await?;
|
||||
status.set("relay", &route.id);
|
||||
return run_ws(tcp, data, signal).await;
|
||||
}
|
||||
Err(_) => {
|
||||
send(
|
||||
&mut signal,
|
||||
serde_json::json!({"type":"route-error","node":route.id}),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
bail!("P2P unavailable and all configured relay nodes failed")
|
||||
}
|
||||
|
||||
pub async fn agent_session(tcp: TcpStream, mut signal: Ws, network: &NetworkArgs) -> Result<()> {
|
||||
let manifest = timeout(Duration::from_secs(15), next_control(&mut signal)).await??;
|
||||
if manifest.kind != "session" {
|
||||
bail!("expected agent session manifest")
|
||||
}
|
||||
let mut peer: Option<Peer> = None;
|
||||
let mut pending: Option<(String, Ws)> = None;
|
||||
loop {
|
||||
let message = next_control(&mut signal).await?;
|
||||
match message.kind.as_str() {
|
||||
"offer" => {
|
||||
let attempt = async {
|
||||
let value = Peer::new(false, &manifest).await?;
|
||||
value
|
||||
.pc
|
||||
.set_remote_description(RTCSessionDescription::offer(
|
||||
message.sdp.context("missing offer SDP")?,
|
||||
)?)
|
||||
.await?;
|
||||
let answer = value.description(false).await?;
|
||||
Ok::<_, anyhow::Error>((value, answer))
|
||||
};
|
||||
match timeout(Duration::from_secs(5), attempt).await {
|
||||
Ok(Ok((value, answer))) => {
|
||||
peer = Some(value);
|
||||
send(
|
||||
&mut signal,
|
||||
serde_json::json!({"type":"answer","sdp":answer}),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
_ => {
|
||||
send(
|
||||
&mut signal,
|
||||
serde_json::json!({"type":"route-error","node":"p2p"}),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
"p2p-ready" => {
|
||||
if let Some(mut value) = peer.take() {
|
||||
match timeout(Duration::from_secs(2), value.take_channel()).await {
|
||||
Ok(Ok(channel)) => {
|
||||
send(&mut signal, serde_json::json!({"type":"p2p-ready"})).await?;
|
||||
return run_p2p(tcp, channel, signal, value).await;
|
||||
}
|
||||
_ => {
|
||||
send(
|
||||
&mut signal,
|
||||
serde_json::json!({"type":"route-error","node":"p2p"}),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"relay" => {
|
||||
peer = None;
|
||||
pending = None;
|
||||
let id = message.node.context("missing relay node")?;
|
||||
if let Some(route) = manifest.routes.iter().find(|route| route.id == id) {
|
||||
match node_dial(network, route).await {
|
||||
Ok(data) => {
|
||||
pending = Some((id.clone(), data));
|
||||
send(
|
||||
&mut signal,
|
||||
serde_json::json!({"type":"route-ready","node":id}),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
Err(_) => {
|
||||
send(
|
||||
&mut signal,
|
||||
serde_json::json!({"type":"route-error","node":id}),
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
bail!("unknown relay node")
|
||||
}
|
||||
}
|
||||
"route-ready" => {
|
||||
if let Some((id, data)) = pending.take() {
|
||||
if message.node.as_deref() != Some(id.as_str()) {
|
||||
bail!("relay commit does not match selected node")
|
||||
}
|
||||
return run_ws(tcp, data, signal).await;
|
||||
}
|
||||
}
|
||||
"route-error" => {
|
||||
pending = None;
|
||||
}
|
||||
_ => bail!("unexpected session signal"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize)]
|
||||
struct RouteState {
|
||||
transport: String,
|
||||
node: String,
|
||||
}
|
||||
pub struct TransportStatus {
|
||||
file: Option<PathBuf>,
|
||||
next: AtomicU64,
|
||||
streams: Mutex<BTreeMap<u64, RouteState>>,
|
||||
}
|
||||
pub struct StreamStatus {
|
||||
owner: Arc<TransportStatus>,
|
||||
id: u64,
|
||||
}
|
||||
impl TransportStatus {
|
||||
pub fn new(file: Option<PathBuf>) -> Arc<Self> {
|
||||
Arc::new(Self {
|
||||
file,
|
||||
next: AtomicU64::new(1),
|
||||
streams: Mutex::new(BTreeMap::new()),
|
||||
})
|
||||
}
|
||||
pub fn stream(self: &Arc<Self>) -> StreamStatus {
|
||||
let id = self.next.fetch_add(1, Ordering::Relaxed);
|
||||
let value = StreamStatus {
|
||||
owner: self.clone(),
|
||||
id,
|
||||
};
|
||||
value.set("negotiating", "");
|
||||
value
|
||||
}
|
||||
fn publish(&self, streams: &BTreeMap<u64, RouteState>) {
|
||||
if let Some(file) = &self.file {
|
||||
let p2p = streams.values().filter(|v| v.transport == "p2p").count();
|
||||
let relay = streams
|
||||
.values()
|
||||
.filter(|v| v.transport == "relay" || v.transport == "compat")
|
||||
.count();
|
||||
let transport = if p2p > 0 && relay > 0 {
|
||||
"mixed"
|
||||
} else if p2p > 0 {
|
||||
"p2p"
|
||||
} else if relay > 0 {
|
||||
"relay"
|
||||
} else if streams.is_empty() {
|
||||
"idle"
|
||||
} else {
|
||||
"negotiating"
|
||||
};
|
||||
let nodes = streams
|
||||
.values()
|
||||
.filter(|v| !v.node.is_empty())
|
||||
.map(|v| v.node.clone())
|
||||
.collect::<std::collections::BTreeSet<_>>();
|
||||
let body = serde_json::json!({"transport":transport,"p2p_streams":p2p,"relay_streams":relay,"active_streams":streams.len(),"nodes":nodes});
|
||||
if let Some(parent) = file.parent() {
|
||||
let _ = std::fs::create_dir_all(parent);
|
||||
}
|
||||
let temp = file.with_extension("tmp");
|
||||
if std::fs::write(&temp, body.to_string()).is_ok() {
|
||||
let _ = std::fs::rename(temp, file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
impl StreamStatus {
|
||||
pub fn set(&self, transport: &str, node: &str) {
|
||||
let mut streams = self.owner.streams.lock().unwrap_or_else(|e| e.into_inner());
|
||||
streams.insert(
|
||||
self.id,
|
||||
RouteState {
|
||||
transport: transport.into(),
|
||||
node: node.into(),
|
||||
},
|
||||
);
|
||||
self.owner.publish(&streams);
|
||||
if transport != "negotiating" {
|
||||
tracing::info!(transport, node, "transport selected");
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Drop for StreamStatus {
|
||||
fn drop(&mut self) {
|
||||
let mut streams = self.owner.streams.lock().unwrap_or_else(|e| e.into_inner());
|
||||
streams.remove(&self.id);
|
||||
self.owner.publish(&streams);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
# Process management examples
|
||||
|
||||
Actual `ttstd.com` deployment and startup commands are documented in `docs/deployment-ttstd.md`.
|
||||
Actual multi-node deployment is documented in [docs/cluster.md](../docs/cluster.md). Earlier single-node deployment and startup commands are in [docs/deployment-ttstd.md](../docs/deployment-ttstd.md).
|
||||
|
||||
Examples are templates, not installed automatically. Replace paths, domain, user and tokens before use. Public endpoints require HTTPS/WSS.
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
[Unit]
|
||||
Description=ADS control center
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=ads
|
||||
WorkingDirectory=/opt/ads
|
||||
EnvironmentFile=/etc/ads/server.env
|
||||
Environment=GOMEMLIMIT=128MiB
|
||||
ExecStart=/opt/ads/ads-server --listen 127.0.0.1:18081 --cluster-config /etc/ads/cluster.json
|
||||
Restart=on-failure
|
||||
RestartSec=3
|
||||
NoNewPrivileges=true
|
||||
PrivateTmp=true
|
||||
ProtectSystem=strict
|
||||
ProtectHome=true
|
||||
MemoryMax=256M
|
||||
LimitNOFILE=8192
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -0,0 +1,25 @@
|
||||
[Unit]
|
||||
Description=ADS relay node
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=ads
|
||||
WorkingDirectory=/opt/ads
|
||||
EnvironmentFile=/etc/ads/relay.env
|
||||
Environment=GOMEMLIMIT=64MiB
|
||||
ExecStart=/opt/ads/ads-relay --id relay-2 --listen 0.0.0.0:443 --capacity 128 --tls-cert /etc/ads/tls/fullchain.pem --tls-key /etc/ads/tls/privkey.pem
|
||||
Restart=on-failure
|
||||
RestartSec=3
|
||||
AmbientCapabilities=CAP_NET_BIND_SERVICE
|
||||
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
|
||||
NoNewPrivileges=true
|
||||
PrivateTmp=true
|
||||
ProtectSystem=strict
|
||||
ProtectHome=true
|
||||
MemoryMax=128M
|
||||
LimitNOFILE=8192
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -0,0 +1,23 @@
|
||||
[Unit]
|
||||
Description=ADS relay node
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=ads
|
||||
WorkingDirectory=/opt/ads
|
||||
EnvironmentFile=/etc/ads/relay.env
|
||||
Environment=GOMEMLIMIT=64MiB
|
||||
ExecStart=/opt/ads/ads-relay --id relay-1 --listen 127.0.0.1:18082 --capacity 128
|
||||
Restart=on-failure
|
||||
RestartSec=3
|
||||
NoNewPrivileges=true
|
||||
PrivateTmp=true
|
||||
ProtectSystem=strict
|
||||
ProtectHome=true
|
||||
MemoryMax=128M
|
||||
LimitNOFILE=8192
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"nodes": [
|
||||
{"id":"relay-1","url":"wss://ttstd.com/ads-relay-1","connect_address":"175.178.213.60:443","priority":10},
|
||||
{"id":"relay-2","url":"wss://ttstd.com","connect_address":"47.242.112.133:443","priority":20}
|
||||
],
|
||||
"ice_servers": ["stun:47.242.112.133:3478", "stun:175.178.213.60:3478"],
|
||||
"p2p_timeout_ms": 10000
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
# Include inside the existing ttstd.com HTTPS server block.
|
||||
# URI prefix /ads-relay-1/ is stripped before forwarding to the relay.
|
||||
location = /ads-relay-1 {
|
||||
return 308 /ads-relay-1/;
|
||||
}
|
||||
location ^~ /ads-relay-1/ {
|
||||
proxy_pass http://127.0.0.1:18082/;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection "upgrade";
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_buffering off;
|
||||
proxy_request_buffering off;
|
||||
proxy_read_timeout 3600s;
|
||||
proxy_send_timeout 3600s;
|
||||
# Do not cache authenticated requests or log tickets in query strings.
|
||||
proxy_cache off;
|
||||
proxy_hide_header Cache-Control;
|
||||
add_header Cache-Control "no-store" always;
|
||||
access_log off;
|
||||
}
|
||||
@@ -62,6 +62,7 @@ internal static class Program
|
||||
{
|
||||
Require(window.FindControl<TextBox>("ServerBox")?.Text == RelayOptions.DefaultServer, "Server binding is wrong");
|
||||
Require(window.FindControl<TextBox>("OriginBox")?.Text == RelayOptions.DefaultOrigin, "Origin binding is wrong");
|
||||
Require(window.FindControl<CheckBox>("RelayOnlyCheck")?.IsChecked == vm.RelayOnly, "Transport preference binding is wrong");
|
||||
if (!live)
|
||||
{
|
||||
Require(window.FindControl<Button>("ConnectButton")?.IsEffectivelyEnabled == false, "Connect should be disabled before device selection");
|
||||
@@ -97,10 +98,14 @@ internal static class Program
|
||||
}
|
||||
await vm.InitializeAsync();
|
||||
await UntilAsync(() => vm.RelayOnline && vm.Devices.Any(d => d.Id == "home-phone"), "Public gateway was not discovered: " + vm.Notice);
|
||||
Require(vm.RelayNodes.Count == 2 && vm.RelayNodes.All(n => n.Healthy), "Both relay nodes should be healthy");
|
||||
vm.SelectedDevice = vm.Devices.Single(d => d.Id == "home-phone");
|
||||
Require(!vm.SelectedDevice.Busy, "The gateway is in use; smoke test will not steal its lease");
|
||||
await vm.ConnectCommand.ExecuteAsync(null);
|
||||
await UntilAsync(() => vm.Ready && vm.Model != "—", "Device did not become ready: " + vm.Notice);
|
||||
await UntilAsync(() => vm.TransportStatus.Contains("P2P") || vm.TransportStatus.Contains("中继连接"), "Transport status was not updated");
|
||||
Require(window.FindControl<TextBlock>("TransportStatusText")?.Text == vm.TransportStatus, "Actual route binding failed");
|
||||
Console.WriteLine("PASS: two healthy nodes and actual transport shown: " + vm.TransportStatus);
|
||||
Require(window.FindControl<TextBlock>("ModelText")?.Text == vm.Model, "Model UI binding failed");
|
||||
Require(window.FindControl<Button>("ReconnectButton")?.IsEffectivelyEnabled == true, "Reconnect button not enabled");
|
||||
Console.WriteLine("PASS: public device list, GUI connect, local ADB ready and model information: " + vm.Model);
|
||||
|
||||
@@ -13,6 +13,7 @@ public sealed class DesktopSettings
|
||||
public int Port { get; set; } = 15555;
|
||||
public int Size { get; set; } = 1920;
|
||||
public int Fps { get; set; } = 60;
|
||||
public bool RelayOnly { get; set; }
|
||||
public bool Audio { get; set; }
|
||||
public bool ViewOnly { get; set; }
|
||||
public bool Tray { get; set; } = true;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using Avalonia.Threading;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
@@ -23,6 +24,8 @@ public partial class MainViewModel : ObservableObject, IDisposable
|
||||
[ObservableProperty] private int port = 15555;
|
||||
[ObservableProperty] private int maxSize = 1920;
|
||||
[ObservableProperty] private int maxFps = 60;
|
||||
[ObservableProperty] private bool relayOnly;
|
||||
[ObservableProperty] private string transportStatus = "尚未建立数据通道";
|
||||
[ObservableProperty] private bool audio;
|
||||
[ObservableProperty] private bool viewOnly;
|
||||
[ObservableProperty] private bool minimizeToTray = true;
|
||||
@@ -46,13 +49,14 @@ public partial class MainViewModel : ObservableObject, IDisposable
|
||||
[ObservableProperty] private bool isWorking;
|
||||
[ObservableProperty] private bool isRefreshing;
|
||||
|
||||
public ObservableCollection<RelayNode> RelayNodes { get; } = new();
|
||||
public ObservableCollection<DeviceItem> Devices { get; } = new();
|
||||
public ObservableCollection<string> Logs { get; } = new();
|
||||
public string? Endpoint => endpoint;
|
||||
public string? ActiveDeviceId => activeId;
|
||||
|
||||
private Process? client, scrcpy;
|
||||
private string? endpoint, activeAdb, activeId;
|
||||
private string? endpoint, activeAdb, activeId, transportFile;
|
||||
private string credentialFile = "", preferredDevice = "home-phone";
|
||||
private bool monitoring, disposed, initialized;
|
||||
private DateTime lastRefresh;
|
||||
@@ -77,7 +81,7 @@ public partial class MainViewModel : ObservableObject, IDisposable
|
||||
if (settings.Adb.Length > 0) AdbPath = settings.Adb;
|
||||
if (settings.Scrcpy.Length > 0) ScrcpyPath = settings.Scrcpy;
|
||||
Port = settings.Port; MaxSize = settings.Size; MaxFps = settings.Fps;
|
||||
Audio = settings.Audio; ViewOnly = settings.ViewOnly;
|
||||
RelayOnly = settings.RelayOnly; Audio = settings.Audio; ViewOnly = settings.ViewOnly;
|
||||
MinimizeToTray = settings.Tray; AutoRefresh = settings.AutoRefresh;
|
||||
credentialFile = settings.CredentialFile;
|
||||
preferredDevice = settings.PreferredDevice;
|
||||
@@ -186,7 +190,15 @@ public partial class MainViewModel : ObservableObject, IDisposable
|
||||
foreach (var row in rows) Devices.Add(new DeviceItem(row.Id, row.Busy, HasConnection && row.Id == activeId));
|
||||
SelectedDevice = Devices.FirstOrDefault(d => d.Id == selectedId) ?? Devices.FirstOrDefault(d => !d.Busy) ?? Devices.FirstOrDefault();
|
||||
RelayOnline = true;
|
||||
RelayStatus = "中继在线";
|
||||
RelayStatus = "控制中心在线";
|
||||
try {
|
||||
var nodes = await RelayApi.GetNodesAsync(options, lifetime.Token);
|
||||
if (Server.Trim() != options.BaseUri.OriginalString || Origin.Trim() != options.Origin || Token != options.Token) return;
|
||||
RelayNodes.Clear(); foreach (var node in nodes) RelayNodes.Add(node);
|
||||
if (nodes.Length > 0) RelayStatus += $" · 中继 {nodes.Count(n => n.Healthy)}/{nodes.Length} 在线";
|
||||
} catch (Exception error) when (error is HttpRequestException or TaskCanceledException) {
|
||||
RelayNodes.Clear(); RelayStatus += " · 节点状态暂不可用";
|
||||
}
|
||||
DeviceSummary = $"{rows.Length} 台在线 · {DateTime.Now:HH:mm:ss} 更新";
|
||||
lastRefreshError = null;
|
||||
if (announce) { Notice = "设备列表已更新。"; Log($"中继在线,发现 {rows.Length} 台设备。"); }
|
||||
@@ -274,6 +286,11 @@ public partial class MainViewModel : ObservableObject, IDisposable
|
||||
var adb = DesktopPaths.ResolveTool(AdbPath, "adb");
|
||||
var address = $"127.0.0.1:{Port}";
|
||||
var info = ProcessTools.ClientStartInfo(clientExecutable, adb, relay, device.Id, address);
|
||||
Directory.CreateDirectory(folder);
|
||||
transportFile = Path.Combine(folder, "transport-" + Guid.NewGuid().ToString("N") + ".json");
|
||||
info.ArgumentList.Add("--transport"); info.ArgumentList.Add(RelayOnly ? "relay" : "auto");
|
||||
info.ArgumentList.Add("--status-file"); info.ArgumentList.Add(transportFile);
|
||||
TransportStatus = "正在协商连接路线";
|
||||
client = Start(info);
|
||||
endpoint = address; activeAdb = adb; activeId = device.Id; preferredDevice = device.Id;
|
||||
OnPropertyChanged(nameof(Endpoint)); OnPropertyChanged(nameof(ActiveDeviceId));
|
||||
@@ -326,6 +343,21 @@ public partial class MainViewModel : ObservableObject, IDisposable
|
||||
Log($"手机控制窗口已关闭,退出码 {scrcpy.ExitCode}。");
|
||||
scrcpy.Dispose(); scrcpy = null; ScreenRunning = false;
|
||||
}
|
||||
if (transportFile != null && File.Exists(transportFile))
|
||||
{
|
||||
try {
|
||||
using var state = JsonDocument.Parse(await File.ReadAllTextAsync(transportFile, lifetime.Token));
|
||||
var root = state.RootElement;
|
||||
var nodes = string.Join("、", root.GetProperty("nodes").EnumerateArray().Select(n => n.GetString()));
|
||||
TransportStatus = root.GetProperty("transport").GetString() switch {
|
||||
"p2p" => "P2P 直连 · 数据不经过中继",
|
||||
"relay" => "中继连接 · " + nodes,
|
||||
"mixed" => "P2P 与中继并用 · " + nodes,
|
||||
"negotiating" => "正在协商连接路线",
|
||||
_ => "等待 ADB 数据通道"
|
||||
};
|
||||
} catch (IOException) { } catch (JsonException) { }
|
||||
}
|
||||
var current = client;
|
||||
if (current != null)
|
||||
{
|
||||
@@ -418,6 +450,8 @@ public partial class MainViewModel : ObservableObject, IDisposable
|
||||
if (endpoint != null && activeAdb != null)
|
||||
try { await AdbAsync(activeAdb, CancellationToken.None, "disconnect", endpoint); }
|
||||
catch (Exception error) { Log(error.Message); }
|
||||
if (transportFile != null) { try { File.Delete(transportFile); } catch (IOException) { } }
|
||||
transportFile = null; TransportStatus = "尚未建立数据通道";
|
||||
endpoint = activeAdb = activeId = null;
|
||||
HasConnection = Ready = false;
|
||||
UpdateLocalOwnership();
|
||||
@@ -486,7 +520,7 @@ public partial class MainViewModel : ObservableObject, IDisposable
|
||||
DesktopPaths.SaveSettings(settingsFile, new DesktopSettings
|
||||
{
|
||||
Server = Server.Trim(), Origin = Origin.Trim(), Client = ClientPath, Adb = AdbPath, Scrcpy = ScrcpyPath,
|
||||
Port = Port, Size = MaxSize, Fps = MaxFps, Audio = Audio, ViewOnly = ViewOnly, Tray = MinimizeToTray,
|
||||
RelayOnly = RelayOnly, Port = Port, Size = MaxSize, Fps = MaxFps, Audio = Audio, ViewOnly = ViewOnly, Tray = MinimizeToTray,
|
||||
AutoRefresh = AutoRefresh, CredentialFile = credentialFile, PreferredDevice = activeId ?? SelectedDevice?.Id ?? preferredDevice
|
||||
});
|
||||
Notice = "配置已保存;令牌内容未写入配置。";
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
<StackPanel Spacing="12">
|
||||
<TextBlock Text="连接管理" Classes="section"/>
|
||||
<TextBlock x:Name="ConnectionStatusText" Text="{Binding Status}" Foreground="#66D9C0" FontSize="19" TextWrapping="Wrap"/>
|
||||
<TextBlock x:Name="TransportStatusText" Text="{Binding TransportStatus}" Foreground="#A8B7CC" TextWrapping="Wrap"/>
|
||||
<TextBlock x:Name="EndpointText" Text="{Binding ActiveEndpoint}" Classes="label" TextWrapping="Wrap"/>
|
||||
<WrapPanel><Button x:Name="ConnectButton" Content="连接所选设备" Command="{Binding ConnectCommand}"/><Button x:Name="ReconnectButton" Content="重新连接" Command="{Binding ReconnectCommand}"/><Button x:Name="DisconnectButton" Content="断开连接" Command="{Binding DisconnectCommand}"/><Button Content="复制 ADB 地址" IsEnabled="{Binding HasConnection}" Click="CopyEndpointClick"/><Button x:Name="ClearPortButton" Content="清理占用端口" Command="{Binding ClearPortCommand}"/></WrapPanel>
|
||||
<TextBlock Text="断开连接只释放本机通道,远程手机守护程序会继续在线。" TextWrapping="Wrap" Classes="label" FontSize="12"/>
|
||||
@@ -74,8 +75,9 @@
|
||||
<ScrollViewer>
|
||||
<StackPanel Margin="4,18" MaxWidth="880" Spacing="16">
|
||||
<Border Classes="card"><StackPanel>
|
||||
<TextBlock Text="公网中继" Classes="section" Margin="0,0,0,12"/>
|
||||
<TextBlock Text="中继地址(WSS,可包含路径)"/><TextBox x:Name="ServerBox" Text="{Binding Server}" Watermark="wss://ttstd.com/ads"/>
|
||||
<TextBlock Text="控制中心" Classes="section" Margin="0,0,0,12"/>
|
||||
<ItemsControl ItemsSource="{Binding RelayNodes}" Margin="0,0,0,12"><ItemsControl.ItemTemplate><DataTemplate><TextBlock Text="{Binding Description}" Foreground="#66D9C0" Margin="0,3"/></DataTemplate></ItemsControl.ItemTemplate></ItemsControl>
|
||||
<TextBlock Text="控制中心地址(WSS,可包含路径)"/><TextBox x:Name="ServerBox" Text="{Binding Server}" Watermark="wss://ttstd.com/ads"/>
|
||||
<TextBlock Text="直连服务器(可选)"/><TextBox x:Name="OriginBox" Text="{Binding Origin}" Watermark="175.178.213.60:443"/>
|
||||
<TextBlock Text="留空则按域名解析;指定公网 IP 时仍验证域名证书。当前部署使用此设置避开 Cloudflare 连接超时。" Classes="label" TextWrapping="Wrap"/>
|
||||
</StackPanel></Border>
|
||||
@@ -95,6 +97,7 @@
|
||||
</StackPanel></Border>
|
||||
<Border Classes="card"><StackPanel Spacing="12">
|
||||
<TextBlock Text="本地连接" Classes="section"/>
|
||||
<CheckBox x:Name="RelayOnlyCheck" Content="仅使用中继(关闭时 P2P 优先,失败自动选择中继节点)" IsChecked="{Binding RelayOnly}"/>
|
||||
<Grid ColumnDefinitions="*,180"><TextBlock Text="本地 ADB 端口" VerticalAlignment="Center"/><NumericUpDown Grid.Column="1" Value="{Binding Port}" Minimum="1024" Maximum="65535"/></Grid>
|
||||
<WrapPanel><Button Content="清理占用端口" Command="{Binding ClearPortCommand}" ToolTip.Tip="结束当前所选端口的监听进程"/><TextBlock Text="{Binding PortCleanupStatus}" Classes="label" TextWrapping="Wrap" MaxWidth="580" VerticalAlignment="Center"/></WrapPanel>
|
||||
<CheckBox Content="关闭窗口时保留连接并最小化到托盘" IsChecked="{Binding MinimizeToTray}"/>
|
||||
|
||||
@@ -50,6 +50,12 @@ public sealed record RelayOptions(Uri BaseUri, string Origin, string Token)
|
||||
|
||||
public sealed record RelayDevice(string Id, bool Busy);
|
||||
|
||||
public sealed record RelayNode(string Id, bool Healthy, int Sessions, int Capacity)
|
||||
{
|
||||
public string Description => $"{Id} · {(Healthy ? "在线" : "不可用")} · 通道 {Sessions}/{Capacity}";
|
||||
}
|
||||
public sealed record RelayCluster(RelayNode[] Nodes);
|
||||
|
||||
public static class RelayApi
|
||||
{
|
||||
public static HttpClient CreateClient(RelayOptions options)
|
||||
@@ -79,6 +85,18 @@ public static class RelayApi
|
||||
return new HttpClient(handler) { Timeout = TimeSpan.FromSeconds(12) };
|
||||
}
|
||||
|
||||
public static async Task<RelayNode[]> GetNodesAsync(RelayOptions options, CancellationToken cancellation = default)
|
||||
{
|
||||
using var http = CreateClient(options);
|
||||
using var request = new HttpRequestMessage(HttpMethod.Get,
|
||||
new UriBuilder(options.DevicesUri) { Path = options.BaseUri.AbsolutePath.TrimEnd('/') + "/v2/nodes" }.Uri);
|
||||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", options.Token);
|
||||
using var response = await http.SendAsync(request, cancellation);
|
||||
if (response.StatusCode == HttpStatusCode.NotFound) return [];
|
||||
response.EnsureSuccessStatusCode();
|
||||
return (await response.Content.ReadFromJsonAsync<RelayCluster>(cancellationToken: cancellation))?.Nodes ?? [];
|
||||
}
|
||||
|
||||
public static async Task<RelayDevice[]> GetDevicesAsync(RelayOptions options, CancellationToken cancellation = default)
|
||||
{
|
||||
using var http = CreateClient(options);
|
||||
|
||||
@@ -54,3 +54,7 @@ dotnet run --project Ads.Desktop.Smoke -c Debug -- --live --screen
|
||||
```
|
||||
|
||||
单元测试覆盖路径前缀、源站配置、旧默认地址迁移、角色凭据隔离、不可信证书拒绝、环境变量与保存配置。离屏测试加载真实 XAML,覆盖按钮可用状态、实际设备发现/连接/型号显示/重连/释放、scrcpy 视频纹理启动与停止。示例布局图片写入 `work/`。原生 Windows 自动化运行时本次初始化失败,因此未自动点击真实原生窗口或验证托盘菜单;不是把无头测试当作原生点击验收。
|
||||
|
||||
## 集群连接
|
||||
|
||||
地址指向控制中心。默认 P2P 优先,连接配置的“仅使用中继”可跳过直连;修改在下次连接生效。连接管理显示实际 P2P/中继节点,控制中心配置卡显示节点健康及通道数。节点健康约每 5 秒由中心探测,桌面刷新设备时读取。
|
||||
@@ -0,0 +1,74 @@
|
||||
# 控制中心、多中继与 P2P
|
||||
|
||||
## 当前部署
|
||||
|
||||
| 角色 | 主机 | 服务与入口 |
|
||||
| --- | --- | --- |
|
||||
| 控制中心 | 175.178.213.60 | ads-server,127.0.0.1:18081,`wss://ttstd.com/ads` |
|
||||
| relay-1 | 175.178.213.60 | ads-relay,127.0.0.1:18082,Nginx `wss://ttstd.com/ads-relay-1` |
|
||||
| relay-2 | 47.242.112.133 | ads-relay,443 TLS,`wss://ttstd.com`,origin 指定 47.242.112.133:443 |
|
||||
| STUN | 两台公网主机 | 复用已有 coturn 的 UDP 3478;已验证公网 Binding 响应 |
|
||||
| 设备端 | Windows 192.168.5.222 | ADS-Agent 计划任务;home-phone → 192.168.100.141:5555 |
|
||||
|
||||
两节点均使用 ttstd.com 的有效证书;origin 覆盖 TCP 地址,TLS SNI、主机名和证书校验保持开启。无需新增 DNS 才能使用当前配置。当前证书到期时间为 **2026-11-28 06:59:59 UTC**,续期后必须同步到 relay-2 的 `/etc/ads/tls/` 并重启其 ads-relay;第一台使用原 Nginx 证书。若改用不同子域名,需要分别配置 A 记录和匹配证书,再更新 cluster.json 的 URL;可去掉 connect_address 使用域名解析。
|
||||
|
||||
此处 IP 为部署信息,不包含登录密码或角色令牌。两端只需要控制中心地址和各自角色令牌,不需要知道中继共享密钥。
|
||||
|
||||
## 连接与回退
|
||||
|
||||
1. 设备端带 agent 角色注册,本地端带 client 角色取得独占租约。
|
||||
2. 中心生成会话,将 SDP 与双方各自的中继票据发给对应端。
|
||||
3. 默认通过 STUN 收集地址,以可靠有序的 WebRTC DataChannel 连接。默认也允许 LAN/VPN 候选地址;`--p2p-public-only` 仅公布公网候选用于诊断。
|
||||
4. P2P 默认最多尝试 10 秒,失败后尝试中继:健康节点优先,同一健康状态按 priority(越小越优先),相同 priority 按负载排序。每个节点连接有超时,失败尝试下一节点。
|
||||
5. 中心每 5 秒探测一次 `/healthz`。节点健康是中心视角;两端仍各自验证能否连接。
|
||||
6. 租约或 agent 控制断线会关闭会话信令;两端停止数据通道,中心向各节点发送撤销。节点保留 2 分钟撤销记录,阻止旧票据重新连接。
|
||||
|
||||
P2P 需要两端能够互通 UDP。复杂 NAT、UDP 被阻止时会使用 WSS 中继;无需为桌面或远程电脑做路由器映射。已有 TCP 流不能在故障时无缝迁移:流断开后由 ADB 每 5 秒重试并重新选路。scrcpy 会话中断后可能需要重新打开控制窗口。
|
||||
|
||||
当前是单控制中心,状态在内存,无数据库、账号多租户或控制中心高可用。中心重启后两端重连;不是多控制中心一致性系统。v1 兼容客户端仍可能通过中心转发数据,v2 的 data_bytes=0 仅描述 v2 信令端点。
|
||||
|
||||
## 配置与服务
|
||||
|
||||
- `deploy/cluster.example.json`:节点 ID、WSS URL、可选 origin、priority、STUN URL 和 P2P 超时。
|
||||
- `deploy/ads-control.service`:控制中心,读取 `/etc/ads/server.env`,需 ADS_AGENT_TOKEN、ADS_CLIENT_TOKEN、ADS_RELAY_SECRET;原先的 ads-server.service 仍作为单机模式示例。
|
||||
- `deploy/ads-relay.service`:节点 1,读取 `/etc/ads/relay.env` 中 ADS_RELAY_SECRET。
|
||||
- `deploy/ads-relay-tls.service`:节点 2,直接 TLS,使用 CAP_NET_BIND_SERVICE 绑定 443,进程以 ads 用户运行。
|
||||
- `deploy/nginx-ads-relay-path.conf`:加入现有 HTTPS server,转发 relay-1 前缀;不缓存、不记录带票据的 URL。
|
||||
- 全部节点的 ADS_RELAY_SECRET 与控制中心一致,至少 32 个随机字符;root 私有环境文件不打包进客户端。
|
||||
- 节点可以选择 `--stun-listen IP:端口` 启用内置 Binding 服务(仅 Binding、无 TURN,固定全局速率预算);当前两台部署使用已有 coturn,因此不启用此选项。
|
||||
|
||||
新增节点:构建 ads-relay、配置同一中继密钥和有效 TLS,开放其 WSS 端口;把节点加到控制中心 cluster.json 后重启中心。配置目前静态加载,重启会短暂中断已有连接。更改优先级同样需要重启。节点容量默认 256,可用 --capacity 调整;当前两台配置 128 通道。节点健康、通道数和累计转发字节可从各自 healthz 读取。
|
||||
|
||||
## 协议与边界
|
||||
|
||||
保留 `/v1/agent?v=2` 与 `/v1/lease?v=2` 协商;两端支持且中心配置集群时租约回复 protocol=2。旧端和未配置集群的中心仍使用 v1。
|
||||
|
||||
- `/v2/nodes`(client 角色):节点配置及健康、v2 signaling_bytes,data_bytes 固定为 0,因为 v2 会话端点拒绝二进制数据。
|
||||
- `/v2/connect?device=&lease=`(client):创建会话,接收含 STUN、超时和本端票据的 manifest。
|
||||
- `/v2/signal?session=&key=`(agent):使用短期会话 key 绑定,接收设备端 manifest。
|
||||
- 信令只允许 offer、answer、p2p-ready、relay、route-ready、route-error;二进制及未知消息关闭会话。单条上限 128 KiB。
|
||||
- 节点 `/v2/tunnel`:Authorization Bearer 是中心签发的 HMAC-SHA256 票据,绑定 session、lease、node、side、到期时间;签发有效 60 秒,每侧只用一次。长期角色令牌不能连接节点。
|
||||
- 节点 `/internal/revoke`:仅中继共享密钥允许撤销租约。
|
||||
- P2P 使用 DTLS/SCTP 加密;WSS 节点终止双方 TLS,属于受信基础设施,尚未在中继之上增加端到端载荷加密。
|
||||
|
||||
数据通道按 16 KiB 分块,可靠有序,接收队列及发送积压有界;每个设备/本地守护程序最多 32 并发流。票据、密钥与 SDP 不写入桌面状态文件。
|
||||
|
||||
## 验证
|
||||
|
||||
```powershell
|
||||
.\scripts\build.ps1
|
||||
python tests/cluster_e2e.py
|
||||
python tests/e2e.py
|
||||
```
|
||||
|
||||
集群测试使用真实可执行文件与模拟手机 TCP,覆盖 2 MiB 完整性、P2P 时中继零数据、强制中继、活动节点故障后同一客户端租约重建通道、P2P 不可达回退和租约撤销。Go 的 `go test -race ./...` 覆盖票据篡改/过期/角色、重放、撤销、升级失败资源清理、STUN Binding 和中心拒绝数据载荷。
|
||||
|
||||
真机只读测试可加 `--transport relay` 或 `--p2p-public-only`:
|
||||
|
||||
```powershell
|
||||
python tests/real_device.py --existing-device home-phone --relay-url wss://ttstd.com/ads --connect-address 175.178.213.60:443 --credentials-dir config/private --adb bin/tools/scrcpy/adb.exe --p2p-public-only
|
||||
```
|
||||
|
||||
Windows 的双端与桌面、Linux amd64 的控制中心/节点已验证;macOS/Linux 桌面及 Rust 两端仍待对应平台验证。
|
||||
|
||||
2026-11-12 实机结果:公网候选限定的 P2P、relay-1 强制中继、停止 relay-1 后经 relay-2 的真机 ADB 识别与 shell 均通过;测试后 relay-1 恢复。桌面读取双节点健康、显示实际路线,并通过真实 scrcpy 视频、重连和释放租约验证。
|
||||
@@ -1,5 +1,7 @@
|
||||
# ttstd.com 实际部署
|
||||
|
||||
2026-11-12 已升级为控制中心 + 两个独立中继 + P2P 优先,最新拓扑和节点配置见 [集群说明](cluster.md)。本页保留原始安装和手动启动步骤。
|
||||
|
||||
## 服务器与连接地址
|
||||
|
||||
- 服务器:`175.178.213.60`,Debian 12 amd64。
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
> v2 集群信令、WebRTC 和独立中继协议见 [cluster.md](cluster.md)。本页描述保留的 v1 兼容转发。
|
||||
|
||||
# ADS relay protocol v1
|
||||
|
||||
All requests use `Authorization: Bearer <token>`. Agent and client roles have separate shared tokens. Tickets are opaque random 192-bit values and must not be logged. Public deployments use HTTPS/WSS with normal certificate verification; TLS ends at the relay.
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
> 2026-11-12 集群验证结果见 [cluster.md](cluster.md):公网 P2P、双节点真机回退、Go race、Rust 测试及桌面真机投屏均已验证。以下保留早期单中继检查记录。
|
||||
|
||||
# Verification record
|
||||
|
||||
Validated on Windows x64 during implementation:
|
||||
|
||||
+3
-1
@@ -15,6 +15,8 @@ try {
|
||||
try {
|
||||
go build -trimpath -o (Join-Path $projectRoot 'bin/ads-server.exe') .
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Go build failed' }
|
||||
go build -trimpath -o (Join-Path $projectRoot 'bin/ads-relay.exe') ./cmd/ads-relay
|
||||
if ($LASTEXITCODE -ne 0) { throw 'Relay build failed' }
|
||||
} finally { Pop-Location }
|
||||
} finally { Pop-Location }
|
||||
Write-Host "Built server, agent and client in $projectRoot/bin"
|
||||
Write-Host "Built control server, relay node, agent and client in $projectRoot/bin"
|
||||
@@ -7,3 +7,4 @@ mkdir -p bin
|
||||
cp target/release/ads-agent target/release/ads-client bin/
|
||||
cd server
|
||||
go build -trimpath -o ../bin/ads-server .
|
||||
go build -trimpath -o ../bin/ads-relay ./cmd/ads-relay
|
||||
+10
-9
@@ -1,12 +1,13 @@
|
||||
# ads-server(Go 公网服务器)
|
||||
# ADS 控制中心与中继(Go)
|
||||
|
||||
入口:`main.go`。独立构建:`go build -o ads-server .`。
|
||||
`main.go` / `cluster.go` 是 ads-server 控制中心。`go build -o ads-server .`。
|
||||
配置 ADS_AGENT_TOKEN、ADS_CLIENT_TOKEN(不同且至少 32 字符);默认回环监听,可用 --listen 修改。
|
||||
|
||||
- 配置:环境变量 `ADS_AGENT_TOKEN`、`ADS_CLIENT_TOKEN`,要求不同且各至少 32 字符。
|
||||
- 默认监听:`127.0.0.1:8080`,用 `--listen` 修改。
|
||||
- TLS:反向代理或 `--tls-cert` 与 `--tls-key`;公网必须使用有效证书。
|
||||
- 路由:`/healthz`、`/v1/agent`、`/v1/devices`、`/v1/lease`、`/v1/connect`、`/v1/data`。
|
||||
- 状态存内存;一个 ID 对应一个在线 agent,一个设备对应一个独占租约。
|
||||
- Ctrl+C / SIGTERM 关闭控制与数据连接;两端支持重连。
|
||||
加上 `--cluster-config /etc/ads/cluster.json` 和至少 32 字符的 ADS_RELAY_SECRET 启用 v2:控制中心负责注册、独占租约、SDP 信令与节点健康,不转发 v2 ADB 数据。未配置集群则保留 v1 数据转发。公网使用有效证书或 HTTPS 反向代理。
|
||||
|
||||
部署、令牌配置和三端完整命令见项目根目录 `README.md`。
|
||||
| 内部目录 | 用途 | 语言 |
|
||||
| --- | --- | --- |
|
||||
| `cmd/ads-relay/` | 独立节点入口,`go build -o ads-relay ./cmd/ads-relay` | Go |
|
||||
| `internal/relay/` | HMAC 票据、WSS 配对、撤销、可选 STUN 和测试 | Go |
|
||||
|
||||
集群配置、角色、协议及部署见 [集群说明](../docs/cluster.md)。执行 `go test -race ./...` 检查并发行为。
|
||||
@@ -0,0 +1,343 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/subtle"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"android-device-streaming/server/internal/relay"
|
||||
"github.com/coder/websocket"
|
||||
)
|
||||
|
||||
type NodeConfig struct {
|
||||
ID string `json:"id"`
|
||||
URL string `json:"url"`
|
||||
ConnectAddress string `json:"connect_address,omitempty"`
|
||||
Priority int `json:"priority"`
|
||||
}
|
||||
type clusterConfig struct {
|
||||
Nodes []NodeConfig `json:"nodes"`
|
||||
ICEServers []string `json:"ice_servers"`
|
||||
P2PTimeoutMS int `json:"p2p_timeout_ms"`
|
||||
}
|
||||
type nodeHealth struct {
|
||||
Healthy bool `json:"healthy"`
|
||||
Sessions int `json:"sessions"`
|
||||
Capacity int `json:"capacity"`
|
||||
}
|
||||
type cluster struct {
|
||||
config clusterConfig
|
||||
secret string
|
||||
mu sync.Mutex
|
||||
health map[string]nodeHealth
|
||||
signalingBytes atomic.Int64
|
||||
}
|
||||
type route struct {
|
||||
ID string `json:"id"`
|
||||
URL string `json:"url"`
|
||||
ConnectAddress string `json:"connect_address,omitempty"`
|
||||
Ticket string `json:"ticket"`
|
||||
}
|
||||
|
||||
func loadCluster(path, secret string, allowInsecure bool) (*cluster, error) {
|
||||
if len(secret) < 32 {
|
||||
return nil, errors.New("cluster mode requires ADS_RELAY_SECRET (32+ characters)")
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var config clusterConfig
|
||||
if err = json.Unmarshal(data, &config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if config.ICEServers == nil {
|
||||
config.ICEServers = []string{}
|
||||
}
|
||||
if len(config.Nodes) == 0 || len(config.Nodes) > 32 {
|
||||
return nil, errors.New("configure 1–32 relay nodes")
|
||||
}
|
||||
if config.P2PTimeoutMS == 0 {
|
||||
config.P2PTimeoutMS = 10000
|
||||
}
|
||||
if config.P2PTimeoutMS < 1000 || config.P2PTimeoutMS > 30000 {
|
||||
return nil, errors.New("p2p_timeout_ms must be 1000–30000")
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, node := range config.Nodes {
|
||||
u, e := url.Parse(node.URL)
|
||||
if e != nil || u.Host == "" || u.User != nil || u.RawQuery != "" || u.Fragment != "" || (u.Scheme != "wss" && !(allowInsecure && u.Scheme == "ws")) || !validID(node.ID) || seen[node.ID] {
|
||||
return nil, errors.New("invalid or duplicate relay node")
|
||||
}
|
||||
if node.ConnectAddress != "" {
|
||||
if _, _, e = net.SplitHostPort(node.ConnectAddress); e != nil {
|
||||
return nil, errors.New("invalid node connect_address")
|
||||
}
|
||||
}
|
||||
seen[node.ID] = true
|
||||
}
|
||||
for _, ice := range config.ICEServers {
|
||||
if !strings.HasPrefix(ice, "stun:") {
|
||||
return nil, errors.New("only STUN URLs are accepted; fallback uses WSS relay nodes")
|
||||
}
|
||||
}
|
||||
return &cluster{config: config, secret: secret, health: map[string]nodeHealth{}}, nil
|
||||
}
|
||||
|
||||
func nodeHTTP(node NodeConfig, path string) (*http.Client, string) {
|
||||
u, _ := url.Parse(node.URL)
|
||||
if u.Scheme == "wss" {
|
||||
u.Scheme = "https"
|
||||
} else {
|
||||
u.Scheme = "http"
|
||||
}
|
||||
u.Path = strings.TrimRight(u.Path, "/") + path
|
||||
transport := &http.Transport{TLSClientConfig: &tls.Config{MinVersion: tls.VersionTLS12}, Proxy: nil}
|
||||
if node.ConnectAddress != "" {
|
||||
transport.DialContext = func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
return (&net.Dialer{Timeout: 3 * time.Second}).DialContext(ctx, network, node.ConnectAddress)
|
||||
}
|
||||
}
|
||||
return &http.Client{Transport: transport, Timeout: 3 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}, u.String()
|
||||
}
|
||||
|
||||
func (c *cluster) monitor(ctx context.Context) {
|
||||
probe := func() {
|
||||
var wg sync.WaitGroup
|
||||
for _, node := range c.config.Nodes {
|
||||
wg.Add(1)
|
||||
go func(node NodeConfig) {
|
||||
defer wg.Done()
|
||||
client, address := nodeHTTP(node, "/healthz")
|
||||
defer client.CloseIdleConnections()
|
||||
req, _ := http.NewRequestWithContext(ctx, "GET", address, nil)
|
||||
response, err := client.Do(req)
|
||||
health := nodeHealth{}
|
||||
if err == nil {
|
||||
defer response.Body.Close()
|
||||
var body struct {
|
||||
ID string `json:"id"`
|
||||
Sessions int `json:"sessions"`
|
||||
Capacity int `json:"capacity"`
|
||||
}
|
||||
if response.StatusCode == 200 && json.NewDecoder(io.LimitReader(response.Body, 4096)).Decode(&body) == nil && body.ID == node.ID && body.Capacity > 0 {
|
||||
health = nodeHealth{true, body.Sessions, body.Capacity}
|
||||
}
|
||||
}
|
||||
c.mu.Lock()
|
||||
c.health[node.ID] = health
|
||||
c.mu.Unlock()
|
||||
}(node)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
probe()
|
||||
ticker := time.NewTicker(5 * time.Second)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
probe()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *cluster) routes(session, lease, side string) []route {
|
||||
nodes := append([]NodeConfig(nil), c.config.Nodes...)
|
||||
c.mu.Lock()
|
||||
sort.SliceStable(nodes, func(i, j int) bool {
|
||||
a, b := c.health[nodes[i].ID], c.health[nodes[j].ID]
|
||||
if a.Healthy != b.Healthy {
|
||||
return a.Healthy
|
||||
}
|
||||
if nodes[i].Priority != nodes[j].Priority {
|
||||
return nodes[i].Priority < nodes[j].Priority
|
||||
}
|
||||
return float64(a.Sessions)/float64(max(a.Capacity, 1)) < float64(b.Sessions)/float64(max(b.Capacity, 1))
|
||||
})
|
||||
c.mu.Unlock()
|
||||
result := make([]route, 0, len(nodes))
|
||||
for _, node := range nodes {
|
||||
result = append(result, route{ID: node.ID, URL: node.URL, ConnectAddress: node.ConnectAddress,
|
||||
Ticket: relay.Sign(c.secret, relay.Ticket{Session: session, Lease: lease, Node: node.ID, Side: side, Expires: time.Now().Add(time.Minute).Unix()})})
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (c *cluster) revoke(lease string) {
|
||||
if c == nil {
|
||||
return
|
||||
}
|
||||
body, _ := json.Marshal(map[string]string{"lease": lease})
|
||||
var wg sync.WaitGroup
|
||||
for _, node := range c.config.Nodes {
|
||||
wg.Add(1)
|
||||
go func(node NodeConfig) {
|
||||
defer wg.Done()
|
||||
client, address := nodeHTTP(node, "/internal/revoke")
|
||||
defer client.CloseIdleConnections()
|
||||
request, _ := http.NewRequest("POST", address, bytes.NewReader(body))
|
||||
request.Header.Set("Authorization", "Bearer "+c.secret)
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
if response, err := client.Do(request); err == nil {
|
||||
response.Body.Close()
|
||||
}
|
||||
}(node)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func (b *broker) nodesEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
if b.cluster == nil {
|
||||
http.Error(w, "cluster not configured", 404)
|
||||
return
|
||||
}
|
||||
b.cluster.mu.Lock()
|
||||
defer b.cluster.mu.Unlock()
|
||||
type node struct {
|
||||
NodeConfig
|
||||
nodeHealth
|
||||
}
|
||||
nodes := []node{}
|
||||
for _, configured := range b.cluster.config.Nodes {
|
||||
nodes = append(nodes, node{configured, b.cluster.health[configured.ID]})
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]any{"nodes": nodes, "signaling_bytes": b.cluster.signalingBytes.Load(), "data_bytes": 0})
|
||||
}
|
||||
|
||||
func (b *broker) clusterConnectEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
if b.cluster == nil {
|
||||
http.Error(w, "cluster not configured", 404)
|
||||
return
|
||||
}
|
||||
id, leaseID := r.URL.Query().Get("device"), r.URL.Query().Get("lease")
|
||||
b.mu.Lock()
|
||||
a, l := b.agents[id], b.leases[id]
|
||||
if a == nil || !a.v2 || a.ws == nil || l == nil || l.ws == nil || l.id != leaseID {
|
||||
b.mu.Unlock()
|
||||
http.Error(w, "no active v2 lease", 409)
|
||||
return
|
||||
}
|
||||
count := 0
|
||||
for _, s := range b.sessions {
|
||||
if s.agent == a {
|
||||
count++
|
||||
}
|
||||
}
|
||||
if count >= 32 {
|
||||
b.mu.Unlock()
|
||||
http.Error(w, "stream limit reached", 429)
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithCancel(b.ctx)
|
||||
s := &session{id: newID(), agent: a, lease: l, ready: make(chan *websocket.Conn, 1), ctx: ctx, cancel: cancel, v2: true, signalKey: newID()}
|
||||
b.sessions[s.id] = s
|
||||
b.mu.Unlock()
|
||||
defer func() { cancel(); b.mu.Lock(); delete(b.sessions, s.id); b.mu.Unlock() }()
|
||||
client, err := accept(w, r)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer client.CloseNow()
|
||||
publicOnly := r.URL.Query().Get("public_only") == "1"
|
||||
manifest := func(side string) map[string]any {
|
||||
return map[string]any{"type": "session", "protocol": 2, "session": s.id,
|
||||
"ice_servers": b.cluster.config.ICEServers, "p2p_timeout_ms": b.cluster.config.P2PTimeoutMS, "public_only": publicOnly, "routes": b.cluster.routes(s.id, l.id, side)}
|
||||
}
|
||||
if writeJSON(ctx, client, manifest("client")) != nil {
|
||||
return
|
||||
}
|
||||
if writeJSON(ctx, a.ws, map[string]any{"type": "open_v2", "session": s.id, "signal_key": s.signalKey}) != nil {
|
||||
return
|
||||
}
|
||||
timer := time.NewTimer(15 * time.Second)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case peer := <-s.ready:
|
||||
defer peer.CloseNow()
|
||||
if writeJSON(ctx, peer, manifest("agent")) != nil {
|
||||
return
|
||||
}
|
||||
b.bridgeSignals(ctx, client, peer)
|
||||
case <-ctx.Done():
|
||||
case <-timer.C:
|
||||
}
|
||||
}
|
||||
|
||||
func (b *broker) signalEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
b.mu.Lock()
|
||||
s := b.sessions[r.URL.Query().Get("session")]
|
||||
if s == nil || !s.v2 || s.claimed || s.ctx.Err() != nil || b.agents[s.agent.id] != s.agent ||
|
||||
subtle.ConstantTimeCompare([]byte(s.signalKey), []byte(r.URL.Query().Get("key"))) != 1 {
|
||||
b.mu.Unlock()
|
||||
http.Error(w, "invalid signaling ticket", 404)
|
||||
return
|
||||
}
|
||||
s.claimed = true
|
||||
b.mu.Unlock()
|
||||
ws, err := accept(w, r)
|
||||
if err != nil {
|
||||
s.cancel()
|
||||
return
|
||||
}
|
||||
defer ws.CloseNow()
|
||||
select {
|
||||
case s.ready <- ws:
|
||||
case <-s.ctx.Done():
|
||||
return
|
||||
}
|
||||
<-s.ctx.Done()
|
||||
}
|
||||
|
||||
func (b *broker) bridgeSignals(parent context.Context, a, z *websocket.Conn) {
|
||||
ctx, cancel := context.WithCancel(parent)
|
||||
defer cancel()
|
||||
copyOne := func(dst, src *websocket.Conn) {
|
||||
defer cancel()
|
||||
for {
|
||||
typ, data, err := src.Read(ctx)
|
||||
if err != nil || typ != websocket.MessageText {
|
||||
return
|
||||
}
|
||||
var message struct {
|
||||
Type string `json:"type"`
|
||||
}
|
||||
if json.Unmarshal(data, &message) != nil {
|
||||
return
|
||||
}
|
||||
switch message.Type {
|
||||
case "offer", "answer", "relay", "route-ready", "route-error", "p2p-ready":
|
||||
default:
|
||||
return
|
||||
}
|
||||
deadline, stop := context.WithTimeout(ctx, 10*time.Second)
|
||||
err = dst.Write(deadline, typ, data)
|
||||
stop()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
b.cluster.signalingBytes.Add(int64(len(data)))
|
||||
}
|
||||
}
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(2)
|
||||
go func() { defer wg.Done(); copyOne(a, z) }()
|
||||
go func() { defer wg.Done(); copyOne(z, a) }()
|
||||
wg.Wait()
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/coder/websocket"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func readV2(t *testing.T, ws *websocket.Conn) map[string]any {
|
||||
t.Helper()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
_, data, err := ws.Read(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var result map[string]any
|
||||
if json.Unmarshal(data, &result) != nil {
|
||||
t.Fatal("invalid json")
|
||||
}
|
||||
return result
|
||||
}
|
||||
func TestV2SignalRoleScopeAndPayloadRejection(t *testing.T) {
|
||||
b, base := testServer(t)
|
||||
b.cluster = &cluster{secret: strings.Repeat("s", 32), config: clusterConfig{Nodes: []NodeConfig{{ID: "n", URL: "ws://127.0.0.1:1"}}, ICEServers: []string{}, P2PTimeoutMS: 1000}, health: map[string]nodeHealth{}}
|
||||
for _, path := range []string{"/v2/nodes", "/v2/connect"} {
|
||||
if got := status(t, base, path, testAgentToken); got != 401 {
|
||||
t.Fatal(got)
|
||||
}
|
||||
}
|
||||
if got := status(t, base, "/v2/signal", testClientToken); got != 401 {
|
||||
t.Fatal(got)
|
||||
}
|
||||
agent := dialTest(t, base, "/v1/agent?device=phone&v=2", testAgentToken)
|
||||
readV2(t, agent)
|
||||
owner := dialTest(t, base, "/v1/lease?device=phone&v=2", testClientToken)
|
||||
lease := readV2(t, owner)
|
||||
if lease["protocol"] != float64(2) {
|
||||
t.Fatal(lease)
|
||||
}
|
||||
client := dialTest(t, base, "/v2/connect?device=phone&lease="+lease["lease"].(string), testClientToken)
|
||||
manifest := readV2(t, client)
|
||||
open := readV2(t, agent)
|
||||
if manifest["protocol"] != float64(2) || open["type"] != "open_v2" {
|
||||
t.Fatal(manifest, open)
|
||||
}
|
||||
if got := status(t, base, "/v2/signal?session="+open["session"].(string)+"&key=wrong", testAgentToken); got != 404 {
|
||||
t.Fatal(got)
|
||||
}
|
||||
signal := dialTest(t, base, "/v2/signal?session="+open["session"].(string)+"&key="+open["signal_key"].(string), testAgentToken)
|
||||
readV2(t, signal)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
if err := client.Write(ctx, websocket.MessageText, []byte(`{"type":"offer","sdp":"test"}`)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if readV2(t, signal)["sdp"] != "test" {
|
||||
t.Fatal("signal not forwarded")
|
||||
}
|
||||
if err := client.Write(ctx, websocket.MessageBinary, []byte("adb-payload")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, _, err := signal.Read(ctx); err == nil {
|
||||
t.Fatal("controller accepted binary data")
|
||||
}
|
||||
}
|
||||
func TestSignalBridgeRejectsUnknownMessages(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
b := newBroker(ctx, testAgentToken, testClientToken)
|
||||
b.cluster = &cluster{}
|
||||
mux := http.NewServeMux()
|
||||
var first *websocket.Conn
|
||||
ready := make(chan struct{})
|
||||
mux.HandleFunc("/first", func(w http.ResponseWriter, r *http.Request) {
|
||||
a, e := accept(w, r)
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
defer a.CloseNow()
|
||||
first = a
|
||||
close(ready)
|
||||
<-ctx.Done()
|
||||
})
|
||||
mux.HandleFunc("/second", func(w http.ResponseWriter, r *http.Request) {
|
||||
a, e := accept(w, r)
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
defer a.CloseNow()
|
||||
<-ready
|
||||
b.bridgeSignals(ctx, first, a)
|
||||
})
|
||||
srv := httptest.NewServer(mux)
|
||||
defer srv.Close()
|
||||
a := dialTest(t, srv.URL, "/first", testClientToken)
|
||||
z := dialTest(t, srv.URL, "/second", testClientToken)
|
||||
deadline, stop := context.WithTimeout(ctx, time.Second)
|
||||
defer stop()
|
||||
a.Write(deadline, websocket.MessageText, []byte(`{"type":"arbitrary"}`))
|
||||
if _, _, err := z.Read(deadline); err == nil {
|
||||
t.Fatal("unknown signal accepted")
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
# ads-relay
|
||||
|
||||
Standalone Go data-plane node. Build from `server/` with `go build -o ../bin/ads-relay ./cmd/ads-relay`. Set `ADS_RELAY_SECRET` to the controller's private signing key, and run behind a TLS reverse proxy. `--id` must match the controller node configuration.
|
||||
@@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"android-device-streaming/server/internal/relay"
|
||||
"context"
|
||||
"flag"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
id := flag.String("id", "relay-1", "Unique node ID")
|
||||
listen := flag.String("listen", "127.0.0.1:18082", "Listen address behind a WSS reverse proxy")
|
||||
capacity := flag.Int("capacity", 256, "Maximum concurrent paired streams")
|
||||
cert := flag.String("tls-cert", "", "TLS certificate path")
|
||||
key := flag.String("tls-key", "", "TLS private key path")
|
||||
stunAddress := flag.String("stun-listen", "", "Optional UDP STUN binding address")
|
||||
flag.Parse()
|
||||
secret := os.Getenv("ADS_RELAY_SECRET")
|
||||
if len(secret) < 32 || *id == "" || *capacity < 1 {
|
||||
log.Fatal("provide a node ID, positive capacity and ADS_RELAY_SECRET (32+ characters)")
|
||||
}
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
if *stunAddress != "" {
|
||||
address, err := net.ResolveUDPAddr("udp", *stunAddress)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
conn, err := net.ListenUDP("udp", address)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
go func() {
|
||||
if err := relay.ServeSTUN(ctx, conn); err != nil {
|
||||
log.Printf("STUN stopped: %v", err)
|
||||
stop()
|
||||
}
|
||||
}()
|
||||
}
|
||||
node := relay.NewNode(ctx, *id, secret, *capacity)
|
||||
server := &http.Server{Addr: *listen, Handler: node.Handler(), ReadHeaderTimeout: 10 * time.Second, IdleTimeout: 60 * time.Second, MaxHeaderBytes: 16 * 1024}
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
node.Close()
|
||||
shutdown, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
server.Shutdown(shutdown)
|
||||
}()
|
||||
log.Printf("relay node %s listening on %s", *id, *listen)
|
||||
var err error
|
||||
if *cert != "" && *key != "" {
|
||||
err = server.ListenAndServeTLS(*cert, *key)
|
||||
} else {
|
||||
err = server.ListenAndServe()
|
||||
}
|
||||
if err != nil && err != http.ErrServerClosed {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
+13
-1
@@ -2,4 +2,16 @@ module android-device-streaming/server
|
||||
|
||||
go 1.24.0
|
||||
|
||||
require github.com/coder/websocket v1.8.14
|
||||
require (
|
||||
github.com/coder/websocket v1.8.14
|
||||
github.com/pion/stun/v3 v3.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/pion/dtls/v3 v3.0.7 // indirect
|
||||
github.com/pion/logging v0.2.4 // indirect
|
||||
github.com/pion/transport/v3 v3.0.8 // indirect
|
||||
github.com/wlynxg/anet v0.0.5 // indirect
|
||||
golang.org/x/crypto v0.32.0 // indirect
|
||||
golang.org/x/sys v0.29.0 // indirect
|
||||
)
|
||||
@@ -1,2 +1,26 @@
|
||||
github.com/coder/websocket v1.8.14 h1:9L0p0iKiNOibykf283eHkKUHHrpG7f65OE3BhhO7v9g=
|
||||
github.com/coder/websocket v1.8.14/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6pumgx0mVg=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/pion/dtls/v3 v3.0.7 h1:bItXtTYYhZwkPFk4t1n3Kkf5TDrfj6+4wG+CZR8uI9Q=
|
||||
github.com/pion/dtls/v3 v3.0.7/go.mod h1:uDlH5VPrgOQIw59irKYkMudSFprY9IEFCqz/eTz16f8=
|
||||
github.com/pion/logging v0.2.4 h1:tTew+7cmQ+Mc1pTBLKH2puKsOvhm32dROumOZ655zB8=
|
||||
github.com/pion/logging v0.2.4/go.mod h1:DffhXTKYdNZU+KtJ5pyQDjvOAh/GsNSyv1lbkFbe3so=
|
||||
github.com/pion/stun/v3 v3.0.1 h1:jx1uUq6BdPihF0yF33Jj2mh+C9p0atY94IkdnW174kA=
|
||||
github.com/pion/stun/v3 v3.0.1/go.mod h1:RHnvlKFg+qHgoKIqtQWMOJF52wsImCAf/Jh5GjX+4Tw=
|
||||
github.com/pion/transport/v3 v3.0.8 h1:oI3myyYnTKUSTthu/NZZ8eu2I5sHbxbUNNFW62olaYc=
|
||||
github.com/pion/transport/v3 v3.0.8/go.mod h1:+c2eewC5WJQHiAA46fkMMzoYZSuGzA/7E2FPrOYHctQ=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
github.com/wlynxg/anet v0.0.5 h1:J3VJGi1gvo0JwZ/P1/Yc/8p63SoW98B5dHkYDmpgvvU=
|
||||
github.com/wlynxg/anet v0.0.5/go.mod h1:eay5PRQr7fIVAMbTbchTnO9gG65Hg/uYGdc7mguHxoA=
|
||||
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
|
||||
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
|
||||
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
|
||||
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
|
||||
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
|
||||
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
@@ -0,0 +1,3 @@
|
||||
# Relay internals
|
||||
|
||||
Go package implementing short-lived HMAC route tickets, replay/revocation checks, bounded WebSocket stream pairing node health/load counters and optional rate-bounded STUN Binding. Shared by the controller and the standalone node binary. This directory contains Go source; no credentials are stored here.
|
||||
@@ -0,0 +1,204 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/subtle"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
)
|
||||
|
||||
type pair struct {
|
||||
lease string
|
||||
peers map[string]*websocket.Conn
|
||||
ready chan struct{}
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
expires int64
|
||||
}
|
||||
|
||||
type Node struct {
|
||||
ID, Secret string
|
||||
Capacity int
|
||||
mu sync.Mutex
|
||||
pairs map[string]*pair
|
||||
used map[string]int64
|
||||
revoked map[string]int64
|
||||
bytes atomic.Int64
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func NewNode(ctx context.Context, id, secret string, capacity int) *Node {
|
||||
return &Node{ID: id, Secret: secret, Capacity: capacity, ctx: ctx, pairs: make(map[string]*pair), used: make(map[string]int64), revoked: make(map[string]int64)}
|
||||
}
|
||||
|
||||
func (n *Node) cleanupLocked() {
|
||||
now := time.Now().Unix()
|
||||
for id, expiry := range n.used {
|
||||
if expiry <= now {
|
||||
delete(n.used, id)
|
||||
}
|
||||
}
|
||||
for id, expiry := range n.revoked {
|
||||
if expiry <= now {
|
||||
delete(n.revoked, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Node) Handler() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, r *http.Request) {
|
||||
n.mu.Lock()
|
||||
count := len(n.pairs)
|
||||
n.mu.Unlock()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]any{"id": n.ID, "sessions": count, "capacity": n.Capacity, "bytes": n.bytes.Load()})
|
||||
})
|
||||
mux.HandleFunc("POST /internal/revoke", n.revoke)
|
||||
mux.HandleFunc("GET /v2/tunnel", n.tunnel)
|
||||
return mux
|
||||
}
|
||||
|
||||
func (n *Node) revoke(w http.ResponseWriter, r *http.Request) {
|
||||
if subtle.ConstantTimeCompare([]byte(r.Header.Get("Authorization")), []byte("Bearer "+n.Secret)) != 1 {
|
||||
http.Error(w, "unauthorized", 401)
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Lease string `json:"lease"`
|
||||
}
|
||||
if json.NewDecoder(io.LimitReader(r.Body, 4096)).Decode(&body) != nil || body.Lease == "" {
|
||||
http.Error(w, "bad request", 400)
|
||||
return
|
||||
}
|
||||
n.mu.Lock()
|
||||
n.cleanupLocked()
|
||||
n.revoked[body.Lease] = time.Now().Add(2 * time.Minute).Unix()
|
||||
for _, p := range n.pairs {
|
||||
if p.lease == body.Lease {
|
||||
p.cancel()
|
||||
}
|
||||
}
|
||||
n.mu.Unlock()
|
||||
w.WriteHeader(204)
|
||||
}
|
||||
|
||||
func (n *Node) tunnel(w http.ResponseWriter, r *http.Request) {
|
||||
ticket, err := Verify(n.Secret, strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer "), n.ID, time.Now())
|
||||
if err != nil {
|
||||
http.Error(w, "unauthorized", 401)
|
||||
return
|
||||
}
|
||||
n.mu.Lock()
|
||||
n.cleanupLocked()
|
||||
key := ticket.Session + ":" + ticket.Side
|
||||
if n.revoked[ticket.Lease] > 0 || n.used[key] > 0 {
|
||||
n.mu.Unlock()
|
||||
http.Error(w, "revoked or replayed ticket", 409)
|
||||
return
|
||||
}
|
||||
p := n.pairs[ticket.Session]
|
||||
if p == nil {
|
||||
if len(n.pairs) >= n.Capacity {
|
||||
n.mu.Unlock()
|
||||
http.Error(w, "node full", 429)
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithCancel(n.ctx)
|
||||
p = &pair{lease: ticket.Lease, peers: make(map[string]*websocket.Conn), ready: make(chan struct{}), ctx: ctx, cancel: cancel, expires: ticket.Expires}
|
||||
n.pairs[ticket.Session] = p
|
||||
}
|
||||
if p.lease != ticket.Lease || p.ctx.Err() != nil {
|
||||
n.mu.Unlock()
|
||||
http.Error(w, "session closed", 409)
|
||||
return
|
||||
}
|
||||
n.used[key] = ticket.Expires
|
||||
n.mu.Unlock()
|
||||
defer func() {
|
||||
p.cancel()
|
||||
n.mu.Lock()
|
||||
if n.pairs[ticket.Session] == p {
|
||||
delete(n.pairs, ticket.Session)
|
||||
}
|
||||
n.mu.Unlock()
|
||||
}()
|
||||
ws, err := websocket.Accept(w, r, nil)
|
||||
if err != nil {
|
||||
p.cancel()
|
||||
return
|
||||
}
|
||||
defer ws.CloseNow()
|
||||
ws.SetReadLimit(128 * 1024)
|
||||
n.mu.Lock()
|
||||
if p.ctx.Err() != nil {
|
||||
n.mu.Unlock()
|
||||
return
|
||||
}
|
||||
p.peers[ticket.Side] = ws
|
||||
if len(p.peers) == 2 {
|
||||
close(p.ready)
|
||||
}
|
||||
n.mu.Unlock()
|
||||
timer := time.NewTimer(15 * time.Second)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-p.ready:
|
||||
case <-p.ctx.Done():
|
||||
return
|
||||
case <-timer.C:
|
||||
return
|
||||
}
|
||||
// Only the client handler owns copying; both handlers remain until it ends.
|
||||
if ticket.Side == "client" {
|
||||
n.mu.Lock()
|
||||
agent := p.peers["agent"]
|
||||
n.mu.Unlock()
|
||||
n.copy(p.ctx, ws, agent)
|
||||
p.cancel()
|
||||
} else {
|
||||
<-p.ctx.Done()
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Node) copy(parent context.Context, a, b *websocket.Conn) {
|
||||
ctx, cancel := context.WithCancel(parent)
|
||||
defer cancel()
|
||||
var wg sync.WaitGroup
|
||||
copyOne := func(dst, src *websocket.Conn) {
|
||||
defer wg.Done()
|
||||
defer cancel()
|
||||
for {
|
||||
typ, data, err := src.Read(ctx)
|
||||
if err != nil || typ != websocket.MessageBinary {
|
||||
return
|
||||
}
|
||||
deadline, stop := context.WithTimeout(ctx, 30*time.Second)
|
||||
err = dst.Write(deadline, typ, data)
|
||||
stop()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
n.bytes.Add(int64(len(data)))
|
||||
}
|
||||
}
|
||||
wg.Add(2)
|
||||
go copyOne(a, b)
|
||||
go copyOne(b, a)
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func (n *Node) Close() {
|
||||
n.mu.Lock()
|
||||
defer n.mu.Unlock()
|
||||
for _, p := range n.pairs {
|
||||
p.cancel()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/coder/websocket"
|
||||
"github.com/pion/stun/v3"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const secret = "test-cluster-secret-at-least-thirty-two-bytes"
|
||||
|
||||
func TestTicketScope(t *testing.T) {
|
||||
now := time.Now()
|
||||
good := Ticket{"s", "l", "n", "client", now.Add(time.Minute).Unix()}
|
||||
encoded := Sign(secret, good)
|
||||
if _, err := Verify(secret, encoded, "n", now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cases := []struct {
|
||||
token, node string
|
||||
when time.Time
|
||||
}{{encoded, "other", now}, {encoded, "n", now.Add(2 * time.Minute)}, {encoded + "x", "n", now}, {Sign(secret, Ticket{"s", "l", "n", "admin", good.Expires}), "n", now}, {Sign(secret, Ticket{"s", "l", "n", "agent", now.Add(time.Hour).Unix()}), "n", now}}
|
||||
for _, c := range cases {
|
||||
if _, err := Verify(secret, c.token, c.node, c.when); err == nil {
|
||||
t.Fatal("accepted invalid ticket")
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestNodePairReplayAndRevocation(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
n := NewNode(ctx, "n", secret, 2)
|
||||
defer n.Close()
|
||||
s := httptest.NewServer(n.Handler())
|
||||
defer s.Close()
|
||||
ticket := func(side string) string {
|
||||
return Sign(secret, Ticket{"s", "l", "n", side, time.Now().Add(time.Minute).Unix()})
|
||||
}
|
||||
dial := func(token string) (*websocket.Conn, *http.Response, error) {
|
||||
return websocket.Dial(ctx, strings.Replace(s.URL, "http:", "ws:", 1)+"/v2/tunnel", &websocket.DialOptions{HTTPHeader: http.Header{"Authorization": []string{"Bearer " + token}}})
|
||||
}
|
||||
if c, r, e := dial(secret); e == nil {
|
||||
c.CloseNow()
|
||||
t.Fatal("accepted master secret as ticket")
|
||||
} else if r.StatusCode != 401 {
|
||||
t.Fatal(r.StatusCode)
|
||||
}
|
||||
a, _, err := dial(ticket("agent"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer a.CloseNow()
|
||||
c, _, err := dial(ticket("client"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer c.CloseNow()
|
||||
if other, r, e := dial(ticket("client")); e == nil {
|
||||
other.CloseNow()
|
||||
t.Fatal("replay accepted")
|
||||
} else if r.StatusCode != 409 {
|
||||
t.Fatal(r.StatusCode)
|
||||
}
|
||||
if err = c.Write(ctx, websocket.MessageBinary, []byte("adb")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
kind, data, err := a.Read(ctx)
|
||||
if err != nil || kind != websocket.MessageBinary || string(data) != "adb" {
|
||||
t.Fatal(kind, string(data), err)
|
||||
}
|
||||
req, _ := http.NewRequest("POST", s.URL+"/internal/revoke", bytes.NewBufferString(`{"lease":"l"}`))
|
||||
req.Header.Set("Authorization", "Bearer "+secret)
|
||||
response, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
response.Body.Close()
|
||||
if response.StatusCode != 204 {
|
||||
t.Fatal(response.StatusCode)
|
||||
}
|
||||
if _, _, err := c.Read(ctx); err == nil {
|
||||
t.Fatal("revocation did not close stream")
|
||||
}
|
||||
if other, r, e := dial(ticket("agent")); e == nil {
|
||||
other.CloseNow()
|
||||
t.Fatal("revoked ticket accepted")
|
||||
} else if r.StatusCode != 409 {
|
||||
t.Fatal(r.StatusCode)
|
||||
}
|
||||
}
|
||||
func TestFailedUpgradeReleasesCapacity(t *testing.T) {
|
||||
n := NewNode(context.Background(), "n", secret, 1)
|
||||
defer n.Close()
|
||||
req := httptest.NewRequest("GET", "/v2/tunnel", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+Sign(secret, Ticket{"s", "l", "n", "agent", time.Now().Add(time.Minute).Unix()}))
|
||||
n.Handler().ServeHTTP(httptest.NewRecorder(), req)
|
||||
recorder := httptest.NewRecorder()
|
||||
n.Handler().ServeHTTP(recorder, httptest.NewRequest("GET", "/healthz", nil))
|
||||
var value struct{ Sessions int }
|
||||
json.Unmarshal(recorder.Body.Bytes(), &value)
|
||||
if value.Sessions != 0 {
|
||||
t.Fatal(value)
|
||||
}
|
||||
}
|
||||
func TestSTUNBinding(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
conn, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1)})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
go ServeSTUN(ctx, conn)
|
||||
client, err := net.DialUDP("udp", nil, conn.LocalAddr().(*net.UDPAddr))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer client.Close()
|
||||
client.SetDeadline(time.Now().Add(time.Second))
|
||||
request := stun.MustBuild(stun.TransactionID, stun.BindingRequest)
|
||||
client.Write(request.Raw)
|
||||
buffer := make([]byte, 1500)
|
||||
size, err := client.Read(buffer)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
response := &stun.Message{Raw: buffer[:size]}
|
||||
if err = response.Decode(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var mapped stun.XORMappedAddress
|
||||
if err = mapped.GetFrom(response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if mapped.Port != client.LocalAddr().(*net.UDPAddr).Port || !mapped.IP.Equal(net.IPv4(127, 0, 0, 1)) || response.TransactionID != request.TransactionID {
|
||||
t.Fatal(mapped)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/pion/stun/v3"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ServeSTUN handles binding only; no TURN allocations or arbitrary destinations.
|
||||
// A fixed global budget bounds work and reflection traffic without a per-IP map.
|
||||
func ServeSTUN(ctx context.Context, conn *net.UDPConn) error {
|
||||
go func() { <-ctx.Done(); conn.Close() }()
|
||||
buffer := make([]byte, 1500)
|
||||
budget, reset := 100, time.Now()
|
||||
for {
|
||||
size, address, err := conn.ReadFromUDP(buffer)
|
||||
if err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
if time.Since(reset) >= time.Second {
|
||||
budget = 100
|
||||
reset = time.Now()
|
||||
}
|
||||
if budget == 0 || size < 20 || size > 1200 {
|
||||
continue
|
||||
}
|
||||
budget--
|
||||
message := &stun.Message{Raw: buffer[:size]}
|
||||
if message.Decode() != nil || message.Type != stun.BindingRequest {
|
||||
continue
|
||||
}
|
||||
response, err := stun.Build(stun.NewTransactionIDSetter(message.TransactionID), stun.BindingSuccess,
|
||||
&stun.XORMappedAddress{IP: address.IP, Port: address.Port})
|
||||
if err == nil {
|
||||
conn.WriteToUDP(response.Raw, address)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package relay
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Ticket struct {
|
||||
Session string `json:"session"`
|
||||
Lease string `json:"lease"`
|
||||
Node string `json:"node"`
|
||||
Side string `json:"side"`
|
||||
Expires int64 `json:"expires"`
|
||||
}
|
||||
|
||||
func Sign(secret string, ticket Ticket) string {
|
||||
payload, _ := json.Marshal(ticket)
|
||||
encoded := base64.RawURLEncoding.EncodeToString(payload)
|
||||
mac := hmac.New(sha256.New, []byte(secret))
|
||||
mac.Write([]byte(encoded))
|
||||
return encoded + "." + base64.RawURLEncoding.EncodeToString(mac.Sum(nil))
|
||||
}
|
||||
|
||||
func Verify(secret, encoded, node string, now time.Time) (Ticket, error) {
|
||||
var ticket Ticket
|
||||
parts := strings.Split(encoded, ".")
|
||||
if len(parts) != 2 {
|
||||
return ticket, errors.New("invalid ticket")
|
||||
}
|
||||
signature, err := base64.RawURLEncoding.DecodeString(parts[1])
|
||||
if err != nil {
|
||||
return ticket, errors.New("invalid ticket")
|
||||
}
|
||||
mac := hmac.New(sha256.New, []byte(secret))
|
||||
mac.Write([]byte(parts[0]))
|
||||
if !hmac.Equal(signature, mac.Sum(nil)) {
|
||||
return ticket, errors.New("invalid ticket")
|
||||
}
|
||||
payload, err := base64.RawURLEncoding.DecodeString(parts[0])
|
||||
if err != nil || json.Unmarshal(payload, &ticket) != nil {
|
||||
return ticket, errors.New("invalid ticket")
|
||||
}
|
||||
if ticket.Node != node || ticket.Expires <= now.Unix() || ticket.Expires > now.Add(2*time.Minute).Unix() || ticket.Session == "" || ticket.Lease == "" || (ticket.Side != "client" && ticket.Side != "agent") {
|
||||
return ticket, errors.New("expired or invalid ticket")
|
||||
}
|
||||
return ticket, nil
|
||||
}
|
||||
+41
-11
@@ -25,6 +25,7 @@ import (
|
||||
type agent struct {
|
||||
id string
|
||||
ws *websocket.Conn
|
||||
v2 bool
|
||||
}
|
||||
type lease struct {
|
||||
id string
|
||||
@@ -32,13 +33,15 @@ type lease struct {
|
||||
ws *websocket.Conn
|
||||
}
|
||||
type session struct {
|
||||
id string
|
||||
agent *agent
|
||||
lease *lease
|
||||
ready chan *websocket.Conn
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
claimed bool
|
||||
id string
|
||||
agent *agent
|
||||
lease *lease
|
||||
ready chan *websocket.Conn
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
claimed bool
|
||||
v2 bool
|
||||
signalKey string
|
||||
}
|
||||
type broker struct {
|
||||
mu sync.Mutex
|
||||
@@ -47,6 +50,7 @@ type broker struct {
|
||||
sessions map[string]*session
|
||||
agentToken, clientToken string
|
||||
ctx context.Context
|
||||
cluster *cluster
|
||||
}
|
||||
|
||||
func newBroker(ctx context.Context, agentToken, clientToken string) *broker {
|
||||
@@ -132,7 +136,7 @@ func (b *broker) agentEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
// Reserve before upgrading, so duplicate agents never replace an existing one.
|
||||
a := &agent{id: id}
|
||||
a := &agent{id: id, v2: r.URL.Query().Get("v") == "2"}
|
||||
b.mu.Lock()
|
||||
if b.agents[id] != nil {
|
||||
b.mu.Unlock()
|
||||
@@ -169,7 +173,11 @@ func (b *broker) agentEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
b.mu.Lock()
|
||||
a.ws = ws
|
||||
b.mu.Unlock()
|
||||
if err := writeJSON(b.ctx, ws, map[string]string{"type": "registered", "device": id}); err != nil {
|
||||
registered := map[string]any{"type": "registered", "device": id}
|
||||
if a.v2 {
|
||||
registered["protocol"] = 2
|
||||
}
|
||||
if err := writeJSON(b.ctx, ws, registered); err != nil {
|
||||
return
|
||||
}
|
||||
log.Printf("agent online: %s", id)
|
||||
@@ -220,6 +228,7 @@ func (b *broker) leaseEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
b.mu.Unlock()
|
||||
b.cluster.revoke(l.id)
|
||||
}()
|
||||
ws, err := accept(w, r)
|
||||
if err != nil {
|
||||
@@ -233,7 +242,15 @@ func (b *broker) leaseEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
l.ws = ws
|
||||
b.mu.Unlock()
|
||||
if err := writeJSON(b.ctx, ws, map[string]string{"type": "leased", "lease": l.id}); err != nil {
|
||||
protocol := 1
|
||||
if b.cluster != nil && a.v2 && r.URL.Query().Get("v") == "2" {
|
||||
protocol = 2
|
||||
}
|
||||
leased := map[string]any{"type": "leased", "lease": l.id}
|
||||
if r.URL.Query().Get("v") == "2" {
|
||||
leased["protocol"] = protocol
|
||||
}
|
||||
if err := writeJSON(b.ctx, ws, leased); err != nil {
|
||||
return
|
||||
}
|
||||
hold(b.ctx, ws)
|
||||
@@ -287,7 +304,7 @@ func (b *broker) dataEndpoint(w http.ResponseWriter, r *http.Request) {
|
||||
ticket, id := r.URL.Query().Get("session"), r.URL.Query().Get("device")
|
||||
b.mu.Lock()
|
||||
s := b.sessions[ticket]
|
||||
if s == nil || s.agent.id != id || b.agents[id] != s.agent || s.claimed || s.ctx.Err() != nil {
|
||||
if s == nil || s.v2 || s.agent.id != id || b.agents[id] != s.agent || s.claimed || s.ctx.Err() != nil {
|
||||
b.mu.Unlock()
|
||||
http.Error(w, "invalid or expired session", 404)
|
||||
return
|
||||
@@ -342,12 +359,17 @@ func (b *broker) handler() http.Handler {
|
||||
mux.HandleFunc("GET /v1/devices", b.auth(b.clientToken, b.devicesEndpoint))
|
||||
mux.HandleFunc("GET /v1/lease", b.auth(b.clientToken, b.leaseEndpoint))
|
||||
mux.HandleFunc("GET /v1/connect", b.auth(b.clientToken, b.connectEndpoint))
|
||||
mux.HandleFunc("GET /v2/nodes", b.auth(b.clientToken, b.nodesEndpoint))
|
||||
mux.HandleFunc("GET /v2/connect", b.auth(b.clientToken, b.clusterConnectEndpoint))
|
||||
mux.HandleFunc("GET /v2/signal", b.auth(b.agentToken, b.signalEndpoint))
|
||||
return mux
|
||||
}
|
||||
func main() {
|
||||
listen := flag.String("listen", "127.0.0.1:8080", "HTTP listen address (use a TLS reverse proxy for public access)")
|
||||
cert := flag.String("tls-cert", "", "TLS certificate PEM path")
|
||||
key := flag.String("tls-key", "", "TLS private key PEM path")
|
||||
clusterFile := flag.String("cluster-config", "", "Cluster configuration JSON; empty preserves standalone v1 mode")
|
||||
insecureNodes := flag.Bool("allow-insecure-nodes", false, "Allow ws relay URLs for isolated integration tests")
|
||||
flag.Parse()
|
||||
agentToken, clientToken := os.Getenv("ADS_AGENT_TOKEN"), os.Getenv("ADS_CLIENT_TOKEN")
|
||||
if len(agentToken) < 32 || len(clientToken) < 32 || agentToken == clientToken {
|
||||
@@ -359,6 +381,14 @@ func main() {
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
b := newBroker(ctx, agentToken, clientToken)
|
||||
if *clusterFile != "" {
|
||||
configured, err := loadCluster(*clusterFile, os.Getenv("ADS_RELAY_SECRET"), *insecureNodes)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
b.cluster = configured
|
||||
go configured.monitor(ctx)
|
||||
}
|
||||
server := &http.Server{Addr: *listen, Handler: b.handler(), ReadHeaderTimeout: 10 * time.Second, IdleTimeout: 60 * time.Second, MaxHeaderBytes: 16 * 1024}
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
"""Real-binary P2P and two-node failover test; loopback, no credentials required."""
|
||||
from e2e import *
|
||||
|
||||
def main():
|
||||
suffix = ".exe" if os.name == "nt" else ""
|
||||
logs = ROOT / "work" / "cluster-e2e"; logs.mkdir(parents=True, exist_ok=True)
|
||||
processes, handles = [], []
|
||||
tokens = {"ADS_AGENT_TOKEN": secrets.token_hex(32), "ADS_CLIENT_TOKEN": secrets.token_hex(32), "ADS_RELAY_SECRET": secrets.token_hex(32)}
|
||||
ports = [free_port() for _ in range(4)]
|
||||
api = f"http://127.0.0.1:{ports[0]}"
|
||||
status = logs / "status.json"
|
||||
config = logs / "cluster.json"
|
||||
config.write_text(json.dumps({"nodes":[{"id":f"relay-{i}","url":f"ws://127.0.0.1:{ports[i]}","priority":i} for i in (1,2)], "ice_servers":[], "p2p_timeout_ms":8000}))
|
||||
def launch(name, executable, args):
|
||||
binary = (ROOT / ("target/debug" if executable in ("ads-agent","ads-client") else "bin") / (executable+suffix)).resolve()
|
||||
handle = open(logs / (name+".log"), "wb"); handles.append(handle)
|
||||
env=os.environ.copy();env.pop("ADS_CONNECT_ADDRESS",None);env.update(tokens)
|
||||
p=subprocess.Popen([str(binary),*args],env=env,stdout=handle,stderr=handle,creationflags=subprocess.CREATE_NO_WINDOW if os.name=="nt" else 0);processes.append(p);return p
|
||||
def stop(p):
|
||||
if p.poll() is None:p.terminate();p.wait(timeout=10)
|
||||
def get(path):
|
||||
req=urllib.request.Request(api+path,headers={"Authorization":"Bearer "+tokens["ADS_CLIENT_TOKEN"]})
|
||||
with urllib.request.urlopen(req,timeout=2) as response:return json.load(response)
|
||||
def health(i):
|
||||
with urllib.request.urlopen(f"http://127.0.0.1:{ports[i]}/healthz",timeout=2) as response:return json.load(response)
|
||||
def route():
|
||||
try:return json.loads(status.read_text())
|
||||
except (OSError,ValueError):return {}
|
||||
with Phone(("127.0.0.1",0),Echo) as phone:
|
||||
threading.Thread(target=phone.serve_forever,daemon=True).start()
|
||||
try:
|
||||
nodes=[launch(f"relay-{i}","ads-relay",["--id",f"relay-{i}","--listen",f"127.0.0.1:{ports[i]}"]) for i in (1,2)]
|
||||
launch("controller","ads-server",["--listen",f"127.0.0.1:{ports[0]}","--cluster-config",str(config),"--allow-insecure-nodes"])
|
||||
wait_until(lambda:get('/v1/devices')==[],"controller")
|
||||
launch("agent","ads-agent",["--server",api.replace('http:','ws:'),"--allow-insecure","--device","test-phone","--target",f"127.0.0.1:{phone.server_address[1]}"])
|
||||
wait_until(lambda:len(get('/v1/devices'))==1,"agent")
|
||||
def client(mode,public=False):
|
||||
if status.exists():status.unlink()
|
||||
p=launch("client-"+mode+str(public),"ads-client",["--server",api.replace('http:','ws:'),"--allow-insecure","--device","test-phone","--listen",f"127.0.0.1:{ports[3]}","--no-auto-connect","--transport",mode,"--status-file",str(status)]+(["--p2p-public-only"] if public else []))
|
||||
wait_until(lambda:any(d['busy'] for d in get('/v1/devices')),"lease")
|
||||
return p
|
||||
def transfer(expected,node=None):
|
||||
with socket.create_connection(("127.0.0.1",ports[3]),timeout=5) as sock:
|
||||
sock.settimeout(25)
|
||||
payload=secrets.token_bytes(2*1024*1024)
|
||||
with concurrent.futures.ThreadPoolExecutor() as pool:
|
||||
writer=pool.submit(sock.sendall,payload)
|
||||
data=bytearray()
|
||||
while len(data)<len(payload):
|
||||
chunk=sock.recv(65536);assert chunk,"premature close";data.extend(chunk)
|
||||
writer.result(timeout=5)
|
||||
assert data==payload
|
||||
wait_until(lambda:route().get('transport')==expected,"route status")
|
||||
if node:assert node in route()['nodes'],route()
|
||||
assert get('/v2/nodes')['data_bytes']==0
|
||||
def release(p):
|
||||
stop(p);wait_until(lambda:not any(d['busy'] for d in get('/v1/devices')),"release")
|
||||
wait_until(lambda:health(2)['sessions']==0,"revocation")
|
||||
p=client('auto');transfer('p2p');assert health(1)['bytes']==0 and health(2)['bytes']==0;release(p)
|
||||
print('PASS: P2P 2 MiB integrity; both relay nodes carried zero data',flush=True)
|
||||
p=client('relay');transfer('relay','relay-1')
|
||||
print('PASS: forced relay-1',flush=True)
|
||||
with socket.create_connection(("127.0.0.1",ports[3]),timeout=5) as active:
|
||||
active.settimeout(10);active.sendall(b'before-node-failure');assert active.recv(64)==b'before-node-failure'
|
||||
stop(nodes[0])
|
||||
try:assert active.recv(64)==b'',"failed node kept stream open"
|
||||
except ConnectionResetError:pass
|
||||
transfer('relay','relay-2');release(p)
|
||||
print('PASS: active node failure closes old stream; same client lease reconnects via relay-2; revocation clears streams',flush=True)
|
||||
p=client('auto',True);transfer('relay','relay-2');release(p)
|
||||
print('PASS: unreachable P2P (no public candidates) falls back to relay-2',flush=True)
|
||||
finally:
|
||||
for p in reversed(processes):stop(p)
|
||||
for h in handles:h.close()
|
||||
phone.shutdown()
|
||||
|
||||
if __name__=='__main__':main()
|
||||
@@ -26,6 +26,8 @@ def main():
|
||||
parser.add_argument("--relay-url", help="Use an already deployed wss:// relay instead of a temporary local server")
|
||||
parser.add_argument("--connect-address", help="Optional origin host:port; TLS still verifies relay hostname")
|
||||
parser.add_argument("--credentials-dir", type=Path)
|
||||
parser.add_argument("--transport", choices=("auto","relay"), default="auto")
|
||||
parser.add_argument("--p2p-public-only", action="store_true")
|
||||
args = parser.parse_args()
|
||||
if args.existing_device and not args.relay_url:
|
||||
parser.error('--existing-device requires --relay-url')
|
||||
@@ -71,7 +73,7 @@ def main():
|
||||
|
||||
def adb(*command):
|
||||
return subprocess.run([args.adb, *command], env=env, capture_output=True,
|
||||
text=True, timeout=10)
|
||||
text=True, timeout=45)
|
||||
|
||||
def leased():
|
||||
if args.relay_url:
|
||||
@@ -107,7 +109,7 @@ def main():
|
||||
"--adb-serial", args.serial])
|
||||
start("client", [(rust_dir / f"ads-client{suffix}").resolve(), "--server", server_url,
|
||||
*network_args, "--device", device_id, "--listen", endpoint,
|
||||
"--no-auto-connect"])
|
||||
"--no-auto-connect", "--transport", args.transport, "--status-file", str(folder / "transport.json"), *(["--p2p-public-only"] if args.p2p_public_only else [])])
|
||||
wait_until(leased, "remote phone lease", seconds=20)
|
||||
print(adb("connect", endpoint).stdout.strip(), flush=True)
|
||||
print("If requested, approve this computer's debugging key on the phone.", flush=True)
|
||||
@@ -128,6 +130,7 @@ def main():
|
||||
assert marker.returncode == 0 and marker.stdout.strip() == "ads-read-only-smoke-ok", marker.stderr
|
||||
print("PASS: ordinary local adb devices and remote shell via Go server + both Rust daemons")
|
||||
print("Remote model:", model.stdout.strip())
|
||||
print("Transport:", (folder / "transport.json").read_text() if (folder / "transport.json").exists() else "legacy")
|
||||
finally:
|
||||
try:
|
||||
adb("disconnect", endpoint)
|
||||
|
||||
Reference in new issue
Block a user