From fa8a30463ca76518c7de0706ead696ca30a73603 Mon Sep 17 00:00:00 2001 From: jetsparrow Date: Sun, 5 Apr 2026 22:53:18 +0300 Subject: [PATCH] proxy support --- JetKarmaBot/Config.cs | 10 +--------- JetKarmaBot/JetKarmaBot.cs | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/JetKarmaBot/Config.cs b/JetKarmaBot/Config.cs index 59ca932..af698f1 100644 --- a/JetKarmaBot/Config.cs +++ b/JetKarmaBot/Config.cs @@ -12,15 +12,7 @@ public class Config : ConfigBase public string ApiKey { get; private set; } public string ConnectionString { get; private set; } - public class ProxySettings - { - public string Url { get; private set; } - public int Port { get; private set; } - public string Login { get; private set; } - public string Password { get; private set; } - } - - public ProxySettings Proxy { get; private set; } + public string Proxy { get; private set; } public class TimeoutConfig { public int DebtLimitSeconds { get; private set; } = 60 * 60 * 2; diff --git a/JetKarmaBot/JetKarmaBot.cs b/JetKarmaBot/JetKarmaBot.cs index 057e587..e67ba0d 100644 --- a/JetKarmaBot/JetKarmaBot.cs +++ b/JetKarmaBot/JetKarmaBot.cs @@ -3,7 +3,8 @@ using JetKarmaBot.Models; using JetKarmaBot.Services; using JetKarmaBot.Services.Handling; using NLog; - +using System.Net; +using System.Net.Http; using Telegram.Bot; using Telegram.Bot.Polling; using Telegram.Bot.Types; @@ -32,8 +33,17 @@ public class JetKarmaBot : IDisposable { using (KarmaContext db = Db.GetContext()) await db.Database.EnsureCreatedAsync(); - - Client = new TelegramBotClient(Config.ApiKey); + + var httpClientHandler = new HttpClientHandler(); + + if (Config.Proxy is string url) + { + httpClientHandler.Proxy = new WebProxy(url); + httpClientHandler.UseProxy = true; + } + var httpClient = new HttpClient(httpClientHandler); + + Client = new TelegramBotClient(Config.ApiKey, httpClient); Container.AddInstance(Client); timeoutWaitTaskToken = new CancellationTokenSource();