From 2dd117dc3b7a2a8e0e0030b14691ac04f4ed744c Mon Sep 17 00:00:00 2001 From: Ivandro Jao Date: Fri, 25 Oct 2024 20:41:21 +0100 Subject: [PATCH] Use proxy if configured in HttpClient settings Signed-off-by: Ivandro Jao --- src/libse/AutoTranslate/AnthropicTranslate.cs | 3 ++- src/libse/AutoTranslate/ChatGptTranslate.cs | 3 ++- src/libse/AutoTranslate/DeepLTranslate.cs | 3 ++- src/libse/AutoTranslate/DeepLXTranslate.cs | 3 ++- src/libse/AutoTranslate/GeminiTranslate.cs | 3 ++- src/libse/AutoTranslate/GoogleTranslateV1.cs | 3 ++- src/libse/AutoTranslate/GoogleTranslateV2.cs | 2 +- src/libse/AutoTranslate/GroqTranslate.cs | 3 ++- src/libse/AutoTranslate/LibreTranslate.cs | 3 ++- src/libse/AutoTranslate/LmStudioTranslate.cs | 3 ++- src/libse/AutoTranslate/MicrosoftTranslator.cs | 6 +++--- src/libse/AutoTranslate/MyMemoryApi.cs | 2 +- .../AutoTranslate/NoLanguageLeftBehindApi.cs | 2 +- .../AutoTranslate/NoLanguageLeftBehindServe.cs | 2 +- src/libse/AutoTranslate/OllamaTranslate.cs | 3 ++- src/libse/AutoTranslate/OpenRouterTranslate.cs | 3 ++- src/libse/AutoTranslate/PapagoTranslate.cs | 3 ++- src/libse/AutoTranslate/SeamlessM4TTranslate.cs | 3 ++- src/libse/Forms/CheckForUpdatesHelper.cs | 2 +- ...ownloaderFactory.cs => HttpClientFactory.cs} | 11 ++++++++--- .../VobSub/Ocr/Service/GoogleCloudVisionApi.cs | 2 +- src/ui/Forms/AudioToText/VoskModelDownload.cs | 2 +- src/ui/Forms/AudioToText/WhisperDownload.cs | 2 +- .../Forms/AudioToText/WhisperModelDownload.cs | 2 +- src/ui/Forms/DownloadFfmpeg.cs | 2 +- src/ui/Forms/DownloadVosk.cs | 2 +- src/ui/Forms/DownloadYouTubeDl.cs | 2 +- src/ui/Forms/GetDictionaries.cs | 2 +- src/ui/Forms/Ocr/DownloadTesseract302.cs | 2 +- src/ui/Forms/Ocr/DownloadTesseract5.cs | 2 +- src/ui/Forms/Ocr/GetTesseract302Dictionaries.cs | 2 +- src/ui/Forms/Ocr/GetTesseractDictionaries.cs | 2 +- src/ui/Forms/Options/SettingsMpv.cs | 2 +- src/ui/Forms/PluginsGet.cs | 4 ++-- src/ui/Forms/Translate/AutoTranslate.cs | 3 ++- src/ui/Forms/Tts/PiperDownload.cs | 2 +- src/ui/Forms/Tts/TextToSpeech.cs | 17 +++++++++-------- src/ui/Logic/CheckForUpdatesHelper.cs | 2 +- .../Plugins/OnlinePluginMetadataProvider.cs | 2 +- 39 files changed, 71 insertions(+), 51 deletions(-) rename src/libse/Http/{DownloaderFactory.cs => HttpClientFactory.cs} (84%) diff --git a/src/libse/AutoTranslate/AnthropicTranslate.cs b/src/libse/AutoTranslate/AnthropicTranslate.cs index 8f164dd813..34b66407b3 100644 --- a/src/libse/AutoTranslate/AnthropicTranslate.cs +++ b/src/libse/AutoTranslate/AnthropicTranslate.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; +using Nikse.SubtitleEdit.Core.Http; using Nikse.SubtitleEdit.Core.Settings; namespace Nikse.SubtitleEdit.Core.AutoTranslate @@ -38,7 +39,7 @@ public class AnthropicTranslate : IAutoTranslator public void Initialize() { _httpClient?.Dispose(); - _httpClient = new HttpClient(); + _httpClient = HttpClientFactory.CreateProxiedHttpClient(); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json"); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("anthropic-version", "2023-06-01"); diff --git a/src/libse/AutoTranslate/ChatGptTranslate.cs b/src/libse/AutoTranslate/ChatGptTranslate.cs index 2cd5cc4de7..7bc5daf1ca 100644 --- a/src/libse/AutoTranslate/ChatGptTranslate.cs +++ b/src/libse/AutoTranslate/ChatGptTranslate.cs @@ -10,6 +10,7 @@ using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; +using Nikse.SubtitleEdit.Core.Http; namespace Nikse.SubtitleEdit.Core.AutoTranslate { @@ -49,7 +50,7 @@ public static string RemovePreamble(string original, string translation) public void Initialize() { _httpClient?.Dispose(); - _httpClient = new HttpClient(); + _httpClient = HttpClientFactory.CreateProxiedHttpClient(); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json"); _httpClient.BaseAddress = new Uri(Configuration.Settings.Tools.ChatGptUrl.TrimEnd('/')); diff --git a/src/libse/AutoTranslate/DeepLTranslate.cs b/src/libse/AutoTranslate/DeepLTranslate.cs index a1a4dc2d4f..89cf793ae3 100644 --- a/src/libse/AutoTranslate/DeepLTranslate.cs +++ b/src/libse/AutoTranslate/DeepLTranslate.cs @@ -5,6 +5,7 @@ using System.Threading; using System.Threading.Tasks; using Nikse.SubtitleEdit.Core.Common; +using Nikse.SubtitleEdit.Core.Http; using Nikse.SubtitleEdit.Core.Translate; namespace Nikse.SubtitleEdit.Core.AutoTranslate @@ -37,7 +38,7 @@ public void Initialize() return; } - _client = new HttpClient(); + _client = HttpClientFactory.CreateProxiedHttpClient(); _client.BaseAddress = new Uri(_apiUrl.Trim().TrimEnd('/')); _client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "DeepL-Auth-Key " + _apiKey.Trim()); _formality = string.IsNullOrWhiteSpace(_formality) ? "default" : _formality.Trim(); diff --git a/src/libse/AutoTranslate/DeepLXTranslate.cs b/src/libse/AutoTranslate/DeepLXTranslate.cs index 5daba0ecb2..808fe07499 100644 --- a/src/libse/AutoTranslate/DeepLXTranslate.cs +++ b/src/libse/AutoTranslate/DeepLXTranslate.cs @@ -5,6 +5,7 @@ using System.Threading; using System.Threading.Tasks; using Nikse.SubtitleEdit.Core.Common; +using Nikse.SubtitleEdit.Core.Http; using Nikse.SubtitleEdit.Core.Translate; namespace Nikse.SubtitleEdit.Core.AutoTranslate @@ -32,7 +33,7 @@ public void Initialize() } _apiUrl = Configuration.Settings.Tools.AutoTranslateDeepLXUrl; - _client = new HttpClient(); + _client = HttpClientFactory.CreateProxiedHttpClient(); _client.BaseAddress = new Uri(_apiUrl.Trim().TrimEnd('/')); } diff --git a/src/libse/AutoTranslate/GeminiTranslate.cs b/src/libse/AutoTranslate/GeminiTranslate.cs index e63868842c..c60b88c801 100644 --- a/src/libse/AutoTranslate/GeminiTranslate.cs +++ b/src/libse/AutoTranslate/GeminiTranslate.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; +using Nikse.SubtitleEdit.Core.Http; namespace Nikse.SubtitleEdit.Core.AutoTranslate { @@ -25,7 +26,7 @@ public class GeminiTranslate : IAutoTranslator public void Initialize() { _httpClient?.Dispose(); - _httpClient = new HttpClient(); + _httpClient = HttpClientFactory.CreateProxiedHttpClient(); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json"); _httpClient.BaseAddress = new Uri("https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent"); diff --git a/src/libse/AutoTranslate/GoogleTranslateV1.cs b/src/libse/AutoTranslate/GoogleTranslateV1.cs index 1c5f160572..f897759a87 100644 --- a/src/libse/AutoTranslate/GoogleTranslateV1.cs +++ b/src/libse/AutoTranslate/GoogleTranslateV1.cs @@ -9,6 +9,7 @@ using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; +using Nikse.SubtitleEdit.Core.Http; namespace Nikse.SubtitleEdit.Core.AutoTranslate { @@ -29,7 +30,7 @@ public class GoogleTranslateV1 : IAutoTranslator public void Initialize() { _httpClient?.Dispose(); - _httpClient = new HttpClient(); //DownloaderFactory.MakeHttpClient(); + _httpClient = HttpClientFactory.CreateProxiedHttpClient(); //HttpClientFactory.CreateHttpClientDownloader(); _httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=UTF-8"); _httpClient.BaseAddress = new Uri("https://translate.googleapis.com/"); diff --git a/src/libse/AutoTranslate/GoogleTranslateV2.cs b/src/libse/AutoTranslate/GoogleTranslateV2.cs index f2d2767441..526fc2bb06 100644 --- a/src/libse/AutoTranslate/GoogleTranslateV2.cs +++ b/src/libse/AutoTranslate/GoogleTranslateV2.cs @@ -31,7 +31,7 @@ public class GoogleTranslateV2 : IAutoTranslator, IDisposable public void Initialize() { _apiKey = Configuration.Settings.Tools.GoogleApiV2Key; - _httpClient = DownloaderFactory.MakeHttpClient(); + _httpClient = HttpClientFactory.CreateHttpClientDownloader(); _httpClient.BaseAddress = new Uri("https://translation.googleapis.com/language/translate/v2/"); _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); } diff --git a/src/libse/AutoTranslate/GroqTranslate.cs b/src/libse/AutoTranslate/GroqTranslate.cs index 488515edce..f120dacd3b 100644 --- a/src/libse/AutoTranslate/GroqTranslate.cs +++ b/src/libse/AutoTranslate/GroqTranslate.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; +using Nikse.SubtitleEdit.Core.Http; using Nikse.SubtitleEdit.Core.Settings; namespace Nikse.SubtitleEdit.Core.AutoTranslate @@ -38,7 +39,7 @@ public class GroqTranslate : IAutoTranslator public void Initialize() { _httpClient?.Dispose(); - _httpClient = new HttpClient(); + _httpClient = HttpClientFactory.CreateProxiedHttpClient(); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json"); _httpClient.BaseAddress = new Uri(Configuration.Settings.Tools.GroqUrl.TrimEnd('/')); diff --git a/src/libse/AutoTranslate/LibreTranslate.cs b/src/libse/AutoTranslate/LibreTranslate.cs index 5669a9c83d..7e33eef4c4 100644 --- a/src/libse/AutoTranslate/LibreTranslate.cs +++ b/src/libse/AutoTranslate/LibreTranslate.cs @@ -9,6 +9,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; +using Nikse.SubtitleEdit.Core.Http; namespace Nikse.SubtitleEdit.Core.AutoTranslate { @@ -26,7 +27,7 @@ public class LibreTranslate : IAutoTranslator public void Initialize() { _httpClient?.Dispose(); - _httpClient = new HttpClient(); + _httpClient = HttpClientFactory.CreateProxiedHttpClient(); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json"); _httpClient.BaseAddress = new Uri(Configuration.Settings.Tools.AutoTranslateLibreUrl); diff --git a/src/libse/AutoTranslate/LmStudioTranslate.cs b/src/libse/AutoTranslate/LmStudioTranslate.cs index 987c078e53..41988e6b1f 100644 --- a/src/libse/AutoTranslate/LmStudioTranslate.cs +++ b/src/libse/AutoTranslate/LmStudioTranslate.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; +using Nikse.SubtitleEdit.Core.Http; using Nikse.SubtitleEdit.Core.Settings; namespace Nikse.SubtitleEdit.Core.AutoTranslate @@ -26,7 +27,7 @@ public class LmStudioTranslate : IAutoTranslator public void Initialize() { _httpClient?.Dispose(); - _httpClient = new HttpClient(); + _httpClient = HttpClientFactory.CreateProxiedHttpClient(); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json"); _httpClient.BaseAddress = new Uri(Configuration.Settings.Tools.LmStudioApiUrl.TrimEnd('/')); diff --git a/src/libse/AutoTranslate/MicrosoftTranslator.cs b/src/libse/AutoTranslate/MicrosoftTranslator.cs index 3e25472c18..f0384ccebe 100644 --- a/src/libse/AutoTranslate/MicrosoftTranslator.cs +++ b/src/libse/AutoTranslate/MicrosoftTranslator.cs @@ -118,7 +118,7 @@ private IDownloader GetTranslateClient() { if (_httpClient == null) { - _httpClient = DownloaderFactory.MakeHttpClient(); + _httpClient = HttpClientFactory.CreateHttpClientDownloader(); _httpClient.BaseAddress = new Uri("https://api.cognitive.microsofttranslator.com/"); _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken); @@ -129,7 +129,7 @@ private IDownloader GetTranslateClient() private static string GetAccessToken(string apiKey, string tokenEndpoint) { - using (var httpClient = DownloaderFactory.MakeHttpClient()) + using (var httpClient = HttpClientFactory.CreateHttpClientDownloader()) { httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); httpClient.DefaultRequestHeaders.TryAddWithoutValidation(SecurityHeaderName, apiKey); @@ -151,7 +151,7 @@ private static List GetTranslationPairs() return _translationPairs; } - using (var httpClient = DownloaderFactory.MakeHttpClient()) + using (var httpClient = HttpClientFactory.CreateHttpClientDownloader()) { httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=UTF-8"); diff --git a/src/libse/AutoTranslate/MyMemoryApi.cs b/src/libse/AutoTranslate/MyMemoryApi.cs index fde81d42dd..81510c750e 100644 --- a/src/libse/AutoTranslate/MyMemoryApi.cs +++ b/src/libse/AutoTranslate/MyMemoryApi.cs @@ -24,7 +24,7 @@ public class MyMemoryApi : IAutoTranslator, IDisposable public void Initialize() { - _httpClient = DownloaderFactory.MakeHttpClient(); + _httpClient = HttpClientFactory.CreateHttpClientDownloader(); _httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0"); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=UTF-8"); _httpClient.BaseAddress = new Uri("https://api.mymemory.translated.net/get"); diff --git a/src/libse/AutoTranslate/NoLanguageLeftBehindApi.cs b/src/libse/AutoTranslate/NoLanguageLeftBehindApi.cs index 24d53b6634..2df79c13b1 100644 --- a/src/libse/AutoTranslate/NoLanguageLeftBehindApi.cs +++ b/src/libse/AutoTranslate/NoLanguageLeftBehindApi.cs @@ -25,7 +25,7 @@ public class NoLanguageLeftBehindApi : IAutoTranslator, IDisposable public void Initialize() { _httpClient?.Dispose(); - _httpClient = DownloaderFactory.MakeHttpClient(); + _httpClient = HttpClientFactory.CreateHttpClientDownloader(); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json"); _httpClient.BaseAddress = new Uri(Configuration.Settings.Tools.AutoTranslateNllbApiUrl); diff --git a/src/libse/AutoTranslate/NoLanguageLeftBehindServe.cs b/src/libse/AutoTranslate/NoLanguageLeftBehindServe.cs index 88b7f6d809..d05e794659 100644 --- a/src/libse/AutoTranslate/NoLanguageLeftBehindServe.cs +++ b/src/libse/AutoTranslate/NoLanguageLeftBehindServe.cs @@ -26,7 +26,7 @@ public class NoLanguageLeftBehindServe : IAutoTranslator, IDisposable public void Initialize() { _httpClient?.Dispose(); - _httpClient = DownloaderFactory.MakeHttpClient(); + _httpClient = HttpClientFactory.CreateHttpClientDownloader(); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json"); _httpClient.BaseAddress = new Uri(Configuration.Settings.Tools.AutoTranslateNllbServeUrl); diff --git a/src/libse/AutoTranslate/OllamaTranslate.cs b/src/libse/AutoTranslate/OllamaTranslate.cs index dc6653bb85..2a8447f376 100644 --- a/src/libse/AutoTranslate/OllamaTranslate.cs +++ b/src/libse/AutoTranslate/OllamaTranslate.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; +using Nikse.SubtitleEdit.Core.Http; using Nikse.SubtitleEdit.Core.Settings; namespace Nikse.SubtitleEdit.Core.AutoTranslate @@ -26,7 +27,7 @@ public class OllamaTranslate : IAutoTranslator public void Initialize() { _httpClient?.Dispose(); - _httpClient = new HttpClient(); + _httpClient = HttpClientFactory.CreateProxiedHttpClient(); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json"); _httpClient.BaseAddress = new Uri(Configuration.Settings.Tools.OllamaApiUrl.TrimEnd('/')); diff --git a/src/libse/AutoTranslate/OpenRouterTranslate.cs b/src/libse/AutoTranslate/OpenRouterTranslate.cs index 999fc0c7d6..691612d4ba 100644 --- a/src/libse/AutoTranslate/OpenRouterTranslate.cs +++ b/src/libse/AutoTranslate/OpenRouterTranslate.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; +using Nikse.SubtitleEdit.Core.Http; using Nikse.SubtitleEdit.Core.Settings; namespace Nikse.SubtitleEdit.Core.AutoTranslate @@ -37,7 +38,7 @@ public class OpenRouterTranslate : IAutoTranslator public void Initialize() { _httpClient?.Dispose(); - _httpClient = new HttpClient(); + _httpClient = HttpClientFactory.CreateProxiedHttpClient(); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json"); _httpClient.BaseAddress = new Uri(Configuration.Settings.Tools.OpenRouterUrl.TrimEnd('/')); diff --git a/src/libse/AutoTranslate/PapagoTranslate.cs b/src/libse/AutoTranslate/PapagoTranslate.cs index 05c15ff3cc..2598515a93 100644 --- a/src/libse/AutoTranslate/PapagoTranslate.cs +++ b/src/libse/AutoTranslate/PapagoTranslate.cs @@ -9,6 +9,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; +using Nikse.SubtitleEdit.Core.Http; namespace Nikse.SubtitleEdit.Core.AutoTranslate { @@ -26,7 +27,7 @@ public class PapagoTranslate : IAutoTranslator public void Initialize() { _httpClient?.Dispose(); - _httpClient = new HttpClient(); + _httpClient = HttpClientFactory.CreateProxiedHttpClient(); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json"); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("X-NCP-APIGW-API-KEY-ID", Configuration.Settings.Tools.AutoTranslatePapagoApiKeyId); diff --git a/src/libse/AutoTranslate/SeamlessM4TTranslate.cs b/src/libse/AutoTranslate/SeamlessM4TTranslate.cs index b9b582d884..e04c26fb0e 100644 --- a/src/libse/AutoTranslate/SeamlessM4TTranslate.cs +++ b/src/libse/AutoTranslate/SeamlessM4TTranslate.cs @@ -9,6 +9,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; +using Nikse.SubtitleEdit.Core.Http; namespace Nikse.SubtitleEdit.Core.AutoTranslate { @@ -26,7 +27,7 @@ public class SeamlessM4TTranslate : IAutoTranslator public void Initialize() { _httpClient?.Dispose(); - _httpClient = new HttpClient(); + _httpClient = HttpClientFactory.CreateProxiedHttpClient(); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); _httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json"); _httpClient.BaseAddress = new Uri(Configuration.Settings.Tools.AutoTranslateSeamlessM4TUrl.TrimEnd('/') + "/"); diff --git a/src/libse/Forms/CheckForUpdatesHelper.cs b/src/libse/Forms/CheckForUpdatesHelper.cs index c49d29d5e8..e4c79036aa 100644 --- a/src/libse/Forms/CheckForUpdatesHelper.cs +++ b/src/libse/Forms/CheckForUpdatesHelper.cs @@ -93,7 +93,7 @@ public void CheckForUpdates() { try { - using (var httpClient = DownloaderFactory.MakeHttpClient()) + using (var httpClient = HttpClientFactory.CreateHttpClientDownloader()) { _changeLog = httpClient.GetStringAsync(ChangeLogUrl).Result; } diff --git a/src/libse/Http/DownloaderFactory.cs b/src/libse/Http/HttpClientFactory.cs similarity index 84% rename from src/libse/Http/DownloaderFactory.cs rename to src/libse/Http/HttpClientFactory.cs index 707abf747a..37b6c0880e 100644 --- a/src/libse/Http/DownloaderFactory.cs +++ b/src/libse/Http/HttpClientFactory.cs @@ -6,11 +6,11 @@ namespace Nikse.SubtitleEdit.Core.Http { - public static class DownloaderFactory + public static class HttpClientFactory { - public static IDownloader MakeHttpClient() + public static IDownloader CreateHttpClientDownloader() { - var httpClient = new HttpClient(CreateHandler(Configuration.Settings.Proxy)); + var httpClient = CreateProxiedHttpClient(); if (Configuration.Settings.General.UseLegacyDownloader) { return new LegacyDownloader(httpClient); @@ -19,6 +19,11 @@ public static IDownloader MakeHttpClient() return new HttpClientDownloader(httpClient); } + public static HttpClient CreateProxiedHttpClient() + { + return new HttpClient(CreateHandler(Configuration.Settings.Proxy)); + } + private static HttpClientHandler CreateHandler(ProxySettings proxySettings) { var handler = new HttpClientHandler(); diff --git a/src/libse/VobSub/Ocr/Service/GoogleCloudVisionApi.cs b/src/libse/VobSub/Ocr/Service/GoogleCloudVisionApi.cs index 300cefcd27..509019958b 100644 --- a/src/libse/VobSub/Ocr/Service/GoogleCloudVisionApi.cs +++ b/src/libse/VobSub/Ocr/Service/GoogleCloudVisionApi.cs @@ -153,7 +153,7 @@ public List GetLanguages() public GoogleCloudVisionApi(string apiKey) { _apiKey = apiKey; - _httpClient = DownloaderFactory.MakeHttpClient(); + _httpClient = HttpClientFactory.CreateHttpClientDownloader(); _httpClient.BaseAddress = new Uri("https://vision.googleapis.com/v1/images:annotate"); _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); } diff --git a/src/ui/Forms/AudioToText/VoskModelDownload.cs b/src/ui/Forms/AudioToText/VoskModelDownload.cs index 19fcd9b63f..e15deb26a0 100644 --- a/src/ui/Forms/AudioToText/VoskModelDownload.cs +++ b/src/ui/Forms/AudioToText/VoskModelDownload.cs @@ -71,7 +71,7 @@ private void buttonOK_Click(object sender, EventArgs e) buttonDownload.Enabled = false; Refresh(); Cursor = Cursors.WaitCursor; - using (var httpClient = DownloaderFactory.MakeHttpClient()) + using (var httpClient = HttpClientFactory.CreateHttpClientDownloader()) using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(url, downloadStream, new Progress((progress) => diff --git a/src/ui/Forms/AudioToText/WhisperDownload.cs b/src/ui/Forms/AudioToText/WhisperDownload.cs index e19ede4700..2c9e113bd9 100644 --- a/src/ui/Forms/AudioToText/WhisperDownload.cs +++ b/src/ui/Forms/AudioToText/WhisperDownload.cs @@ -184,7 +184,7 @@ private void WhisperDownload_Shown(object sender, EventArgs e) try { - using (var httpClient = DownloaderFactory.MakeHttpClient()) + using (var httpClient = HttpClientFactory.CreateHttpClientDownloader()) using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(downloadUrl, downloadStream, new Progress((progress) => diff --git a/src/ui/Forms/AudioToText/WhisperModelDownload.cs b/src/ui/Forms/AudioToText/WhisperModelDownload.cs index d04ee21973..3d8a6f3be3 100644 --- a/src/ui/Forms/AudioToText/WhisperModelDownload.cs +++ b/src/ui/Forms/AudioToText/WhisperModelDownload.cs @@ -110,7 +110,7 @@ private void MultiFileDownload() }); foreach (var url in LastDownloadedModel.Urls) { - using (var httpClient = DownloaderFactory.MakeHttpClient()) + using (var httpClient = HttpClientFactory.CreateHttpClientDownloader()) { currentDownloadUrl = url; _downloadFileName = MakeDownloadFileName(LastDownloadedModel, url) + ".$$$"; diff --git a/src/ui/Forms/DownloadFfmpeg.cs b/src/ui/Forms/DownloadFfmpeg.cs index 5c608c7e91..f7ad09fce5 100644 --- a/src/ui/Forms/DownloadFfmpeg.cs +++ b/src/ui/Forms/DownloadFfmpeg.cs @@ -66,7 +66,7 @@ private void DownloadFfmpeg_Shown(object sender, EventArgs e) { labelPleaseWait.Text = LanguageSettings.Current.General.PleaseWait; Cursor = Cursors.WaitCursor; - using (var httpClient = DownloaderFactory.MakeHttpClient()) + using (var httpClient = HttpClientFactory.CreateHttpClientDownloader()) using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(url, downloadStream, new Progress((progress) => diff --git a/src/ui/Forms/DownloadVosk.cs b/src/ui/Forms/DownloadVosk.cs index 1eb1625685..8e3cb18364 100644 --- a/src/ui/Forms/DownloadVosk.cs +++ b/src/ui/Forms/DownloadVosk.cs @@ -64,7 +64,7 @@ private void DownloadVosk_Shown(object sender, EventArgs e) Refresh(); Cursor = Cursors.WaitCursor; - using (var httpClient = DownloaderFactory.MakeHttpClient()) + using (var httpClient = HttpClientFactory.CreateHttpClientDownloader()) using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(VoskUrl, downloadStream, new Progress((progress) => diff --git a/src/ui/Forms/DownloadYouTubeDl.cs b/src/ui/Forms/DownloadYouTubeDl.cs index b2ef0ff529..764344d453 100644 --- a/src/ui/Forms/DownloadYouTubeDl.cs +++ b/src/ui/Forms/DownloadYouTubeDl.cs @@ -55,7 +55,7 @@ private void DownloadFfmpeg_Shown(object sender, EventArgs e) buttonOK.Enabled = false; Refresh(); Cursor = Cursors.WaitCursor; - using (var httpClient = DownloaderFactory.MakeHttpClient()) + using (var httpClient = HttpClientFactory.CreateHttpClientDownloader()) using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(Url, downloadStream, new Progress((progress) => diff --git a/src/ui/Forms/GetDictionaries.cs b/src/ui/Forms/GetDictionaries.cs index f1dce5a4b3..97bf9763e0 100644 --- a/src/ui/Forms/GetDictionaries.cs +++ b/src/ui/Forms/GetDictionaries.cs @@ -196,7 +196,7 @@ private void buttonDownload_Click(object sender, EventArgs e) Refresh(); Cursor = Cursors.WaitCursor; - using (var httpClient = DownloaderFactory.MakeHttpClient()) + using (var httpClient = HttpClientFactory.CreateHttpClientDownloader()) using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(_downloadLink, downloadStream, new Progress((progress) => diff --git a/src/ui/Forms/Ocr/DownloadTesseract302.cs b/src/ui/Forms/Ocr/DownloadTesseract302.cs index 558cd0c4b5..1f5331e847 100644 --- a/src/ui/Forms/Ocr/DownloadTesseract302.cs +++ b/src/ui/Forms/Ocr/DownloadTesseract302.cs @@ -31,7 +31,7 @@ private void DownloadTesseract302_Shown(object sender, EventArgs e) try { - using (var httpClient = DownloaderFactory.MakeHttpClient()) + using (var httpClient = HttpClientFactory.CreateHttpClientDownloader()) using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(url, downloadStream, new Progress((progress) => diff --git a/src/ui/Forms/Ocr/DownloadTesseract5.cs b/src/ui/Forms/Ocr/DownloadTesseract5.cs index ea87c3ce3b..5e26f8acad 100644 --- a/src/ui/Forms/Ocr/DownloadTesseract5.cs +++ b/src/ui/Forms/Ocr/DownloadTesseract5.cs @@ -30,7 +30,7 @@ private void DownloadTesseract5_Shown(object sender, EventArgs e) try { Utilities.SetSecurityProtocol(); - using (var httpClient = DownloaderFactory.MakeHttpClient()) + using (var httpClient = HttpClientFactory.CreateHttpClientDownloader()) using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(TesseractDownloadUrl, downloadStream, new Progress((progress) => diff --git a/src/ui/Forms/Ocr/GetTesseract302Dictionaries.cs b/src/ui/Forms/Ocr/GetTesseract302Dictionaries.cs index f9a1c4e593..77e39db4c4 100644 --- a/src/ui/Forms/Ocr/GetTesseract302Dictionaries.cs +++ b/src/ui/Forms/Ocr/GetTesseract302Dictionaries.cs @@ -160,7 +160,7 @@ private void buttonDownload_Click(object sender, EventArgs e) try { - using (var httpClient = DownloaderFactory.MakeHttpClient()) + using (var httpClient = HttpClientFactory.CreateHttpClientDownloader()) using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(url, downloadStream, new Progress((progress) => diff --git a/src/ui/Forms/Ocr/GetTesseractDictionaries.cs b/src/ui/Forms/Ocr/GetTesseractDictionaries.cs index 2677c448bd..ed3ff0ec47 100644 --- a/src/ui/Forms/Ocr/GetTesseractDictionaries.cs +++ b/src/ui/Forms/Ocr/GetTesseractDictionaries.cs @@ -138,7 +138,7 @@ private void buttonDownload_Click(object sender, EventArgs e) ChosenLanguage = dictionary.ToString(); - using (var httpClient = DownloaderFactory.MakeHttpClient()) + using (var httpClient = HttpClientFactory.CreateHttpClientDownloader()) using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(url, downloadStream, new Progress((progress) => diff --git a/src/ui/Forms/Options/SettingsMpv.cs b/src/ui/Forms/Options/SettingsMpv.cs index 22a19b7482..7a53cef6ce 100644 --- a/src/ui/Forms/Options/SettingsMpv.cs +++ b/src/ui/Forms/Options/SettingsMpv.cs @@ -37,7 +37,7 @@ private void ButtonDownloadClick(object sender, EventArgs e) labelPleaseWait.Text = LanguageSettings.Current.General.PleaseWait; Refresh(); Cursor = Cursors.WaitCursor; - using (var httpClient = DownloaderFactory.MakeHttpClient()) + using (var httpClient = HttpClientFactory.CreateHttpClientDownloader()) using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(_downloadUrl, downloadStream, new Progress((progress) => diff --git a/src/ui/Forms/PluginsGet.cs b/src/ui/Forms/PluginsGet.cs index df1f5cb717..d80e449a70 100644 --- a/src/ui/Forms/PluginsGet.cs +++ b/src/ui/Forms/PluginsGet.cs @@ -213,7 +213,7 @@ private void buttonDownload_Click(object sender, EventArgs e) Refresh(); Cursor = Cursors.WaitCursor; - using (var httpClient = DownloaderFactory.MakeHttpClient()) + using (var httpClient = HttpClientFactory.CreateHttpClientDownloader()) using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(url, downloadStream, new Progress((progress) => @@ -405,7 +405,7 @@ private void buttonUpdateAll_Click(object sender, EventArgs e) _updatingAllPluginsCount = 0; _updatingAllPlugins = true; - using (var httpClient = DownloaderFactory.MakeHttpClient()) + using (var httpClient = HttpClientFactory.CreateHttpClientDownloader()) { foreach (var url in _updateAllListUrls) { diff --git a/src/ui/Forms/Translate/AutoTranslate.cs b/src/ui/Forms/Translate/AutoTranslate.cs index b94435fc78..0a65238b3d 100644 --- a/src/ui/Forms/Translate/AutoTranslate.cs +++ b/src/ui/Forms/Translate/AutoTranslate.cs @@ -14,6 +14,7 @@ using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; +using Nikse.SubtitleEdit.Core.Http; using MessageBox = Nikse.SubtitleEdit.Forms.SeMsgBox.MessageBox; using Timer = System.Windows.Forms.Timer; @@ -1617,7 +1618,7 @@ async Task> GetModelsAsync(string url) private HttpClient _httpClient; - private HttpClient GetOllamaClient() => _httpClient ?? (_httpClient = new HttpClient()); + private HttpClient GetOllamaClient() => _httpClient ?? (_httpClient = HttpClientFactory.CreateProxiedHttpClient()); private void FillOllamaModels(string[] models) { diff --git a/src/ui/Forms/Tts/PiperDownload.cs b/src/ui/Forms/Tts/PiperDownload.cs index 00a6597ba7..738a1f87d3 100644 --- a/src/ui/Forms/Tts/PiperDownload.cs +++ b/src/ui/Forms/Tts/PiperDownload.cs @@ -64,7 +64,7 @@ private void DownloadFfmpeg_Shown(object sender, EventArgs e) { labelPleaseWait.Text = LanguageSettings.Current.General.PleaseWait; Cursor = Cursors.WaitCursor; - using (var httpClient = DownloaderFactory.MakeHttpClient()) + using (var httpClient = HttpClientFactory.CreateHttpClientDownloader()) using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(url, downloadStream, new Progress((progress) => diff --git a/src/ui/Forms/Tts/TextToSpeech.cs b/src/ui/Forms/Tts/TextToSpeech.cs index ae71818e2b..df3e15c6d2 100644 --- a/src/ui/Forms/Tts/TextToSpeech.cs +++ b/src/ui/Forms/Tts/TextToSpeech.cs @@ -15,6 +15,7 @@ using System.Text; using System.Threading; using System.Windows.Forms; +using Nikse.SubtitleEdit.Core.Http; using Nikse.SubtitleEdit.Core.Translate; using MessageBox = Nikse.SubtitleEdit.Forms.SeMsgBox.MessageBox; @@ -949,7 +950,7 @@ private bool GenerateParagraphAudioTortoiseTts(Subtitle subtitle, bool showProgr private bool GenerateParagraphAudioCoqui(Subtitle subtitle, bool showProgressBar, string overrideFileName) { - using (var httpClient = new HttpClient()) + using (var httpClient = HttpClientFactory.CreateProxiedHttpClient()) { httpClient.BaseAddress = new Uri(nikseComboBoxVoice.Text.StartsWith("http", StringComparison.OrdinalIgnoreCase) ? nikseComboBoxVoice.Text : "http://localhost:5002/api/tts"); @@ -998,7 +999,7 @@ private bool GenerateParagraphAudioCoqui(Subtitle subtitle, bool showProgressBar private bool GenerateParagraphAudioAllTalk(Subtitle subtitle, bool showProgressBar, string overrideFileName) { - using (var httpClient = new HttpClient()) + using (var httpClient = HttpClientFactory.CreateProxiedHttpClient()) { httpClient.BaseAddress = new Uri("http://127.0.0.1:7851"); @@ -1084,7 +1085,7 @@ private bool GenerateParagraphAudioElevenLabs(Subtitle subtitle, bool showProgre return false; } - using (var httpClient = new HttpClient()) + using (var httpClient = HttpClientFactory.CreateProxiedHttpClient()) { httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "audio/mpeg"); @@ -1213,7 +1214,7 @@ private List GetAzureVoices(bool useCache) if (!useCache) { - using (var httpClient = new HttpClient()) + using (var httpClient = HttpClientFactory.CreateProxiedHttpClient()) { httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Ocp-Apim-Subscription-Key", nikseTextBoxApiKey.Text.Trim()); var url = $"https://{nikseComboBoxRegion.Text.Trim()}.tts.speech.microsoft.com/cognitiveservices/voices/list"; @@ -1273,7 +1274,7 @@ private bool GenerateParagraphAudioAzure(Subtitle subtitle, bool showProgressBar return false; } - using (var httpClient = new HttpClient()) + using (var httpClient = HttpClientFactory.CreateProxiedHttpClient()) { httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "ssml+xml"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "audio/mpeg"); @@ -1943,7 +1944,7 @@ private List GetPiperVoices(bool useCache) if (!useCache) { - using (var httpClient = new HttpClient()) + using (var httpClient = HttpClientFactory.CreateProxiedHttpClient()) { httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json"); @@ -2042,7 +2043,7 @@ private List GetElevenLabVoices(bool useCache) if (!useCache) { - using (var httpClient = new HttpClient()) + using (var httpClient = HttpClientFactory.CreateProxiedHttpClient()) { httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json"); @@ -2132,7 +2133,7 @@ private List GetAllTalkVoices(bool useCache) if (!useCache) { - using(var httpClient = new HttpClient()) + using(var httpClient = HttpClientFactory.CreateProxiedHttpClient()) { httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json"); diff --git a/src/ui/Logic/CheckForUpdatesHelper.cs b/src/ui/Logic/CheckForUpdatesHelper.cs index 9db98a2ffc..4f550f4b0c 100644 --- a/src/ui/Logic/CheckForUpdatesHelper.cs +++ b/src/ui/Logic/CheckForUpdatesHelper.cs @@ -99,7 +99,7 @@ public void CheckForUpdates(bool manualCheck) try { - using (var httpClient = DownloaderFactory.MakeHttpClient()) + using (var httpClient = HttpClientFactory.CreateHttpClientDownloader()) { _changeLog = httpClient.GetStringAsync(ChangeLogUrl).Result; } diff --git a/src/ui/Logic/Plugins/OnlinePluginMetadataProvider.cs b/src/ui/Logic/Plugins/OnlinePluginMetadataProvider.cs index f791daae9c..45655c957d 100644 --- a/src/ui/Logic/Plugins/OnlinePluginMetadataProvider.cs +++ b/src/ui/Logic/Plugins/OnlinePluginMetadataProvider.cs @@ -20,7 +20,7 @@ public OnlinePluginMetadataProvider(string githubUrl) public IReadOnlyCollection GetPlugins() { XDocument xDocument; - using (var httpClient = DownloaderFactory.MakeHttpClient()) + using (var httpClient = HttpClientFactory.CreateHttpClientDownloader()) using (var downloadStream = new MemoryStream()) { var downloadTask = httpClient.DownloadAsync(_githubUrl, downloadStream, null, CancellationToken.None);