130 lines
5.6 KiB
C#
130 lines
5.6 KiB
C#
using System.Text.Json;
|
|
|
|
namespace Ads.Desktop;
|
|
|
|
public sealed class DesktopSettings
|
|
{
|
|
public int Version { get; set; } = 2;
|
|
public string Server { get; set; } = RelayOptions.DefaultServer;
|
|
public string? Origin { get; set; }
|
|
public string Client { get; set; } = "";
|
|
public string Adb { get; set; } = "";
|
|
public string Scrcpy { get; set; } = "";
|
|
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;
|
|
public bool AutoRefresh { get; set; } = true;
|
|
public string CredentialFile { get; set; } = "";
|
|
public string PreferredDevice { get; set; } = "home-phone";
|
|
|
|
public void Migrate()
|
|
{
|
|
var oldDefault = Server.TrimEnd('/') is "wss://ads.ttstd.com" or "wss://devices.example.com";
|
|
if (oldDefault) Server = RelayOptions.DefaultServer;
|
|
Origin ??= oldDefault || Server.TrimEnd('/') == RelayOptions.DefaultServer ? RelayOptions.DefaultOrigin : "";
|
|
Version = 2;
|
|
}
|
|
}
|
|
|
|
public sealed record ClientCredential(string Token, string? Origin)
|
|
{
|
|
public override string ToString() => "ClientCredential { Token = [REDACTED] }";
|
|
public static ClientCredential Read(string path)
|
|
{
|
|
if (!File.Exists(path)) throw new InvalidOperationException("凭据文件不存在。");
|
|
if (new FileInfo(path).Length > 64 * 1024) throw new InvalidOperationException("凭据文件过大。");
|
|
string? token = null, origin = null;
|
|
foreach (var line in File.ReadLines(path))
|
|
{
|
|
var separator = line.IndexOf('=');
|
|
if (separator < 0) continue;
|
|
var name = line[..separator].Trim();
|
|
var value = line[(separator + 1)..].Trim();
|
|
if (name == "ADS_CLIENT_TOKEN") token = value;
|
|
if (name == "ADS_CONNECT_ADDRESS") origin = value;
|
|
}
|
|
if (token is null || token.Length < 32 || token.Contains('\r') || token.Contains('\n'))
|
|
throw new InvalidOperationException("文件中没有有效的 ADS_CLIENT_TOKEN,请选择 client.env。");
|
|
if (!string.IsNullOrWhiteSpace(origin)) RelayOptions.ParseOrigin(origin);
|
|
return new ClientCredential(token, origin);
|
|
}
|
|
}
|
|
|
|
public static class DesktopPaths
|
|
{
|
|
public static string SettingsFolder => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "TTSTD", "ADS");
|
|
|
|
public static string? ProjectRoot()
|
|
{
|
|
foreach (var start in new[] { AppContext.BaseDirectory, Environment.CurrentDirectory })
|
|
for (var directory = new DirectoryInfo(start); directory != null; directory = directory.Parent)
|
|
if (File.Exists(Path.Combine(directory.FullName, "Cargo.toml")) && Directory.Exists(Path.Combine(directory.FullName, "server")))
|
|
return directory.FullName;
|
|
return null;
|
|
}
|
|
|
|
public static string DefaultCredentialFile()
|
|
{
|
|
var adjacent = Path.Combine(AppContext.BaseDirectory, "client.env");
|
|
if (File.Exists(adjacent)) return adjacent;
|
|
var root = ProjectRoot();
|
|
return root is null ? "" : Path.Combine(root, "config", "private", "client.env");
|
|
}
|
|
|
|
public static string FindTool(string name)
|
|
{
|
|
var executable = OperatingSystem.IsWindows() ? name + ".exe" : name;
|
|
var candidates = new List<string>
|
|
{
|
|
Path.Combine(AppContext.BaseDirectory, executable),
|
|
Path.Combine(AppContext.BaseDirectory, "tools", executable),
|
|
Path.Combine(AppContext.BaseDirectory, "tools", "scrcpy", executable)
|
|
};
|
|
if (ProjectRoot() is { } root)
|
|
{
|
|
candidates.Add(Path.Combine(root, "bin", executable));
|
|
candidates.Add(Path.Combine(root, "bin", "tools", "scrcpy", executable));
|
|
}
|
|
if (name == "adb")
|
|
foreach (var variable in new[] { "ANDROID_HOME", "ANDROID_SDK_ROOT" })
|
|
if (Environment.GetEnvironmentVariable(variable) is { Length: > 0 } sdk)
|
|
candidates.Add(Path.Combine(sdk, "platform-tools", executable));
|
|
candidates.AddRange((Environment.GetEnvironmentVariable("PATH") ?? "").Split(Path.PathSeparator)
|
|
.Where(p => !string.IsNullOrWhiteSpace(p)).Select(p => Path.Combine(p.Trim('"'), executable)));
|
|
return candidates.FirstOrDefault(File.Exists) ?? name;
|
|
}
|
|
|
|
public static string ResolveTool(string value, string name)
|
|
{
|
|
value = Environment.ExpandEnvironmentVariables(value.Trim().Trim('"'));
|
|
if (File.Exists(value)) return Path.GetFullPath(value);
|
|
if (value == name || value == name + ".exe")
|
|
{
|
|
var found = FindTool(name);
|
|
if (File.Exists(found)) return Path.GetFullPath(found);
|
|
}
|
|
throw new InvalidOperationException($"找不到 {name},请在连接配置中选择可执行文件。");
|
|
}
|
|
|
|
public static DesktopSettings LoadSettings(string file)
|
|
{
|
|
var settings = File.Exists(file)
|
|
? JsonSerializer.Deserialize<DesktopSettings>(File.ReadAllText(file)) ?? new DesktopSettings()
|
|
: new DesktopSettings();
|
|
settings.Migrate();
|
|
return settings;
|
|
}
|
|
|
|
public static void SaveSettings(string file, DesktopSettings settings)
|
|
{
|
|
Directory.CreateDirectory(Path.GetDirectoryName(file)!);
|
|
var temporary = file + ".tmp";
|
|
File.WriteAllText(temporary, JsonSerializer.Serialize(settings, new JsonSerializerOptions { WriteIndented = true }));
|
|
File.Move(temporary, file, overwrite: true);
|
|
}
|
|
}
|