diff --git a/ArchiSteamFarm.sln.DotSettings b/ArchiSteamFarm.sln.DotSettings index 991387ba3..eec170b77 100644 --- a/ArchiSteamFarm.sln.DotSettings +++ b/ArchiSteamFarm.sln.DotSettings @@ -422,6 +422,7 @@ WCF WS WTF + WWW XML <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb"><ExtraRule Prefix="I" Suffix="" Style="AaBb" /></Policy> <Policy Inspect="True" Prefix="" Suffix="" Style="AaBb"><ExtraRule Prefix="_" Suffix="" Style="AaBb" /><ExtraRule Prefix="_" Suffix="" Style="aaBb" /></Policy> diff --git a/ArchiSteamFarm/IPC.cs b/ArchiSteamFarm/IPC.cs index 3653716b9..bae620632 100644 --- a/ArchiSteamFarm/IPC.cs +++ b/ArchiSteamFarm/IPC.cs @@ -183,6 +183,8 @@ namespace ArchiSteamFarm { return await HandleApiStructure(context.Request, context.Response, arguments, ++argumentsIndex).ConfigureAwait(false); case "Type/": return await HandleApiType(context.Request, context.Response, arguments, ++argumentsIndex).ConfigureAwait(false); + case "WWW/": + return await HandleApiWWW(context.Request, context.Response, arguments, ++argumentsIndex).ConfigureAwait(false); default: return false; } @@ -725,6 +727,72 @@ namespace ArchiSteamFarm { return true; } + private static async Task HandleApiWWW(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; + } + + if (arguments.Length <= argumentsIndex) { + return false; + } + + switch (arguments[argumentsIndex]) { + case "Directory/": + return await HandleApiWWWDirectory(request, response, arguments, ++argumentsIndex).ConfigureAwait(false); + default: + return false; + } + } + + private static async Task HandleApiWWWDirectory(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; + } + + switch (request.HttpMethod) { + case HttpMethods.Get: + return await HandleApiWWWDirectoryGet(request, response, arguments, argumentsIndex).ConfigureAwait(false); + default: + await ResponseStatusCode(request, response, HttpStatusCode.MethodNotAllowed).ConfigureAwait(false); + return true; + } + } + + private static async Task HandleApiWWWDirectoryGet(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; + } + + if (arguments.Length <= argumentsIndex) { + return false; + } + + string argument = WebUtility.UrlDecode(string.Join("", arguments.Skip(argumentsIndex))); + + string directory = Path.Combine(SharedInfo.WebsiteDirectory, argument); + if (!Directory.Exists(directory)) { + await ResponseJsonObject(request, response, new GenericResponse(false, string.Format(Strings.ErrorIsInvalid, nameof(directory))), HttpStatusCode.BadRequest).ConfigureAwait(false); + return true; + } + + string[] files; + + try { + files = Directory.GetFiles(directory); + } catch (Exception e) { + await ResponseJsonObject(request, response, new GenericResponse(false, string.Format(Strings.ErrorParsingObject, nameof(files)) + Environment.NewLine + e), HttpStatusCode.BadRequest).ConfigureAwait(false); + return true; + } + + HashSet result = new HashSet(files.Select(Path.GetFileName)); + + await ResponseJsonObject(request, response, new GenericResponse(true, "OK", result)).ConfigureAwait(false); + return true; + } + private static async Task HandleFile(HttpListenerRequest request, HttpListenerResponse response, string absolutePath) { if ((request == null) || (response == null) || string.IsNullOrEmpty(absolutePath)) { ASF.ArchiLogger.LogNullError(nameof(request) + " || " + nameof(response) + " || " + nameof(absolutePath));