From 756e837165e97ecee46fc8fc5f976d3bb104c8f9 Mon Sep 17 00:00:00 2001 From: Mili Date: Wed, 1 Mar 2023 23:57:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=81=E8=A3=85=E9=82=AE=E4=BB=B6=E3=80=81?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E7=A0=81=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FunGame.Core/Api/Transmittal/MailSender.cs | 28 +++++ .../Api/{Data => Transmittal}/SQLHelper.cs | 0 FunGame.Core/Api/Utility/General.cs | 105 ++++++++++++++++++ FunGame.Core/Interface/Base/IMailSender.cs | 11 ++ .../Library/Common/Network/MailObject.cs | 28 +++++ .../Library/Common/Network/ServerSocket.cs | 18 +-- FunGame.Core/Library/Constant/ConstantSet.cs | 1 + FunGame.Core/Library/Constant/ResultEnum.cs | 7 ++ FunGame.Core/Library/Constant/TypeEnum.cs | 8 ++ FunGame.Core/Library/Exception/Exception.cs | 5 + .../Library/Server/BaseServerModel.cs | 26 ----- FunGame.Core/Library/Server/SmtpClientInfo.cs | 31 ++++++ FunGame.Core/Service/MailManager.cs | 54 +++++++++ FunGame.Desktop/Library/Config/Constant.cs | 1 + 14 files changed, 288 insertions(+), 35 deletions(-) create mode 100644 FunGame.Core/Api/Transmittal/MailSender.cs rename FunGame.Core/Api/{Data => Transmittal}/SQLHelper.cs (100%) create mode 100644 FunGame.Core/Interface/Base/IMailSender.cs create mode 100644 FunGame.Core/Library/Common/Network/MailObject.cs create mode 100644 FunGame.Core/Library/Server/SmtpClientInfo.cs create mode 100644 FunGame.Core/Service/MailManager.cs diff --git a/FunGame.Core/Api/Transmittal/MailSender.cs b/FunGame.Core/Api/Transmittal/MailSender.cs new file mode 100644 index 0000000..bf9c1a1 --- /dev/null +++ b/FunGame.Core/Api/Transmittal/MailSender.cs @@ -0,0 +1,28 @@ +using Milimoe.FunGame.Core.Interface.Base; +using Milimoe.FunGame.Core.Library.Common.Network; +using Milimoe.FunGame.Core.Library.Constant; +using Milimoe.FunGame.Core.Library.Server; +using Milimoe.FunGame.Core.Service; + +namespace Milimoe.FunGame.Core.Api.Transmittal +{ + public class MailSender + { + public SmtpClientInfo SmtpClientInfo => _SmtpClientInfo; + public MailSendResult LastestResult => _LastestResult; + + private SmtpClientInfo _SmtpClientInfo; + private MailSendResult _LastestResult = MailSendResult.NotSend; + + public MailSender(string SenderMailAddress, string SenderName, string SenderPassword, string Host, int Port, bool OpenSSL) + { + _SmtpClientInfo = new SmtpClientInfo(SenderMailAddress, SenderName, SenderPassword, Host, Port, OpenSSL); + } + + public MailSendResult Send(MailObject Mail) + { + _LastestResult = MailManager.Send(this, Mail); + return _LastestResult; + } + } +} diff --git a/FunGame.Core/Api/Data/SQLHelper.cs b/FunGame.Core/Api/Transmittal/SQLHelper.cs similarity index 100% rename from FunGame.Core/Api/Data/SQLHelper.cs rename to FunGame.Core/Api/Transmittal/SQLHelper.cs diff --git a/FunGame.Core/Api/Utility/General.cs b/FunGame.Core/Api/Utility/General.cs index 56dedb9..581f5cb 100644 --- a/FunGame.Core/Api/Utility/General.cs +++ b/FunGame.Core/Api/Utility/General.cs @@ -255,4 +255,109 @@ namespace Milimoe.FunGame.Core.Api.Utility } #endregion + + #region 验证服务 + + public class Verification + { + /// + /// 生成验证码 + /// + /// 类型 + /// 长度 + /// + public static string CreateVerifyCode(VerifyCodeType type, int length) + { + return type switch + { + VerifyCodeType.MixVerifyCode => MixVerifyCode(length), + VerifyCodeType.LetterVerifyCode => LetterVerifyCode(length), + _ => NumberVerifyCode(length), + }; + } + + /// + /// 数字验证码 + /// + /// + /// + private static string NumberVerifyCode(int length) + { + int[] RandMembers = new int[length]; + int[] GetNumbers = new int[length]; + string VerifyCode = ""; + //生成起始序列值 + int seekSeek = unchecked((int)DateTime.Now.Ticks); + Random seekRand = new(seekSeek); + int beginSeek = seekRand.Next(0, int.MaxValue - length * 10000); + int[] seeks = new int[length]; + for (int i = 0; i < length; i++) + { + beginSeek += 10000; + seeks[i] = beginSeek; + } + //生成随机数字 + for (int i = 0; i < length; i++) + { + Random rand = new(seeks[i]); + int pownum = 1 * (int)Math.Pow(10, length); + RandMembers[i] = rand.Next(pownum, int.MaxValue); + } + //抽取随机数字 + for (int i = 0; i < length; i++) + { + string numStr = RandMembers[i].ToString(); + int numLength = numStr.Length; + Random rand = new(); + int numPosition = rand.Next(0, numLength - 1); + GetNumbers[i] = int.Parse(numStr.Substring(numPosition, 1)); + } + //生成验证码 + for (int i = 0; i < length; i++) + { + VerifyCode += GetNumbers[i].ToString(); + } + return VerifyCode; + } + + /// + /// 字母验证码 + /// + /// + /// + private static string LetterVerifyCode(int length) + { + char[] Verification = new char[length]; + char[] Dictionary = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; + Random random = new(); + for (int i = 0; i < length; i++) + { + Verification[i] = Dictionary[random.Next(Dictionary.Length - 1)]; + } + return new string(Verification); + } + + /// + /// 混合验证码 + /// + /// + /// + private static string MixVerifyCode(int length) + { + char[] Verification = new char[length]; + char[] Dictionary = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' + }; + Random random = new(); + for (int i = 0; i < length; i++) + { + Verification[i] = Dictionary[random.Next(Dictionary.Length - 1)]; + } + return new string(Verification); + } + } + + #endregion } diff --git a/FunGame.Core/Interface/Base/IMailSender.cs b/FunGame.Core/Interface/Base/IMailSender.cs new file mode 100644 index 0000000..5a3adf9 --- /dev/null +++ b/FunGame.Core/Interface/Base/IMailSender.cs @@ -0,0 +1,11 @@ +using Milimoe.FunGame.Core.Library.Constant; + +namespace Milimoe.FunGame.Core.Interface.Base +{ + public interface IMailSender + { + public string SenderMailAddress { get; } + public string SenderName { get; } + public string SenderPassword { get; } + } +} diff --git a/FunGame.Core/Library/Common/Network/MailObject.cs b/FunGame.Core/Library/Common/Network/MailObject.cs new file mode 100644 index 0000000..5000760 --- /dev/null +++ b/FunGame.Core/Library/Common/Network/MailObject.cs @@ -0,0 +1,28 @@ +using System.Net.Mail; + +namespace Milimoe.FunGame.Core.Library.Common.Network +{ + public class MailObject + { + public string Sender { get; } = ""; + public string SenderName { get; } = ""; + public string Subject { get; } = ""; + public string Body { get; } = ""; + public MailPriority Priority { get; } = MailPriority.Normal; + public bool HTML { get; } = false; + public string[] ToList { get; } = Array.Empty(); + public string[] CCList { get; } = Array.Empty(); + + public MailObject(string Sender, string SenderName, string Subject, string Body, MailPriority Priority, bool HTML, string[] ToList, string[] CcList) + { + this.Sender = Sender; + this.SenderName = SenderName; + this.Subject = Subject; + this.Body = Body; + this.Priority = Priority; + this.HTML = HTML; + this.ToList = ToList; + this.CCList = CcList; + } + } +} diff --git a/FunGame.Core/Library/Common/Network/ServerSocket.cs b/FunGame.Core/Library/Common/Network/ServerSocket.cs index a232b07..8835a47 100644 --- a/FunGame.Core/Library/Common/Network/ServerSocket.cs +++ b/FunGame.Core/Library/Common/Network/ServerSocket.cs @@ -16,10 +16,10 @@ namespace Milimoe.FunGame.Core.Library.Common.Network public string ServerNotice { get; } = ""; public bool Connected => Instance != null && Instance.Connected; public bool Receiving => _Receiving; - public List GetUsersList => UserThreads.GetList(); - public int UsersCount => UserThreads.Count; + public List GetUsersList => OnlineUsers.GetList(); + public int UsersCount => OnlineUsers.Count; - private readonly ThreadManager UserThreads; + private readonly ThreadManager OnlineUsers; private bool _Receiving = false; private ServerSocket(System.Net.Sockets.Socket Instance, int ServerPort, int MaxConnection = 0) @@ -27,9 +27,9 @@ namespace Milimoe.FunGame.Core.Library.Common.Network this.Instance = Instance; this.ServerPort = ServerPort; if (MaxConnection <= 0) - UserThreads = new ThreadManager(); + OnlineUsers = new ThreadManager(); else - UserThreads = new ThreadManager(MaxConnection); + OnlineUsers = new ThreadManager(MaxConnection); } public static ServerSocket StartListening(int Port = 22222, int MaxConnection = 0) @@ -54,22 +54,22 @@ namespace Milimoe.FunGame.Core.Library.Common.Network public bool AddUser(string UserName, BaseModel t) { - return UserThreads.Add(UserName, t); + return OnlineUsers.Add(UserName, t); } public bool RemoveUser(string UserName) { - return UserThreads.Remove(UserName); + return OnlineUsers.Remove(UserName); } public bool ContainsUser(string UserName) { - return UserThreads.ContainsKey(UserName); + return OnlineUsers.ContainsKey(UserName); } public BaseModel GetUser(string UserName) { - return UserThreads[UserName]; + return OnlineUsers[UserName]; } public SocketResult Send(SocketMessageType type, params object[] objs) diff --git a/FunGame.Core/Library/Constant/ConstantSet.cs b/FunGame.Core/Library/Constant/ConstantSet.cs index aa1da29..33b9fbf 100644 --- a/FunGame.Core/Library/Constant/ConstantSet.cs +++ b/FunGame.Core/Library/Constant/ConstantSet.cs @@ -34,6 +34,7 @@ public const string HeartBeat = "HeartBeat"; public const string IntoRoom = "IntoRoom"; public const string Chat = "Chat"; + public const string QuitRoom = "QuitRoom"; } public class ReflectionSet diff --git a/FunGame.Core/Library/Constant/ResultEnum.cs b/FunGame.Core/Library/Constant/ResultEnum.cs index 8c5280a..8f70119 100644 --- a/FunGame.Core/Library/Constant/ResultEnum.cs +++ b/FunGame.Core/Library/Constant/ResultEnum.cs @@ -40,4 +40,11 @@ SQLError, IsExist } + + public enum MailSendResult + { + Success, + Fail, + NotSend + } } diff --git a/FunGame.Core/Library/Constant/TypeEnum.cs b/FunGame.Core/Library/Constant/TypeEnum.cs index 4c58996..c231407 100644 --- a/FunGame.Core/Library/Constant/TypeEnum.cs +++ b/FunGame.Core/Library/Constant/TypeEnum.cs @@ -157,6 +157,14 @@ Particle } + public enum VerifyCodeType + { + NumberVerifyCode, + LetterVerifyCode, + MixVerifyCode, + ImageVerifyCode + } + public enum RoleType { Core, diff --git a/FunGame.Core/Library/Exception/Exception.cs b/FunGame.Core/Library/Exception/Exception.cs index 00ac5ee..7b446e7 100644 --- a/FunGame.Core/Library/Exception/Exception.cs +++ b/FunGame.Core/Library/Exception/Exception.cs @@ -119,4 +119,9 @@ { public override string Message => "无法发送公共信息 (#10024)"; } + + public class CanNotSendEmailException : Exception + { + public override string Message => "无法发送邮件 (#10025)"; + } } diff --git a/FunGame.Core/Library/Server/BaseServerModel.cs b/FunGame.Core/Library/Server/BaseServerModel.cs index 1b977fe..9d8017a 100644 --- a/FunGame.Core/Library/Server/BaseServerModel.cs +++ b/FunGame.Core/Library/Server/BaseServerModel.cs @@ -1,7 +1,6 @@ using Milimoe.FunGame.Core.Entity; using Milimoe.FunGame.Core.Library.Common.Network; using Milimoe.FunGame.Core.Library.Constant; -using Milimoe.FunGame.Core.Service; namespace Milimoe.FunGame.Core.Library.Server { @@ -12,34 +11,9 @@ namespace Milimoe.FunGame.Core.Library.Server public abstract Task? Task { get; } public abstract string ClientName { get; } public abstract User? User { get; } - public List GetClientsList => ClientThreads.GetList(); - public int ClientsCount => ClientThreads.Count; public abstract bool Read(ClientSocket socket); public abstract bool Send(ClientSocket socket, SocketMessageType type, params object[] objs); public abstract void Start(); - - private readonly ThreadManager ClientThreads = new(); - - public bool AddClient(string ClientName, BaseModel t) - { - return ClientThreads.Add(ClientName, t); - } - - public bool RemoveClient(string ClientName) - { - return ClientThreads.Remove(ClientName); - } - - public bool ContainsClient(string ClientName) - { - return ClientThreads.ContainsKey(ClientName); - } - - public BaseModel GetClient(string ClientName) - { - return ClientThreads[ClientName]; - } - } } diff --git a/FunGame.Core/Library/Server/SmtpClientInfo.cs b/FunGame.Core/Library/Server/SmtpClientInfo.cs new file mode 100644 index 0000000..d220375 --- /dev/null +++ b/FunGame.Core/Library/Server/SmtpClientInfo.cs @@ -0,0 +1,31 @@ +using Milimoe.FunGame.Core.Interface.Base; + +namespace Milimoe.FunGame.Core.Library.Server +{ + public class SmtpClientInfo : IMailSender + { + public string Host => _Host; + public int Port => _Port; + public bool OpenSSL => _OpenSSL; + public string SenderMailAddress => _SenderMailAddress; + public string SenderName => _SenderName; + public string SenderPassword => _SenderPassword; + + private string _Host = ""; + private int _Port = 587; + private bool _OpenSSL = true; + private string _SenderMailAddress = ""; + private string _SenderName = ""; + private string _SenderPassword = ""; + + internal SmtpClientInfo(string SenderMailAddress, string SenderName, string SenderPassword, string Host, int Port, bool OpenSSL) + { + _Host = Host; + _Port = Port; + _OpenSSL = OpenSSL; + _SenderMailAddress = SenderMailAddress; + _SenderName = SenderName; + _SenderPassword = SenderPassword; + } + } +} diff --git a/FunGame.Core/Service/MailManager.cs b/FunGame.Core/Service/MailManager.cs new file mode 100644 index 0000000..8f6116c --- /dev/null +++ b/FunGame.Core/Service/MailManager.cs @@ -0,0 +1,54 @@ +using System.Net; +using System.Net.Mail; +using Milimoe.FunGame.Core.Api.Transmittal; +using Milimoe.FunGame.Core.Library.Common.Network; +using Milimoe.FunGame.Core.Library.Constant; +using Milimoe.FunGame.Core.Library.Server; + +namespace Milimoe.FunGame.Core.Service +{ + internal class MailManager + { + internal SmtpClient? SmtpClient { get; } + + internal static MailSendResult Send(MailSender Sender, MailObject Mail) + { + SmtpClientInfo Info = Sender.SmtpClientInfo; + SmtpClient Smtp = new() + { + Host = Info.Host, + Port = Info.Port, + EnableSsl = Info.OpenSSL, + DeliveryMethod = SmtpDeliveryMethod.Network, + Credentials = new NetworkCredential(Info.SenderMailAddress, Info.SenderPassword) + }; + MailMessage Msg = new() + { + Subject = Mail.Subject, + SubjectEncoding = General.DefaultEncoding, + Body = Mail.Body, + BodyEncoding = General.DefaultEncoding, + From = new MailAddress(Mail.Sender, Mail.SenderName, General.DefaultEncoding), + IsBodyHtml = Mail.HTML, + Priority = Mail.Priority + }; + foreach (string To in Mail.ToList) + { + Msg.To.Add(To); + } + foreach (string CC in Mail.CCList) + { + Msg.CC.Add(CC); + } + try + { + Smtp.Send(Msg); + return MailSendResult.Success; + } + catch + { + return MailSendResult.Fail; + } + } + } +} diff --git a/FunGame.Desktop/Library/Config/Constant.cs b/FunGame.Desktop/Library/Config/Constant.cs index 3e46a34..21fd00b 100644 --- a/FunGame.Desktop/Library/Config/Constant.cs +++ b/FunGame.Desktop/Library/Config/Constant.cs @@ -22,6 +22,7 @@ namespace Milimoe.FunGame.Desktop.Library public const string Close = ".close"; public const string IntoRoom = ".intoroom"; public const string Chat = ".chat"; + public const string QuitRoom = ".quitroom"; } ///