Skip to content

Commit e112c74

Browse files
committed
demo增加代理扫描
1 parent 17a97aa commit e112c74

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

Demo/Program.cs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
using Demo.HttpServices;
33
using System;
44
using System.Diagnostics;
5-
using System.Net.Http;
5+
using System.Linq;
6+
using System.Net;
7+
using System.Threading;
68
using System.Threading.Tasks;
79
using WebApiClient;
810
using WebApiClient.Parameterables;
@@ -11,14 +13,40 @@ namespace Demo
1113
{
1214
class Program
1315
{
16+
private static long totalProxyCount = 0;
17+
private static int completedProxyCount = 0;
18+
1419
static void Main(string[] args)
1520
{
1621
HttpServer.Start(9999);
17-
Program.RunIUserApi(1);
22+
Program.RunIUserApi(1).ContinueWith(t => ScanProxy());
1823
Console.ReadLine();
1924
}
2025

21-
static async void RunIUserApi(int loop = 1)
26+
/// <summary>
27+
/// 扫描代理ip
28+
/// </summary>
29+
static async void ScanProxy()
30+
{
31+
var proxys = HttpProxy.Range(IPAddress.Parse("221.122.13.1"), 8080, 9999).ToArray();
32+
var target = new Uri("http://www.baidu.com");
33+
var tasks = proxys.Select(async p =>
34+
{
35+
Interlocked.Increment(ref totalProxyCount);
36+
var state = await ProxyValidator.ValidateAsync(p, target, TimeSpan.FromMilliseconds(500d));
37+
if (state == HttpStatusCode.OK)
38+
{
39+
Console.WriteLine($"扫描到代理服务:{p.Host}:{p.Port}");
40+
}
41+
42+
var completed = Interlocked.Increment(ref completedProxyCount);
43+
Console.Title = $"扫描进度:{completed}/{Interlocked.Read(ref totalProxyCount)}";
44+
});
45+
await Task.WhenAll(tasks);
46+
}
47+
48+
49+
static async Task RunIUserApi(int loop = 1)
2250
{
2351
var watch = new Stopwatch();
2452
watch.Start();

0 commit comments

Comments
 (0)