diff --git a/Protest/Protocols/Icmp.cs b/Protest/Protocols/Icmp.cs index af58c154..5ca38a10 100644 --- a/Protest/Protocols/Icmp.cs +++ b/Protest/Protocols/Icmp.cs @@ -180,10 +180,10 @@ private static async Task PingAsync(string hostname, string id, int time (int)IPStatus.DestinationHostUnreachable or (int)IPStatus.DestinationNetworkUnreachable => id + ((char)127).ToString() + "Unreachable", - (int)IPStatus.Success => id + ((char)127).ToString() + reply.RoundtripTime.ToString(), + (int)IPStatus.Success => id + ((char)127).ToString() + reply.RoundtripTime.ToString(), (int)IPStatus.TimedOut => id + ((char)127).ToString() + "Timed out", - 11050 => id + ((char)127).ToString() + "General failure", - _ => id + ((char)127).ToString() + reply.Status.ToString(), + 11050 => id + ((char)127).ToString() + "General failure", + _ => id + ((char)127).ToString() + reply.Status.ToString(), }; } catch (ArgumentException) { @@ -224,5 +224,4 @@ private static async Task ArpPingAsync(string name, string id) { } } - } \ No newline at end of file diff --git a/Protest/Tasks/LastSeen.cs b/Protest/Tasks/LastSeen.cs index 25509252..34312de6 100644 --- a/Protest/Tasks/LastSeen.cs +++ b/Protest/Tasks/LastSeen.cs @@ -1,13 +1,25 @@ using System.IO; +using System.Collections.Concurrent; using System.Net.NetworkInformation; +using System.Threading; namespace Protest.Tasks { internal static class LastSeen { + + private static ConcurrentDictionary mutexes = new ConcurrentDictionary(); + public static void Seen(in string ip) { + string filename = $"{Data.DIR_LASTSEEN}\\{ip}.txt"; try { - string filename = $"{Data.DIR_LASTSEEN}\\{ip}.txt"; - File.WriteAllText(filename, DateTime.Now.ToString(Data.DATETIME_FORMAT_LONG)); - //File.WriteAllText(filename, DateTime.UtcNow.ToString()); + if (!mutexes.TryGetValue(ip, out object mutex)) { + mutex = new object(); + mutexes[ip] = mutex; + } + + lock (mutex) { + File.WriteAllText(filename, DateTime.Now.ToString(Data.DATETIME_FORMAT_LONG)); + //File.WriteAllText(filename, DateTime.UtcNow.ToString()); + } } catch (Exception ex) { Logger.Error(ex); @@ -17,17 +29,18 @@ public static void Seen(in string ip) { public static string HasBeenSeen(in string[] para, bool recordOnly = false) { string ip = null; for (int i = 1; i < para.Length; i++) { - if (para[i].StartsWith("ip=")) + if (para[i].StartsWith("ip=")) { ip = para[i][3..]; + } } + return HasBeenSeen(ip, recordOnly); } public static string HasBeenSeen(string ip, bool recordOnly = false) { - if (ip is null) - return null; + if (ip is null) return null; - if (!recordOnly) + if (!recordOnly) { try { using Ping p = new Ping(); if (p.Send(ip, 1000).Status == IPStatus.Success) { @@ -36,12 +49,21 @@ public static string HasBeenSeen(string ip, bool recordOnly = false) { } } catch { } + } string filename = $"{Data.DIR_LASTSEEN}\\{ip}.txt"; try { - if (File.Exists(filename)) - return File.ReadAllText(filename); + if (File.Exists(filename)) { + if (!mutexes.TryGetValue(ip, out object mutex)) { + mutex = new object(); + mutexes[ip] = mutex; + } + + lock (mutex) { + return File.ReadAllText(filename); + } + } } catch { }