diff --git a/FunGame.WebAPI/Architecture/SecurityDocumentTransformer.cs b/FunGame.WebAPI/Architecture/SecurityDocumentTransformer.cs new file mode 100644 index 0000000..5852035 --- /dev/null +++ b/FunGame.WebAPI/Architecture/SecurityDocumentTransformer.cs @@ -0,0 +1,38 @@ +using Microsoft.AspNetCore.OpenApi; +using Microsoft.OpenApi.Models; + +namespace Milimoe.FunGame.WebAPI.Architecture +{ + public class SecurityDocumentTransformer : IOpenApiDocumentTransformer + { + public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerContext context, CancellationToken cancellationToken) + { + document.Components ??= new OpenApiComponents(); + document.Components.SecuritySchemes = new Dictionary + { + { + "Bearer", new OpenApiSecurityScheme + { + Type = SecuritySchemeType.Http, + Scheme = "bearer", + BearerFormat = "JWT", + Description = "BearerToken" + } + } + }; + document.SecurityRequirements = [ + new() + { + { + new OpenApiSecurityScheme + { + Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" } + }, + Array.Empty() + } + } + ]; + await Task.CompletedTask; + } + } +} diff --git a/FunGame.WebAPI/Program.cs b/FunGame.WebAPI/Program.cs index 242786f..fc4bef6 100644 --- a/FunGame.WebAPI/Program.cs +++ b/FunGame.WebAPI/Program.cs @@ -30,14 +30,14 @@ try Console.WriteLine(FunGameInfo.GetInfo(Config.FunGameType)); Config.AspNetCore = true; - ServerHelper.WriteLine("正在读取配置文件并初始化服务 . . ."); + ServerHelper.WriteLine("姝e湪璇诲彇閰嶇疆鏂囦欢骞跺垵濮嬪寲鏈嶅姟 . . ."); - // 检查是否存在配置文件 + // 妫鏌ユ槸鍚﹀瓨鍦ㄩ厤缃枃浠 if (!INIHelper.ExistINIFile()) { - ServerHelper.WriteLine("未检测到配置文件,将自动创建配置文件 . . ."); + ServerHelper.WriteLine("鏈娴嬪埌閰嶇疆鏂囦欢锛屽皢鑷姩鍒涘缓閰嶇疆鏂囦欢 . . ."); INIHelper.Init(Config.FunGameType); - ServerHelper.WriteLine("配置文件FunGame.ini创建成功,请修改该配置文件,然后重启服务器。"); + ServerHelper.WriteLine("閰嶇疆鏂囦欢FunGame.ini鍒涘缓鎴愬姛锛岃淇敼璇ラ厤缃枃浠讹紝鐒跺悗閲嶅惎鏈嶅姟鍣ㄣ"); Console.ReadKey(); return; } @@ -46,48 +46,48 @@ try ServerHelper.GetServerSettings(); } - // 初始化命令菜单 + // 鍒濆鍖栧懡浠よ彍鍗 ServerHelper.InitOrderList(); - // 初始化SQLHelper + // 鍒濆鍖朣QLHelper FunGameSystem.InitSQLHelper(); - // 初始化MailSender + // 鍒濆鍖朚ailSender FunGameSystem.InitMailSender(); - // 读取Server插件 + // 璇诲彇Server鎻掍欢 FunGameSystem.GetServerPlugins(); // Add services to the container. WebApplicationBuilder builder = WebApplication.CreateBuilder(args); - // 读取Web API插件 + // 璇诲彇Web API鎻掍欢 object[] otherobjs = [builder]; FunGameSystem.GetWebAPIPlugins(otherobjs); - // 读取游戏模组 + // 璇诲彇娓告垙妯$粍 if (!FunGameSystem.GetGameModuleList()) { - ServerHelper.WriteLine("服务器似乎未安装任何游戏模组,请检查是否正确安装它们。"); + ServerHelper.WriteLine("鏈嶅姟鍣ㄤ技涔庢湭瀹夎浠讳綍娓告垙妯$粍锛岃妫鏌ユ槸鍚︽纭畨瑁呭畠浠"); } - // 创建单例 + // 鍒涘缓鍗曚緥 RESTfulAPIListener apiListener = new(); RESTfulAPIListener.Instance = apiListener; - ServerHelper.WriteLine("请输入 help 来获取帮助,按下 Ctrl+C 关闭服务器。"); + ServerHelper.WriteLine("璇疯緭鍏 help 鏉ヨ幏鍙栧府鍔╋紝鎸変笅 Ctrl+C 鍏抽棴鏈嶅姟鍣ㄣ"); ServerHelper.PrintFunGameTitle(); if (Config.ServerNotice != "") - Console.WriteLine("\r \n********** 服务器公告 **********\n\n" + Config.ServerNotice + "\n"); + Console.WriteLine("\r \n********** 鏈嶅姟鍣ㄥ叕鍛 **********\n\n" + Config.ServerNotice + "\n"); else - Console.WriteLine("无法读取服务器公告"); + Console.WriteLine("鏃犳硶璇诲彇鏈嶅姟鍣ㄥ叕鍛"); - ServerHelper.WriteLine("正在启动 Web API 监听 . . ."); + ServerHelper.WriteLine("姝e湪鍚姩 Web API 鐩戝惉 . . ."); Console.WriteLine("\r "); - // 读取扩展控制器 + // 璇诲彇鎵╁睍鎺у埗鍣 if (Config.WebAPIPluginLoader != null) { foreach (WebAPIPlugin plugin in Config.WebAPIPluginLoader.Plugins.Values) @@ -96,13 +96,13 @@ try if (pluginAssembly != null) { - // 注册所有控制器 + // 娉ㄥ唽鎵鏈夋帶鍒跺櫒 builder.Services.AddControllers().PartManager.ApplicationParts.Add(new AssemblyPart(pluginAssembly)); } } } - // 添加 JSON 转换器 + // 娣诲姞 JSON 杞崲鍣 builder.Services.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.WriteIndented = true; @@ -115,8 +115,11 @@ try }); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); - builder.Services.AddOpenApi(); - // 添加 CORS 服务 + builder.Services.AddOpenApi(options => + { + options.AddDocumentTransformer(new SecurityDocumentTransformer()); + }); + // 娣诲姞 CORS 鏈嶅姟 builder.Services.AddCors(options => { options.AddPolicy("AllowSpecificOrigin", policy => @@ -124,7 +127,7 @@ try policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod(); }); }); - // 添加 JWT 认证 + // 娣诲姞 JWT 璁よ瘉 builder.Services.AddScoped(); builder.Services.AddAuthentication(options => { @@ -152,7 +155,7 @@ try WebApplication app = builder.Build(); - // 启用 CORS + // 鍚敤 CORS app.UseCors("AllowSpecificOrigin"); // Configure the HTTP request pipeline. @@ -191,21 +194,21 @@ try }); }); - // 捕捉关闭程序事件 + // 鎹曟崏鍏抽棴绋嬪簭浜嬩欢 IHostApplicationLifetime lifetime = app.Services.GetRequiredService(); lifetime.ApplicationStopping.Register(CloseServer); - // 启用 WebSockets 中间件 + // 鍚敤 WebSockets 涓棿浠 WebSocketOptions webSocketOptions = new() { - KeepAliveInterval = TimeSpan.FromMinutes(2) // 设置 WebSocket 的保活间隔 + KeepAliveInterval = TimeSpan.FromMinutes(2) // 璁剧疆 WebSocket 鐨勪繚娲婚棿闅 }; app.UseWebSockets(webSocketOptions); - // 路由到 WebSocket 处理器 + // 璺敱鍒 WebSocket 澶勭悊鍣 app.Map("/ws", WebSocketConnectionHandler); - // 开始监听连接 + // 寮濮嬬洃鍚繛鎺 listener.BannedList.AddRange(Config.ServerBannedList); Task order = Task.Factory.StartNew(GetConsoleOrder); @@ -252,7 +255,7 @@ async Task WebSocketConnectionHandler(HttpContext context) bool isConnected = false; bool isDebugMode = false; - // 开始处理客户端连接请求 + // 寮濮嬪鐞嗗鎴风杩炴帴璇锋眰 IEnumerable objs = []; while (!objs.Any(o => o.SocketType == SocketMessageType.Connect)) { @@ -267,7 +270,7 @@ async Task WebSocketConnectionHandler(HttpContext context) } else { - ServerHelper.WriteLine(ServerHelper.MakeClientName(clientip) + " 连接失败。", InvokeMessageType.Core); + ServerHelper.WriteLine(ServerHelper.MakeClientName(clientip) + " 杩炴帴澶辫触銆", InvokeMessageType.Core); await socket.CloseAsync(); } Config.ConnectingPlayerCount--; @@ -280,7 +283,7 @@ async Task WebSocketConnectionHandler(HttpContext context) catch (Exception e) { if (--Config.ConnectingPlayerCount < 0) Config.ConnectingPlayerCount = 0; - ServerHelper.WriteLine(ServerHelper.MakeClientName(clientip) + " 中断连接!", InvokeMessageType.Core); + ServerHelper.WriteLine(ServerHelper.MakeClientName(clientip) + " 涓柇杩炴帴锛", InvokeMessageType.Core); ServerHelper.Error(e); } }