feat: add control center, multiple relay nodes and P2P-first transport

This commit is contained in:
tongtong committed 2026-11-12 16:56:06 +08:00
1 parent 0988e9c363
commit 2abfb63da9
44 files changed
+3477 -67

No files matched your search

+38 -4
View File
@@ -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 = "配置已保存;令牌内容未写入配置。";