11using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
24using System . Net ;
35using 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}
0 commit comments