|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Microsoft.Win32; |
| 5 | + |
| 6 | +namespace Microsoft.DotNet.Cli.Telemetry |
| 7 | +{ |
| 8 | + internal static class DeviceIdGetter |
| 9 | + { |
| 10 | + public static string GetDeviceId() |
| 11 | + { |
| 12 | + string deviceId = GetCachedDeviceId(); |
| 13 | + |
| 14 | + // Check if the device Id is already cached |
| 15 | + if (string.IsNullOrEmpty(deviceId)) |
| 16 | + { |
| 17 | + // Generate a new guid |
| 18 | + deviceId = Guid.NewGuid().ToString("D").ToLowerInvariant(); |
| 19 | + |
| 20 | + // Cache the new device Id |
| 21 | + try |
| 22 | + { |
| 23 | + CacheDeviceId(deviceId); |
| 24 | + } |
| 25 | + catch |
| 26 | + { |
| 27 | + // If caching fails, return empty string to avoid sending a non-stored id |
| 28 | + deviceId = ""; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + return deviceId; |
| 33 | + } |
| 34 | + |
| 35 | + private static string GetCachedDeviceId() |
| 36 | + { |
| 37 | + string deviceId = null; |
| 38 | + |
| 39 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 40 | + { |
| 41 | + // Get device Id from Windows registry matching the OS architecture |
| 42 | + using (var key = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64).OpenSubKey(@"SOFTWARE\Microsoft\DeveloperTools")) |
| 43 | + { |
| 44 | + deviceId = key?.GetValue("deviceid") as string; |
| 45 | + } |
| 46 | + } |
| 47 | + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) |
| 48 | + { |
| 49 | + // Get device Id from Linux cache file |
| 50 | + string cacheFilePath; |
| 51 | + string xdgCacheHome = Environment.GetEnvironmentVariable("XDG_CACHE_HOME"); |
| 52 | + if (!string.IsNullOrEmpty(xdgCacheHome)) |
| 53 | + { |
| 54 | + cacheFilePath = Path.Combine(xdgCacheHome, "Microsoft", "DeveloperTools", "deviceid"); |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + cacheFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".cache", "deviceid"); |
| 59 | + } |
| 60 | + |
| 61 | + if (File.Exists(cacheFilePath)) |
| 62 | + { |
| 63 | + deviceId = File.ReadAllText(cacheFilePath); |
| 64 | + } |
| 65 | + } |
| 66 | + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) |
| 67 | + { |
| 68 | + // Get device Id from macOS cache file |
| 69 | + string cacheFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Library", "Application Support", "Microsoft", "DeveloperTools", "deviceid"); |
| 70 | + if (File.Exists(cacheFilePath)) |
| 71 | + { |
| 72 | + deviceId = File.ReadAllText(cacheFilePath); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return deviceId; |
| 77 | + } |
| 78 | + |
| 79 | + private static void CacheDeviceId(string deviceId) |
| 80 | + { |
| 81 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 82 | + { |
| 83 | + // Cache device Id in Windows registry matching the OS architecture |
| 84 | + using (RegistryKey baseKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64)) |
| 85 | + { |
| 86 | + using(var key = baseKey.CreateSubKey(@"SOFTWARE\Microsoft\DeveloperTools")) |
| 87 | + { |
| 88 | + if (key != null) |
| 89 | + { |
| 90 | + key.SetValue("deviceid", deviceId); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) |
| 96 | + { |
| 97 | + // Cache device Id in Linux cache file |
| 98 | + string cacheFilePath; |
| 99 | + string xdgCacheHome = Environment.GetEnvironmentVariable("XDG_CACHE_HOME"); |
| 100 | + if (!string.IsNullOrEmpty(xdgCacheHome)) |
| 101 | + { |
| 102 | + cacheFilePath = Path.Combine(xdgCacheHome, "Microsoft", "DeveloperTools", "deviceId"); |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + cacheFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".cache", "deviceid"); |
| 107 | + } |
| 108 | + |
| 109 | + CreateDirectoryAndWriteToFile(cacheFilePath, deviceId); |
| 110 | + } |
| 111 | + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) |
| 112 | + { |
| 113 | + // Cache device Id in macOS cache file |
| 114 | + string cacheFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Library", "Application Support", "Microsoft", "DeveloperTools", "deviceid"); |
| 115 | + |
| 116 | + CreateDirectoryAndWriteToFile(cacheFilePath, deviceId); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private static void CreateDirectoryAndWriteToFile(string filePath, string content) |
| 121 | + { |
| 122 | + string directory = Path.GetDirectoryName(filePath); |
| 123 | + if (!Directory.Exists(directory)) |
| 124 | + { |
| 125 | + Directory.CreateDirectory(directory); |
| 126 | + } |
| 127 | + File.WriteAllText(filePath, content); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments