feat: add optional Windows virtual USB ADB device

This commit is contained in:
tongtong committed 2026-11-12 17:34:23 +08:00
1 parent 2abfb63da9
commit 08d60de364
23 files changed
+802 -26

No files matched your search

+42 -9
View File
@@ -24,6 +24,9 @@ public partial class MainViewModel : ObservableObject, IDisposable
[ObservableProperty] private int port = 15555;
[ObservableProperty] private int maxSize = 1920;
[ObservableProperty] private int maxFps = 60;
[ObservableProperty] private bool usbMode;
[ObservableProperty] private string usbipPath = WindowsUsbSession.DefaultUsbipPath();
[ObservableProperty] private int usbPort = 3240;
[ObservableProperty] private bool relayOnly;
[ObservableProperty] private string transportStatus = "尚未建立数据通道";
[ObservableProperty] private bool audio;
@@ -55,8 +58,10 @@ public partial class MainViewModel : ObservableObject, IDisposable
public string? Endpoint => endpoint;
public string? ActiveDeviceId => activeId;
private WindowsUsbSession? usbSession;
private string? leaseFile;
private Process? client, scrcpy;
private string? endpoint, activeAdb, activeId, transportFile;
private string? endpoint, rawEndpoint, activeAdb, activeId, transportFile;
private string credentialFile = "", preferredDevice = "home-phone";
private bool monitoring, disposed, initialized;
private DateTime lastRefresh;
@@ -81,6 +86,8 @@ 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;
UsbMode = settings.UsbMode; UsbPort = settings.UsbPort;
if (settings.UsbipPath.Length > 0) UsbipPath = settings.UsbipPath;
RelayOnly = settings.RelayOnly; Audio = settings.Audio; ViewOnly = settings.ViewOnly;
MinimizeToTray = settings.Tray; AutoRefresh = settings.AutoRefresh;
credentialFile = settings.CredentialFile;
@@ -224,7 +231,8 @@ public partial class MainViewModel : ObservableObject, IDisposable
{
var selectedPort = Port;
if (selectedPort == 5037) throw new InvalidOperationException("不能清理 ADB 主服务端口 5037,请选择隧道端口。");
var releasedOwnConnection = HasConnection && endpoint == $"127.0.0.1:{selectedPort}";
var releasedOwnConnection = HasConnection && rawEndpoint == $"127.0.0.1:{selectedPort}";
var releasedUsb = releasedOwnConnection && usbSession != null;
if (releasedOwnConnection)
{
Log($"清理端口 {selectedPort}:先断开当前工作台管理的连接。");
@@ -238,7 +246,7 @@ public partial class MainViewModel : ObservableObject, IDisposable
try
{
var adb = DesktopPaths.ResolveTool(AdbPath, "adb");
await AdbAsync(adb, lifetime.Token, "disconnect", $"127.0.0.1:{selectedPort}");
if (!releasedUsb) await AdbAsync(adb, lifetime.Token, "disconnect", $"127.0.0.1:{selectedPort}");
}
catch (Exception error) { Log("清除 ADB 端点:" + error.Message); }
PortCleanupStatus = releasedOwnConnection && ended == 0
@@ -265,7 +273,7 @@ public partial class MainViewModel : ObservableObject, IDisposable
await ConnectCoreAsync(selected);
}));
private Task ConnectCoreAsync(DeviceItem device)
private async Task ConnectCoreAsync(DeviceItem device)
{
if (client != null) throw new InvalidOperationException("请先断开当前连接。");
if (device.Busy) throw new InvalidOperationException("设备已被其他客户端占用。");
@@ -281,6 +289,10 @@ public partial class MainViewModel : ObservableObject, IDisposable
throw new InvalidOperationException($"本地端口 {Port} 已被占用,请修改端口或先停止另一个连接客户端。");
}
finally { portCheck.Stop(); }
if (UsbMode) {
if (UsbPort == Port) throw new InvalidOperationException("USB/IP 端口不能与 ADB 隧道端口相同。");
WindowsUsbSession.Validate(UsbipPath, UsbPort);
}
var relay = RelayOptions.Parse(Server, Origin, Token);
var clientExecutable = DesktopPaths.ResolveTool(ClientPath, "ads-client");
var adb = DesktopPaths.ResolveTool(AdbPath, "adb");
@@ -290,9 +302,14 @@ public partial class MainViewModel : ObservableObject, IDisposable
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);
if (UsbMode) {
leaseFile = Path.Combine(folder, "lease-" + Guid.NewGuid().ToString("N") + ".json");
info.ArgumentList.Add("--no-auto-connect");
info.ArgumentList.Add("--lease-status-file"); info.ArgumentList.Add(leaseFile);
}
TransportStatus = "正在协商连接路线";
client = Start(info);
endpoint = address; activeAdb = adb; activeId = device.Id; preferredDevice = device.Id;
rawEndpoint = address; endpoint = address; activeAdb = adb; activeId = device.Id; preferredDevice = device.Id;
OnPropertyChanged(nameof(Endpoint)); OnPropertyChanged(nameof(ActiveDeviceId));
HasConnection = true; Ready = false;
UpdateLocalOwnership();
@@ -301,7 +318,15 @@ public partial class MainViewModel : ObservableObject, IDisposable
Notice = "连接已启动;首次使用时请在手机上允许调试。";
Model = AndroidVersion = ApiLevel = "—";
Log($"连接 {device.Id},本地 ADB 地址 {address}。");
return Task.CompletedTask;
if (UsbMode) {
usbSession = new WindowsUsbSession(device.Id);
try {
await usbSession.StartAsync(UsbipPath, UsbPort, address, leaseFile!, Log, lifetime.Token);
endpoint = usbSession.Serial; OnPropertyChanged(nameof(Endpoint));
ActiveEndpoint = $"{device.Id} → USB ADB · {endpoint}";
Notice = "USB 模拟已挂载,等待本机 ADB 枚举;首次使用可能需要手机授权。";
} catch { await DisconnectCoreAsync(); throw; }
}
}
private void UpdateLocalOwnership()
@@ -358,6 +383,10 @@ public partial class MainViewModel : ObservableObject, IDisposable
};
} catch (IOException) { } catch (JsonException) { }
}
if (usbSession is { HasExited: true }) {
await Exclusive(DisconnectCoreAsync);
Notice = "USB 模拟进程已退出,请查看日志后重连。";
}
var current = client;
if (current != null)
{
@@ -445,14 +474,18 @@ public partial class MainViewModel : ObservableObject, IDisposable
private async Task DisconnectCoreAsync()
{
await StopScrcpyAsync();
var usb = usbSession; usbSession = null;
if (usb != null) await usb.StopAsync(Log);
var current = client; client = null;
await ProcessTools.StopAsync(current);
if (endpoint != null && activeAdb != null)
if (usb == null && 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) { } }
if (leaseFile != null) { try { File.Delete(leaseFile); } catch (IOException) { } }
leaseFile = null;
transportFile = null; TransportStatus = "尚未建立数据通道";
endpoint = activeAdb = activeId = null;
rawEndpoint = endpoint = activeAdb = activeId = null;
HasConnection = Ready = false;
UpdateLocalOwnership();
OnPropertyChanged(nameof(Endpoint)); OnPropertyChanged(nameof(ActiveDeviceId));
@@ -520,7 +553,7 @@ public partial class MainViewModel : ObservableObject, IDisposable
DesktopPaths.SaveSettings(settingsFile, new DesktopSettings
{
Server = Server.Trim(), Origin = Origin.Trim(), Client = ClientPath, Adb = AdbPath, Scrcpy = ScrcpyPath,
RelayOnly = RelayOnly, Port = Port, Size = MaxSize, Fps = MaxFps, Audio = Audio, ViewOnly = ViewOnly, Tray = MinimizeToTray,
UsbMode = UsbMode, UsbipPath = UsbipPath, UsbPort = UsbPort, RelayOnly = RelayOnly, Port = Port, Size = MaxSize, Fps = MaxFps, Audio = Audio, ViewOnly = ViewOnly, Tray = MinimizeToTray,
AutoRefresh = AutoRefresh, CredentialFile = credentialFile, PreferredDevice = activeId ?? SelectedDevice?.Id ?? preferredDevice
});
Notice = "配置已保存;令牌内容未写入配置。";