This repository has been archived by the owner on Jul 19, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
ReporterService.cs
96 lines (82 loc) · 3.04 KB
/
ReporterService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using KHSave.SaveEditor.Interfaces;
using KHSave.SaveEditor.VersionCheck;
using Newtonsoft.Json;
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace KHSave.SaveEditor.Services
{
public class ReporterService : IReporter
{
internal static class GlobalReporterService
{
private const string AnonymousCookie = "YW5vbnltb3Vz";
public static string Os => Environment.OSVersion.VersionString;
public static string Ver => new DesktopCheckCurrentVersion().GetCurrentVersion();
public static void SendGameName(string name)
{
Send("open", new
{
gameId = name
});
}
public static void SendCrashReport(Exception ex)
{
Send("crash", new
{
exception = ex
});
}
public static void DeleteCookies()
{
Send("delete", new
{
cookie = GetCookie()
});
Common.Global.Cookie = string.Empty;
}
private static string GetCookie()
{
if (Common.Global.AnonymousReporting)
return AnonymousCookie;
var cookie = Common.Global.Cookie;
if (string.IsNullOrEmpty(cookie))
cookie = GenerateCookie();
return cookie;
}
private static string GenerateCookie() =>
Common.Global.Cookie = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
private static Task Send(string endpoint, object obj)
{
#if !DEBUG
if (Debugger.IsAttached)
return Task.CompletedTask;
return Task.Run(async () =>
{
try
{
using var client = new HttpClient();
var content = new StringContent(
JsonConvert.SerializeObject(obj),
Encoding.UTF8,
"application/json");
content.Headers.Add("kse-os", Os);
content.Headers.Add("kse-ver", Ver);
content.Headers.Add("kse-cookie", GetCookie());
var result = await client.PostAsync($"https://api.xee.dev/v1/khsaveeditor/{endpoint}", content);
}
catch { }
});
#else
return Task.CompletedTask;
#endif
}
}
public static ReporterService Instance { get; } = new ReporterService();
public void SendGameName(string name) => GlobalReporterService.SendGameName(name);
public void SendCrashReport(Exception ex) => GlobalReporterService.SendCrashReport(ex);
public void DeleteCookies() => GlobalReporterService.DeleteCookies();
}
}