节点在线、应用在线、配置在线使用令牌查询
大石头 authored at 2021-12-16 19:49:30
25.93 KiB
Stardust
using System.Diagnostics;
using System.Net;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using NewLife;
using NewLife.Data;
using NewLife.Log;
using NewLife.Model;
using NewLife.Reflection;
using NewLife.Remoting;
using NewLife.Remoting.Clients;
using NewLife.Remoting.Models;
using NewLife.Security;
using NewLife.Threading;
using Stardust.Managers;
using Stardust.Models;
using Stardust.Monitors;

namespace Stardust;

/// <summary>星星客户端。每个设备节点有一个客户端连接服务端</summary>
public partial class StarClient : ClientBase, ICommandClient, IEventProvider
{
    #region 属性
    /// <summary>产品编码</summary>
    public String? ProductCode { get; set; }

    /// <summary>服务迁移</summary>
    public event EventHandler<MigrationEventArgs>? OnMigration;

    /// <summary>插件列表</summary>
    public String[]? Plugins { get; set; }

    /// <summary>接受平台下发的默认语言。默认true</summary>
    /// <remarks>桌面宿主应同时关闭 <see cref="StarClient"/> 与 AppClient 的平台语言,避免节点通道覆盖本地用户语言。</remarks>
    public Boolean AcceptPlatformLanguage { get; set; } = true;

    private FrameworkManager _frameworkManager = new();
    private RelayManager _relayManager = new();
    #endregion

    #region 构造
    /// <summary>实例化</summary>
    public StarClient() => SetDefault();

    private void SetDefault()
    {
        Features = Features.Login | Features.Logout | Features.Ping | Features.Upgrade | Features.Notify | Features.CommandReply | Features.PostEvent;
        SetActions("Node/");

        Log = XTrace.Log;

        // NativeAOT 下模型属性必须显式保留,否则序列化为空对象
        AotRoots.Ensure();
    }

    /// <summary>使用客户端设置来初始化</summary>
    /// <param name="setting"></param>
    public StarClient(IClientSetting setting) : base(setting)
    {
        SetDefault();
    }

    /// <summary>实例化</summary>
    /// <param name="urls"></param>
    public StarClient(String urls) : this() => Server = urls;
    #endregion

    #region 多语言
    /// <summary>应用平台下发的默认语言。本地固定语言时不接受平台默认语言</summary>
    /// <param name="language">语言名称。为空时不改动</param>
    /// <param name="save">是否保存到核心配置。匿名 /api 传入 false,只影响当前进程</param>
    /// <remarks>
    /// 优先级为本地固定语言 &gt; 平台默认语言 &gt; 系统语言。核心配置 <c>Language</c> 或环境变量
    /// <c>Language</c> 已固定进程语言时,平台下发的默认语言不能改动界面与日志,也不写入核心配置。
    /// 平台下发 <see cref="StarLocalization.AutoLanguage"/> 时清除本机保存的平台语言,恢复系统语言;匿名 /api 传入 save=false 时只清进程内。
    /// </remarks>
    protected override void ApplyDefaultLanguage(String? language, Boolean save = true)
    {
        if (language.IsNullOrWhiteSpace()) return;
        if (!AcceptPlatformLanguage) return;
        if (StarLocalization.HasFixedLanguage()) return;

        if (language.EqualIgnoreCase(StarLocalization.AutoLanguage))
        {
            StarLocalization.ResetDefaultLanguage(save);
            return;
        }

        base.ApplyDefaultLanguage(language, save);
    }
    #endregion

