pop server could not connect => add it to black list

This commit is contained in:
Laurențiu Constantin 2024-06-25 14:00:32 +03:00
parent 65a08de6d8
commit 377c21c338
3 changed files with 86 additions and 22 deletions

View File

@ -2265,6 +2265,7 @@ namespace AppServer
if (Program.cfg.enableEmailService) if (Program.cfg.enableEmailService)
{ {
SM.Debug(new string[] { "Checking mail at:" + DateTime.Now.ToString(), "For " + Program.cfg.emailAddress.ToString() + " on " + Program.cfg.pop3Server.ToString() }); SM.Debug(new string[] { "Checking mail at:" + DateTime.Now.ToString(), "For " + Program.cfg.emailAddress.ToString() + " on " + Program.cfg.pop3Server.ToString() });
emailHT = dbsubsoperManage.getAll_imei_sc_id("ByMail"); emailHT = dbsubsoperManage.getAll_imei_sc_id("ByMail");
if (emailHT.Count > 0) if (emailHT.Count > 0)
{ {
@ -2288,11 +2289,15 @@ namespace AppServer
DB.SetLastEmailTime(sslServer.getLastEmailTime()); DB.SetLastEmailTime(sslServer.getLastEmailTime());
} }
DBsmsManager DBsms = new DBsmsManager(Program.cfg.DB_IP, Program.cfg.DB_schema, Program.cfg.DB_user, Program.cfg.DB_passwd, Program.cfg.DB_port);
DBvehiclesManager dbvehs = new DBvehiclesManager(Program.cfg.DB_IP, Program.cfg.DB_schema, Program.cfg.DB_user, Program.cfg.DB_passwd, Program.cfg.DB_port); DBvehiclesManager dbvehs = new DBvehiclesManager(Program.cfg.DB_IP, Program.cfg.DB_schema, Program.cfg.DB_user, Program.cfg.DB_passwd, Program.cfg.DB_port);
foreach (EmailtoSMS obj in EmailList) foreach (EmailtoSMS obj in EmailList)
{ {
String seqID = "0." + (DateTime.Now.GetSecondsLocalFromDT()) + DateTime.Now.Millisecond.ToString(); String seqID = "0." + (DateTime.Now.GetSecondsLocalFromDT()) + DateTime.Now.Millisecond.ToString();
SM.Debug(new string[] { "-----START OK----", SM.Debug(new string[] { "-----START OK----",
"From: " + obj.from, "From: " + obj.from,
"ID: " + obj.id, "ID: " + obj.id,
@ -2300,7 +2305,7 @@ namespace AppServer
try try
{ {
// app server idx =0 // app server idx =0
DBsmsManager DB = new DBsmsManager(Program.cfg.DB_IP, Program.cfg.DB_schema, Program.cfg.DB_user, Program.cfg.DB_passwd, Program.cfg.DB_port);
//bigu code remove # from text and also replace "'" //bigu code remove # from text and also replace "'"
try try
{ {
@ -2311,13 +2316,15 @@ namespace AppServer
{ {
SM.Debug("Error on converting email:" + ex.ToString()); SM.Debug("Error on converting email:" + ex.ToString());
} }
//end bigu code //end bigu code
if (!obj.is_group) DB.insert_sms_received(obj.id, obj.text, 7, obj.from, seqID); if (!obj.is_group) DBsms.insert_sms_received(obj.id, obj.text, 7, obj.from, seqID);
if (obj.is_sierra) if (obj.is_sierra)
{ {
if (emailHT.Contains(obj.from)) if (emailHT.Contains(obj.from))
{ {
SM.Debug("Email received <<" + obj.text + ">> from address: " + obj.from); SM.Debug("Email received <<" + obj.text + ">> from address: " + obj.from);
seqID = "1." + Utils.GetSecondsLocalFromDT(DateTime.Now).ToString() + DateTime.Now.Millisecond.ToString(); seqID = "1." + Utils.GetSecondsLocalFromDT(DateTime.Now).ToString() + DateTime.Now.Millisecond.ToString();
string test = "#132#" + ((SW_Subscriber)emailHT[obj.from]).imei + "#" + obj.text + "#hyt#"; string test = "#132#" + ((SW_Subscriber)emailHT[obj.from]).imei + "#" + obj.text + "#hyt#";
String cmdok = "#" + seqID + test; Int32 tmp = cmdok.Length + 1; tmp += tmp.ToString().Length; cmdok = "#" + tmp.ToString() + cmdok; String cmdok = "#" + seqID + test; Int32 tmp = cmdok.Length + 1; tmp += tmp.ToString().Length; cmdok = "#" + tmp.ToString() + cmdok;
@ -2325,6 +2332,7 @@ namespace AppServer
byte[] buf = enc.GetBytes(cmdok); byte[] buf = enc.GetBytes(cmdok);
//send to messagebus //send to messagebus
MainForm.udp.Send(buf, buf.Length); MainForm.udp.Send(buf, buf.Length);
SM.Debug("Email thread successfully sent data to message bus"); SM.Debug("Email thread successfully sent data to message bus");
} }
} }
@ -2339,15 +2347,21 @@ namespace AppServer
obj.text = obj.text.Remove(140 - 3 - obj.from.Length) + " [" + obj.from + "]"; obj.text = obj.text.Remove(140 - 3 - obj.from.Length) + " [" + obj.from + "]";
else else
obj.text = obj.text + " [" + obj.from + "]"; obj.text = obj.text + " [" + obj.from + "]";
//send SMS on message buss //send SMS on message buss
if (!obj.is_sierra) if (!obj.is_sierra)
{
if (dbvehs != null)
{ {
UnitSysPosition tmpX = dbvehs.getSystemPosition(dbvehs.getSCID(obj.id)); UnitSysPosition tmpX = dbvehs.getSystemPosition(dbvehs.getSCID(obj.id));
string msg = "#143#" + tmpX.Gw_id + "." + tmpX.R_gw_id + "." + obj.id + "#" + obj.text + "#" + (DateTime.Now.GetSecondsLocalFromDT()) + "#"; string cmd = $"{tmpX.Gw_id}.{tmpX.R_gw_id}.{obj.id}#{obj.text}#{DateTime.Now.GetSecondsLocalFromDT()}#";
if (obj.is_group) msg = "#144#" + tmpX.Gw_id + "." + tmpX.R_gw_id + "." + obj.id + "#" + obj.text + "#" + (DateTime.Now.GetSecondsLocalFromDT()) + "#"; string code = (obj.is_group) ? "#144#" : "#143#" ;
byte[] msg = Utils.Convert_text_For_multicast("#0.0" + code + cmd);
MainForm.udp.Send(SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + msg), SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + msg).Length); if (msg != null && msg.Length > 0)
MainForm.udp.Send(msg, msg.Length);
if (obj.is_group) if (obj.is_group)
{ {
@ -2357,8 +2371,12 @@ namespace AppServer
if (oldgatewayID != obj2.Gw_id) if (oldgatewayID != obj2.Gw_id)
{ {
oldgatewayID = (Int32)obj2.Gw_id; oldgatewayID = (Int32)obj2.Gw_id;
string msg2 = "#144#" + obj2.Gw_id + "." + obj2.Id + "." + obj.id + "#" + obj.text + "#" + (DateTime.Now.GetSecondsLocalFromDT()) + "#"; cmd = $"#144#{obj2.Gw_id}.{obj2.Id}.{obj.id}#{obj.text}#{DateTime.Now.GetSecondsLocalFromDT()}#";
MainForm.udp.Send(SafeMobileLib.Utils.Convert_text_For_multicast("#1." + (DateTime.Now.GetSecondsLocalFromDT()) + DateTime.Now.Ticks.ToString() + msg2), SafeMobileLib.Utils.Convert_text_For_multicast("#1." + (DateTime.Now.GetSecondsLocalFromDT()) + DateTime.Now.Ticks.ToString() + msg2).Length); byte[] msg2 = Utils.Convert_text_For_multicast("#1." + (DateTime.Now.GetSecondsLocalFromDT()) + DateTime.Now.Ticks.ToString() + cmd);
if( msg2 != null && msg2.Length > 0)
MainForm.udp.Send(msg2, msg2.Length);
}
} }
} }
} }

View File

@ -25,6 +25,8 @@ namespace SafeMobileLib
private bool isSSL = true; private bool isSSL = true;
private Int64 lastEmailTime = 0; private Int64 lastEmailTime = 0;
private Int64 startEmailServiceTime = 0; private Int64 startEmailServiceTime = 0;
public static Dictionary<string, int> blackList = new Dictionary<string, int>();
public const int MAX_ATTEMPTS = 100;
// blank constructor in order to be used in Email Checking // blank constructor in order to be used in Email Checking
public EmailServerSSL() public EmailServerSSL()
@ -378,11 +380,46 @@ namespace SafeMobileLib
return ret; return ret;
} }
public static void HandleBlacklist(string key)
{
// add key to backlist
if (!blackList.ContainsKey(key))
blackList.Add(key, MAX_ATTEMPTS);
}
public static bool IsBlacklisted(string key)
{
if (!blackList.ContainsKey(key))
return false;
// avoid to connect to pop3 MAX_ATTEMPTS times => give another chance to try to connect to pop 3 when count = 0
int count = blackList[key];
if (count == 0)
{
blackList.Remove(key);
return false;
}
blackList[key] = count - 1;
return true;
}
/// <summary> /// <summary>
/// Connect to the Pop Server in order to be used to get the emails /// Connect to the Pop Server in order to be used to get the emails
/// </summary> /// </summary>
private void ConnectEmailPopServer() private void ConnectEmailPopServer()
{ {
if (IsBlacklisted(serverIP))
{
Utils.WriteLine($"ConnectEmailPopServer : Could not conenct to the pop server '{serverIP}'. The pop server '{serverIP}' is in blacklist count = {blackList[serverIP]}");
return;
}
Thread t = new Thread(() => Thread t = new Thread(() =>
{ {
ConnectAsync(); ConnectAsync();
@ -421,18 +458,25 @@ namespace SafeMobileLib
Console.ForegroundColor = ConsoleColor.Yellow; Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("PopServerNotFoundException -> " + ee.ToString()); Console.WriteLine("PopServerNotFoundException -> " + ee.ToString());
Console.ForegroundColor = ConsoleColor.Gray; Console.ForegroundColor = ConsoleColor.Gray;
HandleBlacklist(serverIP);
} }
catch (InvalidLoginException e) catch (InvalidLoginException e)
{ {
Console.ForegroundColor = ConsoleColor.Yellow; Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("InvalidLoginException -> " + e.ToString()); Console.WriteLine("InvalidLoginException -> " + e.ToString());
Console.ForegroundColor = ConsoleColor.Gray; Console.ForegroundColor = ConsoleColor.Gray;
HandleBlacklist(serverIP);
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.ForegroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("ConnectEmailPopServer -> " + ex.ToString()); Console.WriteLine("ConnectEmailPopServer -> " + ex.ToString());
Console.ForegroundColor = ConsoleColor.Gray; Console.ForegroundColor = ConsoleColor.Gray;
HandleBlacklist(serverIP);
} }
} }

View File

@ -441,8 +441,10 @@ namespace SafeMobileLib
Int32 tmp = cmdok.Length + 1; Int32 tmp = cmdok.Length + 1;
tmp += tmp.ToString().Length; tmp += tmp.ToString().Length;
String TMPcmdok = "#" + tmp.ToString() + cmdok; String TMPcmdok = "#" + tmp.ToString() + cmdok;
if (tmp != TMPcmdok.Length) cmdok = "#" + TMPcmdok.Length + cmdok; if (tmp != TMPcmdok.Length)
else cmdok = TMPcmdok; cmdok = "#" + TMPcmdok.Length + cmdok;
else
cmdok = TMPcmdok;
System.Text.Encoding enc = System.Text.Encoding.ASCII; System.Text.Encoding enc = System.Text.Encoding.ASCII;
byte[] buf = enc.GetBytes(cmdok); byte[] buf = enc.GetBytes(cmdok);