添加注册逻辑和创建MailSender工具方法

This commit is contained in:
Mili 2023-03-04 01:32:58 +08:00
parent 43b6251634
commit fcdc70f46b
4 changed files with 136 additions and 2 deletions

View File

@ -2,6 +2,7 @@
using Milimoe.FunGame.Core.Api.Utility;
using Milimoe.FunGame.Core.Library.Common.Network;
using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Core.Library.SQLScript.Common;
using Milimoe.FunGame.Server.Model;
using Milimoe.FunGame.Server.Others;
using Milimoe.FunGame.Server.Utility;
@ -181,6 +182,6 @@ bool Send(ClientSocket socket)
SQLResult TestSQLConnection()
{
new MySQLHelper(SQLConstant.Insert_ServerLoginLogs(Config.ServerName, Config.ServerKey)).Execute(out SQLResult TestResult);
new MySQLHelper(ServerLoginLogs.Insert_ServerLoginLogs(Config.ServerName, Config.ServerKey)).Execute(out SQLResult TestResult);
return TestResult;
}

View File

@ -1,9 +1,11 @@
using System.Data;
using Milimoe.FunGame.Core.Api.Transmittal;
using Milimoe.FunGame.Core.Api.Utility;
using Milimoe.FunGame.Core.Entity;
using Milimoe.FunGame.Core.Library.Common.Network;
using Milimoe.FunGame.Core.Library.Constant;
using Milimoe.FunGame.Core.Library.Server;
using Milimoe.FunGame.Core.Library.SQLScript.Common;
using Milimoe.FunGame.Server.Others;
using Milimoe.FunGame.Server.Utility;
@ -30,12 +32,14 @@ namespace Milimoe.FunGame.Server.Model
private string _ClientName = "";
private Guid CheckLoginKey = Guid.Empty;
private string RegVerify = "";
private int FailedTimes = 0; // 超过一定次数断开连接
private string UserName = "";
private string Password = "";
private string RoomID = "";
private readonly ServerSocket Server;
private readonly MySQLHelper SQLHelper;
private readonly MailSender? MailSender;
public ServerModel(ServerSocket server, ClientSocket socket, bool running)
{
@ -43,6 +47,7 @@ namespace Milimoe.FunGame.Server.Model
_Socket = socket;
_Running = running;
SQLHelper = new(this);
MailSender = SmtpHelper.GetMailSender();
}
public override bool Read(ClientSocket socket)
@ -84,7 +89,7 @@ namespace Milimoe.FunGame.Server.Model
if (username != null && password != null)
{
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(type) + "] UserName: " + username);
SQLHelper.Script = $"{SQLConstant.Select_Users} {SQLConstant.Command_Where} Username = '{username}' And Password = '{password}'";
SQLHelper.Script = Core.Library.SQLScript.Entity.UserQuery.Select_Users_LoginQuery(username, password);
SQLHelper.ExecuteDataSet(out SQLResult result);
if (result == SQLResult.Success)
{
@ -168,6 +173,74 @@ namespace Milimoe.FunGame.Server.Model
}
}
return true;
case SocketMessageType.Reg:
if (args != null)
{
string? username = "", email = "";
if (args.Length > 0) username = NetworkUtility.ConvertJsonObject<string>(args[0]);
if (args.Length > 1) email = NetworkUtility.ConvertJsonObject<string>(args[1]);
if (username != null && email != null)
{
RegVerify = Verification.CreateVerifyCode(VerifyCodeType.NumberVerifyCode, 6);
SQLHelper.Script = RegVerifyCodes.Insert_RegVerifyCodes(username, email, RegVerify);
SQLHelper.Execute(out SQLResult result);
if (result == SQLResult.Success && MailSender != null)
{
string ServerName = Config.ServerName;
string Subject = $"[{ServerName}] FunGame 注册验证码";
string Body = $"亲爱的 {username} <br/> 感谢您注册[{ServerName}],您的验证码是 {RegVerify} 10分钟内有效请及时输入<br/><br/>{ServerName}<br/>{DateTimeUtility.GetDateTimeToString(TimeType.DateOnly)}";
string[] To = new string[] { email };
string[] CC = new string[] { OfficialEmail.SupportEmail };
if (MailSender.Send(MailSender.CreateMail(Subject, Body, System.Net.Mail.MailPriority.Normal, true, To, CC)) == MailSendResult.Success)
{
ServerHelper.WriteLine(SocketHelper.MakeClientName(ClientName, User) + $" 已向{email}发送验证码:{RegVerify}");
}
else
{
ServerHelper.WriteLine(SocketHelper.MakeClientName(ClientName, User) + " 无法发送验证码。");
ServerHelper.WriteLine(MailSender.ErrorMsg);
}
}
}
}
return true;
case SocketMessageType.CheckReg:
if (args != null)
{
string? username = "", password = "", email = "", verifycode = "";
if (args.Length > 0) username = NetworkUtility.ConvertJsonObject<string>(args[0]);
if (args.Length > 1) password = NetworkUtility.ConvertJsonObject<string>(args[1]);
if (args.Length > 2) email = NetworkUtility.ConvertJsonObject<string>(args[2]);
if (args.Length > 3) verifycode = NetworkUtility.ConvertJsonObject<string>(args[3]);
if (username != null && password != null && email != null && verifycode != null)
{
// 先检查验证码
SQLHelper.Script = RegVerifyCodes.Select_RegVerifyCode(username, email, verifycode);
SQLHelper.ExecuteDataSet(out SQLResult result);
if (result == SQLResult.Success)
{
if (RegVerify.Equals(SQLHelper.DataSet.Tables[0].Rows[0][RegVerifyCodes.Column_RegVerifyCode]))
{
// 注册
ServerHelper.WriteLine("[" + ServerSocket.GetTypeString(type) + "] UserName: " + username + " Email: " + email);
SQLHelper.Script = Core.Library.SQLScript.Entity.UserQuery.Register(username, password, email);
SQLHelper.Execute(out result);
if (result == SQLResult.Success)
{
msg = "注册成功!请牢记您的账号与密码!";
return Send(socket, type, true, msg);
}
else msg = "服务器无法处理您的注册,注册失败!";
}
else msg = "验证码不正确,请重新输入!";
}
else msg = "服务器无法处理您的注册,注册失败!";
}
}
else msg = "注册失败!";
return Send(socket, type, false, msg);
}
return Send(socket, type, msg);
}
@ -312,6 +385,7 @@ namespace Milimoe.FunGame.Server.Model
try
{
SQLHelper.Close();
MailSender?.Dispose();
if (Socket != null)
{
Socket.Close();

View File

@ -22,4 +22,10 @@ namespace Milimoe.FunGame.Server.Others
public static Hashtable OrderList { get; } = new();
public static Hashtable OnlineClients { get; } = new Hashtable();
}
public static class OfficialEmail
{
public static string Email { get; set; } = "";
public static string SupportEmail { get; set; } = "";
}
}