    #region 方法
    /// <summary>初始化</summary>
    protected override void OnInit()
    {
        var provider = ServiceProvider ??= ObjectContainer.Provider;

        // 基类盐值取 Runtime.UtcNow,而 Runtime.UtcNow 又走下面的全局时间提供者,因此盐值与业务时间同源
        PasswordProvider = new SaltPasswordProvider { Algorithm = "md5", SaltTime = 60 };

        // 接入星尘即为进程装上「本机时间 + 服务端时间差」的时间源,业务直接用 Runtime.UtcNow 就能屏蔽本机时钟误差
        if (TimerScheduler.GlobalTimeProvider is not ServerTimeProvider)
            TimerScheduler.GlobalTimeProvider = new ServerTimeProvider { Client = this };

        // 找到容器,注册默认的模型实现,供后续InvokeAsync时自动创建正确的模型对象
        var container = ModelExtension.GetService<IObjectContainer>(provider) ?? ObjectContainer.Current;
        if (container != null)
        {
            container.AddTransient<ILoginRequest, LoginInfo>();
            container.AddTransient<IPingRequest, PingInfo>();
        }

        _frameworkManager.Attach(this);
        _relayManager.Attach(this);

        base.OnInit();
    }

    #endregion

    #region 登录
    /// <summary>创建登录请求</summary>
    /// <returns></returns>
    public override ILoginRequest BuildLoginRequest()
    {
        ApplyServerInfo();

        var request = new LoginInfo();
        FillLoginRequest(request);

        request.ProductCode = ProductCode;
        request.Node = GetNodeInfo();

        return request;
    }

    /// <summary>填充登录请求。网络未就绪时 Runtime.ClientId 缺少主机段,改用机器名补足</summary>
    /// <remarks>
    /// <c>Runtime.ClientId</c> 为 <c>ip@pid</c>,拿不到本机 IPv4 时退化为 <c>@pid</c>。
    /// 这种标识既无法按主机归组在线实例,进程重启后也完全变样,服务端会把每次重连当成新实例。
    /// 这里用机器名补足主机段,保持标识可读且稳定。
    /// </remarks>
    /// <param name="request">登录请求</param>
    protected override void FillLoginRequest(ILoginRequest request)
    {
        base.FillLoginRequest(request);

        request.ClientId = StarHelper.FixClientId(request.ClientId);
    }

    /// <summary>登录前对齐服务端时间与默认语言</summary>
    /// <remarks>
    /// 本机时钟偏差超过盐值窗口时,用本机时间生成的盐值会被服务端判定过期,登录失败并反复重试。
    /// 登录前先用匿名接口取一次服务端信息,把时间差写入客户端,使盐值按服务端时间生成;
    /// 同时应用平台默认语言,让登录前的界面与提示也能用平台语言。匿名接口的语言只影响当前进程,不保存配置。
    /// 探测最多等待 <see cref="ServerProbe.WaitTimeout"/>,失败不阻塞登录,保持原有行为。
    /// </remarks>
    private void ApplyServerInfo()
    {
        // 时间差已经就绪时无需再探测。进程内首次登录才会走到探测,之后由登录响应与心跳维护
        if (Span != TimeSpan.Zero) return;

        var info = ServerProbe.GetServerInfo(Server ?? Setting?.Server);
        if (info == null) return;

        if (info.ServerTime > 0)
        {
            FixTime(0, info.ServerTime);

            // 时间差跳变多少,按日历调度的定时逻辑就跳多少,写入日志便于解释定时任务的异常触发
            if (Math.Abs(Span.TotalSeconds) >= 1) WriteLog("Server time offset applied: {0}", Span);
        }
        if (!info.DefaultLanguage.IsNullOrWhiteSpace()) ApplyDefaultLanguage(info.DefaultLanguage, false);
    }

