Browse Source

Obfuscate device IDs in log files, as well as the console

Antony Male 10 years ago
parent
commit
3cd1b8466b

+ 1 - 14
src/SyncTrayzor/Pages/ConsoleViewModel.cs

@@ -16,11 +16,7 @@ namespace SyncTrayzor.Pages
     {
         private const int maxLogMessages = 1500;
 
-        // Leave just the first set of digits, removing everything after it
-        private static readonly Regex deviceIdObfuscationRegex = new Regex(@"-[0-9A-Z]{7}-[0-9A-Z]{7}-[0-9A-Z]{7}-[0-9A-Z]{7}-[0-9A-Z]{7}-[0-9A-Z]{7}-[0-9A-Z]{7}");
-
         private readonly ISyncThingManager syncThingManager;
-        private readonly IConfigurationProvider configurationProvider;
         private readonly Buffer<string> logMessagesBuffer;
 
         public Queue<string> LogMessages { get; private set; }
@@ -30,24 +26,15 @@ namespace SyncTrayzor.Pages
             IConfigurationProvider configurationProvider)
         {
             this.syncThingManager = syncThingManager;
-            this.configurationProvider = configurationProvider;
             this.LogMessages = new Queue<string>();
 
-            var configuration = this.configurationProvider.Load();
-            this.configurationProvider.ConfigurationChanged += (o, e) => configuration = e.NewConfiguration;
-
             // Display log messages 100ms after the previous message, or every 500ms if they're arriving thick and fast
             this.logMessagesBuffer = new Buffer<string>(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(500));
             this.logMessagesBuffer.Delivered += (o, e) =>
             {
                 foreach (var message in e.Items)
                 {
-                    var processedMessage = message;
-                    // Check if device IDs need to be obfuscated
-                    if (configuration.ObfuscateDeviceIDs)
-                        processedMessage = deviceIdObfuscationRegex.Replace(message, "");
-
-                    this.LogMessages.Enqueue(processedMessage);
+                    this.LogMessages.Enqueue(message);
                     if (this.LogMessages.Count > maxLogMessages)
                         this.LogMessages.Dequeue();
                 }

+ 1 - 0
src/SyncTrayzor/Services/ConfigurationApplicator.cs

@@ -74,6 +74,7 @@ namespace SyncTrayzor.Services
             this.syncThingManager.SyncthingCustomHomeDir = configuration.SyncthingUseCustomHome ? this.configurationProvider.SyncthingCustomHomePath : null;
             this.syncThingManager.SyncthingDenyUpgrade = configuration.SyncthingDenyUpgrade;
             this.syncThingManager.SyncthingRunLowPriority = configuration.SyncthingRunLowPriority;
+            this.syncThingManager.SyncthingHideDeviceIds = configuration.ObfuscateDeviceIDs;
 
             this.watchedFolderMonitor.WatchedFolderIDs = configuration.Folders.Where(x => x.IsWatched).Select(x => x.ID);
 

+ 3 - 0
src/SyncTrayzor/SyncThing/SyncThingManager.cs

@@ -36,6 +36,7 @@ namespace SyncTrayzor.SyncThing
         string SyncthingCustomHomeDir { get; set; }
         bool SyncthingDenyUpgrade { get; set; }
         bool SyncthingRunLowPriority { get; set; }
+        bool SyncthingHideDeviceIds { get; set; }
         DateTime StartedTime { get; }
         DateTime LastConnectivityEventTime { get; }
         SyncthingVersion Version { get; }
@@ -116,6 +117,7 @@ namespace SyncTrayzor.SyncThing
         public string SyncthingTraceFacilities { get; set; }
         public bool SyncthingDenyUpgrade { get; set; }
         public bool SyncthingRunLowPriority { get; set; }
+        public bool SyncthingHideDeviceIds { get; set; }
 
         // Folders is a ConcurrentDictionary, which suffices for most access
         // However, it is sometimes set outright (in the case of an initial load or refresh), so we need this lock
@@ -167,6 +169,7 @@ namespace SyncTrayzor.SyncThing
                 this.processRunner.Traces = this.SyncthingTraceFacilities;
                 this.processRunner.DenyUpgrade = this.SyncthingDenyUpgrade;
                 this.processRunner.RunLowPriority = this.SyncthingRunLowPriority;
+                this.processRunner.HideDeviceIds = this.SyncthingHideDeviceIds;
 
                 this.SetState(SyncThingState.Starting);
             };

+ 9 - 0
src/SyncTrayzor/SyncThing/SyncThingProcessRunner.cs

@@ -8,6 +8,7 @@ using System.Linq;
 using System.Management;
 using System.Runtime.InteropServices;
 using System.Text;
+using System.Text.RegularExpressions;
 using System.Threading.Tasks;
 
 namespace SyncTrayzor.SyncThing
@@ -41,6 +42,7 @@ namespace SyncTrayzor.SyncThing
         string Traces { get; set; }
         bool DenyUpgrade { get; set; }
         bool RunLowPriority { get; set; }
+        bool HideDeviceIds { get; set; }
 
         event EventHandler Starting;
         event EventHandler<MessageLoggedEventArgs> MessageLogged;
@@ -55,6 +57,8 @@ namespace SyncTrayzor.SyncThing
     {
         private static readonly Logger logger = LogManager.GetCurrentClassLogger();
         private static readonly string[] defaultArguments = new[] { "-no-browser", "-no-restart" };
+        // Leave just the first set of digits, removing everything after it
+        private static readonly Regex deviceIdHideRegex = new Regex(@"-[0-9A-Z]{7}-[0-9A-Z]{7}-[0-9A-Z]{7}-[0-9A-Z]{7}-[0-9A-Z]{7}-[0-9A-Z]{7}-[0-9A-Z]{7}");
 
         private readonly object processLock = new object();
         private Process process;
@@ -66,6 +70,7 @@ namespace SyncTrayzor.SyncThing
         public string Traces { get; set; }
         public bool DenyUpgrade { get; set; }
         public bool RunLowPriority { get; set; }
+        public bool HideDeviceIds { get; set; }
 
         public event EventHandler Starting;
         public event EventHandler<MessageLoggedEventArgs> MessageLogged;
@@ -163,7 +168,11 @@ namespace SyncTrayzor.SyncThing
         private void DataReceived(string data)
         {
             if (!String.IsNullOrWhiteSpace(data))
+            {
+                if (this.HideDeviceIds)
+                    data = deviceIdHideRegex.Replace(data, "");
                 this.OnMessageLogged(data);
+            }
         }
 
         public void Dispose()