private ProxiedConnection(Socket socket, IPEndPoint endPoint, bool isIncoming) { this.socket = socket; this.endPoint = endPoint; this.isIncoming = isIncoming; if (Username == null) this.proxyClient = new Socks5ProxyClient(ProxyHostname, ProxyPort); else this.proxyClient = new Socks5ProxyClient(ProxyHostname, ProxyPort, Username, Password); proxyClient.CreateConnectionAsyncCompleted += CreateConnectionAsyncCompleted; pendingOperations = new ConcurrentQueue<ProxyConnectionResult>(); }
public static ProxyWithType CheckProxy(string IP, int port, int timeout) { string connectionTestIPAddress = ConfigurationManager.AppSettings["ConnectionTestIP"]; string connectionTestWebAddress = ConfigurationManager.AppSettings["ConnectionTestWebAddress"]; string connectionTestPort = ConfigurationManager.AppSettings["ConnectionTestPort"]; ProxyWithType proxyWithType = new ProxyWithType(); proxyWithType.IP = IP; proxyWithType.Port = port.ToString(); proxyWithType.ProxyType = Model.ProxyType.Dead; TaskFactory taskFactory = new TaskFactory(); List<Task> tasks = new List<Task>(); tasks.Add(taskFactory.StartNew(delegate { try { Socks4ProxyClient proxyClient4 = new Socks4ProxyClient(IP, port); proxyClient4.CreateConnection(connectionTestIPAddress, int.Parse(connectionTestPort)); proxyWithType.ProxyType = Model.ProxyType.Socks4; } catch (Exception) { //If proxy timeouts don't do anything } })); tasks.Add(taskFactory.StartNew(delegate { try { Socks5ProxyClient proxyClient5 = new Socks5ProxyClient(IP, port); proxyClient5.CreateConnection(connectionTestIPAddress, int.Parse(connectionTestPort)); proxyWithType.ProxyType = Model.ProxyType.Socks5; } catch (Exception) { //If proxy timeouts don't do anything } })); tasks.Add(taskFactory.StartNew(delegate { try { WebClient wc = new WebClient(); wc.Proxy = new WebProxy(IP, port); wc.DownloadString(connectionTestWebAddress); proxyWithType.ProxyType = Model.ProxyType.HTTP; } catch (Exception) { //If proxy timeouts don't do anything } })); if (timeout > 0) Task.WaitAll(tasks.ToArray(), timeout); else Task.WaitAll(tasks.ToArray()); return proxyWithType; }