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,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"); }
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user