mirror of
https://github.com/project-redbud/FunGame-Server.git
synced 2025-04-23 04:29:38 +08:00
添加了连接检查事件
This commit is contained in:
parent
77e8fc9a82
commit
29d661f8a0
@ -1,4 +1,5 @@
|
|||||||
using Milimoe.FunGame.Core.Interface.Base;
|
using System.Text;
|
||||||
|
using Milimoe.FunGame.Core.Interface.Base;
|
||||||
using Milimoe.FunGame.Core.Library.Common.Network;
|
using Milimoe.FunGame.Core.Library.Common.Network;
|
||||||
using Milimoe.FunGame.Core.Library.Constant;
|
using Milimoe.FunGame.Core.Library.Constant;
|
||||||
using Milimoe.FunGame.Server.Others;
|
using Milimoe.FunGame.Server.Others;
|
||||||
@ -24,9 +25,8 @@ namespace Milimoe.FunGame.Server.Controller
|
|||||||
{
|
{
|
||||||
bool isConnected = false;
|
bool isConnected = false;
|
||||||
bool isDebugMode = false;
|
bool isDebugMode = false;
|
||||||
|
objs = objs.Where(o => o.SocketType == SocketMessageType.Connect);
|
||||||
foreach (SocketObject obj in objs)
|
foreach (SocketObject obj in objs)
|
||||||
{
|
|
||||||
if (obj.SocketType == SocketMessageType.Connect)
|
|
||||||
{
|
{
|
||||||
if (Config.ConnectingPlayerCount + Config.OnlinePlayerCount > Config.MaxPlayers)
|
if (Config.ConnectingPlayerCount + Config.OnlinePlayerCount > Config.MaxPlayers)
|
||||||
{
|
{
|
||||||
@ -44,22 +44,8 @@ namespace Milimoe.FunGame.Server.Controller
|
|||||||
|
|
||||||
ServerHelper.WriteLine("[" + SocketSet.GetTypeString(obj.SocketType) + "] " + ServerHelper.MakeClientName(socket.ClientIP), InvokeMessageType.Core);
|
ServerHelper.WriteLine("[" + SocketSet.GetTypeString(obj.SocketType) + "] " + ServerHelper.MakeClientName(socket.ClientIP), InvokeMessageType.Core);
|
||||||
|
|
||||||
// 读取参数
|
// 验证客户端参数
|
||||||
// 参数1:客户端的游戏模组列表,没有服务器的需要拒绝
|
string msg = OnValidateClientParameters(ref isDebugMode, obj);
|
||||||
string[] modes = obj.GetParam<string[]>(0) ?? [];
|
|
||||||
// 参数2:客户端是否开启了开发者模式,开启开发者模式部分功能不可用
|
|
||||||
isDebugMode = obj.GetParam<bool>(1);
|
|
||||||
if (isDebugMode) ServerHelper.WriteLine("客户端已开启开发者模式");
|
|
||||||
|
|
||||||
string msg = "";
|
|
||||||
List<string> ClientDontHave = [];
|
|
||||||
string strDontHave = string.Join("\r\n", Config.GameModuleSupported.Where(mode => !modes.Contains(mode)));
|
|
||||||
if (strDontHave != "")
|
|
||||||
{
|
|
||||||
strDontHave = "客户端缺少服务器所需的模组:" + strDontHave;
|
|
||||||
ServerHelper.WriteLine(strDontHave, InvokeMessageType.Core);
|
|
||||||
msg += strDontHave;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (msg == "" && await socket.SendAsync(SocketMessageType.Connect, true, msg, token, Config.ServerName, Config.ServerNotice) == SocketResult.Success)
|
if (msg == "" && await socket.SendAsync(SocketMessageType.Connect, true, msg, token, Config.ServerName, Config.ServerNotice) == SocketResult.Success)
|
||||||
{
|
{
|
||||||
@ -78,7 +64,6 @@ namespace Milimoe.FunGame.Server.Controller
|
|||||||
return (isConnected, isDebugMode);
|
return (isConnected, isDebugMode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
await SendRefuseConnect(socket, "服务器已拒绝连接。");
|
await SendRefuseConnect(socket, "服务器已拒绝连接。");
|
||||||
ServerHelper.WriteLine("客户端发送了不符合FunGame规定的字符,拒绝连接。", InvokeMessageType.Core);
|
ServerHelper.WriteLine("客户端发送了不符合FunGame规定的字符,拒绝连接。", InvokeMessageType.Core);
|
||||||
@ -130,5 +115,60 @@ namespace Milimoe.FunGame.Server.Controller
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 验证客户端参数事件
|
||||||
|
/// </summary>
|
||||||
|
public static event Func<SocketObject, string>? ValidateClientParameters;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 触发验证客户端参数事件
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="obj"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string OnValidateClientParameters(ref bool isDebugMode, SocketObject obj)
|
||||||
|
{
|
||||||
|
StringBuilder builder = new();
|
||||||
|
|
||||||
|
// 先检查 Desktop 参数
|
||||||
|
if (Config.UseDesktopParameters)
|
||||||
|
{
|
||||||
|
// 参数1:客户端的游戏模组列表,没有服务器的需要拒绝
|
||||||
|
if (obj.Parameters.Length > 0)
|
||||||
|
{
|
||||||
|
string[] modes = obj.GetParam<string[]>(0) ?? [];
|
||||||
|
List<string> ClientDontHave = [];
|
||||||
|
string strDontHave = string.Join("\r\n", Config.GameModuleSupported.Where(mode => !modes.Contains(mode)));
|
||||||
|
if (strDontHave != "")
|
||||||
|
{
|
||||||
|
strDontHave = "客户端缺少服务器所需的模组:" + strDontHave;
|
||||||
|
ServerHelper.WriteLine(strDontHave, InvokeMessageType.Core);
|
||||||
|
builder.AppendLine(strDontHave);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 参数2:客户端是否开启了开发者模式,开启开发者模式部分功能不可用
|
||||||
|
if (obj.Parameters.Length > 1)
|
||||||
|
{
|
||||||
|
isDebugMode = obj.GetParam<bool>(1);
|
||||||
|
if (isDebugMode) ServerHelper.WriteLine("客户端已开启开发者模式");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ValidateClientParameters is null)
|
||||||
|
{
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (Func<SocketObject, string> handler in ValidateClientParameters.GetInvocationList().Cast<Func<SocketObject, string>>())
|
||||||
|
{
|
||||||
|
string msg = handler(obj);
|
||||||
|
if (msg != "")
|
||||||
|
{
|
||||||
|
builder.AppendLine(msg);
|
||||||
|
ServerHelper.WriteLine(msg, InvokeMessageType.Error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return builder.ToString().Trim();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -212,7 +212,7 @@ void StartServer()
|
|||||||
IEnumerable<SocketObject> objs = [];
|
IEnumerable<SocketObject> objs = [];
|
||||||
while (!objs.Any(o => o.SocketType == SocketMessageType.Connect))
|
while (!objs.Any(o => o.SocketType == SocketMessageType.Connect))
|
||||||
{
|
{
|
||||||
objs = objs.Union(await socket.ReceiveAsync());
|
objs = await socket.ReceiveAsync();
|
||||||
}
|
}
|
||||||
(isConnected, isDebugMode) = await ConnectController.Connect(listener, socket, token, clientip, objs.Where(o => o.SocketType == SocketMessageType.Connect));
|
(isConnected, isDebugMode) = await ConnectController.Connect(listener, socket, token, clientip, objs.Where(o => o.SocketType == SocketMessageType.Connect));
|
||||||
if (isConnected)
|
if (isConnected)
|
||||||
|
@ -103,6 +103,11 @@ namespace Milimoe.FunGame.Server.Others
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static List<string> ServerBannedList { get; set; } = [];
|
public static List<string> ServerBannedList { get; set; } = [];
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 是否使用 FunGame.Desktop 的参数检查
|
||||||
|
/// </summary>
|
||||||
|
public static bool UseDesktopParameters { get; set; } = true;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 最多接受连接的玩家数量
|
/// 最多接受连接的玩家数量
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -155,6 +155,7 @@ namespace Milimoe.FunGame.Server.Utility
|
|||||||
settings.Add("Key", INIHelper.ReadINI("Server", "Key"));
|
settings.Add("Key", INIHelper.ReadINI("Server", "Key"));
|
||||||
settings.Add("Status", Convert.ToInt32(INIHelper.ReadINI("Server", "Status")));
|
settings.Add("Status", Convert.ToInt32(INIHelper.ReadINI("Server", "Status")));
|
||||||
settings.Add("BannedList", INIHelper.ReadINI("Server", "BannedList"));
|
settings.Add("BannedList", INIHelper.ReadINI("Server", "BannedList"));
|
||||||
|
settings.Add("UseDesktopParameters", Convert.ToBoolean(INIHelper.ReadINI("Server", "UseDesktopParameters")));
|
||||||
settings.Add("OfficialMail", INIHelper.ReadINI("ServerMail", "OfficialMail"));
|
settings.Add("OfficialMail", INIHelper.ReadINI("ServerMail", "OfficialMail"));
|
||||||
settings.Add("SupportMail", INIHelper.ReadINI("ServerMail", "SupportMail"));
|
settings.Add("SupportMail", INIHelper.ReadINI("ServerMail", "SupportMail"));
|
||||||
settings.Add("Port", Convert.ToInt32(INIHelper.ReadINI("Socket", "Port")));
|
settings.Add("Port", Convert.ToInt32(INIHelper.ReadINI("Socket", "Port")));
|
||||||
@ -186,13 +187,15 @@ namespace Milimoe.FunGame.Server.Utility
|
|||||||
string? Notice = (string?)settings["Notice"];
|
string? Notice = (string?)settings["Notice"];
|
||||||
string? Key = (string?)settings["Key"];
|
string? Key = (string?)settings["Key"];
|
||||||
string? BannedList = (string?)settings["BannedList"];
|
string? BannedList = (string?)settings["BannedList"];
|
||||||
|
bool? UseDesktopParameters = (bool?)settings["UseDesktopParameters"];
|
||||||
|
|
||||||
if (Name != null) Config.ServerName = Name;
|
if (Name != null) Config.ServerName = Name;
|
||||||
if (Password != null) Config.ServerPassword = Password;
|
if (Password != null) Config.ServerPassword = Password;
|
||||||
if (Description != null) Config.ServerDescription = Description;
|
if (Description != null) Config.ServerDescription = Description;
|
||||||
if (Notice != null) Config.ServerNotice = Notice;
|
if (Notice != null) Config.ServerNotice = Notice;
|
||||||
if (Key != null) Config.ServerKey = Key;
|
if (Key != null) Config.ServerKey = Key;
|
||||||
if (BannedList != null) Config.ServerBannedList = BannedList.Split(',').Select(s => s.Trim()).ToList();
|
if (BannedList != null) Config.ServerBannedList = [.. BannedList.Split(',').Select(s => s.Trim())];
|
||||||
|
if (UseDesktopParameters != null) Config.UseDesktopParameters = (bool)UseDesktopParameters;
|
||||||
|
|
||||||
string? OfficialMail = (string?)settings["OfficialMail"];
|
string? OfficialMail = (string?)settings["OfficialMail"];
|
||||||
string? SupportMail = (string?)settings["SupportMail"];
|
string? SupportMail = (string?)settings["SupportMail"];
|
||||||
|
@ -259,7 +259,7 @@ async Task WebSocketConnectionHandler(HttpContext context)
|
|||||||
IEnumerable<SocketObject> objs = [];
|
IEnumerable<SocketObject> objs = [];
|
||||||
while (!objs.Any(o => o.SocketType == SocketMessageType.Connect))
|
while (!objs.Any(o => o.SocketType == SocketMessageType.Connect))
|
||||||
{
|
{
|
||||||
objs = objs.Union(await socket.ReceiveAsync());
|
objs = await socket.ReceiveAsync();
|
||||||
}
|
}
|
||||||
(isConnected, isDebugMode) = await ConnectController.Connect(listener, socket, token, clientip, objs.Where(o => o.SocketType == SocketMessageType.Connect));
|
(isConnected, isDebugMode) = await ConnectController.Connect(listener, socket, token, clientip, objs.Where(o => o.SocketType == SocketMessageType.Connect));
|
||||||
if (isConnected)
|
if (isConnected)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user