From 1a5b5a90fe20c8b56600c221b7ac69837ac1dd0a Mon Sep 17 00:00:00 2001 From: JustArchi Date: Wed, 6 Dec 2017 09:47:25 +0100 Subject: [PATCH] API updates --- ArchiSteamFarm/IPC.cs | 79 +++++-------------------------------------- 1 file changed, 9 insertions(+), 70 deletions(-) diff --git a/ArchiSteamFarm/IPC.cs b/ArchiSteamFarm/IPC.cs index 4a5f5d249..eac505ccb 100644 --- a/ArchiSteamFarm/IPC.cs +++ b/ArchiSteamFarm/IPC.cs @@ -20,7 +20,6 @@ // limitations under the License. using System; -using System.Diagnostics.CodeAnalysis; using System.IO; using System.IO.Compression; using System.Linq; @@ -238,18 +237,23 @@ namespace ArchiSteamFarm { return false; } + if (Program.GlobalConfig.SteamOwnerID == 0) { + await ResponseJsonObject(request, response, new GenericResponse(false, string.Format(Strings.ErrorIsInvalid, nameof(Program.GlobalConfig.SteamOwnerID))), HttpStatusCode.ServiceUnavailable).ConfigureAwait(false); + return true; + } + switch (request.HttpMethod) { case HttpMethods.Get: - return await HandleApiCommandGet(request, response, arguments, argumentsIndex).ConfigureAwait(false); + return await HandleApiCommandGeneric(request, response, arguments, argumentsIndex).ConfigureAwait(false); case HttpMethods.Post: - return await HandleApiCommandPost(request, response, arguments, argumentsIndex).ConfigureAwait(false); + return await HandleApiCommandGeneric(request, response, arguments, argumentsIndex).ConfigureAwait(false); default: await ResponseStatusCode(request, response, HttpStatusCode.MethodNotAllowed).ConfigureAwait(false); return true; } } - private static async Task HandleApiCommandGet(HttpListenerRequest request, HttpListenerResponse response, string[] arguments, byte argumentsIndex) { + private static async Task HandleApiCommandGeneric(HttpListenerRequest request, HttpListenerResponse response, string[] arguments, byte argumentsIndex) { if ((request == null) || (response == null) || (arguments == null) || (argumentsIndex == 0)) { ASF.ArchiLogger.LogNullError(nameof(request) + " || " + nameof(response) + " || " + nameof(arguments) + " || " + nameof(argumentsIndex)); return false; @@ -261,60 +265,10 @@ namespace ArchiSteamFarm { string command = WebUtility.UrlDecode(arguments[argumentsIndex]); if (string.IsNullOrEmpty(command)) { - await ResponseText(request, response, string.Format(Strings.ErrorIsEmpty, nameof(command)), HttpStatusCode.BadRequest).ConfigureAwait(false); + await ResponseJsonObject(request, response, new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(command))), HttpStatusCode.BadRequest).ConfigureAwait(false); return true; } - Bot targetBot = Bot.Bots.OrderBy(bot => bot.Key).Select(bot => bot.Value).FirstOrDefault(); - if (targetBot == null) { - await ResponseText(request, response, Strings.ErrorNoBotsDefined, HttpStatusCode.BadRequest).ConfigureAwait(false); - return true; - } - - if (command[0] != '!') { - command = "!" + command; - } - - string content = await targetBot.Response(Program.GlobalConfig.SteamOwnerID, command).ConfigureAwait(false); - - await ResponseText(request, response, content).ConfigureAwait(false); - return true; - } - - private static async Task HandleApiCommandPost(HttpListenerRequest request, HttpListenerResponse response, string[] arguments, byte argumentsIndex) { - if ((request == null) || (response == null) || (arguments == null) || (argumentsIndex == 0)) { - ASF.ArchiLogger.LogNullError(nameof(request) + " || " + nameof(response) + " || " + nameof(arguments) + " || " + nameof(argumentsIndex)); - return false; - } - - const string requiredContentType = "application/json"; - - if (request.ContentType != requiredContentType) { - await ResponseJsonObject(request, response, new GenericResponse(false, nameof(request.ContentType) + " must be declared as " + requiredContentType), HttpStatusCode.NotAcceptable).ConfigureAwait(false); - return true; - } - - string body; - using (StreamReader reader = new StreamReader(request.InputStream)) { - body = await reader.ReadToEndAsync().ConfigureAwait(false); - } - - if (string.IsNullOrEmpty(body)) { - await ResponseJsonObject(request, response, new GenericResponse(false, string.Format(Strings.ErrorIsEmpty, nameof(body))), HttpStatusCode.BadRequest).ConfigureAwait(false); - return true; - } - - ApiCommandRequest apiCommandRequest; - - try { - apiCommandRequest = JsonConvert.DeserializeObject(body); - } catch (Exception e) { - await ResponseJsonObject(request, response, new GenericResponse(false, string.Format(Strings.ErrorParsingObject, nameof(apiCommandRequest)) + Environment.NewLine + e), HttpStatusCode.BadRequest).ConfigureAwait(false); - return true; - } - - string command = apiCommandRequest.Command; - Bot targetBot = Bot.Bots.OrderBy(bot => bot.Key).Select(bot => bot.Value).FirstOrDefault(); if (targetBot == null) { await ResponseJsonObject(request, response, new GenericResponse(false, Strings.ErrorNoBotsDefined), HttpStatusCode.BadRequest).ConfigureAwait(false); @@ -367,11 +321,6 @@ namespace ArchiSteamFarm { } try { - if (Program.GlobalConfig.SteamOwnerID == 0) { - await ResponseStatusCode(context.Request, context.Response, HttpStatusCode.Forbidden).ConfigureAwait(false); - return; - } - if (!string.IsNullOrEmpty(Program.GlobalConfig.IPCPassword)) { string password = context.Request.Headers.Get("Authentication"); if (string.IsNullOrEmpty(password)) { @@ -526,16 +475,6 @@ namespace ArchiSteamFarm { await ResponseString(request, response, text, "text/plain", statusCode).ConfigureAwait(false); } - [SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] - private sealed class ApiCommandRequest { -#pragma warning disable 649 - [JsonProperty(Required = Required.Always)] - internal readonly string Command; -#pragma warning restore 649 - - private ApiCommandRequest() { } - } - private sealed class GenericResponse { [JsonProperty] internal readonly string Message;