-
Notifications
You must be signed in to change notification settings - Fork 594
Replace source hash with Telemetry UUID #3409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| namespace Stripe.Infrastructure | ||
| { | ||
| using System; | ||
| using System.IO; | ||
| using System.Security.Cryptography; | ||
|
|
||
| internal static class TelemetryId | ||
| { | ||
| private static readonly object LockObj = new object(); | ||
|
|
||
| private static string cachedId; | ||
| private static bool loaded; | ||
|
|
||
| // For testing: overrides GetConfigDir() when set. | ||
| internal static string ConfigDirOverride { get; set; } | ||
|
|
||
| public static string Get() | ||
| { | ||
| if (loaded) | ||
| { | ||
| return cachedId; | ||
| } | ||
|
|
||
| lock (LockObj) | ||
| { | ||
| if (loaded) | ||
| { | ||
| return cachedId; | ||
| } | ||
|
|
||
| cachedId = Resolve(); | ||
| loaded = true; | ||
| } | ||
|
|
||
| return cachedId; | ||
| } | ||
|
|
||
| internal static string GetConfigDir() | ||
| { | ||
| if (Environment.OSVersion.Platform == PlatformID.Win32NT) | ||
| { | ||
| var appData = Environment.GetFolderPath( | ||
| Environment.SpecialFolder.ApplicationData); | ||
| if (string.IsNullOrEmpty(appData)) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| return Path.Combine(appData, "Stripe"); | ||
| } | ||
|
|
||
| var xdg = Environment.GetEnvironmentVariable("XDG_CONFIG_HOME"); | ||
| if (!string.IsNullOrEmpty(xdg)) | ||
| { | ||
| return Path.Combine(xdg, "stripe"); | ||
| } | ||
|
|
||
| var home = Environment.GetFolderPath( | ||
| Environment.SpecialFolder.UserProfile); | ||
| if (string.IsNullOrEmpty(home)) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| return Path.Combine(home, ".config", "stripe"); | ||
| } | ||
|
|
||
| private static string Resolve() | ||
| { | ||
| var configDir = ConfigDirOverride ?? GetConfigDir(); | ||
| if (configDir == null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| var filePath = Path.Combine(configDir, "telemetry_id"); | ||
|
|
||
| try | ||
| { | ||
| if (File.Exists(filePath)) | ||
| { | ||
| var content = File.ReadAllText(filePath).Trim(); | ||
| if (!string.IsNullOrEmpty(content)) | ||
| { | ||
| return content; | ||
| } | ||
| } | ||
| } | ||
| catch | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| var newId = GenerateId(); | ||
| try | ||
| { | ||
| Directory.CreateDirectory(configDir); | ||
| File.WriteAllText(filePath, newId); | ||
| } | ||
| catch | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| return newId; | ||
| } | ||
|
|
||
| private static string GenerateId() | ||
| { | ||
| var bytes = new byte[16]; | ||
| using (var rng = RandomNumberGenerator.Create()) | ||
| { | ||
| rng.GetBytes(bytes); | ||
| } | ||
|
|
||
| return BitConverter.ToString(bytes) | ||
| .Replace("-", string.Empty) | ||
| .ToLowerInvariant(); | ||
| } | ||
|
|
||
| // For testing | ||
| internal static void Reset() | ||
| { | ||
| lock (LockObj) | ||
| { | ||
| cachedId = null; | ||
| loaded = false; | ||
| ConfigDirOverride = null; | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.