44 lines
2.4 KiB
C#
44 lines
2.4 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)
|
|
{
|
|
if (!authorizedSlugs.Contains(SingleNodeClientPolicy.FixedNodeSlug, StringComparer.Ordinal))
|
|
throw new InvalidOperationException("默认线路暂不可用,请联系管理员。");
|
|
return new(SingleNodeClientPolicy.FixedNodeSlug, false);
|
|
}
|
|
if (string.IsNullOrWhiteSpace(selectedSlug) || selectedSlug == "auto"
|
|
|| !authorizedSlugs.Contains(selectedSlug, StringComparer.Ordinal))
|
|
throw new InvalidOperationException("当前账号的默认线路授权不可用。");
|
|
return new(selectedSlug, true);
|
|
}
|
|
|
|
internal int Attempts(int availableCount) => NodeSlug == SingleNodeClientPolicy.FixedNodeSlug ? 1
|
|
: Manual ? 1 : Math.Clamp(availableCount, 0, 3);
|
|
|
|
internal void ValidateResponse(string actualSlug)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(actualSlug) || actualSlug == "auto"
|
|
|| !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("平台返回的线路已被本次连接排除,未再次尝试。");
|
|
}
|
|
}
|