From 153d9c577b01f2128081a1f61a26bcfb1ab1d43f Mon Sep 17 00:00:00 2001 From: Mohammad Mahdi Mohammadi Date: Thu, 2 Jan 2025 16:01:42 +0330 Subject: [PATCH] Add socks proxy support --- .gitignore | 1 + Models/ConfigData.cs | 7 +++++-- Program.cs | 19 +++++++++++++++++-- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 7c870fa..902b347 100755 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ obj/ .env data/ Folder.DotSettings.user +docker-compose.yaml diff --git a/Models/ConfigData.cs b/Models/ConfigData.cs index 1317c39..2ea22b4 100755 --- a/Models/ConfigData.cs +++ b/Models/ConfigData.cs @@ -19,10 +19,11 @@ public class ConfigData var adminNumId = DotNetEnv.Env.GetString("ADMIN_NUMID") ?? Environment.GetEnvironmentVariable("ADMIN_NUMID"); var instance = DotNetEnv.Env.GetString("CUSTOM_INSTANCE") ?? Environment.GetEnvironmentVariable("CUSTOM_INSTANCE") ?? "https://haminoa.net"; + var socksProxy = DotNetEnv.Env.GetString("SOCKS_PROXY") ?? Environment.GetEnvironmentVariable("SOCKS_PROXY") ?? String.Empty; // Check if any of the values are still null or empty if (string.IsNullOrEmpty(botToken) || string.IsNullOrEmpty(tag) - || string.IsNullOrEmpty(channelNumId) || string.IsNullOrEmpty(adminNumId)) + || string.IsNullOrEmpty(channelNumId) || string.IsNullOrEmpty(adminNumId) || string.IsNullOrEmpty(mongoDbConnectionString)) return null; // Failure if any are missing // If all required variables are present, assign to the config @@ -34,7 +35,8 @@ public class ConfigData TAG = tag, CHANNEL_NUMID = channelNumId, ADMIN_NUMID = adminNumId, - INSTANCE = instance + INSTANCE = instance, + SOCKS_PROXY = socksProxy }; return config; // Success @@ -49,5 +51,6 @@ public class ConfigData public string ADMIN_NUMID { get; set; } public string INSTANCE { get; set; } public string MONGODB_CONNECTION_STRING { get; set; } + public string SOCKS_PROXY { get; set; } } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index 18b5bc6..3dc4791 100755 --- a/Program.cs +++ b/Program.cs @@ -1,4 +1,5 @@ -using CatsOfMastodonBot.Models; +using System.Net; +using CatsOfMastodonBot.Models; using CatsOfMastodonBot.Services; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Hosting; @@ -48,7 +49,21 @@ public class MastodonBot // Setup bot - var bot = new TelegramBotClient(config.BOT_TOKEN); + TelegramBotClient bot; + if (!String.IsNullOrEmpty(config.SOCKS_PROXY)) + { + WebProxy proxy = new (config.SOCKS_PROXY); + + HttpClient httpClient = new ( + new SocketsHttpHandler { Proxy = proxy, UseProxy = true } + ); + + bot = new TelegramBotClient(config.BOT_TOKEN, httpClient); + } + else + { + bot = new TelegramBotClient(config.BOT_TOKEN); + } var me = await bot.GetMe(); await bot.DropPendingUpdates();