feat: discover ADB devices from agent management

This commit is contained in:
tongtong committed 2026-11-12 22:30:05 +08:00
1 parent 8ecfe880cc
commit 3a7bb071f0
12 files changed
+132 -5

No files matched your search

+40
View File
@@ -204,6 +204,22 @@ pub async fn tcp_connect(address: &str) -> Result<TcpStream> {
.context("TCP connect timed out")??)
}
pub async fn adb_devices(server: &str) -> Result<String> {
timeout(Duration::from_secs(5), async {
let mut stream = TcpStream::connect(server)
.await
.context("cannot connect to Agent host ADB server")?;
adb_request(&mut stream, "host:devices-l").await?;
let mut size = [0; 4];
stream.read_exact(&mut size).await?;
let size = usize::from_str_radix(std::str::from_utf8(&size)?, 16)?;
let mut data = vec![0; size];
stream.read_exact(&mut data).await?;
Ok::<_, anyhow::Error>(String::from_utf8(data)?)
})
.await
.context("ADB discovery timed out")?
}
async fn adb_request(stream: &mut TcpStream, service: &str) -> Result<()> {
if service.len() > 65535 {
bail!("ADB service name too long");
@@ -285,6 +301,30 @@ mod tests {
assert_eq!(DEFAULT_RELAY, "wss://ttstd.com/ads");
}
#[tokio::test]
async fn discovers_adb_devices_with_length_framing() {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap().to_string();
let peer = tokio::spawn(async move {
let (mut socket, _) = listener.accept().await.unwrap();
let mut size = [0; 4];
socket.read_exact(&mut size).await.unwrap();
let count = usize::from_str_radix(std::str::from_utf8(&size).unwrap(), 16).unwrap();
let mut service = vec![0; count];
socket.read_exact(&mut service).await.unwrap();
assert_eq!(service, b"host:devices-l");
let body = "192.168.1.2:5555\tdevice model:Phone\nUSB123\tunauthorized\n";
let reply = format!("OKAY{:04x}{}", body.len(), body);
for chunk in reply.as_bytes().chunks(3) {
socket.write_all(chunk).await.unwrap();
}
});
let list = adb_devices(&address).await.unwrap();
assert!(list.contains("model:Phone"));
assert!(list.contains("unauthorized"));
peer.await.unwrap();
}
#[tokio::test]
async fn tls_failure_is_reported_without_a_provider_panic() {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();