From 8c34066a4537e83ed79146c626012b122aef2058 Mon Sep 17 00:00:00 2001 From: Timothy Baldridge Date: Fri, 3 Jan 2020 19:49:08 -0700 Subject: [PATCH] Include an anonymous key with metrics --- Wabbajack.CacheServer/DTOs/Metric.cs | 1 + Wabbajack.CacheServer/Metrics.cs | 9 +++++---- Wabbajack.Common/Consts.cs | 1 + Wabbajack.Common/Metrics.cs | 8 ++++++++ Wabbajack.Common/Utils.cs | 8 ++++++++ 5 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Wabbajack.CacheServer/DTOs/Metric.cs b/Wabbajack.CacheServer/DTOs/Metric.cs index 9b090214..bf373d76 100644 --- a/Wabbajack.CacheServer/DTOs/Metric.cs +++ b/Wabbajack.CacheServer/DTOs/Metric.cs @@ -12,5 +12,6 @@ namespace Wabbajack.CacheServer.DTOs public DateTime Timestamp; public string Action; public string Subject; + public string MetricsKey; } } diff --git a/Wabbajack.CacheServer/Metrics.cs b/Wabbajack.CacheServer/Metrics.cs index 12b4a799..d38817f3 100644 --- a/Wabbajack.CacheServer/Metrics.cs +++ b/Wabbajack.CacheServer/Metrics.cs @@ -4,6 +4,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using Alphaleonis.Win32.Filesystem; +using GraphQL; using MongoDB.Driver; using MongoDB.Driver.Linq; using Nancy; @@ -19,12 +20,12 @@ namespace Wabbajack.CacheServer { private static SemaphoreSlim _lockObject = new SemaphoreSlim(1); - public static async Task Log(DateTime timestamp, string action, string subject) + public static async Task Log(DateTime timestamp, string action, string subject, string metricsKey = null) { - var msg = new[] {string.Join("\t", new[]{timestamp.ToString(), action, subject})}; + var msg = new[] {string.Join("\t", new[]{timestamp.ToString(), metricsKey, action, subject})}; Utils.Log(msg.First()); var db = Server.Config.Metrics.Connect(); - await db.InsertOneAsync(new Metric {Timestamp = timestamp, Action = action, Subject = subject}); + await db.InsertOneAsync(new Metric {Timestamp = timestamp, Action = action, Subject = subject, MetricsKey = metricsKey}); } public static Task Log(string action, string subject) @@ -63,7 +64,7 @@ namespace Wabbajack.CacheServer private async Task HandleMetrics(dynamic arg) { var date = DateTime.UtcNow; - await Log(date, arg.Action, arg.Value); + await Log(date, arg.Action, arg.Value, Request.Headers[Consts.MetricsKeyHeader].FirstOrDefault()); return date.ToString(); } diff --git a/Wabbajack.Common/Consts.cs b/Wabbajack.Common/Consts.cs index 05ca0975..4e8239ac 100644 --- a/Wabbajack.Common/Consts.cs +++ b/Wabbajack.Common/Consts.cs @@ -90,6 +90,7 @@ namespace Wabbajack.Common public static string HashFileExtension => ".xxHash"; public static string LocalAppDataPath => Path.Combine(KnownFolders.LocalAppData.Path, "Wabbajack"); + public static string MetricsKeyHeader => "x-metrics-key"; public static string WabbajackCacheLocation = "http://build.wabbajack.org/nexus_api_cache/"; diff --git a/Wabbajack.Common/Metrics.cs b/Wabbajack.Common/Metrics.cs index a276e362..55bbb8a4 100644 --- a/Wabbajack.Common/Metrics.cs +++ b/Wabbajack.Common/Metrics.cs @@ -9,6 +9,13 @@ namespace Wabbajack.Common { public class Metrics { + static Metrics() + { + if (!Utils.HaveEncryptedJson(Consts.MetricsKeyHeader)) + { + Utils.ToEcryptedJson(Consts.MetricsKeyHeader, Utils.MakeRandomKey()); + } + } /// /// This is all we track for metrics, action, and value. The action will be like /// "downloaded", the value "Joe's list". @@ -20,6 +27,7 @@ namespace Wabbajack.Common var client = new HttpClient(); try { + client.DefaultRequestHeaders.Add(Consts.MetricsKeyHeader, Utils.FromEncryptedJson(Consts.MetricsKeyHeader)); await client.GetAsync($"http://build.wabbajack.org/metrics/{action}/{value}"); } catch (Exception) { } diff --git a/Wabbajack.Common/Utils.cs b/Wabbajack.Common/Utils.cs index 6dad9a71..fbdc878d 100644 --- a/Wabbajack.Common/Utils.cs +++ b/Wabbajack.Common/Utils.cs @@ -1123,5 +1123,13 @@ namespace Wabbajack.Common GlobalMemoryStatusEx(mstat); return mstat; } + + public static string MakeRandomKey() + { + var random = new Random(); + byte[] bytes = new byte[32]; + random.NextBytes(bytes); + return bytes.ToHex(); + } } }