public NullProxyProvider(Action <HttpClientHandler> configureClientHandlerAction = null) : base(configureClientHandlerAction) { // use only a direct connection ProxyClients.Add(new HttpClientProxy(CreateNewClient((IWebProxy)null), "baseconnection/none")); }
public override async Task InitializeAsync() { List <HttpClientProxy> proxies = new List <HttpClientProxy>(); if (JsonArray != null) { foreach (JObject obj in JsonArray) { string proxyType = obj["type"]?.Value <string>() ?? string.Empty; if (proxyType.Equals("http", StringComparison.OrdinalIgnoreCase)) { string url = obj["url"]?.Value <string>(); if (string.IsNullOrWhiteSpace(url)) { throw new Exception("Proxy URL must be specified and not empty."); } string username = obj["username"]?.Value <string>(); string password = obj["password"]?.Value <string>(); IWebProxy proxy = username != null ? new WebProxy(url, false, new string[0], new NetworkCredential(username, password)) : new WebProxy(url); proxies.Add(new HttpClientProxy(CreateNewClient(proxy), $"{username}@{url}")); } else if (proxyType.Equals("socks", StringComparison.OrdinalIgnoreCase)) { string url = obj["url"]?.Value <string>(); if (string.IsNullOrWhiteSpace(url)) { throw new Exception("Proxy URL must be specified and not empty."); } Uri uri = new Uri(url); string username = obj["username"]?.Value <string>(); string password = obj["password"]?.Value <string>(); var handler = new Socks5Handler(uri, username, password); handler.ResolveDnsLocally = ResolveDnsLocally; proxies.Add(new HttpClientProxy(CreateNewClient(handler), $"{username}@{uri.Host}:{uri.Port}")); } else { if (proxyType == string.Empty) { throw new Exception("Proxy type must be specified."); } throw new Exception($"Unknown proxy type: {proxyType}"); } } } // add a direct connection client too proxies.Add(new HttpClientProxy(CreateNewClient((IWebProxy)null), "baseconnection/none")); var testTasks = proxies.Select(proxy => Task.Run(async() => { bool success = true; for (int i = 0; i < 4; i++) { success = true; try { var result = await proxy.Client.GetAsync("https://a.4cdn.org/po/catalog.json"); if (!result.IsSuccessStatusCode) { success = false; } } catch (Exception ex) { Program.Log($"{proxy.Name} failed: {ex}", true); success = false; } if (success) { break; } } if (success) { Program.Log($"Proxy '{proxy.Name}' tested successfully"); ProxyClients.Add(proxy); Interlocked.Increment(ref _proxyCount); } else { Program.Log($"Proxy '{proxy.Name}' failed test, will be ignored"); } })); await Task.WhenAll(testTasks); }
/// <summary> /// Rents a <see cref="HttpClientProxy"/> object, encapsulated in a <see cref="PoolObject{HttpClientProxy}"/> object. /// </summary> /// <returns></returns> public virtual async Task <PoolObject <HttpClientProxy> > RentHttpClient() { return(new PoolObject <HttpClientProxy>(await ProxyClients.TakeAsync(), proxy => ProxyClients.AddAsync(proxy))); }
public ConfigProxyProvider(JArray jsonArray, Action <HttpClientHandler> configureClientHandlerAction = null) : base(configureClientHandlerAction) { if (jsonArray != null) { foreach (JObject obj in jsonArray) { string proxyType = obj["type"]?.Value <string>() ?? string.Empty; if (proxyType.Equals("http", StringComparison.OrdinalIgnoreCase)) { string url = obj["url"]?.Value <string>(); if (string.IsNullOrWhiteSpace(url)) { throw new Exception("Proxy URL must be specified and not empty."); } string username = obj["username"]?.Value <string>(); string password = obj["password"]?.Value <string>(); IWebProxy proxy = username != null ? new WebProxy(url, false, new string[0], new NetworkCredential(username, password)) : new WebProxy(url); ProxyClients.Add(new HttpClientProxy(CreateNewClient(proxy), $"{username}@{url}")); } else if (proxyType.Equals("socks", StringComparison.OrdinalIgnoreCase)) { string url = obj["url"]?.Value <string>(); if (string.IsNullOrWhiteSpace(url)) { throw new Exception("Proxy URL must be specified and not empty."); } Uri uri = new Uri(url); string username = obj["username"]?.Value <string>(); string password = obj["password"]?.Value <string>(); IWebProxy proxy = username != null ? new HttpToSocks5Proxy(uri.Host, uri.Port, username, password) : new HttpToSocks5Proxy(uri.Host, uri.Port); ProxyClients.Add(new HttpClientProxy(CreateNewClient(proxy), $"{username}@{uri.Host}")); } else { if (proxyType == string.Empty) { throw new Exception("Proxy type must be specified."); } throw new Exception($"Unknown proxy type: {proxyType}"); } } } // add a direct connection client too ProxyClients.Add(new HttpClientProxy(CreateNewClient(null), "baseconnection/none")); }