    /// <summary>获取设备信息</summary>
    /// <returns></returns>
    public NodeInfo GetNodeInfo()
    {
        var mi = MachineInfo.GetCurrent();

        var asm = AssemblyX.Entry ?? AssemblyX.Create(Assembly.GetExecutingAssembly());
        var mcs = NetHelper.GetMacs().Select(e => e.ToHex("-")).Where(e => e != "00-00-00-00-00-00").OrderBy(e => e).Join(",");
        var path = ".".GetFullPath();
        var drives = GetDrives();
        var driveInfo = DriveInfo.GetDrives().FirstOrDefault(e => path.StartsWithIgnoreCase(e.Name));
        var di = new NodeInfo
        {
            Version = asm?.FileVersion,
            Compile = asm?.Compile ?? DateTime.MinValue,

            OSName = mi.OSName,
            OSVersion = mi.OSVersion,

            MachineName = Environment.MachineName,
            UserName = Environment.UserName,
            IP = AgentInfo.GetIps(),
            Gateway = AgentInfo.GetGateway(),
            Dns = AgentInfo.GetDnsList(),

            ProcessorCount = Environment.ProcessorCount,
            Memory = mi.Memory,
            AvailableMemory = mi.AvailableMemory,
            FreeMemory = mi.FreeMemory,
            TotalSize = (UInt64)(driveInfo?.TotalSize ?? 0),
            AvailableFreeSpace = (UInt64)(driveInfo?.AvailableFreeSpace ?? 0),
            DriveSize = (UInt64)drives.Sum(e => e.TotalSize),
            DriveInfo = drives.Join(",", e => $"{e.Name}[{e.DriveFormat}]={e.AvailableFreeSpace.ToGMK()}/{e.TotalSize.ToGMK()}"),

            Product = mi.Product,
            Vendor = mi.Vendor,
            Processor = mi.Processor,
            //CpuRate = mi.CpuRate,
            UUID = mi.UUID,
            MachineGuid = mi.Guid,
            SerialNumber = mi.Serial,
            Board = mi.Board,
            DiskID = mi.DiskID,

            Macs = mcs,

            InstallPath = ".".GetFullPath(),
            Runtime = Environment.Version + "",

            Time = DateTime.UtcNow,
            Plugins = Plugins,
        };

        // 获取最新机器名
        if (Runtime.Linux)
        {
            var file = @"/etc/hostname";
            if (File.Exists(file)) di.MachineName = File.ReadAllText(file).Trim();
        }

        // 获取时区
        try
        {
            var tz = TimeZoneInfo.Local;
            // 优先使用IANA时区名称(如Asia/Shanghai),否则使用偏移量格式
            di.TimeZone = tz.Id;
            if (di.TimeZone.IsNullOrEmpty() || di.TimeZone.Contains(' '))
            {
                var offset = tz.BaseUtcOffset;
                di.TimeZone = $"GMT{(offset >= TimeSpan.Zero ? "+" : "")}{offset.Hours:00}:{offset.Minutes:00}";
            }
        }
        catch { }

        // 目标框架
        di.Framework = _frameworkManager.GetAllVersions().Join(",", e => e.TrimStart('v'));

#if NETCOREAPP || NETSTANDARD
        di.Framework ??= RuntimeInformation.FrameworkDescription?.TrimPrefix(".NET Framework").TrimPrefix(".NET Core").TrimPrefix(".NET Native").TrimPrefix(".NET").Trim();

        di.Architecture = RuntimeInformation.ProcessArchitecture + "";

        if (Runtime.Linux)
        {
            // 识别Alpine
            var nr = new NetRuntime();
            if (nr.IsAlpine() && !di.OSName.StartsWithIgnoreCase("Alpine")) di.OSName = $"{di.OSName}(Alpine)";
        }
#else
        var ver = "";
        var tar = asm?.Asm.GetCustomAttribute<TargetFrameworkAttribute>();
        if (tar != null) ver = !tar.FrameworkDisplayName.IsNullOrEmpty() ? tar.FrameworkDisplayName : tar.FrameworkName;

        di.Framework ??= ver?.TrimPrefix(".NET Framework").TrimPrefix(".NET Core").TrimPrefix(".NET Native").TrimPrefix(".NET").Trim();
        di.Architecture = IntPtr.Size == 8 ? "X64" : "X86";
#endif

#if NET5_0_OR_GREATER
        if (OperatingSystem.IsWindows()) FillOnWindows(di);
        if (OperatingSystem.IsLinux()) FillOnLinux(di);
        if (OperatingSystem.IsMacOS()) FillOnOSX(di);
#else
        if (Runtime.Windows) FillOnWindows(di);
        if (Runtime.Linux) FillOnLinux(di);
        if (Runtime.OSX) FillOnOSX(di);
#endif

        return di;
    }

