/// <summary> /// Выполняет отправку ip и port на почтовый сервер /// </summary> /// <param name="hostname"></param> /// <param name="port"></param> /// <param name="login"></param> /// <param name="pass"></param> /// <param name="data"></param> /// <returns></returns> public static bool SendServerIpPortToMail(string hostname, int port, string login, string pass, IpPortData data) { bool res = true; TcpClient tc = null; SslStream ssl = null; try { LogWriter.SendLog($"Передача данных о сервере на {login}"); tc = new TcpClient(); tc.Connect(hostname, port); ssl = new SslStream(tc.GetStream()); ssl.AuthenticateAsClient(hostname); ssl.ReadTimeout = 2000; ssl.Write(toByte($"EHLO A01\r\nAUTH LOGIN {toBase64(login)}\r\n{toBase64(pass)}\r\nMAIL FROM: <{login}>\r\nrcpt to: <{login}>\r\nDATA\r\nSubject: RC\r\n\n")); ssl.Write(codeBuf(Protocol.ObjectToBuffer(data))); ssl.Write(toByte("\n.\r\n")); string reply = readData(ssl); if (reply.Contains("Username and Password not accepted")) { throw new Exception("Username and Password not accepted"); } LogWriter.SendLog($"Передача данных выполнена: {data.Ip}:{data.Port}"); } catch (Exception e) { LogWriter.SendLog(e.Message, ConsoleColor.Red); res = false; } finally { if (ssl != null) { ssl.Close(); } Protocol.ProtectedCloseSocket(tc.Client); } return(res); }
/// <summary> /// Возвращает ip и port хранящиеся на почте либо null /// </summary> /// <param name="hostname"></param> /// <param name="port"></param> /// <param name="login"></param> /// <param name="pass"></param> /// <returns></returns> public static IpPortData ReadServerIpPortFromMail(string hostname, int port, string login, string pass) { IpPortData res = null; LogWriter.SendLog($"Чтение данных о сервере {login}"); TcpClient tc = null; SslStream ssl = null; try { tc = new TcpClient(); tc.Connect(hostname, port); ssl = new SslStream(tc.GetStream()); ssl.AuthenticateAsClient(hostname); ssl.ReadTimeout = 2000; ssl.Write(toByte($"A01 LOGIN {login} {pass}\r\nA02 SELECT INBOX\r\n")); string reply = readData(ssl); if (reply.Contains("[AUTHENTICATIONFAILED]")) { throw new Exception("AUTHENTICATIONFAILED"); } string mailcount = string.Empty; string countstring = string.Empty; try { countstring = reply.Split('\n').First(x => x.Contains("EXISTS")); } catch { throw new Exception($"Получен не верный ответ от почтового сервера\n{reply}"); } for (int i = 0; i < countstring.Length; i += 1) { if (int.TryParse(countstring[i].ToString(), out _)) { mailcount += countstring[i]; } } if (mailcount.Length > 0 && int.Parse(mailcount) == 0) { throw new Exception("Нет сохраненных сообщений"); } ssl.Write(toByte($"A03 FETCH {mailcount} rfc822.text\r\n")); byte[] data = readDataBuf(ssl); byte[] r = new byte[24]; bool okr = false; for (int i = 0; i < data.Length; i += 1) { if (data[i] == 60 && i + 26 < data.Length && data[i + 25] == 62) { Array.Copy(data, i + 1, r, 0, 24); okr = true; for (int ii = 0; ii < 24; ii += 1) { if (r[ii] > 58 || r[ii] < 48) { okr = false; break; } } if (okr) { break; } } } if (!okr) { throw new Exception("Последнее сообщение не содержит полезного контента"); } res = Protocol.BufferToObject <IpPortData>(decodeBuf(r)); LogWriter.SendLog($"Чтение данных выполнено: {res.Ip}:{res.Port}"); } catch (Exception e) { LogWriter.SendLog(e.Message, ConsoleColor.Red); } finally { if (ssl != null) { ssl.Close(); } Protocol.ProtectedCloseSocket(tc.Client); } return(res); }