2019-12-15 04:33:48 +00:00
|
|
|
|
using System;
|
2021-02-04 03:48:30 +00:00
|
|
|
|
using System.Text.Encodings.Web;
|
2019-12-15 04:33:48 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2021-02-04 03:48:30 +00:00
|
|
|
|
using System.Web;
|
2020-06-15 18:03:50 +00:00
|
|
|
|
using Microsoft.Win32;
|
2020-06-26 17:08:30 +00:00
|
|
|
|
using Wabbajack.Common;
|
2019-12-15 04:33:48 +00:00
|
|
|
|
|
2020-06-26 17:08:30 +00:00
|
|
|
|
namespace Wabbajack.Lib
|
2019-12-15 04:33:48 +00:00
|
|
|
|
{
|
|
|
|
|
public class Metrics
|
|
|
|
|
{
|
2020-01-18 19:28:24 +00:00
|
|
|
|
public const string Downloading = "downloading";
|
|
|
|
|
public const string BeginInstall = "begin_install";
|
|
|
|
|
public const string FinishInstall = "finish_install";
|
2020-07-14 12:44:07 +00:00
|
|
|
|
private static readonly AsyncLock _creationLock = new AsyncLock();
|
2020-01-18 19:28:24 +00:00
|
|
|
|
|
2020-06-01 20:26:03 +00:00
|
|
|
|
public static async ValueTask<string> GetMetricsKey()
|
2020-01-04 02:49:08 +00:00
|
|
|
|
{
|
2020-06-01 20:26:03 +00:00
|
|
|
|
using var _ = await _creationLock.WaitAsync();
|
2020-01-04 02:49:08 +00:00
|
|
|
|
if (!Utils.HaveEncryptedJson(Consts.MetricsKeyHeader))
|
|
|
|
|
{
|
2020-06-15 18:03:50 +00:00
|
|
|
|
if (!Utils.HaveRegKeyMetricsKey())
|
|
|
|
|
{
|
|
|
|
|
// When there's no file or regkey
|
|
|
|
|
var key = Utils.MakeRandomKey();
|
|
|
|
|
await key.ToEcryptedJson(Consts.MetricsKeyHeader);
|
2020-07-14 12:44:07 +00:00
|
|
|
|
using RegistryKey regKey = Registry.CurrentUser.CreateSubKey(@"Software\Wabbajack", RegistryKeyPermissionCheck.Default)!;
|
|
|
|
|
regKey.SetValue("x-metrics-key", key);
|
2020-06-15 18:03:50 +00:00
|
|
|
|
return key;
|
|
|
|
|
}
|
2020-07-14 12:44:07 +00:00
|
|
|
|
|
|
|
|
|
// If there is no file but a registry key, create the file and transfer the data from the registry key
|
|
|
|
|
using (RegistryKey regKey = Registry.CurrentUser.OpenSubKey(@"Software\Wabbajack", RegistryKeyPermissionCheck.Default)!)
|
2020-06-15 18:03:50 +00:00
|
|
|
|
{
|
2020-07-14 12:44:07 +00:00
|
|
|
|
string key = (string)regKey.GetValue(Consts.MetricsKeyHeader)!;
|
|
|
|
|
await key.ToEcryptedJson(Consts.MetricsKeyHeader);
|
|
|
|
|
return key;
|
2020-06-15 18:03:50 +00:00
|
|
|
|
}
|
2020-06-01 22:27:57 +00:00
|
|
|
|
}
|
2020-07-14 12:44:07 +00:00
|
|
|
|
|
|
|
|
|
if (Utils.HaveRegKeyMetricsKey())
|
|
|
|
|
{
|
|
|
|
|
// When there's a file and a regkey
|
|
|
|
|
using RegistryKey regKey = Registry.CurrentUser.OpenSubKey(@"Software\Wabbajack", RegistryKeyPermissionCheck.Default)!;
|
|
|
|
|
return (string)regKey.GetValue(Consts.MetricsKeyHeader)!;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If there's a regkey and a file, return regkey
|
|
|
|
|
using (RegistryKey regKey = Registry.CurrentUser.CreateSubKey(@"Software\Wabbajack", RegistryKeyPermissionCheck.Default)!)
|
2020-06-01 22:27:57 +00:00
|
|
|
|
{
|
2020-07-14 12:44:07 +00:00
|
|
|
|
try
|
2020-06-15 18:03:50 +00:00
|
|
|
|
{
|
2020-07-14 12:44:07 +00:00
|
|
|
|
string key = await Utils.FromEncryptedJson<string>(Consts.MetricsKeyHeader)!;
|
|
|
|
|
regKey.SetValue("x-metrics-key", key);
|
|
|
|
|
return key;
|
2020-06-15 18:03:50 +00:00
|
|
|
|
}
|
2020-07-14 12:44:07 +00:00
|
|
|
|
catch (Exception)
|
2020-06-15 18:03:50 +00:00
|
|
|
|
{
|
2020-07-14 12:44:07 +00:00
|
|
|
|
// Probably an encryption error
|
|
|
|
|
await Utils.DeleteEncryptedJson(Consts.MetricsKeyHeader);
|
|
|
|
|
return await GetMetricsKey();
|
2020-06-15 18:03:50 +00:00
|
|
|
|
}
|
2020-07-14 12:44:07 +00:00
|
|
|
|
|
2020-01-04 02:49:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-15 04:33:48 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// This is all we track for metrics, action, and value. The action will be like
|
|
|
|
|
/// "downloaded", the value "Joe's list".
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="action"></param>
|
|
|
|
|
/// <param name="value"></param>
|
2021-02-17 05:46:05 +00:00
|
|
|
|
public static async Task Send(string action, string subject)
|
2019-12-15 04:33:48 +00:00
|
|
|
|
{
|
2020-07-14 12:15:01 +00:00
|
|
|
|
if (BuildServerStatus.IsBuildServerDown)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-06-14 19:55:58 +00:00
|
|
|
|
var key = await GetMetricsKey();
|
|
|
|
|
Utils.Log($"File hash check (-42) {key}");
|
2020-06-14 13:13:29 +00:00
|
|
|
|
var client = new Http.Client();
|
2020-06-14 19:55:58 +00:00
|
|
|
|
client.Headers.Add((Consts.MetricsKeyHeader, key));
|
2021-02-17 05:46:05 +00:00
|
|
|
|
await client.GetAsync($"{Consts.WabbajackBuildServerUri}metrics/{action}/{subject}");
|
2019-12-15 04:33:48 +00:00
|
|
|
|
}
|
2021-02-04 03:48:30 +00:00
|
|
|
|
|
|
|
|
|
public static async Task Error(Type type, Exception exception)
|
|
|
|
|
{
|
|
|
|
|
await Send("Exception", type.Name + "|" + exception.Message);
|
|
|
|
|
await Send("ExceptionData" + type.Name + "|" + Consts.CurrentMinimumWabbajackVersion, HttpUtility.UrlEncode($"{exception.Message}\n{exception.StackTrace}"));
|
|
|
|
|
}
|
2019-12-15 04:33:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|