    private static DateTime _drivesTime;
    private static IList<DriveInfo>? _drives;

    /// <summary>获取驱动器信息。带短缓存,避免每次心跳枚举磁盘</summary>
    /// <returns></returns>
    public static IList<DriveInfo> GetDrives()
    {
        // 磁盘信息短缓存,60秒内复用,避免每次心跳枚举磁盘
        var now = DateTime.Now;
        if (_drives != null && (now - _drivesTime).TotalSeconds < 60) return _drives;

        var list = new List<DriveInfo>();
        foreach (var di in DriveInfo.GetDrives())
        {
            if (!di.IsReady) continue;
            if (di.DriveType is not DriveType.Fixed and not DriveType.Removable) continue;
            if (di.Name != "/" && di.DriveFormat.EqualIgnoreCase("overlay", "squashfs")) continue;
            if (di.Name.Contains("container") && di.Name.EndsWithIgnoreCase("/overlay")) continue;
            if (di.TotalSize <= 0) continue;

            if (!list.Any(e => e.Name == di.Name)) list.Add(di);
        }

        _drives = list;
        _drivesTime = now;

        return list;
    }

    private static String? Execute(String cmd, String? arguments = null)
    {
        try
        {
            var psi = new ProcessStartInfo(cmd, arguments ?? "")
            {
                // UseShellExecute 必须 false,以便于后续重定向输出流
                UseShellExecute = false,
                // 隐藏子进程窗口,避免 WinForms/WPF 等 GUI 宿主启动 wmic 时闪出控制台窗口
                CreateNoWindow = true,
                RedirectStandardOutput = true
            };
            using var process = Process.Start(psi);
            if (process == null) return null;

            if (!process.WaitForExit(3_000))
            {
                process.Kill();
                return null;
            }

            return process.StandardOutput.ReadToEnd();
        }
#if DEBUG
        catch (Exception ex)
        {
            XTrace.WriteLine(ex.Message);
            return null;
        }
#else
        catch
        {
            return null;
        }
#endif
    }
    #endregion

    #region 心跳
    private readonly String[] _excludes = ["Idle", "System", "Registry", "smss", "csrss", "lsass", "wininit", "services", "winlogon", "LogonUI", "SearchUI", "fontdrvhost", "dwm", "svchost", "dllhost", "conhost", "taskhostw", "explorer", "ctfmon", "ChsIME", "WmiPrvSE", "WUDFHost", "TabTip*", "igfxCUIServiceN", "igfxEMN", "smartscreen", "sihost", "RuntimeBroker", "StartMenuExperienceHost", "SecurityHealthSystray", "SecurityHealthService", "ShellExperienceHost", "PerfWatson2", "audiodg", "spoolsv", "*ServiceHub*", "systemd*", "cron", "rsyslogd", "sudo", "dbus*", "bash", "login", "networkd*", "kworker*", "ksoftirqd*", "migration*", "auditd", "polkitd", "atd"];

    private volatile PingResult? _lastGwResult;
    private volatile PingResult? _lastDnsResult;
    private volatile PingResult? _lastSvrResult;
    private DateTime _lastMeasureTime;
    private volatile Boolean _measuring;

