Browse Source

add log fr AcceptWebSocketAsync
skip api key check if key not set

Robert 1 year ago
parent
commit
3bb912dde9

+ 22 - 11
EVCB_OCPP.WSServer/Service/MapApiServce.cs

@@ -1,6 +1,7 @@
 using EVCB_OCPP.WSServer.Service.WsService;
 using Microsoft.AspNetCore.Builder;
 using Microsoft.AspNetCore.Http;
+using Microsoft.AspNetCore.Mvc;
 using NLog;
 using System;
 using System.Collections.Generic;
@@ -30,17 +31,20 @@ public static class MapApiServceExtention
         webApplication.MapGet("/", helpFunc);
         webApplication.MapGet("/help", helpFunc);
 
-        webApplication.MapPost("/stop", (ProtalServer server) => {
+        webApplication.MapPost("/stop", (ProtalServer server) =>
+        {
             server.Stop();
             return "Command stop";
         }).AddAuthFilter(pass);
 
-        webApplication.MapPost("/gc", () => {
+        webApplication.MapPost("/gc", () =>
+        {
             GC.Collect();
             return "Command GC";
         }).AddAuthFilter(pass);
 
-        webApplication.MapPost("/lc", (ProtalServer server) => {
+        webApplication.MapPost("/lc", (ProtalServer server) =>
+        {
             List<string> toReturn = new List<string>() { "Command List Clients" };
             Dictionary<string, WsClientData> clientDic = server.GetClientDic();
             var list = clientDic.Select(c => c.Value).ToList();
@@ -53,7 +57,8 @@ public static class MapApiServceExtention
             return string.Join("\r\n", toReturn);
         }).AddAuthFilter(pass);
 
-        webApplication.MapPost("/lcn", (ProtalServer server) => {
+        webApplication.MapPost("/lcn", (ProtalServer server) =>
+        {
             List<string> toReturn = new List<string> { "Command List Customer Name" };
             Dictionary<string, WsClientData> clientDic = server.GetClientDic();
             var lcn = clientDic.Select(c => c.Value.CustomerName).Distinct().ToList();
@@ -66,7 +71,8 @@ public static class MapApiServceExtention
             return string.Join("\r\n", toReturn);
         }).AddAuthFilter(pass);
 
-        webApplication.MapPost("/silent", () => {
+        webApplication.MapPost("/silent", () =>
+        {
             foreach (var rule in LogManager.Configuration.LoggingRules)
             {
                 if (rule.RuleName != "ConsoleLog")
@@ -84,7 +90,8 @@ public static class MapApiServceExtention
             return "Command silent";
         }).AddAuthFilter(pass);
 
-        webApplication.MapPost("/show", () => {
+        webApplication.MapPost("/show", () =>
+        {
             foreach (var rule in LogManager.Configuration.LoggingRules)
             {
                 if (rule.RuleName != "ConsoleLog")
@@ -102,13 +109,15 @@ public static class MapApiServceExtention
             return "Command show";
         }).AddAuthFilter(pass);
 
-        webApplication.MapGet("/threads", () => {
+        webApplication.MapGet("/threads", () =>
+        {
             ThreadPool.GetMaxThreads(out var maxWorkerThread, out var maxCompletionPortThreads);
             ThreadPool.GetAvailableThreads(out var avaliableWorkerThread, out var avaliableCompletionPortThreads);
             return $"WorkerThread:{avaliableWorkerThread}/{maxWorkerThread} CompletionPortThreads{avaliableCompletionPortThreads}/{maxCompletionPortThreads}";
         }).AddAuthFilter(pass);
 
-        webApplication.MapPost("/threads", (int min, int max) => {
+        webApplication.MapPost("/threads", (int min, int max) =>
+        {
             ThreadPool.GetMaxThreads(out var maxWorkerThread, out var maxCompletionPortThreads);
             ThreadPool.GetAvailableThreads(out var avaliableWorkerThread, out var avaliableCompletionPortThreads);
             ThreadPool.SetMinThreads(min, 0);
@@ -119,9 +128,11 @@ public static class MapApiServceExtention
 
     public static void AddAuthFilter(this RouteHandlerBuilder routeHandlerBuilder, string pass)
     {
-        routeHandlerBuilder.AddEndpointFilter(async (context, next) => {
-            var key = context.HttpContext.Request.Headers["key"];
-            if (key != pass)
+        routeHandlerBuilder.AddEndpointFilter(async (context, next) =>
+        {
+            string key = context.HttpContext.Request.Headers["key"];
+            if (!string.IsNullOrEmpty(key) &&
+                key != pass)
             {
                 return Results.BadRequest();
             }

+ 1 - 1
EVCB_OCPP.WSServer/Service/WsService/OcppWebsocketService.cs

@@ -54,7 +54,7 @@ public class OcppWebsocketService : WebsocketService<WsClientData>
         IServiceProvider serviceProvider,
         IMainDbService mainDbService,
         ILogger<OcppWebsocketService> logger
-        ) : base(serviceProvider)
+        ) : base(serviceProvider, logger)
     {
         this.configuration = configuration;
         this.mainDbService = mainDbService;

+ 16 - 1
EVCB_OCPP.WSServer/Service/WsService/WebsocketService.cs

@@ -1,5 +1,6 @@
 using Microsoft.AspNetCore.Http;
 using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Logging;
 using System.Net;
 using System.Net.Http;
 using System.Net.WebSockets;
@@ -9,12 +10,14 @@ namespace EVCB_OCPP.WSServer.Service.WsService;
 
 public class WebsocketService<T> where T : WsSession
 {
-    public WebsocketService(IServiceProvider serviceProvider)
+    public WebsocketService(IServiceProvider serviceProvider, ILogger logger)
     {
         this.serviceProvider = serviceProvider;
+        this.logger = logger;
     }
 
     private readonly IServiceProvider serviceProvider;
+    private readonly ILogger logger;
 
     public event EventHandler<T> NewSessionConnected;
 
@@ -38,6 +41,8 @@ public class WebsocketService<T> where T : WsSession
         }
 
         using WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync(portocol);
+        LogHandshakeResponse(context);
+
         await AddWebSocket(webSocket, data);
     }
 
@@ -116,4 +121,14 @@ public class WebsocketService<T> where T : WsSession
 
         return toReturn;
     }
+
+    private void LogHandshakeResponse(HttpContext context)
+    {
+        logger.LogInformation("{0} {1} {2}", context.TraceIdentifier, "Date:", context.Response.Headers["Date"]);
+        logger.LogInformation("{0} {1} {2}", context.TraceIdentifier, context.Request.Protocol + " " + context.Response.StatusCode, "Switching Protocols");
+        logger.LogInformation("{0} {1} {2}", context.TraceIdentifier, "Upgrade:", context.Response.Headers.Upgrade);
+        logger.LogInformation("{0} {1} {2}", context.TraceIdentifier, "Connection:", context.Response.Headers.Connection);
+        logger.LogInformation("{0} {1} {2}", context.TraceIdentifier, "SecWebSocketAccept:", context.Response.Headers.SecWebSocketAccept);
+        logger.LogInformation("{0} {1} {2}", context.TraceIdentifier, "SecWebSocketProtocol:", context.Response.Headers.SecWebSocketProtocol);
+    }
 }

+ 1 - 1
version.txt

@@ -1 +1 @@
-Docker_v1.1.10
+Docker_v1.1.11