Skip to content

Commit 17a97aa

Browse files
committed
HttpProxy增加Range方法
1 parent 792b15d commit 17a97aa

File tree

3 files changed

+106
-3
lines changed

3 files changed

+106
-3
lines changed

WebApiClient/HttpProxy.cs

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
24
using System.Net;
35
using System.Text;
46

@@ -193,7 +195,7 @@ public string ToTunnelRequestString(Uri targetAddress)
193195
/// <returns></returns>
194196
public Uri GetProxy(Uri destination)
195197
{
196-
return new Uri($"http://{this.Host}:{this.Port}/");
198+
return new Uri(this.ToString());
197199
}
198200

199201
/// <summary>
@@ -205,5 +207,92 @@ public bool IsBypassed(Uri host)
205207
{
206208
return false;
207209
}
210+
211+
/// <summary>
212+
/// 转换为字符串
213+
/// </summary>
214+
/// <returns></returns>
215+
public override string ToString()
216+
{
217+
return $"http://{this.Host}:{this.Port}/";
218+
}
219+
220+
221+
/// <summary>
222+
/// 生成http代理服务器范围
223+
/// </summary>
224+
/// <param name="start">代理服务器起始ip</param>
225+
/// <param name="port">代理服务器端口</param>
226+
/// <param name="count">ip数量</param>
227+
/// <exception cref="ArgumentNullException"></exception>
228+
/// <returns></returns>
229+
public static IEnumerable<HttpProxy> Range(IPAddress start, int port, int count)
230+
{
231+
if (start == null)
232+
{
233+
throw new ArgumentNullException(nameof(start));
234+
}
235+
236+
foreach (var ip in GetIPAddressRange(start, count))
237+
{
238+
yield return new HttpProxy(ip.ToString(), port);
239+
}
240+
}
241+
242+
/// <summary>
243+
/// 返回ip范围
244+
/// </summary>
245+
/// <param name="start">起始ip</param>
246+
/// <param name="count">ip数量</param>
247+
/// <returns></returns>
248+
private static IEnumerable<IPAddress> GetIPAddressRange(IPAddress start, int count)
249+
{
250+
var c = 0;
251+
var ip = start;
252+
253+
while (c++ < count)
254+
{
255+
yield return ip;
256+
var next = IPAddressToInt32(ip) + 1;
257+
ip = Int32ToIPAddress(next);
258+
}
259+
}
260+
261+
/// <summary>
262+
/// ip转换为int
263+
/// </summary>
264+
/// <param name="ip"></param>
265+
/// <returns></returns>
266+
private static int IPAddressToInt32(IPAddress ip)
267+
{
268+
var bytes = ip.GetAddressBytes();
269+
if (BitConverter.IsLittleEndian == true)
270+
{
271+
return BitConverter.ToInt32(bytes.Reverse().ToArray(), 0);
272+
}
273+
else
274+
{
275+
return BitConverter.ToInt32(bytes, 0);
276+
}
277+
}
278+
279+
/// <summary>
280+
/// int转换为ip
281+
/// </summary>
282+
/// <param name="value">值</param>
283+
/// <returns></returns>
284+
private static IPAddress Int32ToIPAddress(int value)
285+
{
286+
if (BitConverter.IsLittleEndian == true)
287+
{
288+
var bytes = BitConverter.GetBytes(value).Reverse().ToArray();
289+
return new IPAddress(bytes);
290+
}
291+
else
292+
{
293+
var bytes = BitConverter.GetBytes(value);
294+
return new IPAddress(bytes);
295+
}
296+
}
208297
}
209298
}

WebApiClient/WebApiClient.csproj

6 Bytes
Binary file not shown.

WebApiClientTest/HttpProxyTest.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Net;
45
using System.Text;
56
using WebApiClient;
@@ -36,10 +37,14 @@ public void CtorTest()
3637
Assert.True(proxy.Password == "123456");
3738
Assert.True(((IWebProxy)proxy).Credentials.GetCredential(null, null).UserName == "laojiu");
3839
Assert.True(((IWebProxy)proxy).Credentials.GetCredential(null, null).Password == "123456");
40+
}
3941

40-
42+
[Fact]
43+
public void FromWebProxyTest()
44+
{
45+
var target = new Uri("https://www.baidu.com/");
4146
var p = new WebProxy("127.0.0.1", 5000) { Credentials = new NetworkCredential("abc", "123") };
42-
proxy = HttpProxy.FromWebProxy(p, target);
47+
var proxy = HttpProxy.FromWebProxy(p, target);
4348
Assert.True(proxy.Host == "127.0.0.1" && proxy.Port == 5000);
4449
Assert.True(proxy.GetProxy(target) == new Uri("http://127.0.0.1:5000"));
4550
Assert.True(proxy.UserName == "abc");
@@ -48,5 +53,14 @@ public void CtorTest()
4853
Assert.True(((IWebProxy)proxy).Credentials.GetCredential(null, null).Password == "123");
4954

5055
}
56+
57+
[Fact]
58+
public void RangeTest()
59+
{
60+
var proxys = HttpProxy.Range(IPAddress.Parse("221.122.0.1"), 8080, 5).ToArray();
61+
Assert.True(proxys.Length == 5);
62+
Assert.True(proxys.First().Host == "221.122.0.1");
63+
Assert.True(proxys.Last().Host == "221.122.0.5");
64+
}
5165
}
5266
}

0 commit comments

Comments
 (0)