本帖最后由 cfnm123 于 2024-8-16 22:01 编辑
[Asm] 纯文本查看 复制代码 using System;
using System.Diagnostics;
using System.Net.Http;
using System.Net.Http.Json;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
class Program
{
private static readonly SemaphoreSlim semaphore = new SemaphoreSlim(100, 100);
private static readonly string[] Myip;
private static readonly IHost host;
private static readonly HttpClient httpClient;
private static readonly ILogger logger;
private static readonly string url = "http://your-target-url-here";
static Program()
{
// 初始化代{过}{滤}理IP列表
string ipPortString = "115.209.48.179:37608 116.7.173.115:31843 116.7.201.221:35131 119.132.91.47:39837 222.189.81.116:35243 175.146.211.176:34454 121.234.46.195:34738 111.72.196.138:33973 42.57.150.201:39846 111.72.134.86:40290";
Myip = ipPortString.Split(' ');
host = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
services.AddLogging(builder => builder.AddConsole());
services.AddHttpClient();
})
.Build();
logger = host.Services.GetRequiredService<ILogger<Program>>();
httpClient = host.Services.GetRequiredService<IHttpClientFactory>().CreateClient();
}
static async Task Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
await UpdateIpListAsync();
await CreateThreadsAsync();
await Task.Delay(1000); // 每秒创建一次
}
logger.LogInformation("All threads have been created.");
Console.ReadKey();
}
static async Task CreateThreadsAsync()
{
for (int i = 0; i < 10; i++)
{
int index = i;
var task = ExecuteRequestAsync(index);
await task;
}
}
static async Task ExecuteRequestAsync(int index)
{
var handler = new HttpClientHandler
{
Proxy = new WebProxy(Myip[index]),
UseProxy = true
};
using var client = new HttpClient(handler);
try
{
var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30)); // 设置30秒超时
var cancellationToken = cancellationTokenSource.Token;
while (!cancellationToken.IsCancellationRequested)
{
await semaphore.WaitAsync(cancellationToken);
try
{
var stopwatch = Stopwatch.StartNew();
var response = await client.GetAsync(url, cancellationToken);
stopwatch.Stop();
if (response.IsSuccessStatusCode)
{
logger.LogInformation($"Thread {index} made a request to {url} at {DateTime.Now.ToString("HH:mm:ss.fff")}. Response time: {stopwatch.ElapsedMilliseconds} ms.");
}
else
{
logger.LogWarning($"Thread {index} received a non-success status code: {response.StatusCode}.");
}
}
catch (OperationCanceledException)
{
logger.LogInformation($"Thread {index} was canceled.");
}
catch (Exception ex)
{
logger.LogError(ex, $"Error in thread {index}: {ex.Message}");
}
finally
{
semaphore.Release();
await Task.Delay(1000, cancellationToken); // 控制每个线程的请求频率
}
}
}
finally
{
logger.LogInformation($"Thread {index} has been terminated.");
}
}
static async Task UpdateIpListAsync()
{
// 这里可以添加逻辑来获取新的代{过}{滤}理IP列表
logger.LogInformation("Updating IP list...");
await Task.Delay(100); // 假设这里有一些延迟
}
} |