    /// <summary>构建心跳请求</summary>
    /// <returns></returns>
    public override IPingRequest BuildPingRequest()
    {
        var request = new PingInfo();
        FillPingRequest(request);

        // 获取网络质量(使用后台缓存,避免阻塞心跳)
        EnsurePingResults();

        var exs = _excludes.Where(e => e.Contains('*')).ToArray();

        var ps = Process.GetProcesses();
        var pcs = new HashSet<String>(StringComparer.OrdinalIgnoreCase);
        foreach (var item in ps)
        {
            // 有些进程可能已退出,无法获取详细信息
            try
            {
                if (Runtime.Linux && item.SessionId == 0) continue;

                var name = item.GetProcessName();
                if (name.EqualIgnoreCase(_excludes) || exs.Any(e => e.IsMatch(name))) continue;

                pcs.Add(name);
            }
            catch { }
            finally
            {
                // 释放进程句柄,避免每次心跳泄漏句柄
                item.Dispose();
            }
        }

        var mi = MachineInfo.GetCurrent();
        mi.Refresh();

        var drives = GetDrives();

        request.IP = AgentInfo.GetIps();
        request.Gateway = AgentInfo.GetGateway();
        request.Dns = AgentInfo.GetDnsList();
        request.DriveInfo = drives.Join(",", e => $"{e.Name}[{e.DriveFormat}]={e.AvailableFreeSpace.ToGMK()}/{e.TotalSize.ToGMK()}");
        request.Macs = (String?)NetHelper.GetMacs().Select(e => e.ToHex("-")).OrderBy(e => e).Join(",");
        request.ProcessCount = ps.Length;
        request.Processes = pcs.Join();

        // 目标框架
        request.Framework = _frameworkManager.GetAllVersions().Join(",", e => e.TrimStart('v'));

        // 获取Tcp连接信息,某些Linux平台不支持
        try
        {
            var properties = IPGlobalProperties.GetIPGlobalProperties();
            var connections = properties.GetActiveTcpConnections();

            request.TcpConnections = connections.Count(e => e.State == TcpState.Established);
            request.TcpTimeWait = connections.Count(e => e.State == TcpState.TimeWait);
            request.TcpCloseWait = connections.Count(e => e.State == TcpState.CloseWait);
        }
        catch { }

        // 使用缓存的网络质量结果
        var gwResult = _lastGwResult;
        var dnsResult = _lastDnsResult;
        var svrResult = _lastSvrResult;

        // 网关结果
        if (gwResult != null)
        {
            request.IntranetScore = gwResult.Score;
            request.GatewayLatency = gwResult.Latency;
            request.GatewayLossRate = gwResult.LossRate;
        }

        // DNS结果
        if (dnsResult != null)
        {
            request.DnsLatency = dnsResult.Latency;
            request.DnsLossRate = dnsResult.LossRate;
        }

        // 服务器结果
        if (svrResult != null)
        {
            request.ServerLatency = svrResult.Latency;
            request.ServerLossRate = svrResult.LossRate;
        }

        // 外网质量综合评分:DNS占30%,服务器占70%
        request.InternetScore = (dnsResult?.Score ?? 0) * 0.3 + (svrResult?.Score ?? 0) * 0.7;

        if (mi is IExtend ext)
        {
            // 读取无线信号强度
            if (ext.Items.TryGetValue("Signal", out var value)) request.Signal = value.ToInt();
        }

        // 平台特定采集(Load1、DiskIOPS、GPU)
        if (Runtime.Windows) FillPingOnWindows(request);
        if (Runtime.Linux) FillPingOnLinux(request);
        if (Runtime.OSX) FillPingOnOSX(request);

        // 最后设置时间,避免因为代码执行原因导致误差过大
        request.Uptime = (Int32)(Runtime.TickCount64 / 1000);
        request.Time = DateTime.UtcNow.ToLong();

        return request;
    }

    private void EnsurePingResults()
    {
        var now = DateTime.UtcNow;
        if (_lastGwResult != null && _lastDnsResult != null && _lastSvrResult != null &&
            (now - _lastMeasureTime).TotalSeconds < 48)
            return;

        if (_measuring) return;

        _measuring = true;
        Task.Run(MeasurePingResultsAsync);
    }

