|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Net.Http; |
| 5 | +using System.Text; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using NLog; |
| 9 | +using Sandbox.Game; |
| 10 | +using Torch.API; |
| 11 | +using Torch.API.Managers; |
| 12 | +using Torch.API.Session; |
| 13 | +using Torch.Managers; |
| 14 | +using Newtonsoft.Json; |
| 15 | + |
| 16 | +namespace Torch.Server.Managers |
| 17 | +{ |
| 18 | + /// <summary> |
| 19 | + /// Sends anonymous, GDPR-compliant usage telemetry to torchapi.com every 5 minutes. |
| 20 | + /// Only active when <see cref="TorchConfig.EnableAnalytics"/> is true (opt-in, default false). |
| 21 | + /// |
| 22 | + /// Data sent: server name (on registration only), player count, uptime seconds, |
| 23 | + /// sim speed, Torch version, SE version, active plugin GUIDs. No PII is collected. |
| 24 | + /// Plugin GUIDs are public identifiers — they are the same GUIDs listed on the Torch |
| 25 | + /// plugin marketplace and do not identify individual users or servers. |
| 26 | + /// |
| 27 | + /// A random UUID token is auto-generated on first registration and stored in Torch.cfg. |
| 28 | + /// To erase all stored data, send DELETE https://torchapi.com/analytics/server/{token}. |
| 29 | + /// </summary> |
| 30 | + public class AnalyticsManager : Manager |
| 31 | + { |
| 32 | + private static readonly Logger _log = LogManager.GetCurrentClassLogger(); |
| 33 | + |
| 34 | + private CancellationTokenSource _cts; |
| 35 | + |
| 36 | + private static readonly TimeSpan Interval = TimeSpan.FromMinutes(5); |
| 37 | + private static readonly TimeSpan CooldownPeriod = TimeSpan.FromMinutes(30); |
| 38 | + private const int MaxConsecutiveFailures = 5; |
| 39 | + |
| 40 | + private int _consecutiveFailures; |
| 41 | + private DateTime? _cooldownUntil; |
| 42 | + |
| 43 | + private const string RegisterUrl = "https://torchapi.com/analytics/register"; |
| 44 | + private const string ReportUrl = "https://torchapi.com/analytics/report"; |
| 45 | + |
| 46 | + private static readonly HttpClient _http = new HttpClient |
| 47 | + { |
| 48 | + Timeout = TimeSpan.FromSeconds(10) |
| 49 | + }; |
| 50 | + |
| 51 | + public AnalyticsManager(ITorchBase torchInstance) : base(torchInstance) { } |
| 52 | + |
| 53 | + public override void Attach() |
| 54 | + { |
| 55 | + base.Attach(); |
| 56 | + |
| 57 | + if (!((TorchConfig)Torch.Config).EnableAnalytics) |
| 58 | + { |
| 59 | + _log.Debug("Analytics disabled — skipping (set EnableAnalytics=true in Torch.cfg to opt in)."); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + _log.Info("======================================================="); |
| 64 | + _log.Info(" TORCH ANALYTICS — ENABLED"); |
| 65 | + _log.Info(" Torch is reporting anonymous usage data to torchapi.com"); |
| 66 | + _log.Info(" every 5 minutes. Data sent: player count, uptime,"); |
| 67 | + _log.Info(" sim speed, Torch version, SE version,"); |
| 68 | + _log.Info(" active plugin GUIDs (public identifiers, no PII)."); |
| 69 | + _log.Info(" No player names, Steam IDs, IPs, or any PII collected."); |
| 70 | + _log.Info(" Privacy policy : https://torchapi.com/privacy"); |
| 71 | + _log.Info(" To opt out : set EnableAnalytics=false in Torch.cfg"); |
| 72 | + _log.Info("======================================================="); |
| 73 | + |
| 74 | + _cts = new CancellationTokenSource(); |
| 75 | + Task.Run(() => RunLoop(_cts.Token), _cts.Token); |
| 76 | + } |
| 77 | + |
| 78 | + public override void Detach() |
| 79 | + { |
| 80 | + _cts?.Cancel(); |
| 81 | + base.Detach(); |
| 82 | + } |
| 83 | + |
| 84 | + private async Task RunLoop(CancellationToken ct) |
| 85 | + { |
| 86 | + try |
| 87 | + { |
| 88 | + // Jitter: spread registrations/reports when many servers restart together |
| 89 | + // after an update. Random delay 0–60 s so servers don't all hit the |
| 90 | + // endpoint at the same millisecond. |
| 91 | + var jitter = TimeSpan.FromSeconds(new Random().Next(0, 60)); |
| 92 | + _log.Debug($"Analytics starting in {jitter.TotalSeconds:F0}s (startup jitter)."); |
| 93 | + await Task.Delay(jitter, ct); |
| 94 | + |
| 95 | + // Self-register on first run (token is stored in Torch.cfg) |
| 96 | + if (string.IsNullOrEmpty(((TorchConfig)Torch.Config).AnalyticsToken)) |
| 97 | + await Register(ct); |
| 98 | + |
| 99 | + while (!ct.IsCancellationRequested) |
| 100 | + { |
| 101 | + await Report(ct); |
| 102 | + |
| 103 | + try |
| 104 | + { |
| 105 | + await Task.Delay(Interval, ct); |
| 106 | + } |
| 107 | + catch (TaskCanceledException) |
| 108 | + { |
| 109 | + break; |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + catch (TaskCanceledException) { } |
| 114 | + catch (Exception ex) |
| 115 | + { |
| 116 | + _log.Warn($"Analytics loop error (non-fatal): {ex.Message}"); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private async Task Register(CancellationToken ct) |
| 121 | + { |
| 122 | + try |
| 123 | + { |
| 124 | + _log.Info("Registering analytics token with torchapi.com…"); |
| 125 | + |
| 126 | + var payload = new { serverName = Torch.Config.InstanceName }; |
| 127 | + var json = JsonConvert.SerializeObject(payload); |
| 128 | + var content = new StringContent(json, Encoding.UTF8, "application/json"); |
| 129 | + |
| 130 | + var resp = await _http.PostAsync(RegisterUrl, content, ct); |
| 131 | + if (!resp.IsSuccessStatusCode) |
| 132 | + { |
| 133 | + _log.Warn($"Analytics registration failed: HTTP {(int)resp.StatusCode}"); |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + var body = await resp.Content.ReadAsStringAsync(); |
| 138 | + var data = JsonConvert.DeserializeObject<Dictionary<string, string>>(body); |
| 139 | + |
| 140 | + if (data != null && data.TryGetValue("token", out var token)) |
| 141 | + { |
| 142 | + ((TorchConfig)Torch.Config).AnalyticsToken = token; |
| 143 | + // Auto-persisted to Torch.cfg via INotifyPropertyChanged |
| 144 | + _log.Info($"Analytics token registered: {token}"); |
| 145 | + } |
| 146 | + else |
| 147 | + { |
| 148 | + _log.Warn("Analytics registration response missing token field."); |
| 149 | + } |
| 150 | + } |
| 151 | + catch (Exception ex) |
| 152 | + { |
| 153 | + _log.Warn($"Analytics registration error (non-fatal): {ex.Message}"); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private async Task Report(CancellationToken ct) |
| 158 | + { |
| 159 | + try |
| 160 | + { |
| 161 | + // Check if we're in cooldown period |
| 162 | + if (_cooldownUntil.HasValue) |
| 163 | + { |
| 164 | + if (DateTime.UtcNow < _cooldownUntil.Value) |
| 165 | + { |
| 166 | + var remaining = _cooldownUntil.Value - DateTime.UtcNow; |
| 167 | + _log.Debug($"Analytics report skipped: in cooldown for {remaining.TotalMinutes:F1} more minutes after {MaxConsecutiveFailures} consecutive failures."); |
| 168 | + return; |
| 169 | + } |
| 170 | + |
| 171 | + // Cooldown expired, reset and try again |
| 172 | + _log.Info("Analytics cooldown expired, resuming reports."); |
| 173 | + _cooldownUntil = null; |
| 174 | + _consecutiveFailures = 0; |
| 175 | + } |
| 176 | + |
| 177 | + var token = ((TorchConfig)Torch.Config).AnalyticsToken; |
| 178 | + if (string.IsNullOrEmpty(token)) |
| 179 | + { |
| 180 | + _log.Warn("Analytics report skipped: no token. Registration may have failed."); |
| 181 | + return; |
| 182 | + } |
| 183 | + |
| 184 | + int playerCount = 0; |
| 185 | + float simSpeed = 0f; |
| 186 | + long uptimeSecs = (long)(DateTime.UtcNow - Process.GetCurrentProcess().StartTime.ToUniversalTime()).TotalSeconds; |
| 187 | + |
| 188 | + var session = Torch.CurrentSession; |
| 189 | + if (session?.State == TorchSessionState.Loaded) |
| 190 | + { |
| 191 | + var mp = session.Managers.GetManager<MultiplayerManagerDedicated>(); |
| 192 | + playerCount = mp?.Players.Count ?? 0; |
| 193 | + |
| 194 | + // Read the live sim ratio from TorchServer |
| 195 | + if (TorchBase.Instance is TorchServer ts) |
| 196 | + simSpeed = ts.SimulationRatio; |
| 197 | + } |
| 198 | + |
| 199 | + // Collect active plugin GUIDs — public identifiers only, no PII |
| 200 | + var pluginGuids = new List<string>(); |
| 201 | + var pluginManager = Torch.Managers.GetManager<IPluginManager>(); |
| 202 | + if (pluginManager != null) |
| 203 | + { |
| 204 | + foreach (var plugin in pluginManager.Plugins) |
| 205 | + pluginGuids.Add(plugin.Key.ToString()); |
| 206 | + } |
| 207 | + |
| 208 | + var payload = new |
| 209 | + { |
| 210 | + token, |
| 211 | + playerCount, |
| 212 | + uptimeSeconds = uptimeSecs, |
| 213 | + simSpeed, |
| 214 | + torchVersion = Torch.TorchVersion.ToString(), |
| 215 | + seVersion = MyPerGameSettings.BasicGameInfo.GameVersion.Value.ToString(), |
| 216 | + pluginGuids |
| 217 | + }; |
| 218 | + |
| 219 | + var json = JsonConvert.SerializeObject(payload); |
| 220 | + var content = new StringContent(json, Encoding.UTF8, "application/json"); |
| 221 | + |
| 222 | + var resp = await _http.PostAsync(ReportUrl, content, ct); |
| 223 | + |
| 224 | + if (resp.IsSuccessStatusCode) |
| 225 | + { |
| 226 | + // Reset failure counter on success |
| 227 | + _consecutiveFailures = 0; |
| 228 | + _log.Debug($"Analytics report sent — players: {playerCount}, plugins: {pluginGuids.Count}, uptime: {uptimeSecs}s, sim: {simSpeed:F2} → HTTP {(int)resp.StatusCode}"); |
| 229 | + } |
| 230 | + else |
| 231 | + { |
| 232 | + _consecutiveFailures++; |
| 233 | + _log.Warn($"Analytics report failed: HTTP {(int)resp.StatusCode} (failure {_consecutiveFailures}/{MaxConsecutiveFailures})"); |
| 234 | + |
| 235 | + if (_consecutiveFailures >= MaxConsecutiveFailures) |
| 236 | + { |
| 237 | + _cooldownUntil = DateTime.UtcNow.Add(CooldownPeriod); |
| 238 | + _log.Warn($"Analytics reports disabled for {CooldownPeriod.TotalMinutes} minutes after {MaxConsecutiveFailures} consecutive failures."); |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | + catch (Exception ex) |
| 243 | + { |
| 244 | + _consecutiveFailures++; |
| 245 | + _log.Warn($"Analytics report error (non-fatal): {ex.Message} (failure {_consecutiveFailures}/{MaxConsecutiveFailures})"); |
| 246 | + |
| 247 | + if (_consecutiveFailures >= MaxConsecutiveFailures) |
| 248 | + { |
| 249 | + _cooldownUntil = DateTime.UtcNow.Add(CooldownPeriod); |
| 250 | + _log.Warn($"Analytics reports disabled for {CooldownPeriod.TotalMinutes} minutes after {MaxConsecutiveFailures} consecutive failures."); |
| 251 | + } |
| 252 | + } |
| 253 | + } |
| 254 | + } |
| 255 | +} |
0 commit comments