private void ClientOnProxyAdded(object sender, WebProxyHolder proxy) { if (this.ProxyAdded != null) { this.ProxyAdded(sender, proxy); } }
private void OnDownloaderBadProxyRemoved(object sender, int threadId, WebProxyHolder proxy) { if (this.DownloaderBadProxyRemoved != null) { this.DownloaderBadProxyRemoved(sender, threadId, proxy); } }
/// <summary> /// Hanvlde bad proxy voting /// </summary> private void HandleBadProxy(int threadIndex, WebProxyHolder proxy) { // If we use proxies might the proxy be bad! // or it might be the url, we vote for bad proxies // to be removed. if (proxy != null && this.useProxies) { logger.Debug("Down voting the proxy {0}.", proxy); if (!this.badProxy.ContainsKey(proxy)) { logger.Debug("First time down voting proxy {0}.", proxy); this.badProxy.GetOrAdd(proxy, 1); } else { this.badProxy[proxy]++; logger.Debug("Proxy {0} have failied {1} times now.", proxy, this.badProxy[proxy]); } if (this.badProxy[proxy] >= this.votesToRemoveProxy) { logger.Debug("Proxy {0} have failied {1} or more times, removing it.", proxy, this.votesToRemoveProxy); if (this.DownloaderBadProxyRemoved != null) { this.DownloaderBadProxyRemoved(this, threadIndex, proxy); } // Loop thoug all the proxies and requirie them again as long as they are not // the bad proxie WebProxyHolder current = null; // To ensure the proxie have not been removed and we start a never ending loop logger.Trace("Ensuring proxy {0} have been removed from the queue.", proxy); int i = this.proxies.Count; while (current != proxy && i >= 0) { // Add the last current, as that is not the bad proxy if (current != null) { this.proxies.Enqueue(current); } else { // The current proxy do not requque it } this.proxies.TryDequeue(out current); i--; } } } }
/// <summary> /// Find a proxy for the WebClient to use /// </summary> /// <returns>Return true if continue, false if there are no proxies</returns> private WebProxy HandleAddProxy(out WebProxyHolder proxy) { // If any proxies exists, dequery the next in line and use it proxy = null; if (!this.useProxies) { return(null); } // No proxies avalible if (this.proxies.Count == 0) { if (this.NoGoodProxyEventFired) { logger.Debug("No good proxies already fired."); // No Good proxies, user will ensure it is handeled correctly. return(null); } // Bad no good proxies left logger.Debug("No good proxies found, and it is the first time."); this.NoGoodProxyEventFired = true; if (this.dieOnProxiesLeft) { this.stopThread = true; } if (this.DownloaderNoGoodProxyLeft != null) { this.DownloaderNoGoodProxyLeft(this); } // No Good proxies, user will ensure it is handeled correctly. return(null); } WebProxy p = null; if (this.proxies.TryDequeue(out proxy)) { logger.Trace("Found proxy {0}.", proxy); this.proxies.Enqueue(proxy); return(new WebProxy(proxy.Ip, proxy.Port)); } logger.Error("Was unable to dequeue a proxy."); // No Good proxies, user will ensure it is handeled correctly. return(null); }
public void RunAsync() { while (executeScan) { logger.Debug("Checking \"{0}\" for new proxy files.", directory.FullName); var files = directory.GetFiles(); logger.Debug("Found {0} files.", files.Length); foreach (var file in files) { try { logger.Info("Loading proxy file \"{0}\".", file.Name); var lines = File.ReadAllLines(file.FullName); int added = 0; foreach (var line in lines) { try { var split = line.Split(new [] { ',', ':' }); var proxyHolder = new WebProxyHolder(); proxyHolder.Ip = split[0]; proxyHolder.Port = int.Parse(split[1]); if (split.Length >= 3) { proxyHolder.Country = split[2]; } this.downloader.AddProxies(new List <WebProxyHolder>() { proxyHolder }); added++; } catch (Exception exp) { logger.Error(exp, "Failed to load proxy line \"{0}\".", line); } } if (added > 0) { logger.Debug("Deleting \"{0}\" the {1} proxies have been loaded.", file.Name, added); File.Delete(file.FullName); } else { logger.Debug("Did not find any proxies in \"{0}\".", file.Name); } } catch (Exception exp) { logger.Error(exp, "Failed to load proxies from \"{0}\".", file.Name); } } if (!this.downloader.IsRunning() && this.downloader.GetCurrentProxyCount() > this.minimumProxies) { logger.Info("Downloader is stopped, and enough proxies have been added to restart the downloader."); this.downloader.Start(); } Thread.Sleep(this.ScanInterval); } }
private void DownloaderOnDownloaderBadProxyRemoved(object sender, int threadId, WebProxyHolder proxy) { CheckIfToStopProxies(); }