From 92452873ef86630a8f541fbaed658e51739ff5ce Mon Sep 17 00:00:00 2001 From: milimoe <110188673+milimoe@users.noreply.github.com> Date: Sun, 16 Mar 2025 01:57:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E8=A1=A8=E7=BB=93?= =?UTF-8?q?=E6=9E=84=E5=92=8C=E5=B7=A5=E5=85=B7=E7=B1=BB=E8=B0=83=E6=95=B4?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=20ApiTokens=20=E8=A1=A8=20(#114)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 数据库表结构和工具类调整,添加 ApiTokens 表 * 修复 Workers 字典没有元素的问题 --- Api/Transmittal/SQLHelper.cs | 30 +++++ Api/Utility/General.cs | 31 +++++ .../Common/Addon/Example/ExampleGameModule.cs | 1 + Library/SQLScript/Common/Common.cs | 36 ++++++ Library/SQLScript/fungame.sql | 108 +++++++++-------- Library/SQLScript/fungame_sqlite.sql | 112 ++++++++++-------- 6 files changed, 220 insertions(+), 98 deletions(-) diff --git a/Api/Transmittal/SQLHelper.cs b/Api/Transmittal/SQLHelper.cs index 59d0b83..f7d431b 100644 --- a/Api/Transmittal/SQLHelper.cs +++ b/Api/Transmittal/SQLHelper.cs @@ -72,6 +72,36 @@ namespace Milimoe.FunGame.Core.Api.Transmittal return null; } + /// + /// 查询数据库是否存在 + /// + /// + public abstract bool DatabaseExists(); + + /// + /// 执行一个 sql 脚本文件 + /// + /// + public virtual void ExecuteSqlFile(string path) + { + if (!File.Exists(path)) + { + throw new FileNotFoundException("SQL 脚本文件不存在", path); + } + + string content = File.ReadAllText(path); + string[] commands = content.Split([";"], StringSplitOptions.RemoveEmptyEntries); + + foreach (string command in commands) + { + string sql = command.Trim(); + if (!string.IsNullOrEmpty(sql)) + { + Execute(sql); + } + } + } + /// /// 关闭连接 /// diff --git a/Api/Utility/General.cs b/Api/Utility/General.cs index 31d7897..805b2ae 100644 --- a/Api/Utility/General.cs +++ b/Api/Utility/General.cs @@ -3,6 +3,7 @@ using System.Globalization; using System.Net; using System.Net.NetworkInformation; using System.Security.Cryptography; +using System.Text; using System.Text.Json; using System.Text.RegularExpressions; using Milimoe.FunGame.Core.Library.Common.Architecture; @@ -489,6 +490,36 @@ namespace Milimoe.FunGame.Core.Api.Utility byte[] decrypted = rsa.Decrypt(secret, true); return General.DefaultEncoding.GetString(decrypted); } + + /// + /// 使用 MD5 算法加密 + /// + /// + /// + public static string MD5(string text) + { + byte[] inputBytes = General.DefaultEncoding.GetBytes(text); + byte[] hash = System.Security.Cryptography.MD5.HashData(inputBytes); + return Convert.ToHexStringLower(hash); + } + + /// + /// 生成随机字符串 + /// + /// + /// + public static string GenerateRandomString(int length = 18) + { + const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+=-`~[]\\{}|;':\",./<>?"; + byte[] data = RandomNumberGenerator.GetBytes(length); + + StringBuilder result = new(length); + foreach (byte b in data) + { + result.Append(chars[b % chars.Length]); + } + return result.ToString(); + } } #endregion diff --git a/Library/Common/Addon/Example/ExampleGameModule.cs b/Library/Common/Addon/Example/ExampleGameModule.cs index 289279c..992634c 100644 --- a/Library/Common/Addon/Example/ExampleGameModule.cs +++ b/Library/Common/Addon/Example/ExampleGameModule.cs @@ -146,6 +146,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Addon.Example { // 因为模组是单例的,需要为这个房间创建一个工作类接收参数,不能直接用本地变量处理 ModuleServerWorker worker = new(obj); + Workers[obj.Room.Roomid] = worker; // 创建一个线程执行Test(),因为这个方法必须立即返回 TaskUtility.NewTask(async () => await Test(obj, worker)).OnError(Controller.Error); return true; diff --git a/Library/SQLScript/Common/Common.cs b/Library/SQLScript/Common/Common.cs index d3c7dc7..e8dcd88 100644 --- a/Library/SQLScript/Common/Common.cs +++ b/Library/SQLScript/Common/Common.cs @@ -53,3 +53,39 @@ namespace Milimoe.FunGame.Core.Library.SQLScript.Common } } } + +namespace Milimoe.FunGame.Core.Library.SQLScript.Common +{ + public class ApiTokens : Constant + { + public const string TableName = "ApiTokens"; + public const string Column_TokenID = "TokenID"; + public const string Column_SecretKey = "SecretKey"; + public const string Column_Reference1 = "Reference1"; + public const string Column_Reference2 = "Reference2"; + + public static string Insert_APITokens(SQLHelper SQLHelper, string TokenID, string SecretKey = "", string Reference1 = "", string Reference2 = "") + { + SQLHelper.Parameters["@TokenID"] = TokenID; + SQLHelper.Parameters["@SecretKey"] = SecretKey; + SQLHelper.Parameters["@Reference1"] = Reference1; + SQLHelper.Parameters["@Reference2"] = Reference2; + return $"{Command_Insert} {Command_Into} {TableName} ({Column_TokenID}, {Column_SecretKey}, {Column_Reference1}, {Column_Reference2}) {Command_Values} (@TokenID, @SecretKey, @Reference1, @Reference2)"; + } + + public static string Select_GetAPIToken(SQLHelper SQLHelper, string TokenID) + { + SQLHelper.Parameters["@TokenID"] = TokenID; + return $"{Command_Select} {Command_All} {Command_From} {TableName} {Command_Where} {Column_TokenID} = @TokenID"; + } + + public static string Update_GetAPIToken(SQLHelper SQLHelper, string TokenID, string SecretKey, string Reference1 = "", string Reference2 = "") + { + SQLHelper.Parameters["@TokenID"] = TokenID; + SQLHelper.Parameters["@SecretKey"] = SecretKey; + SQLHelper.Parameters["@Reference1"] = Reference1; + SQLHelper.Parameters["@Reference2"] = Reference2; + return $"{Command_Update} {TableName} {Command_Set} {Column_TokenID} = @TokenID, {Column_SecretKey} = @SecretKey, {Column_Reference1} = @Reference1, {Column_Reference2} = @Reference2 {Command_Where} {Column_TokenID} = @TokenID"; + } + } +} diff --git a/Library/SQLScript/fungame.sql b/Library/SQLScript/fungame.sql index e95c156..90251ae 100644 --- a/Library/SQLScript/fungame.sql +++ b/Library/SQLScript/fungame.sql @@ -1,76 +1,88 @@ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- --- Table structure for forgetverifycodes +-- Table structure for ForgetVerifyCodes -- ---------------------------- -DROP TABLE IF EXISTS `forgetverifycodes`; -CREATE TABLE `forgetverifycodes` ( - `Username` varchar(255) DEFAULT NULL, - `Email` varchar(255) DEFAULT NULL, - `ForgetVerifyCode` varchar(255) DEFAULT NULL, - `SendTime` datetime DEFAULT NULL +DROP TABLE IF EXISTS `ForgetVerifyCodes`; +CREATE TABLE `ForgetVerifyCodes` ( + `Username` varchar(255) NOT NULL DEFAULT '', + `Email` varchar(255) NOT NULL DEFAULT '', + `ForgetVerifyCode` varchar(255) NOT NULL DEFAULT '', + `SendTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- ---------------------------- --- Table structure for regverifycodes +-- Table structure for RegVerifyCodes -- ---------------------------- -DROP TABLE IF EXISTS `regverifycodes`; -CREATE TABLE `regverifycodes` ( - `Username` varchar(255) DEFAULT NULL, - `Email` varchar(255) DEFAULT NULL, - `RegVerifyCode` varchar(255) DEFAULT NULL, - `RegTime` datetime DEFAULT NULL +DROP TABLE IF EXISTS `RegVerifyCodes`; +CREATE TABLE `RegVerifyCodes` ( + `Username` varchar(255) NOT NULL DEFAULT '', + `Email` varchar(255) NOT NULL DEFAULT '', + `RegVerifyCode` varchar(255) NOT NULL DEFAULT '', + `RegTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- ---------------------------- --- Table structure for rooms +-- Table structure for Rooms -- ---------------------------- -DROP TABLE IF EXISTS `rooms`; -CREATE TABLE `rooms` ( +DROP TABLE IF EXISTS `Rooms`; +CREATE TABLE `Rooms` ( `Id` bigint(20) NOT NULL AUTO_INCREMENT, `Roomid` varchar(255) NOT NULL DEFAULT '-1', - `CreateTime` datetime NOT NULL, + `CreateTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `RoomMaster` bigint(20) NOT NULL DEFAULT '0', - `RoomType` int(8) DEFAULT '0', - `GameModule` varchar(255) DEFAULT '', - `GameMap` varchar(255) DEFAULT '', - `RoomState` int(8) DEFAULT '0', - `IsRank` int(1) DEFAULT '0', - `HasPass` int(1) DEFAULT '0', - `Password` varchar(255) DEFAULT '', - `MaxUsers` int(8) DEFAULT '0', + `RoomType` int(8) NOT NULL DEFAULT '0', + `GameModule` varchar(255) NOT NULL DEFAULT '', + `GameMap` varchar(255) NOT NULL DEFAULT '', + `RoomState` int(8) NOT NULL DEFAULT '0', + `IsRank` int(1) NOT NULL DEFAULT '0', + `HasPass` int(1) NOT NULL DEFAULT '0', + `Password` varchar(255) NOT NULL DEFAULT '', + `MaxUsers` int(8) NOT NULL DEFAULT '0', PRIMARY KEY (`Id`,`Roomid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- ---------------------------- --- Table structure for serverloginlogs +-- Table structure for ServerLoginLogs -- ---------------------------- -DROP TABLE IF EXISTS `serverloginlogs`; -CREATE TABLE `serverloginlogs` ( - `ServerName` varchar(255) DEFAULT NULL, - `ServerKey` varchar(255) DEFAULT NULL, - `LoginTime` datetime DEFAULT NULL +DROP TABLE IF EXISTS `ServerLoginLogs`; +CREATE TABLE `ServerLoginLogs` ( + `ServerName` varchar(255) NOT NULL DEFAULT '', + `ServerKey` varchar(255) NOT NULL DEFAULT '', + `LoginTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- ---------------------------- --- Table structure for users +-- Table structure for ApiTokens -- ---------------------------- -DROP TABLE IF EXISTS `users`; -CREATE TABLE `users` ( +DROP TABLE IF EXISTS `ApiTokens`; +CREATE TABLE `ApiTokens` ( + `TokenID` varchar(255) NOT NULL DEFAULT '', + `SecretKey` varchar(255) NOT NULL DEFAULT '', + `Reference1` varchar(255) NOT NULL DEFAULT '', + `Reference2` varchar(255) NOT NULL DEFAULT '', + PRIMARY KEY (`TokenID`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +-- ---------------------------- +-- Table structure for Users +-- ---------------------------- +DROP TABLE IF EXISTS `Users`; +CREATE TABLE `Users` ( `UID` bigint(20) NOT NULL AUTO_INCREMENT, - `Username` varchar(255) NOT NULL, - `Password` varchar(255) NOT NULL, - `RegTime` datetime DEFAULT NULL, - `LastTime` datetime DEFAULT NULL, - `LastIP` varchar(255) DEFAULT '', + `Username` varchar(255) NOT NULL DEFAULT '', + `Password` varchar(255) NOT NULL DEFAULT '', + `RegTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `LastTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `LastIP` varchar(255) NOT NULL DEFAULT '', `Email` varchar(255) NOT NULL DEFAULT '', - `Nickname` varchar(255) DEFAULT '', - `IsAdmin` int(1) DEFAULT '0', - `IsOperator` int(1) DEFAULT '0', - `IsEnable` int(1) DEFAULT '1', - `Credits` decimal(20,0) DEFAULT '0', - `Materials` decimal(20,0) DEFAULT '0', - `GameTime` decimal(20,0) DEFAULT '0', - `AutoKey` varchar(255) DEFAULT '', + `Nickname` varchar(255) NOT NULL DEFAULT '', + `IsAdmin` int(1) NOT NULL DEFAULT '0', + `IsOperator` int(1) NOT NULL DEFAULT '0', + `IsEnable` int(1) NOT NULL DEFAULT '1', + `Credits` decimal(20,0) NOT NULL DEFAULT '0', + `Materials` decimal(20,0) NOT NULL DEFAULT '0', + `GameTime` decimal(20,0) NOT NULL DEFAULT '0', + `AutoKey` varchar(255) NOT NULL DEFAULT '', PRIMARY KEY (`UID`,`Username`,`Email`) ) ENGINE=InnoDB AUTO_INCREMENT=41 DEFAULT CHARSET=utf8mb4; diff --git a/Library/SQLScript/fungame_sqlite.sql b/Library/SQLScript/fungame_sqlite.sql index c6ba71f..a134b83 100644 --- a/Library/SQLScript/fungame_sqlite.sql +++ b/Library/SQLScript/fungame_sqlite.sql @@ -1,74 +1,86 @@ PRAGMA foreign_keys = OFF; -- ---------------------------- --- Table structure for forgetverifycodes +-- Table structure for ForgetVerifyCodes -- ---------------------------- -DROP TABLE IF EXISTS "main"."forgetverifycodes"; -CREATE TABLE forgetverifycodes ( - Username TEXT, - Email TEXT, - ForgetVerifyCode TEXT, - SendTime DATETIME +DROP TABLE IF EXISTS "main"."ForgetVerifyCodes"; +CREATE TABLE ForgetVerifyCodes ( + Username TEXT NOT NULL DEFAULT '', + Email TEXT NOT NULL DEFAULT '', + ForgetVerifyCode TEXT NOT NULL DEFAULT '', + SendTime DATETIME NOT NULL DEFAULT (DATETIME('now')) ); -- ---------------------------- --- Table structure for regverifycodes +-- Table structure for RegVerifyCodes -- ---------------------------- -DROP TABLE IF EXISTS "main"."regverifycodes"; -CREATE TABLE regverifycodes ( - Username TEXT, - Email TEXT, - RegVerifyCode TEXT, - RegTime DATETIME +DROP TABLE IF EXISTS "main"."RegVerifyCodes"; +CREATE TABLE RegVerifyCodes ( + Username TEXT NOT NULL DEFAULT '', + Email TEXT NOT NULL DEFAULT '', + RegVerifyCode TEXT NOT NULL DEFAULT '', + RegTime DATETIME NOT NULL DEFAULT (DATETIME('now')) ); -- ---------------------------- --- Table structure for rooms +-- Table structure for Rooms -- ---------------------------- -DROP TABLE IF EXISTS "main"."rooms"; -CREATE TABLE "rooms" ( +DROP TABLE IF EXISTS "main"."Rooms"; +CREATE TABLE "Rooms" ( "Id" INTEGER PRIMARY KEY AUTOINCREMENT, "Roomid" TEXT NOT NULL DEFAULT '-1', -"CreateTime" DATETIME NOT NULL, +"CreateTime" DATETIME NOT NULL DEFAULT (DATETIME('now')), "RoomMaster" INTEGER NOT NULL DEFAULT 0, -"RoomType" INTEGER DEFAULT 0, -"GameModule" TEXT DEFAULT '', -"GameMap" TEXT DEFAULT '', -"RoomState" INTEGER DEFAULT 0, -"IsRank" INTEGER DEFAULT 0, -"HasPass" INTEGER DEFAULT 0, -"Password" TEXT DEFAULT '', -"MaxUsers" INTEGER DEFAULT 0 +"RoomType" INTEGER NOT NULL DEFAULT 0, +"GameModule" TEXT NOT NULL DEFAULT '', +"GameMap" TEXT NOT NULL DEFAULT '', +"RoomState" INTEGER NOT NULL DEFAULT 0, +"IsRank" INTEGER NOT NULL DEFAULT 0, +"HasPass" INTEGER NOT NULL DEFAULT 0, +"Password" TEXT NOT NULL DEFAULT '', +"MaxUsers" INTEGER NOT NULL DEFAULT 0 ); -- ---------------------------- --- Table structure for serverloginlogs +-- Table structure for ServerLoginLogs -- ---------------------------- -DROP TABLE IF EXISTS "main"."serverloginlogs"; -CREATE TABLE serverloginlogs ( - ServerName TEXT, - ServerKey TEXT, - LoginTime DATETIME +DROP TABLE IF EXISTS "main"."ServerLoginLogs"; +CREATE TABLE ServerLoginLogs ( + ServerName TEXT NOT NULL DEFAULT '', + ServerKey TEXT NOT NULL DEFAULT '', + LoginTime DATETIME NOT NULL DEFAULT (DATETIME('now')) ); -- ---------------------------- --- Table structure for users +-- Table structure for ApiTokens -- ---------------------------- -DROP TABLE IF EXISTS "main"."users"; -CREATE TABLE users ( - UID INTEGER PRIMARY KEY AUTOINCREMENT, - Username TEXT NOT NULL, - Password TEXT NOT NULL, - RegTime DATETIME, - LastTime DATETIME, - LastIP TEXT DEFAULT '', - Email TEXT NOT NULL DEFAULT '', - Nickname TEXT DEFAULT '', - IsAdmin INTEGER DEFAULT 0, - IsOperator INTEGER DEFAULT 0, - IsEnable INTEGER DEFAULT 1, - Credits REAL DEFAULT 0, - Materials REAL DEFAULT 0, - GameTime REAL DEFAULT 0, - AutoKey TEXT DEFAULT '' +DROP TABLE IF EXISTS "main"."ApiTokens"; +CREATE TABLE ApiTokens ( + TokenID TEXT NOT NULL DEFAULT '', + SecretKey TEXT NOT NULL DEFAULT '', + Reference1 TEXT NOT NULL DEFAULT '', + Reference2 TEXT NOT NULL DEFAULT '', + PRIMARY KEY (TokenID) +); + +-- ---------------------------- +-- Table structure for Users +-- ---------------------------- +DROP TABLE IF EXISTS "main"."Users"; +CREATE TABLE Users ( + UID INTEGER PRIMARY KEY AUTOINCREMENT, + Username TEXT NOT NULL, + Password TEXT NOT NULL, + RegTime DATETIME NOT NULL DEFAULT (DATETIME('now')), + LastTime DATETIME NOT NULL DEFAULT (DATETIME('now')), + LastIP TEXT NOT NULL DEFAULT '', + Email TEXT NOT NULL DEFAULT '', + Nickname TEXT NOT NULL DEFAULT '', + IsAdmin INTEGER NOT NULL DEFAULT 0, + IsOperator INTEGER NOT NULL DEFAULT 0, + IsEnable INTEGER NOT NULL DEFAULT 1, + Credits REAL NOT NULL DEFAULT 0, + Materials REAL NOT NULL DEFAULT 0, + GameTime REAL NOT NULL DEFAULT 0, + AutoKey TEXT NOT NULL DEFAULT '' );