mirror of
https://github.com/DrizzleTime/Foxel.git
synced 2026-09-05 23:57:19 +08:00
feat(TelegramStorage): add proxy configuration support for Telegram API requests #10
This commit is contained in:
@@ -3,6 +3,7 @@ using System.Text.Json;
|
|||||||
using System.Text.Json.Serialization;
|
using System.Text.Json.Serialization;
|
||||||
using Foxel.Services.Attributes;
|
using Foxel.Services.Attributes;
|
||||||
using Foxel.Services.Configuration;
|
using Foxel.Services.Configuration;
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
namespace Foxel.Services.Storage.Providers;
|
namespace Foxel.Services.Storage.Providers;
|
||||||
|
|
||||||
@@ -14,7 +15,7 @@ public class TelegramStorageProvider(IConfigService configService) : IStoragePro
|
|||||||
string botToken = configService["Storage:TelegramStorageBotToken"];
|
string botToken = configService["Storage:TelegramStorageBotToken"];
|
||||||
string chatId = configService["Storage:TelegramStorageChatId"];
|
string chatId = configService["Storage:TelegramStorageChatId"];
|
||||||
|
|
||||||
using var httpClient = new HttpClient();
|
using var httpClient = CreateHttpClient();
|
||||||
using var formData = new MultipartFormDataContent();
|
using var formData = new MultipartFormDataContent();
|
||||||
formData.Add(new StringContent(chatId), "chat_id");
|
formData.Add(new StringContent(chatId), "chat_id");
|
||||||
var safeFileName = Path.GetFileNameWithoutExtension(fileName);
|
var safeFileName = Path.GetFileNameWithoutExtension(fileName);
|
||||||
@@ -83,7 +84,7 @@ public class TelegramStorageProvider(IConfigService configService) : IStoragePro
|
|||||||
|
|
||||||
string botToken = configService["Storage:TelegramStorageBotToken"];
|
string botToken = configService["Storage:TelegramStorageBotToken"];
|
||||||
|
|
||||||
using var httpClient = new HttpClient();
|
using var httpClient = CreateHttpClient();
|
||||||
var url =
|
var url =
|
||||||
$"https://api.telegram.org/bot{botToken}/deleteMessage?chat_id={metadata.ChatId}&message_id={metadata.MessageId}";
|
$"https://api.telegram.org/bot{botToken}/deleteMessage?chat_id={metadata.ChatId}&message_id={metadata.MessageId}";
|
||||||
await httpClient.GetAsync(url);
|
await httpClient.GetAsync(url);
|
||||||
@@ -131,7 +132,7 @@ public class TelegramStorageProvider(IConfigService configService) : IStoragePro
|
|||||||
|
|
||||||
string botToken = configService["Storage:TelegramStorageBotToken"];
|
string botToken = configService["Storage:TelegramStorageBotToken"];
|
||||||
|
|
||||||
using var httpClient = new HttpClient();
|
using var httpClient = CreateHttpClient();
|
||||||
var getFileUrl = $"https://api.telegram.org/bot{botToken}/getFile?file_id={metadata.FileId}";
|
var getFileUrl = $"https://api.telegram.org/bot{botToken}/getFile?file_id={metadata.FileId}";
|
||||||
var getFileResponse = await httpClient.GetAsync(getFileUrl);
|
var getFileResponse = await httpClient.GetAsync(getFileUrl);
|
||||||
|
|
||||||
@@ -183,6 +184,54 @@ public class TelegramStorageProvider(IConfigService configService) : IStoragePro
|
|||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 创建配置了代理的HttpClient
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>已配置的HttpClient</returns>
|
||||||
|
private HttpClient CreateHttpClient()
|
||||||
|
{
|
||||||
|
HttpClient client;
|
||||||
|
|
||||||
|
// 检查是否有代理配置
|
||||||
|
string proxyAddress = configService["Storage:TelegramProxyAddress"];
|
||||||
|
string proxyPort = configService["Storage:TelegramProxyPort"];
|
||||||
|
string proxyUsername = configService["Storage:TelegramProxyUsername"];
|
||||||
|
string proxyPassword = configService["Storage:TelegramProxyPassword"];
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(proxyAddress) && !string.IsNullOrEmpty(proxyPort) && int.TryParse(proxyPort, out int port))
|
||||||
|
{
|
||||||
|
var proxy = new WebProxy
|
||||||
|
{
|
||||||
|
Address = new Uri($"http://{proxyAddress}:{port}"),
|
||||||
|
BypassProxyOnLocal = false,
|
||||||
|
UseDefaultCredentials = false
|
||||||
|
};
|
||||||
|
|
||||||
|
// 如果提供了代理认证信息
|
||||||
|
if (!string.IsNullOrEmpty(proxyUsername))
|
||||||
|
{
|
||||||
|
proxy.Credentials = new NetworkCredential(proxyUsername, proxyPassword);
|
||||||
|
}
|
||||||
|
|
||||||
|
var handler = new HttpClientHandler
|
||||||
|
{
|
||||||
|
Proxy = proxy,
|
||||||
|
UseProxy = true
|
||||||
|
};
|
||||||
|
|
||||||
|
client = new HttpClient(handler);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
client = new HttpClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置超时
|
||||||
|
client.Timeout = TimeSpan.FromMinutes(5);
|
||||||
|
|
||||||
|
return client;
|
||||||
|
}
|
||||||
|
|
||||||
// 用于处理 Telegram API 响应的辅助类
|
// 用于处理 Telegram API 响应的辅助类
|
||||||
private class TelegramResponse
|
private class TelegramResponse
|
||||||
|
|||||||
@@ -659,12 +659,20 @@ const SystemConfig: React.FC = () => {
|
|||||||
groupName="Storage"
|
groupName="Storage"
|
||||||
configs={{
|
configs={{
|
||||||
"TelegramStorageBotToken": configs.Storage?.TelegramStorageBotToken || '',
|
"TelegramStorageBotToken": configs.Storage?.TelegramStorageBotToken || '',
|
||||||
"TelegramStorageChatId": configs.Storage?.TelegramStorageChatId || ''
|
"TelegramStorageChatId": configs.Storage?.TelegramStorageChatId || '',
|
||||||
|
"TelegramProxyAddress": configs.Storage?.TelegramProxyAddress || '',
|
||||||
|
"TelegramProxyPort": configs.Storage?.TelegramProxyPort || '',
|
||||||
|
"TelegramProxyUsername": configs.Storage?.TelegramProxyUsername || '',
|
||||||
|
"TelegramProxyPassword": configs.Storage?.TelegramProxyPassword || ''
|
||||||
}}
|
}}
|
||||||
onSave={handleSaveConfig}
|
onSave={handleSaveConfig}
|
||||||
descriptions={{
|
descriptions={{
|
||||||
"TelegramStorageBotToken": 'Telegram 机器人令牌',
|
"TelegramStorageBotToken": 'Telegram 机器人令牌',
|
||||||
"TelegramStorageChatId": 'Telegram 聊天ID'
|
"TelegramStorageChatId": 'Telegram 聊天ID',
|
||||||
|
"TelegramProxyAddress": '代理服务器地址 (例如: 127.0.0.1)',
|
||||||
|
"TelegramProxyPort": '代理服务器端口 (例如: 1080)',
|
||||||
|
"TelegramProxyUsername": '代理用户名 (可选)',
|
||||||
|
"TelegramProxyPassword": '代理密码 (可选)'
|
||||||
}}
|
}}
|
||||||
secretFields={secretFields.Storage || []}
|
secretFields={secretFields.Storage || []}
|
||||||
isMobile={isMobile}
|
isMobile={isMobile}
|
||||||
|
|||||||
Reference in New Issue
Block a user