106 lines
3.9 KiB
C#
106 lines
3.9 KiB
C#
// // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
|
// //
|
|
// // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
|
// //
|
|
// // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
|
//
|
|
// using System.Net;
|
|
//
|
|
// namespace Admin.NET.Core;
|
|
//
|
|
// /// <summary>
|
|
// /// 代理管理器
|
|
// /// </summary>
|
|
// public class ProxyManager : IProxyManager, IDisposable, ITransient
|
|
// {
|
|
// private List<WebProxy> _proxies = new();
|
|
//
|
|
// private readonly HttpRemotesOptions _options;
|
|
// private readonly object _lock = new();
|
|
// private readonly Timer _refreshTimer;
|
|
// private int _currentIndex = 0;
|
|
//
|
|
// public ProxyManager(IOptions<HttpRemotesOptions> options)
|
|
// {
|
|
// _options = options.Value;
|
|
//
|
|
// // 启动后立即加载一次
|
|
// RefreshProxiesAsync().Wait();
|
|
//
|
|
// // 每 24 小时刷新一次(可改为每天固定时间)
|
|
// _refreshTimer = new Timer(async _ => await RefreshProxiesAsync(),
|
|
// null, TimeSpan.FromHours(24), TimeSpan.FromHours(24));
|
|
// }
|
|
//
|
|
// public async Task RefreshProxiesAsync()
|
|
// {
|
|
// try
|
|
// {
|
|
// var newProxies = await FetchProxiesFromRemoteAsync();
|
|
// if (newProxies != null && newProxies.Any())
|
|
// {
|
|
// lock (_lock)
|
|
// {
|
|
// _proxies = newProxies
|
|
// .Select(p => new WebProxy(_options.Proxy.Address)
|
|
// {
|
|
// Credentials = !string.IsNullOrEmpty(_options.Proxy.Account)
|
|
// ? new NetworkCredential(_options.Proxy.Account, _options.Proxy.Password)
|
|
// : null
|
|
// })
|
|
// .ToList();
|
|
// _currentIndex = 0; // 重置索引
|
|
// }
|
|
// Console.WriteLine($"✅ 代理列表已刷新,共 {_proxies.Count} 个代理");
|
|
// }
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// Console.WriteLine($"❌ 刷新代理列表失败: {ex.Message}");
|
|
// }
|
|
// }
|
|
//
|
|
// private async Task<List<ProxyConfig>> FetchProxiesFromRemoteAsync()
|
|
// {
|
|
// // 示例:从远程 API 获取代理
|
|
// using var client = new HttpClient();
|
|
// var response = await client.GetAsync("https://your-proxy-api.com/daily");
|
|
// if (response.IsSuccessStatusCode)
|
|
// {
|
|
// var json = await response.Content.ReadAsStringAsync();
|
|
// return JsonSerializer.Deserialize<List<ProxyConfig>>(json,
|
|
// new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
|
|
// }
|
|
//
|
|
// // 可替换为数据库、文件、Redis 等来源
|
|
// return new List<ProxyConfig>
|
|
// {
|
|
// new() { Address = "http://proxy1:8080", Username = "user", Password = "pass" },
|
|
// new() { Address = "http://proxy2:8080" },
|
|
// };
|
|
// }
|
|
//
|
|
// public WebProxy GetCurrentProxy()
|
|
// {
|
|
// lock (_lock)
|
|
// {
|
|
// if (!_proxies.Any()) return null;
|
|
// var proxy = _proxies[_currentIndex % _proxies.Count];
|
|
// _currentIndex = (_currentIndex + 1) % _proxies.Count;
|
|
// return proxy;
|
|
// }
|
|
// }
|
|
//
|
|
// public List<WebProxy> GetAllProxies()
|
|
// {
|
|
// lock (_lock)
|
|
// {
|
|
// return new List<WebProxy>(_proxies);
|
|
// }
|
|
// }
|
|
//
|
|
// public void Dispose()
|
|
// {
|
|
// _refreshTimer?.Dispose();
|
|
// }
|
|
// } |