From 6393888c5927786b0e5a66d52a5c5fe7d1f38a6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Go=C5=82=C4=99biowski?= Date: Tue, 11 Feb 2025 12:58:17 +0100 Subject: [PATCH 1/2] Sentry time bomb --- src/Cody.Core/Cody.Core.csproj | 4 +--- src/Cody.Core/Logging/SentryLog.cs | 34 +++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/Cody.Core/Cody.Core.csproj b/src/Cody.Core/Cody.Core.csproj index 8647a04..bf7d45e 100644 --- a/src/Cody.Core/Cody.Core.csproj +++ b/src/Cody.Core/Cody.Core.csproj @@ -130,9 +130,7 @@ 13.0.1 - - 4.13.0 - + diff --git a/src/Cody.Core/Logging/SentryLog.cs b/src/Cody.Core/Logging/SentryLog.cs index 17cc69e..1fc9164 100644 --- a/src/Cody.Core/Logging/SentryLog.cs +++ b/src/Cody.Core/Logging/SentryLog.cs @@ -2,6 +2,7 @@ using Sentry; using System; using System.Diagnostics; +using System.IO; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; @@ -12,6 +13,7 @@ public class SentryLog : ISentryLog { public const string CodyAssemblyPrefix = "Cody."; public const string ErrorData = "ErrorData"; + public const int DaysToLogToSentry = 180; public void Error(string message, Exception ex, [CallerMemberName] string callerName = "") { @@ -37,11 +39,41 @@ public void Error(string message, [CallerMemberName] string callerName = "") }); } + private static DateTime GetLinkerBuildTime(Assembly assembly) + { + try + { + var filePath = assembly.Location; + const int PeHeaderOffset = 60; + const int LinkerTimestampOffset = 8; + + var buffer = new byte[2048]; + + using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) + stream.Read(buffer, 0, buffer.Length); + + var offset = BitConverter.ToInt32(buffer, PeHeaderOffset); + var secondsSince1970 = BitConverter.ToInt32(buffer, offset + LinkerTimestampOffset); + var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + + var linkTimeUtc = epoch.AddSeconds(secondsSince1970); + return linkTimeUtc; + } + catch + { + return DateTime.Now; + } + } + public static void Initialize() { if (!Configuration.IsDebug && !Debugger.IsAttached) { - var version = Assembly.GetExecutingAssembly().GetName().Version; + var assembly = Assembly.GetExecutingAssembly(); + var buildDate = GetLinkerBuildTime(assembly); + if (Math.Abs((DateTime.UtcNow - buildDate).Days) > DaysToLogToSentry) return; + + var version = assembly.GetName().Version; string env = "dev"; if (version.Minor != 0) env = version.Minor % 2 != 0 ? "preview" : "production"; From 8bf8e3811b4f395dc25244034fe023cddd652d9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Go=C5=82=C4=99biowski?= Date: Tue, 11 Feb 2025 15:08:13 +0100 Subject: [PATCH 2/2] DateTime in UTC --- src/Cody.Core/Logging/SentryLog.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Cody.Core/Logging/SentryLog.cs b/src/Cody.Core/Logging/SentryLog.cs index 1fc9164..177ec00 100644 --- a/src/Cody.Core/Logging/SentryLog.cs +++ b/src/Cody.Core/Logging/SentryLog.cs @@ -50,7 +50,9 @@ private static DateTime GetLinkerBuildTime(Assembly assembly) var buffer = new byte[2048]; using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) + { stream.Read(buffer, 0, buffer.Length); + } var offset = BitConverter.ToInt32(buffer, PeHeaderOffset); var secondsSince1970 = BitConverter.ToInt32(buffer, offset + LinkerTimestampOffset); @@ -61,7 +63,7 @@ private static DateTime GetLinkerBuildTime(Assembly assembly) } catch { - return DateTime.Now; + return DateTime.UtcNow; } }