View File

@ -3,6 +3,8 @@ using MySql.Data.MySqlClient;
using Milimoe.FunGame.Core.Api.Utility;
using Milimoe.FunGame.Core.Entity;
using Milimoe.FunGame.Server.Others;
using Milimoe.FunGame.Core.Api.Transmittal;
using System.Xml.Linq;
namespace Milimoe.FunGame.Server.Utility
{
@ -40,6 +42,8 @@ namespace Milimoe.FunGame.Server.Utility
settings.Add("Notice", INIHelper.ReadINI("Server", "Notice"));
settings.Add("Key", INIHelper.ReadINI("Server", "Key"));
settings.Add("Status", Convert.ToInt32(INIHelper.ReadINI("Server", "Status")));
settings.Add("OfficialMail", INIHelper.ReadINI("ServerMail", "OfficialMail"));
settings.Add("SupportMail", INIHelper.ReadINI("ServerMail", "SupportMail"));
settings.Add("Port", Convert.ToInt32(INIHelper.ReadINI("Socket", "Port")));
settings.Add("MaxPlayer", Convert.ToInt32(INIHelper.ReadINI("Socket", "MaxPlayer")));
settings.Add("MaxConnectFailed", Convert.ToInt32(INIHelper.ReadINI("Socket", "MaxConnectFailed")));
@ -64,6 +68,10 @@ namespace Milimoe.FunGame.Server.Utility
if (Describe != null) Config.ServerDescription = Describe;
if (Notice != null) Config.ServerNotice = Notice;
if (Key != null) Config.ServerKey = Key;
string? OfficialMail = (string?)settings["OfficialMail"];
string? SupportMail = (string?)settings["SupportMail"];
if (OfficialMail != null) OfficialEmail.Email = OfficialMail;
if (SupportMail != null) OfficialEmail.SupportEmail = SupportMail;
int? Status = (int?)settings["Status"];
int? Port = (int?)settings["Port"];
int? MaxPlayer = (int?)settings["MaxPlayer"];
@ -123,4 +131,49 @@ namespace Milimoe.FunGame.Server.Utility
return "客户端";
}
}
public class SmtpHelper
{
public static string SenderMailAddress { get; set; } = "";
public static string SenderName { get; set; } = "";
public static string SenderPassword { get; set; } = "";
public static string SmtpHost { get; set; } = "";
public static int SmtpPort { get; set; } = 587;
public static bool OpenSSL { get; set; } = true;
public static MailSender? GetMailSender()
{
try
{
if (SenderMailAddress == "" && SenderName == "" && SenderPassword == "" && SmtpHost == "")
{
if (INIHelper.ExistINIFile())
{
if (bool.TryParse(INIHelper.ReadINI("Mailer", "UseMailSender").ToLower(), out bool use))
{
if (use)
{
SenderMailAddress = INIHelper.ReadINI("Mailer", "MailAddress");
SenderName = INIHelper.ReadINI("Mailer", "Name");
SenderPassword = INIHelper.ReadINI("Mailer", "Password");
SmtpHost = INIHelper.ReadINI("Mailer", "Host");
if (int.TryParse(INIHelper.ReadINI("Mailer", "Port"), out int Port))
SmtpPort = Port;
if (bool.TryParse(INIHelper.ReadINI("Mailer", "OpenSSL").ToLower(), out bool SSL))
OpenSSL = SSL;
if (SmtpPort > 0) return new MailSender(SenderMailAddress, SenderName, SenderPassword, SmtpHost, SmtpPort, OpenSSL);
}
}
}
throw new SmtpHelperException();
}
return new MailSender(SenderMailAddress, SenderName, SenderPassword, SmtpHost, SmtpPort, OpenSSL);
}
catch (Exception e)
{
ServerHelper.Error(e);
}
return null;
}
}
}