From 4f3c7b587029864c6f292cb4701c6d45e770b14f Mon Sep 17 00:00:00 2001 From: Laurentiu Date: Thu, 13 Jun 2024 14:03:34 +0300 Subject: [PATCH] remove email addresses from blacklist after MAX_ATTEMPTS to give them one more chance --- AppServer/LocationThread.cs | 145 ++++++++++++++++++-------------- AppServer/MainForm.cs | 34 ++++++++ AppServer/MulticastListener.cs | 149 ++++++++++++++++++++++----------- 3 files changed, 216 insertions(+), 112 deletions(-) diff --git a/AppServer/LocationThread.cs b/AppServer/LocationThread.cs index 5fa07b8..2c2d41c 100644 --- a/AppServer/LocationThread.cs +++ b/AppServer/LocationThread.cs @@ -22,7 +22,7 @@ namespace AppServer private volatile DBsubsOperationManager dbsubsoperManage; private volatile DBvehiclesManager dbvehs; private DBvehiclesManager DBvehInfo; - List blackListAddress = new List(); + public static volatile Int32 LocationCheckQueuesCount = 0; Int64 count = 0; @@ -185,12 +185,12 @@ namespace AppServer { Vehicle_Data v_data = (Vehicle_Data)MainForm.VehList[radioID2.ToString()]; Alarm alarm = v_data.alm; - bool zone = false; - bool landMark = false; - String SpeedProccess = ""; - if (alarm.Zone != "") zone = true; - if (alarm.Landmark != "") landMark = true; + + bool zone = (alarm.Zone != "") ; + bool landMark = (alarm.Landmark != "") ; + + String SpeedProccess = ""; if (zone || landMark) { SpeedProccess = processZoneAlarm(radioID2, dec2.cell, zone, landMark, alarm.Email, !prevGps); @@ -351,7 +351,7 @@ namespace AppServer string test2 = "#210#1#Can't write GPS data in DB#"; if (ex.Message == "AlarmError") test2 = "#210#5#Can't write Alarm data in DB#"; else if (ex.Message == "StolenError") test2 = "#210#6#Can't write Stolen data in DB#"; - MainForm.udp.Send(SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test2), SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test2).Length); + MainForm.udp.Send(Utils.Convert_text_For_multicast("#0.0" + test2), Utils.Convert_text_For_multicast("#0.0" + test2).Length); } catch (Exception ex2) { @@ -397,7 +397,7 @@ namespace AppServer { // send callout clear string test = "#178#" + callout.RadioID.ToString() + "#" + callout.Severity + "#"; - MainForm.udp.Send(SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test), SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test).Length); + MainForm.udp.Send(Utils.Convert_text_For_multicast("#0.0" + test), Utils.Convert_text_For_multicast("#0.0" + test).Length); Utils.WriteLine($"Sent CallOut Stop request with Severity [{callout.Severity}] for unit {callout.RadioID.ToString()} "); } } @@ -409,41 +409,48 @@ namespace AppServer private void sendMailGeo(string mes, String subj, string mailAdr) { - if (Program.cfg.enableEmailService && !String.IsNullOrEmpty(Program.cfg.emailAddress) && !String.IsNullOrEmpty(mailAdr)) + if (Program.cfg.enableEmailService) { - if (blackListAddress.Contains(mailAdr)) + if (!String.IsNullOrEmpty(Program.cfg.emailAddress) && !String.IsNullOrEmpty(mailAdr)) { - Utils.WriteLine($" sendMailGeo : The email was not sent. Email address '{mailAdr}' is in blacklist "); - return; - } - - try - { - MailAddress from = new MailAddress(Program.cfg.emailAddress); - String[] address = mailAdr.Split(";".ToCharArray()); - MailAddress to = new MailAddress(address[0]); - MailMessage message = new MailMessage(from, to); - if (address.Count() > 1) + if (MainForm.IsBlacklistedAddress(mailAdr)) { - Boolean skipeFirst = true; - foreach (String obj in address) - { - if (skipeFirst) - { skipeFirst = false; } - else - message.To.Add(new MailAddress(obj)); - } + Utils.WriteLine($"sendMailGeo : The email was not sent. Email address '{mailAdr}' is in blacklist "); + return; } - message.Subject = subj; - message.Body = mes; - EmailServerSSL.sendEmail(Program.cfg.smtpServer, Program.cfg.smtpPort, Program.cfg.emailAddress, Program.cfg.emailPassword, message, Program.cfg.smtpSSLState); - } - catch (Exception ex) - { - if (ex.ToString().Contains("timed out")) - blackListAddress.Add(mailAdr); - Utils.WriteLine($"Exception in sendMailGeo: {ex.ToString()}"); + try + { + MailAddress from = new MailAddress(Program.cfg.emailAddress); + String[] address = mailAdr.Split(";".ToCharArray()); + MailAddress to = new MailAddress(address[0]); + MailMessage message = new MailMessage(from, to); + if (address.Count() > 1) + { + Boolean skipeFirst = true; + foreach (String obj in address) + { + if (skipeFirst) + { skipeFirst = false; } + else + message.To.Add(new MailAddress(obj)); + } + } + message.Subject = subj; + message.Body = mes; + EmailServerSSL.sendEmail(Program.cfg.smtpServer, Program.cfg.smtpPort, Program.cfg.emailAddress, Program.cfg.emailPassword, message, Program.cfg.smtpSSLState); + } + catch (Exception ex) + { + if (ex.ToString().Contains("timed out")) + MainForm.HandleTimeoutError(mailAdr); + + Utils.WriteLine($"Exception in sendMailGeo: {ex.ToString()}"); + } + } + else + { + Utils.WriteLine($"Missing email address From = {Program.cfg.emailAddress} To = {mailAdr})", ConsoleColor.Cyan); } } else @@ -453,32 +460,40 @@ namespace AppServer } + private void sendAlarmMail(string title, string mes, string mailAdr) { - if (Program.cfg.enableEmailService && !String.IsNullOrEmpty(Program.cfg.emailAddress) && !String.IsNullOrEmpty(mailAdr)) + if (Program.cfg.enableEmailService) { - - if (blackListAddress.Contains(mailAdr)) + if (!String.IsNullOrEmpty(Program.cfg.emailAddress) && !String.IsNullOrEmpty(mailAdr)) { - Utils.WriteLine($" sendAlarmMail : The email was not sent. Email address '{mailAdr}' is in blacklist "); - return; + + if (MainForm.IsBlacklistedAddress(mailAdr)) + { + Utils.WriteLine($" sendAlarmMail : The email was not sent. Email address '{mailAdr}' is in blacklist "); + return; + } + + try + { + MailAddress from = new MailAddress(Program.cfg.emailAddress); + MailAddress to = new MailAddress(mailAdr); + MailMessage message = new MailMessage(from, to); + message.Subject = title; + message.Body = mes; + EmailServerSSL.sendEmail(Program.cfg.smtpServer, Program.cfg.smtpPort, Program.cfg.emailAddress, Program.cfg.emailPassword, message, Program.cfg.smtpSSLState); + } + catch (Exception ex) + { + if (ex.ToString().Contains("timed out")) + MainForm.HandleTimeoutError(mailAdr); + + Utils.WriteLine("Exception in sendMailAlarm2: {0}" + ex.ToString(), ConsoleColor.Red); + } } - - try + else { - MailAddress from = new MailAddress(Program.cfg.emailAddress); - MailAddress to = new MailAddress(mailAdr); - MailMessage message = new MailMessage(from, to); - message.Subject = title; - message.Body = mes; - EmailServerSSL.sendEmail(Program.cfg.smtpServer, Program.cfg.smtpPort, Program.cfg.emailAddress, Program.cfg.emailPassword, message, Program.cfg.smtpSSLState); - } - catch (Exception ex) - { - if (ex.ToString().Contains("timed out")) - blackListAddress.Add(mailAdr); - - Utils.WriteLine("Exception in sendMailAlarm2: {0}" + ex.ToString(), ConsoleColor.Red); + Utils.WriteLine($"Missing email address From = {Program.cfg.emailAddress} To = {mailAdr})", ConsoleColor.Cyan); } } else @@ -561,7 +576,7 @@ namespace AppServer { //send alarm on message buss test = "#136#" + radioID.ToString() + "#" + tmpresp + "#"; - MainForm.udp.Send(SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test), SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test).Length); + MainForm.udp.Send(Utils.Convert_text_For_multicast("#0.0" + test), Utils.Convert_text_For_multicast("#0.0" + test).Length); } String date = DateTime.Now.ToUniversalTime().DateTo70Format().ToString(); @@ -589,7 +604,7 @@ namespace AppServer tmpX = dbvehs.getSystemPosition(Convert.ToInt32(keyobj)); test = "#142#" + tmpX.Gw_id + "." + tmpX.R_gw_id + "." + (String)tmpHashName[keyobj] + "#" + /*"Message from unit " + ((Vehicle_Data)MainForm.VehList[radioID.ToString()]).Name + " :" +*/ obj2.msgbody + "#" + date + "#"; - MainForm.udp.Send(SafeMobileLib.Utils.Convert_text_For_multicast("#0." + date + test), SafeMobileLib.Utils.Convert_text_For_multicast("#0." + date + test).Length); + MainForm.udp.Send(Utils.Convert_text_For_multicast("#0." + date + test), Utils.Convert_text_For_multicast("#0." + date + test).Length); Thread.Sleep(100); Utils.WriteLine($"Zone alert sms request [{obj2.msgbody}] for unit {(String)tmpHashName[keyobj]} on gw [{(tmpX.Gw_id + "." + tmpX.R_gw_id)}]"); @@ -601,7 +616,7 @@ namespace AppServer { tmpX = dbvehs.getSystemPositionIMEI(radioID); test = "#142#" + tmpX.Gw_id + "." + tmpX.R_gw_id + "." + radioID + "#" + obj2.msgbody2 + "#" + DateTime.Now.ToUniversalTime().DateTo70Format().ToString() + "#"; - MainForm.udp.Send(SafeMobileLib.Utils.Convert_text_For_multicast("#0." + date + test), SafeMobileLib.Utils.Convert_text_For_multicast("#0." + date + test).Length); + MainForm.udp.Send(Utils.Convert_text_For_multicast("#0." + date + test), Utils.Convert_text_For_multicast("#0." + date + test).Length); Utils.WriteLine($"Zone alert sms request [{obj2.msgbody2}] for unit {radioID} on gw [{(tmpX.Gw_id + "." + tmpX.R_gw_id)}]"); } @@ -611,7 +626,7 @@ namespace AppServer { tmpX = dbvehs.getSystemPositionIMEI(radioID); test = "#177#" + radioID.ToString() + "#" + obj2.calloutSeverity + "#" + obj2.name + "#"; - MainForm.udp.Send(SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test), SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test).Length); + MainForm.udp.Send(Utils.Convert_text_For_multicast("#0.0" + test), Utils.Convert_text_For_multicast("#0.0" + test).Length); callOutQueue.PostItem(new CallOut() { RadioID = radioID, Severity = obj2.calloutSeverity, Time = DateTime.Now}); Utils.WriteLine($"Zone CallOut request with Severity [{obj2.calloutSeverity}] for unit {radioID} on gw [{(tmpX.Gw_id + "." + tmpX.R_gw_id)}]"); @@ -667,14 +682,14 @@ namespace AppServer { //send alarm on message buss string test = "#137#" + radioID.ToString() + "#" + tmpresp + "#"; - MainForm.udp.Send(SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test), SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test).Length); + MainForm.udp.Send(Utils.Convert_text_For_multicast("#0.0" + test), Utils.Convert_text_For_multicast("#0.0" + test).Length); } //send CallOut if(obj2.callout && tmpresp.ToString().Contains("IN ")) { string test = "#177#" + radioID + "#" + obj2.calloutSeverity + "#" + obj2.name + "#"; - MainForm.udp.Send(SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test), SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test).Length); + MainForm.udp.Send(Utils.Convert_text_For_multicast("#0.0" + test), Utils.Convert_text_For_multicast("#0.0" + test).Length); callOutQueue.PostItem(new CallOut() { RadioID = radioID, Severity = obj2.calloutSeverity, Time = DateTime.Now }); Utils.WriteLine($"Landmark CallOut request with Severity [{obj2.calloutSeverity}] for unit {radioID} "); @@ -743,7 +758,7 @@ namespace AppServer //send alarm on message buss string speed4send = $"{speed}_{speedUnits}"; string test = "#135#" + radioID.ToString() + "#" + speed4send + "#"; - MainForm.udp.Send(SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test), SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + test).Length); + MainForm.udp.Send(Utils.Convert_text_For_multicast("#0.0" + test), Utils.Convert_text_For_multicast("#0.0" + test).Length); } } diff --git a/AppServer/MainForm.cs b/AppServer/MainForm.cs index e3db6a6..ca94495 100644 --- a/AppServer/MainForm.cs +++ b/AppServer/MainForm.cs @@ -96,6 +96,9 @@ namespace AppServer public static String dbAccess; private WebsocketThread websocket = null; + public static Dictionary blackListAddress = new Dictionary(); + public const int MAX_ATTEMPTS = 100; + public delegate void UpdateLicensesUIInvokeCallBack(); public MainForm() @@ -338,6 +341,37 @@ namespace AppServer } + + public static void HandleTimeoutError(string mailAdr) + { + + // add address to backlist + if (!blackListAddress.ContainsKey(mailAdr)) + blackListAddress.Add(mailAdr, MAX_ATTEMPTS); + + } + + + public static bool IsBlacklistedAddress(string mailAdr) + { + + if (!blackListAddress.ContainsKey(mailAdr)) + return false; + + // avoid to send the email MAX_ATTEMPTS times => give another chance to try to send email when count = 0 + int count = blackListAddress[mailAdr]; + if (count == 0) + { + blackListAddress.Remove(mailAdr); + return false; + } + + + blackListAddress[mailAdr] = count - 1; + return true; + } + + private void ConfigureWebSocket() { if (!checkWebsocketServerStatus.Enabled) diff --git a/AppServer/MulticastListener.cs b/AppServer/MulticastListener.cs index d3f0e23..9ec5892 100644 --- a/AppServer/MulticastListener.cs +++ b/AppServer/MulticastListener.cs @@ -28,7 +28,6 @@ namespace AppServer private DBvehiclesManager dbvehs; internal DBsubsOperationManager dbsubsoperManage; private DBcallPatchManager dbCallPatch; - private List blackListAddress = new List(); #region code for zone private volatile static ArrayList ZoneList = new ArrayList(); @@ -2152,35 +2151,48 @@ namespace AppServer private void sendAlarmMail(string mes, string mailAdr) { - - if (blackListAddress.Contains(mailAdr)) + if (Program.cfg.enableEmailService) { - Utils.WriteLine($" sendAlarmMail : The email was not sent. Email address '{mailAdr}' is in blacklist "); - return; + + if (!String.IsNullOrEmpty(Program.cfg.emailAddress) && !String.IsNullOrEmpty(mailAdr)) + { + if (MainForm.IsBlacklistedAddress(mailAdr)) + { + Utils.WriteLine($" sendAlarmMail : The email was not sent. Email address '{mailAdr}' is in blacklist "); + return; + } + + try + { + MailAddress from = new MailAddress(Program.cfg.emailAddress); + MailAddress to = new MailAddress(mailAdr); + MailMessage message = new MailMessage(from, to); + message.Subject = "Alarm"; + message.Body = mes; + EmailServerSSL.sendEmail(Program.cfg.smtpServer, Program.cfg.smtpPort, Program.cfg.emailAddress, Program.cfg.emailPassword, message, Program.cfg.smtpSSLState); + } + catch (Exception ex) + { + if (ex.ToString().Contains("timed out")) + MainForm.HandleTimeoutError(mailAdr); + + Utils.WriteLine("Exception in sendMailAlarm: " + ex.ToString(), ConsoleColor.Red); + //Console.WriteLine(e.ToString()); + } + } + else + { + Utils.WriteLine($"Missing email address From = {Program.cfg.emailAddress} To = {mailAdr})", ConsoleColor.Red); + } } - - try + else { - MailAddress from = new MailAddress(Program.cfg.emailAddress); - MailAddress to = new MailAddress(mailAdr); - MailMessage message = new MailMessage(from, to); - message.Subject = "Alarm"; - message.Body = mes; - EmailServerSSL.sendEmail(Program.cfg.smtpServer, Program.cfg.smtpPort, Program.cfg.emailAddress, Program.cfg.emailPassword, message, Program.cfg.smtpSSLState); - } - catch (Exception ex) - { - if (ex.ToString().Contains("timed out")) - blackListAddress.Add(mailAdr); - - Utils.WriteLine("Exception in sendMailAlarm: " + ex.ToString(), ConsoleColor.Red); - //Console.WriteLine(e.ToString()); + SM.Debug("email disabled"); } } - - public void sendDailyReportMail(String PdfFile, string mailAdr) + public void sendDailyReportMail(String PdfFile, string mailAdr) { try { @@ -2362,24 +2374,42 @@ namespace AppServer { if (Program.cfg.enableEmailService) { - SM.Debug("Sending mail to " + mailAdr + " from radioId " + radioID.ToString()); - try + if (!String.IsNullOrEmpty(Program.cfg.emailAddress) && !String.IsNullOrEmpty(mailAdr)) { - MailMessage message = new MailMessage(); - message.From = new MailAddress(Program.cfg.emailAddress); - message.To.Add(mailAdr); - message.Subject = "RadioID: " + radioID.ToString() + " Subject:" + (mailBody.Length < 25 ? mailBody : (mailBody.Remove(25) + "...")); - message.Body = "You received a message from radio id " + radioID.ToString() + - "\n------------------------------\n" + - mailBody + - "\n------------------------------\n "; - message.IsBodyHtml = true; - EmailServerSSL.sendEmail(Program.cfg.smtpServer, Program.cfg.smtpPort, Program.cfg.emailAddress, Program.cfg.emailPassword, message, Program.cfg.smtpSSLState); + if (MainForm.IsBlacklistedAddress(mailAdr)) + { + Utils.WriteLine($"sendMailGeo : The email was not sent. Email address '{mailAdr}' is in blacklist "); + return; + } + + + SM.Debug("Sending mail to " + mailAdr + " from radioId " + radioID.ToString()); + try + { + MailMessage message = new MailMessage(); + message.From = new MailAddress(Program.cfg.emailAddress); + message.To.Add(mailAdr); + message.Subject = "RadioID: " + radioID.ToString() + " Subject:" + (mailBody.Length < 25 ? mailBody : (mailBody.Remove(25) + "...")); + message.Body = "You received a message from radio id " + radioID.ToString() + + "\n------------------------------\n" + + mailBody + + "\n------------------------------\n "; + message.IsBodyHtml = true; + + EmailServerSSL.sendEmail(Program.cfg.smtpServer, Program.cfg.smtpPort, Program.cfg.emailAddress, Program.cfg.emailPassword, message, Program.cfg.smtpSSLState); + } + catch (Exception ex) + { + if (ex.ToString().Contains("timed out")) + MainForm.HandleTimeoutError(mailAdr); + + Utils.WriteLine("Exception in sendMail: " + ex.ToString(), ConsoleColor.Red); + } } - catch (Exception ex) + else { - Utils.WriteLine("Exception in sendMail: " + ex.ToString(), ConsoleColor.Red); + Utils.WriteLine($"Missing email address From = {Program.cfg.emailAddress} To = {mailAdr})", ConsoleColor.Red); } } else @@ -2392,20 +2422,45 @@ namespace AppServer //sms-> email Sierra Wireless private void sendMail_SW(string mailAdr, string mailBody) { - SM.Debug("Sending mail to " + mailAdr); - try + if (Program.cfg.enableEmailService) { - MailMessage message = new MailMessage(); - message.From = new MailAddress(Program.cfg.emailAddress); - message.To.Add(mailAdr); - message.Body = mailBody; - message.IsBodyHtml = true; + if (!String.IsNullOrEmpty(Program.cfg.emailAddress) && !String.IsNullOrEmpty(mailAdr)) + { - EmailServerSSL.sendEmail(Program.cfg.smtpServer, Program.cfg.smtpPort, Program.cfg.emailAddress, Program.cfg.emailPassword, message, Program.cfg.smtpSSLState); + if (MainForm.IsBlacklistedAddress(mailAdr)) + { + Utils.WriteLine($"sendMailGeo : The email was not sent. Email address '{mailAdr}' is in blacklist "); + return; + } + + SM.Debug("Sending mail to " + mailAdr); + + try + { + MailMessage message = new MailMessage(); + message.From = new MailAddress(Program.cfg.emailAddress); + message.To.Add(mailAdr); + message.Body = mailBody; + message.IsBodyHtml = true; + + EmailServerSSL.sendEmail(Program.cfg.smtpServer, Program.cfg.smtpPort, Program.cfg.emailAddress, Program.cfg.emailPassword, message, Program.cfg.smtpSSLState); + } + catch (Exception ex) + { + if (ex.ToString().Contains("timed out")) + MainForm.HandleTimeoutError(mailAdr); + + Utils.WriteLine("Exception in sendMail Sierra Wireless Gateway: " + ex.ToString(), ConsoleColor.Red); + } + } + else + { + Utils.WriteLine($"Missing email address From = {Program.cfg.emailAddress} To = {mailAdr})", ConsoleColor.Red); + } } - catch (Exception ex) + else { - Utils.WriteLine("Exception in sendMail Sierra Wireless Gateway: "+ ex.ToString() ,ConsoleColor.Red); + SM.Debug("email disabled"); } }