38 lines
2.1 KiB
C#
38 lines
2.1 KiB
C#
namespace UCVL.VPNPlus;
|
|
|
|
// A manual selection is never allowed to fall back to automatic dispatch.
|
|
internal sealed record ConnectionSelection(string NodeSlug, bool Manual)
|
|
{
|
|
internal static ConnectionSelection Create(bool manual, string? selectedSlug, IEnumerable<string> authorizedSlugs)
|
|
{
|
|
if (!manual) return new("auto", false);
|
|
if (string.IsNullOrWhiteSpace(selectedSlug) || selectedSlug == "auto"
|
|
|| !authorizedSlugs.Contains(selectedSlug, StringComparer.Ordinal))
|
|
throw new InvalidOperationException("请选择当前账号有权使用的节点;节点可能已下线或授权已变更。");
|
|
return new(selectedSlug, true);
|
|
}
|
|
|
|
internal int Attempts(int availableCount) => Manual ? 1 : Math.Clamp(availableCount, 0, 3);
|
|
|
|
internal void ValidateResponse(string actualSlug)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(actualSlug) || actualSlug == "auto"
|
|
|| (Manual && !string.Equals(NodeSlug, actualSlug, StringComparison.Ordinal)))
|
|
throw new InvalidOperationException("服务器返回的节点与连接选择不一致,已拒绝连接。");
|
|
}
|
|
|
|
internal void ValidateConfig(ConnectionAttempt attempt, ConfigResponse response, string routeMode,
|
|
IEnumerable<NodeInfo> authorizedNodes, IReadOnlyDictionary<string, DateTimeOffset> excludedNodes)
|
|
{
|
|
var slug = response.Node.Slug;
|
|
ValidateResponse(slug);
|
|
if ((attempt.Slug != "auto" && slug != attempt.Slug) || response.RouteMode != routeMode)
|
|
throw new InvalidOperationException("服务器返回的节点或代理模式与请求不一致,已拒绝连接。");
|
|
if (!attempt.IsPrivate && (response.ConnectionScope is not ("" or "public")
|
|
|| !authorizedNodes.Any(n => n is not PrivateAuxiliaryNode && n.Slug == slug)))
|
|
throw new InvalidOperationException("公共配置的节点不属于当前账号的授权范围,已拒绝连接。");
|
|
if (attempt.Slug == "auto" && excludedNodes.ContainsKey(slug))
|
|
throw new InvalidOperationException("后台返回了本次连接已排除的节点,未再次尝试。");
|
|
}
|
|
}
|