    private async Task MeasurePingResultsAsync()
    {
        try
        {
            var monitor = new PingMonitor();
            var gw = AgentInfo.GetGateway();
            if (gw != null && gw.Contains('/')) gw = gw.Substring(0, gw.IndexOf("/"));
            var dns = AgentInfo.GetDns();
            if (dns.IsNullOrEmpty() || IPAddress.TryParse(dns, out var ip) && ip.IsLocal()) dns = "223.5.5.5";
            var svr = (Client as ApiHttpClient)?.Current?.Address.Host;

            var gwTask = Task.Run(() => monitor.GetResultAsync(gw));
            var dnsTask = Task.Run(() => monitor.GetResultAsync(dns));
            var svrTask = Task.Run(() => monitor.GetResultAsync(svr));

            _lastGwResult = await gwTask.ConfigureAwait(false);
            _lastDnsResult = await dnsTask.ConfigureAwait(false);
            _lastSvrResult = await svrTask.ConfigureAwait(false);

            _lastMeasureTime = DateTime.UtcNow;
        }
        catch (Exception ex)
        {
            XTrace.WriteLine(L.TLog("Background network quality measurement failed: {0}", ex.Message));
        }
        finally
        {
            _measuring = false;
        }
    }

    /// <summary>心跳</summary>
    /// <returns></returns>
    public override async Task<IPingResponse?> Ping(CancellationToken cancellationToken = default)
    {
        var rs = await base.Ping(cancellationToken).ConfigureAwait(false);
        if (rs != null)
        {
            // 迁移到新服务器
            if (rs is IPingResponse2 prs && !prs.NewServer.IsNullOrEmpty() && prs.NewServer != Server)
            {
                var arg = new MigrationEventArgs { NewServer = prs.NewServer };

                OnMigration?.Invoke(this, arg);
                if (!arg.Cancel)
                {
                    await Logout($"切换新服务器:{prs.NewServer},原服务器:{Server}", cancellationToken).ConfigureAwait(false);

                    // 清空原有链接,添加新链接
                    Server = prs.NewServer;
                    Client = null;
                    Status = LoginStatus.Ready;

                    await Login("切换新服务器", cancellationToken).ConfigureAwait(false);
                }
            }
        }

        return rs;
    }
    #endregion

    #region 部署
    /// <summary>获取分配到本节点的应用服务信息</summary>
    /// <returns></returns>
    public Task<DeployInfo[]?> GetDeploy(Int32 deployId, String deployName, String appName) => InvokeAsync<DeployInfo[]>("Deploy/GetAll", new Dictionary<String, Object?>
    {
        ["deployId"] = deployId,
        ["deployName"] = deployName,
        ["appName"] = appName,
    });

    /// <summary>上传本节点的所有应用服务信息</summary>
    /// <param name="services"></param>
    /// <returns></returns>
    public Task<Int32> UploadDeploy(ServiceInfo[] services) => InvokeAsync<Int32>("Deploy/Upload", services);

    /// <summary>获取分配给本节点的证书信息。服务端只返回与该节点关联的证书</summary>
    /// <param name="certId">证书编号。0 表示该节点全部关联证书</param>
    /// <returns></returns>
    public Task<CertInfo[]?> GetCerts(Int32 certId = 0) => InvokeAsync<CertInfo[]>("Deploy/GetCerts", new Dictionary<String, Object?>
    {
        ["certId"] = certId,
    });

    /// <summary>应用心跳。上报应用信息</summary>
    /// <param name="inf"></param>
    /// <returns></returns>
    public Task<Int32> AppPing(AppInfo inf) => InvokeAsync<Int32>("Deploy/Ping", inf);
    #endregion

    #region 应用包
    /// <summary>搜索应用包</summary>
    /// <param name="key">关键字</param>
    /// <param name="pageIndex">页码,从1开始</param>
    /// <param name="pageSize">页大小</param>
    /// <returns>包列表</returns>
    public Task<PackageInfo[]?> SearchPackage(String? key, Int32 pageIndex = 1, Int32 pageSize = 20) => InvokeAsync<PackageInfo[]>("Package/Search", new Dictionary<String, Object?>
    {
        ["key"] = key,
        ["pageIndex"] = pageIndex,
        ["pageSize"] = pageSize,
    });

