|
| 1 | +namespace Sentinel.SDK.Core; |
| 2 | + |
| 3 | +// ─── App Type Validation Result ─── |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Result of validating an app configuration against its type requirements. |
| 7 | +/// Ported from js-sdk/app-types.js line 188-230. |
| 8 | +/// </summary> |
| 9 | +/// <param name="Valid">True when no errors were found.</param> |
| 10 | +/// <param name="Errors">Hard validation failures (missing required config, unknown type).</param> |
| 11 | +/// <param name="Warnings">Soft issues (misaligned options, missing optional feeGranter).</param> |
| 12 | +/// <param name="TypeDescription">The matched app type description, or null when unknown.</param> |
| 13 | +public record AppConfigValidation( |
| 14 | + bool Valid, |
| 15 | + IReadOnlyList<string> Errors, |
| 16 | + IReadOnlyList<string> Warnings, |
| 17 | + string? TypeDescription); |
| 18 | + |
| 19 | +// ─── App Configuration ─── |
| 20 | + |
| 21 | +/// <summary> |
| 22 | +/// App-level configuration used by <see cref="AppTypeHelpers.ValidateAppConfig"/> |
| 23 | +/// and <see cref="AppTypeHelpers.GetConnectDefaults"/>. Mirrors the JS config object. |
| 24 | +/// </summary> |
| 25 | +public sealed class AppConfig |
| 26 | +{ |
| 27 | + /// <summary>Wallet mnemonic (required for all app types).</summary> |
| 28 | + public string? Mnemonic { get; init; } |
| 29 | + |
| 30 | + /// <summary>Plan ID (required for <see cref="Constants.AppTypes.WhiteLabel"/>).</summary> |
| 31 | + public long? PlanId { get; init; } |
| 32 | + |
| 33 | + /// <summary>Fee granter address (recommended for white-label so users don't pay gas).</summary> |
| 34 | + public string? FeeGranter { get; init; } |
| 35 | + |
| 36 | + /// <summary>DNS preset name or "handshake" (default).</summary> |
| 37 | + public string? Dns { get; init; } |
| 38 | + |
| 39 | + /// <summary>Default gigabytes for direct P2P sessions (default: 1).</summary> |
| 40 | + public int? DefaultGigabytes { get; init; } |
| 41 | + |
| 42 | + /// <summary>Prefer hourly pricing when both GB and hourly are available.</summary> |
| 43 | + public bool PreferHourly { get; init; } |
| 44 | + |
| 45 | + /// <summary>Route all traffic through VPN (default: true).</summary> |
| 46 | + public bool? FullTunnel { get; init; } |
| 47 | + |
| 48 | + /// <summary>Enable kill switch (default: false).</summary> |
| 49 | + public bool KillSwitch { get; init; } |
| 50 | + |
| 51 | + /// <summary>Optional country filter for auto-connect.</summary> |
| 52 | + public string[]? Countries { get; init; } |
| 53 | +} |
| 54 | + |
| 55 | +// ─── Connect Defaults ─── |
| 56 | + |
| 57 | +/// <summary> |
| 58 | +/// Recommended connect options for an app type. Spread into your connect call. |
| 59 | +/// Ported from js-sdk/app-types.js line 242-267. |
| 60 | +/// </summary> |
| 61 | +public sealed class ConnectDefaults |
| 62 | +{ |
| 63 | + /// <summary>DNS preset name.</summary> |
| 64 | + public string Dns { get; init; } = "handshake"; |
| 65 | + |
| 66 | + /// <summary>Route all traffic through VPN.</summary> |
| 67 | + public bool FullTunnel { get; init; } = true; |
| 68 | + |
| 69 | + /// <summary>Kill switch enabled.</summary> |
| 70 | + public bool KillSwitch { get; init; } |
| 71 | + |
| 72 | + /// <summary>Plan ID (white-label only).</summary> |
| 73 | + public long? PlanId { get; init; } |
| 74 | + |
| 75 | + /// <summary>Fee granter address (white-label only).</summary> |
| 76 | + public string? FeeGranter { get; init; } |
| 77 | + |
| 78 | + /// <summary>Gigabytes to purchase (direct-p2p only).</summary> |
| 79 | + public int? Gigabytes { get; init; } |
| 80 | + |
| 81 | + /// <summary>Prefer hourly pricing (direct-p2p only).</summary> |
| 82 | + public bool PreferHourly { get; init; } |
| 83 | +} |
| 84 | + |
| 85 | +// ─── App Type Helpers ─── |
| 86 | + |
| 87 | +/// <summary> |
| 88 | +/// Validation and defaults helpers for the three Sentinel app types. |
| 89 | +/// Ported from js-sdk/app-types.js (validateAppConfig + getConnectDefaults, lines 181-267). |
| 90 | +/// </summary> |
| 91 | +public static class AppTypeHelpers |
| 92 | +{ |
| 93 | + /// <summary> |
| 94 | + /// Validate an app's configuration against its type requirements. |
| 95 | + /// Call at app startup to catch misconfigurations early. |
| 96 | + /// Ported from js-sdk/app-types.js line 195-230. |
| 97 | + /// </summary> |
| 98 | + public static AppConfigValidation ValidateAppConfig(string appType, AppConfig? config = null) |
| 99 | + { |
| 100 | + if (!Constants.AppTypes.All.Contains(appType)) |
| 101 | + { |
| 102 | + return new AppConfigValidation( |
| 103 | + Valid: false, |
| 104 | + Errors: new[] { $"Unknown app type: \"{appType}\". Use: {string.Join(", ", Constants.AppTypes.All)}" }, |
| 105 | + Warnings: Array.Empty<string>(), |
| 106 | + TypeDescription: null); |
| 107 | + } |
| 108 | + |
| 109 | + config ??= new AppConfig(); |
| 110 | + var errors = new List<string>(); |
| 111 | + var warnings = new List<string>(); |
| 112 | + |
| 113 | + // All app types require a mnemonic. |
| 114 | + if (string.IsNullOrEmpty(config.Mnemonic)) |
| 115 | + errors.Add($"Missing required config: \"mnemonic\" (required for {appType} apps)"); |
| 116 | + |
| 117 | + if (appType == Constants.AppTypes.WhiteLabel) |
| 118 | + { |
| 119 | + if (!config.PlanId.HasValue || config.PlanId.Value <= 0) |
| 120 | + errors.Add("White-label apps MUST have a planId configured"); |
| 121 | + if (string.IsNullOrEmpty(config.FeeGranter)) |
| 122 | + warnings.Add("White-label apps should have a feeGranter so users don't pay gas. Without it, users need P2P tokens for gas fees."); |
| 123 | + } |
| 124 | + |
| 125 | + if (appType == Constants.AppTypes.DirectP2P && config.PlanId.HasValue && config.PlanId.Value > 0) |
| 126 | + { |
| 127 | + warnings.Add("planId is set but app type is direct_p2p — plan functions won't be used. Did you mean all_in_one?"); |
| 128 | + } |
| 129 | + |
| 130 | + return new AppConfigValidation( |
| 131 | + Valid: errors.Count == 0, |
| 132 | + Errors: errors, |
| 133 | + Warnings: warnings, |
| 134 | + TypeDescription: DescribeType(appType)); |
| 135 | + } |
| 136 | + |
| 137 | + /// <summary> |
| 138 | + /// Get the recommended connect options for an app type. |
| 139 | + /// Returns a base config that callers can customize per connection. |
| 140 | + /// Ported from js-sdk/app-types.js line 242-267. |
| 141 | + /// </summary> |
| 142 | + public static ConnectDefaults GetConnectDefaults(string appType, AppConfig? appConfig = null) |
| 143 | + { |
| 144 | + appConfig ??= new AppConfig(); |
| 145 | + |
| 146 | + var dns = string.IsNullOrEmpty(appConfig.Dns) ? "handshake" : appConfig.Dns; |
| 147 | + var fullTunnel = appConfig.FullTunnel ?? true; |
| 148 | + |
| 149 | + if (appType == Constants.AppTypes.WhiteLabel) |
| 150 | + { |
| 151 | + return new ConnectDefaults |
| 152 | + { |
| 153 | + Dns = dns, |
| 154 | + FullTunnel = fullTunnel, |
| 155 | + KillSwitch = appConfig.KillSwitch, |
| 156 | + PlanId = appConfig.PlanId, |
| 157 | + FeeGranter = string.IsNullOrEmpty(appConfig.FeeGranter) ? null : appConfig.FeeGranter, |
| 158 | + }; |
| 159 | + } |
| 160 | + |
| 161 | + if (appType == Constants.AppTypes.DirectP2P) |
| 162 | + { |
| 163 | + return new ConnectDefaults |
| 164 | + { |
| 165 | + Dns = dns, |
| 166 | + FullTunnel = fullTunnel, |
| 167 | + KillSwitch = appConfig.KillSwitch, |
| 168 | + Gigabytes = appConfig.DefaultGigabytes ?? 1, |
| 169 | + PreferHourly = appConfig.PreferHourly, |
| 170 | + }; |
| 171 | + } |
| 172 | + |
| 173 | + // ALL_IN_ONE or unknown — caller decides per-connection. |
| 174 | + return new ConnectDefaults |
| 175 | + { |
| 176 | + Dns = dns, |
| 177 | + FullTunnel = fullTunnel, |
| 178 | + KillSwitch = appConfig.KillSwitch, |
| 179 | + }; |
| 180 | + } |
| 181 | + |
| 182 | + private static string DescribeType(string appType) => appType switch |
| 183 | + { |
| 184 | + Constants.AppTypes.WhiteLabel => "White-label dVPN — branded app with pre-loaded plan + fee grant", |
| 185 | + Constants.AppTypes.DirectP2P => "Direct P2P — users pay nodes directly per-GB or per-hour", |
| 186 | + Constants.AppTypes.AllInOne => "All-in-one — plan subscriptions + direct P2P in one app", |
| 187 | + _ => appType, |
| 188 | + }; |
| 189 | +} |
0 commit comments