-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathAnalyticsUtils.cs
304 lines (263 loc) · 10.4 KB
/
AnalyticsUtils.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2022 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License")
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using Mono.Cecil;
using static RealmWeaver.Metric;
using OperatingSystem = RealmWeaver.Metric.OperatingSystem;
namespace RealmWeaver
{
internal static class AnalyticsUtils
{
public static string GetTargetOsName(FrameworkName frameworkName)
{
var targetOs = frameworkName.Identifier;
if (targetOs.ContainsIgnoreCase("android"))
{
return OperatingSystem.Android;
}
if (targetOs.ContainsIgnoreCase("ios"))
{
return OperatingSystem.Ios;
}
if (targetOs.ContainsIgnoreCase("mac"))
{
return OperatingSystem.MacOS;
}
if (targetOs.ContainsIgnoreCase("tvos"))
{
return OperatingSystem.TvOs;
}
if (targetOs.ContainsIgnoreCase("linux"))
{
return OperatingSystem.Linux;
}
if (targetOs.ContainsIgnoreCase("win") || targetOs.ContainsIgnoreCase("net4"))
{
return OperatingSystem.Windows;
}
if (targetOs.ContainsIgnoreCase("core") ||
targetOs.ContainsIgnoreCase("standard") ||
targetOs.ContainsIgnoreCase("net"))
{
return OperatingSystem.CrossPlatform;
}
return Unknown(frameworkName.Identifier);
}
public static FrameworkInfo GetFrameworkAndVersion(ModuleDefinition module)
{
// the order in the array matters as we first need to look at the libraries (maui and forms)
// and then at the frameworks (xamarin native, Catalyst and UWP)
var possibleFrameworks = new Dictionary<string, string>
{
{ "Microsoft.Maui", Framework.Maui },
{ "Xamarin.Forms.Core", Framework.XamarinForms },
{ "Xamarin.iOS", Framework.Xamarin },
{ "Xamarin.tvOS", Framework.Xamarin },
{ "Xamarin.Mac", Framework.Xamarin },
{ "Mono.Android", Framework.Xamarin },
{ "Microsoft.MacCatalyst", Framework.MacCatalyst },
{ "Windows.Foundation.UniversalApiContract", Framework.Uwp },
};
foreach (var kvp in possibleFrameworks)
{
var frameworkUsedInConjunction = module.AssemblyReferences.SingleOrDefault(a => a.Name == kvp.Key);
if (frameworkUsedInConjunction != null)
{
return new(kvp.Value, frameworkUsedInConjunction.Version.ToString());
}
}
return new("No framework of interest", "0.0.0");
}
public static string SHA256Hash(byte[] bytes, bool useLegacyEncoding = false)
{
using var sha256 = SHA256.Create();
var hash = sha256.ComputeHash(bytes);
return useLegacyEncoding ? BitConverter.ToString(hash) : Convert.ToBase64String(hash);
}
public static string GetHostCpuArchitecture() => RuntimeInformation.OSArchitecture switch
{
Architecture.X86 => CpuArchitecture.X86,
Architecture.Arm => CpuArchitecture.Arm,
Architecture.Arm64 => CpuArchitecture.Arm64,
Architecture.X64 => CpuArchitecture.X64,
_ => Unknown(RuntimeInformation.OSArchitecture.ToString())
};
public static string GetHostOsName()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return OperatingSystem.Windows;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return OperatingSystem.Linux;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return OperatingSystem.MacOS;
}
return Unknown(System.Environment.OSVersion.Platform.ToString());
}
public static string InferLanguageVersion(string netFramework, string netFrameworkVersion)
{
// We don't have a reliable way to get the version in the weaver so we're using the default version
// associated with the framework.
// Values taken from https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version
if (!netFramework.ContainsIgnoreCase("net"))
{
// Likely this isn't the model assembly, but the platform specific one
return Unknown(netFramework);
}
if (netFrameworkVersion.ContainsIgnoreCase("2.0") ||
netFrameworkVersion.ContainsIgnoreCase("4."))
{
return "7.3";
}
if (netFrameworkVersion.ContainsIgnoreCase("2.1") ||
netFrameworkVersion.ContainsIgnoreCase("3.1"))
{
return "8";
}
if (netFrameworkVersion.ContainsIgnoreCase("5.0"))
{
return "9";
}
if (netFrameworkVersion.ContainsIgnoreCase("6.0"))
{
return "10";
}
if (netFrameworkVersion.ContainsIgnoreCase("7.0"))
{
return "11";
}
return Unknown();
}
// Knowledge on unique machine Ids for different OSes obtained from https://github.com/denisbrodbeck/machineid
public static string GetAnonymizedUserId()
{
var id = string.Empty;
try
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
var machineIdToParse = RunProcess("reg", "QUERY HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Cryptography -v MachineGuid");
var regex = new Regex("\\s+MachineGuid\\s+\\w+\\s+((\\w+-?)+)", RegexOptions.Multiline);
var match = regex.Match(machineIdToParse);
if (match.Groups.Count > 1)
{
id = match.Groups[1].Value;
}
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
var machineIdToParse = RunProcess("ioreg", "-rd1 -c IOPlatformExpertDevice");
var regex = new Regex(".*\\\"IOPlatformUUID\\\"\\s=\\s\\\"(.+)\\\"", RegexOptions.Multiline);
var match = regex.Match(machineIdToParse);
if (match.Groups.Count > 1)
{
id = match.Groups[1].Value;
}
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
id = File.ReadAllText("/etc/machine-id");
}
if (id.Length == 0)
{
return Unknown();
}
// We're salting the id with an hardcoded byte array just to avoid that a machine is recognizable across
// unrelated projects that use the same mechanics to obtain a machine's ID
const string salt = "Realm is great";
var saltedId = Encoding.UTF8.GetBytes(id + salt);
return SHA256Hash(saltedId);
}
catch
{
return Unknown();
}
}
public static string GetLegacyAnonymizedUserId()
{
try
{
var id = NetworkInterface.GetAllNetworkInterfaces()
.Where(n => n.Name == "en0" || (n.OperationalStatus == OperationalStatus.Up && n.NetworkInterfaceType != NetworkInterfaceType.Loopback))
.Select(n => n.GetPhysicalAddress().GetAddressBytes())
.First();
return SHA256Hash(id, useLegacyEncoding: true);
}
catch
{
return Unknown();
}
}
private static bool ContainsIgnoreCase(this string @this, string strCompare) =>
@this.IndexOf(strCompare, StringComparison.OrdinalIgnoreCase) > -1;
private static string RunProcess(string filename, string arguments)
{
using var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = filename,
Arguments = arguments,
UseShellExecute = false,
RedirectStandardOutput = true,
#if DEBUG
RedirectStandardError = true,
#endif
}
};
proc.Start();
var stdout = new StringBuilder();
while (!proc.HasExited)
{
stdout.AppendLine(proc.StandardOutput.ReadToEnd());
#if DEBUG
stdout.AppendLine(proc.StandardError.ReadToEnd());
#endif
}
stdout.AppendLine(proc.StandardOutput.ReadToEnd());
#if DEBUG
stdout.AppendLine(proc.StandardError.ReadToEnd());
#endif
return stdout.ToString();
}
public readonly struct FrameworkInfo
{
public string Name { get; }
public string Version { get; }
public FrameworkInfo(string name, string version)
{
Name = name;
Version = version;
}
}
}
}