From 31a4e693e03bfd5b5f898991e6105195908ac27e Mon Sep 17 00:00:00 2001 From: Glax Date: Wed, 31 Jan 2024 18:30:44 +0100 Subject: [PATCH] Custom Request Limits #109 --- Tranga/MangaConnectors/DownloadClient.cs | 8 +++++++ Tranga/MangaConnectors/HttpDownloadClient.cs | 14 +++++------- Tranga/Server.cs | 23 ++++++++++++++++++++ Tranga/TrangaSettings.cs | 1 + 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/Tranga/MangaConnectors/DownloadClient.cs b/Tranga/MangaConnectors/DownloadClient.cs index 2e19f98..d60ee5f 100644 --- a/Tranga/MangaConnectors/DownloadClient.cs +++ b/Tranga/MangaConnectors/DownloadClient.cs @@ -15,6 +15,14 @@ internal abstract class DownloadClient : GlobalBase foreach (KeyValuePair limit in rateLimitRequestsPerMinute) _rateLimit.Add(limit.Key, TimeSpan.FromMinutes(1).Divide(limit.Value)); } + + internal void SetCustomRequestLimit(byte requestType, int limit) + { + if (_rateLimit.ContainsKey(requestType)) + _rateLimit[requestType] = TimeSpan.FromMinutes(1).Divide(limit); + else + _rateLimit.Add(requestType, TimeSpan.FromMinutes(1).Divide(limit)); + } public RequestResult MakeRequest(string url, byte requestType, string? referrer = null, string? clickButton = null) { diff --git a/Tranga/MangaConnectors/HttpDownloadClient.cs b/Tranga/MangaConnectors/HttpDownloadClient.cs index 54c9a22..d5843f3 100644 --- a/Tranga/MangaConnectors/HttpDownloadClient.cs +++ b/Tranga/MangaConnectors/HttpDownloadClient.cs @@ -8,20 +8,16 @@ internal class HttpDownloadClient : DownloadClient { private static readonly HttpClient Client = new() { - Timeout = TimeSpan.FromSeconds(60), - DefaultRequestHeaders = - { - UserAgent = - { - new ProductInfoHeaderValue("Tranga", "0.1") - } - } + Timeout = TimeSpan.FromSeconds(10) }; public HttpDownloadClient(GlobalBase clone, Dictionary rateLimitRequestsPerMinute) : base(clone, rateLimitRequestsPerMinute) { - + if (settings.customUserAgent is null || settings.customUserAgent.Length < 1) + Client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Tranga", "1.0")); + else + Client.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", settings.customUserAgent); } protected override RequestResult MakeRequestInternal(string url, string? referrer = null, string? clickButton = null) diff --git a/Tranga/Server.cs b/Tranga/Server.cs index 7f3e3ce..bacb1da 100644 --- a/Tranga/Server.cs +++ b/Tranga/Server.cs @@ -384,6 +384,29 @@ public class Server : GlobalBase settings.UpdateWorkingDirectory(workingDirectory); SendResponse(HttpStatusCode.Accepted, response); break;*/ + case "Settings/customUserAgent": + if(!requestVariables.TryGetValue("userAgent", out string? customUserAgent)) + { + SendResponse(HttpStatusCode.BadRequest, response); + break; + } + settings.customUserAgent = customUserAgent; + SendResponse(HttpStatusCode.Accepted, response); + break; + case "Settings/customRequestLimit": + if (!requestVariables.TryGetValue("requestType", out string? requestTypeStr) || + !requestVariables.TryGetValue("requestsPerMinute", out string? requestsPerMinuteStr) || + !requestVariables.TryGetValue("connector", out connectorName) || + !byte.TryParse(requestTypeStr, out byte requestType) || + !int.TryParse(requestsPerMinuteStr, out int requestsPerMinute) || + !_parent.TryGetConnector(connectorName, out connector)) + { + SendResponse(HttpStatusCode.BadRequest, response); + break; + } + connector!.downloadClient.SetCustomRequestLimit(requestType, requestsPerMinute); + SendResponse(HttpStatusCode.Accepted, response); + break; case "NotificationConnectors/Update": if (!requestVariables.TryGetValue("notificationConnector", out string? notificationConnectorStr) || !Enum.TryParse(notificationConnectorStr, out NotificationConnector.NotificationConnectorType notificationConnectorType)) diff --git a/Tranga/TrangaSettings.cs b/Tranga/TrangaSettings.cs index 335f0f6..ad03f9b 100644 --- a/Tranga/TrangaSettings.cs +++ b/Tranga/TrangaSettings.cs @@ -12,6 +12,7 @@ public class TrangaSettings public string workingDirectory { get; private set; } public int apiPortNumber { get; init; } public string styleSheet { get; private set; } + public string? customUserAgent { get; set; } = null; [JsonIgnore] public string settingsFilePath => Path.Join(workingDirectory, "settings.json"); [JsonIgnore] public string libraryConnectorsFilePath => Path.Join(workingDirectory, "libraryConnectors.json"); [JsonIgnore] public string notificationConnectorsFilePath => Path.Join(workingDirectory, "notificationConnectors.json");