    /// <summary>获取应用包信息</summary>
    /// <param name="name">完整包名或唯一短名</param>
    /// <returns>包信息</returns>
    public Task<PackageInfo?> GetPackage(String name) => InvokeAsync<PackageInfo>("Package/Get", new Dictionary<String, Object?>
    {
        ["name"] = name,
    });

    /// <summary>获取应用包的启用版本列表</summary>
    /// <param name="name">完整包名或唯一短名</param>
    /// <returns>版本列表</returns>
    public Task<PackageVersionInfo[]?> GetPackageVersions(String name) => InvokeAsync<PackageVersionInfo[]>("Package/GetVersions", new Dictionary<String, Object?>
    {
        ["name"] = name,
    });

    /// <summary>获取目标平台的当前版本</summary>
    /// <param name="name">完整包名或唯一短名</param>
    /// <param name="os">目标操作系统。0表示使用节点平台</param>
    /// <param name="arch">目标架构。0表示使用节点平台</param>
    /// <returns>版本信息</returns>
    public Task<PackageVersionInfo?> GetLatestPackage(String name, Int32 os = 0, Int32 arch = 0) => GetLatestPackage(name, os, arch, null);

    /// <summary>获取目标平台的当前版本</summary>
    /// <param name="name">完整包名或唯一短名</param>
    /// <param name="os">目标操作系统。0表示使用节点平台</param>
    /// <param name="arch">目标架构。0表示使用节点平台</param>
    /// <param name="version">指定版本。为空时取部署集当前版本</param>
    /// <returns>版本信息</returns>
    public Task<PackageVersionInfo?> GetLatestPackage(String name, Int32 os, Int32 arch, String? version) => InvokeAsync<PackageVersionInfo>("Package/GetLatest", new Dictionary<String, Object?>
    {
        ["name"] = name,
        ["os"] = os,
        ["arch"] = arch,
        ["version"] = version,
    });

    /// <summary>获取指定版本的下载地址</summary>
    /// <param name="id">版本编号</param>
    /// <returns>版本信息</returns>
    public Task<PackageVersionInfo?> DownloadPackage(Int32 id) => InvokeAsync<PackageVersionInfo>("Package/Download", new Dictionary<String, Object?>
    {
        ["id"] = id,
    });
    #endregion

    #region 升级
    private Int32 _upgrading;
    /// <summary>获取更新信息。如有更新,则下载解压覆盖并重启应用</summary>
    /// <remarks>
    /// 心跳定时检查与平台下发的 node/upgrade 命令可能同时触发升级。两个流程同时下载并覆盖同一批文件,
    /// 会互相破坏安装目录(Linux 下正在运行的 dll 被覆盖还可能直接导致进程崩溃),因此后到者直接跳过。
    /// </remarks>
    /// <param name="channel">更新通道</param>
    /// <param name="version">指定版本。用于手工升级到指定版本,为空时按通道策略匹配</param>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns>更新信息;已有升级进行中时返回null</returns>
    public override async Task<IUpgradeInfo?> Upgrade(String? channel, String? version, CancellationToken cancellationToken = default)
    {
        if (Interlocked.CompareExchange(ref _upgrading, 1, 0) != 0)
        {
            // 平台命令会把 null 解释成“无可用更新”,这里补一条事件,让节点历史能看出是“已有升级进行中”
            var msg = L.TLog("Another upgrade is already in progress; skipping this request.");
            WriteLog(msg);
            WriteEvent("info", "Upgrade", msg);

            return null;
        }

        try
        {
            return await base.Upgrade(channel, version, cancellationToken).ConfigureAwait(false);
        }
        finally
        {
            Interlocked.Exchange(ref _upgrading, 0);
        }
    }
    #endregion
}