From 44f9d80b6057cb325ca7e67509476b5f91458365 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Mon, 4 Dec 2017 22:27:20 +0100 Subject: [PATCH] Use 401 unauthorized for wrong IPC password --- ArchiSteamFarm/IPC.cs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/ArchiSteamFarm/IPC.cs b/ArchiSteamFarm/IPC.cs index 6ed23e4e7..3502d0835 100644 --- a/ArchiSteamFarm/IPC.cs +++ b/ArchiSteamFarm/IPC.cs @@ -199,9 +199,13 @@ namespace ArchiSteamFarm { } try { - if ((Program.GlobalConfig.SteamOwnerID == 0) || (!string.IsNullOrEmpty(Program.GlobalConfig.IPCPassword) && (context.Request.GetQueryStringValue("password") != Program.GlobalConfig.IPCPassword))) { - ASF.ArchiLogger.LogGenericWarning(Strings.ErrorAccessDenied); - await context.StringResponse(Strings.ErrorAccessDenied, statusCode: HttpStatusCode.Forbidden).ConfigureAwait(false); + if (Program.GlobalConfig.SteamOwnerID == 0) { + await context.StatusCodeResponse(HttpStatusCode.Forbidden).ConfigureAwait(false); + return; + } + + if (!string.IsNullOrEmpty(Program.GlobalConfig.IPCPassword) && (context.Request.GetQueryStringValue("password") != Program.GlobalConfig.IPCPassword)) { + await context.StatusCodeResponse(HttpStatusCode.Unauthorized).ConfigureAwait(false); return; } @@ -212,7 +216,7 @@ namespace ArchiSteamFarm { } if (context.Response.ContentLength64 == 0) { - await context.StringResponse("404 - Not Found", statusCode: HttpStatusCode.NotFound); + await context.StatusCodeResponse(HttpStatusCode.NotFound); } } finally { context.Response.Close(); @@ -256,6 +260,16 @@ namespace ArchiSteamFarm { } } + private static async Task StatusCodeResponse(this HttpListenerContext context, HttpStatusCode statusCode) { + if (context == null) { + ASF.ArchiLogger.LogNullError(nameof(context)); + return; + } + + string content = (ushort) statusCode + " - " + statusCode; + await context.StringResponse(content, statusCode: statusCode).ConfigureAwait(false); + } + private static async Task StringResponse(this HttpListenerContext context, string content, string textType = "text/plain", HttpStatusCode statusCode = HttpStatusCode.OK) { if ((context == null) || string.IsNullOrEmpty(content) || string.IsNullOrEmpty(textType)) { ASF.ArchiLogger.LogNullError(nameof(context) + " || " + nameof(content) + " || " + nameof(textType));