Merge pull request 'pop server could not connect => add it to black list' (#11) from blacklist_popServer into master

Reviewed-on: #11
This commit is contained in:
Laurențiu Constantin 2024-06-25 11:01:33 +00:00
commit 6273746693
3 changed files with 86 additions and 22 deletions

View File

@ -2265,6 +2265,7 @@ namespace AppServer
if (Program.cfg.enableEmailService)
{
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");
if (emailHT.Count > 0)
{
@ -2288,19 +2289,23 @@ namespace AppServer
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);
foreach (EmailtoSMS obj in EmailList)
{
String seqID = "0." + (DateTime.Now.GetSecondsLocalFromDT()) + DateTime.Now.Millisecond.ToString();
SM.Debug(new string[] {"-----START OK----",
"From: " + obj.from,
"ID: " + obj.id,
"Text: " + obj.text});
SM.Debug(new string[] { "-----START OK----",
"From: " + obj.from,
"ID: " + obj.id,
"Text: " + obj.text});
try
{
// 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 "'"
try
{
@ -2311,13 +2316,15 @@ namespace AppServer
{
SM.Debug("Error on converting email:" + ex.ToString());
}
//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 (emailHT.Contains(obj.from))
{
SM.Debug("Email received <<" + obj.text + ">> from address: " + obj.from);
seqID = "1." + Utils.GetSecondsLocalFromDT(DateTime.Now).ToString() + DateTime.Now.Millisecond.ToString();
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;
@ -2325,6 +2332,7 @@ namespace AppServer
byte[] buf = enc.GetBytes(cmdok);
//send to messagebus
MainForm.udp.Send(buf, buf.Length);
SM.Debug("Email thread successfully sent data to message bus");
}
}
@ -2339,26 +2347,36 @@ namespace AppServer
obj.text = obj.text.Remove(140 - 3 - obj.from.Length) + " [" + obj.from + "]";
else
obj.text = obj.text + " [" + obj.from + "]";
//send SMS on message buss
if (!obj.is_sierra)
{
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()) + "#";
if (obj.is_group) msg = "#144#" + tmpX.Gw_id + "." + tmpX.R_gw_id + "." + obj.id + "#" + obj.text + "#" + (DateTime.Now.GetSecondsLocalFromDT()) + "#";
MainForm.udp.Send(SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + msg), SafeMobileLib.Utils.Convert_text_For_multicast("#0.0" + msg).Length);
if (obj.is_group)
if (dbvehs != null)
{
Int32 oldgatewayID = 0;
foreach (RadioGateway obj2 in RadioListIP)
UnitSysPosition tmpX = dbvehs.getSystemPosition(dbvehs.getSCID(obj.id));
string cmd = $"{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);
if (msg != null && msg.Length > 0)
MainForm.udp.Send(msg, msg.Length);
if (obj.is_group)
{
if (oldgatewayID != obj2.Gw_id)
Int32 oldgatewayID = 0;
foreach (RadioGateway obj2 in RadioListIP)
{
oldgatewayID = (Int32)obj2.Gw_id;
string msg2 = "#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);
if (oldgatewayID != obj2.Gw_id)
{
oldgatewayID = (Int32)obj2.Gw_id;
cmd = $"#144#{obj2.Gw_id}.{obj2.Id}.{obj.id}#{obj.text}#{DateTime.Now.GetSecondsLocalFromDT()}#";
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 Int64 lastEmailTime = 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
public EmailServerSSL()
@ -378,11 +380,46 @@ namespace SafeMobileLib
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>
/// Connect to the Pop Server in order to be used to get the emails
/// </summary>
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(() =>
{
ConnectAsync();
@ -421,18 +458,25 @@ namespace SafeMobileLib
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("PopServerNotFoundException -> " + ee.ToString());
Console.ForegroundColor = ConsoleColor.Gray;
HandleBlacklist(serverIP);
}
catch (InvalidLoginException e)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("InvalidLoginException -> " + e.ToString());
Console.ForegroundColor = ConsoleColor.Gray;
HandleBlacklist(serverIP);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("ConnectEmailPopServer -> " + ex.ToString());
Console.ForegroundColor = ConsoleColor.Gray;
HandleBlacklist(serverIP);
}
}

View File

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