Class: HttpUtilities::Proxies::ProxyChecker
- Inherits:
-
Object
- Object
- HttpUtilities::Proxies::ProxyChecker
- Defined in:
- lib/http_utilities/proxies/proxy_checker.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#limit ⇒ Object
Returns the value of attribute limit.
-
#maximum_failed_attempts ⇒ Object
Returns the value of attribute maximum_failed_attempts.
-
#minimum_successful_attempts ⇒ Object
Returns the value of attribute minimum_successful_attempts.
-
#processed_proxies ⇒ Object
Returns the value of attribute processed_proxies.
Instance Method Summary collapse
- #check_and_update_proxies(protocol: :all, proxy_type: :all, mode: :synchronous, maximum_failed_attempts: self.maximum_failed_attempts) ⇒ Object
- #check_http_proxy(proxy, timeout = 60) ⇒ Object
- #check_proxies(protocol: :all, proxy_type: :all, mode: :synchronous, maximum_failed_attempts: self.maximum_failed_attempts) ⇒ Object
- #check_proxy(proxy) ⇒ Object
- #check_socks_proxy(proxy, test_host: "whois.verisign-grs.com", test_port: 43, test_query: "=google.com") ⇒ Object
-
#initialize ⇒ ProxyChecker
constructor
A new instance of ProxyChecker.
- #update_proxies ⇒ Object
Constructor Details
#initialize ⇒ ProxyChecker
Returns a new instance of ProxyChecker.
13 14 15 16 17 18 19 20 |
# File 'lib/http_utilities/proxies/proxy_checker.rb', line 13 def initialize self.client = HttpUtilities::Http::Client.new self.processed_proxies = [] self.limit = 1000 self.minimum_successful_attempts = 1 self.maximum_failed_attempts = 10 end |
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
10 11 12 |
# File 'lib/http_utilities/proxies/proxy_checker.rb', line 10 def client @client end |
#limit ⇒ Object
Returns the value of attribute limit.
11 12 13 |
# File 'lib/http_utilities/proxies/proxy_checker.rb', line 11 def limit @limit end |
#maximum_failed_attempts ⇒ Object
Returns the value of attribute maximum_failed_attempts.
11 12 13 |
# File 'lib/http_utilities/proxies/proxy_checker.rb', line 11 def maximum_failed_attempts @maximum_failed_attempts end |
#minimum_successful_attempts ⇒ Object
Returns the value of attribute minimum_successful_attempts.
11 12 13 |
# File 'lib/http_utilities/proxies/proxy_checker.rb', line 11 def minimum_successful_attempts @minimum_successful_attempts end |
#processed_proxies ⇒ Object
Returns the value of attribute processed_proxies.
10 11 12 |
# File 'lib/http_utilities/proxies/proxy_checker.rb', line 10 def processed_proxies @processed_proxies end |
Instance Method Details
#check_and_update_proxies(protocol: :all, proxy_type: :all, mode: :synchronous, maximum_failed_attempts: self.maximum_failed_attempts) ⇒ Object
22 23 24 25 |
# File 'lib/http_utilities/proxies/proxy_checker.rb', line 22 def check_and_update_proxies(protocol: :all, proxy_type: :all, mode: :synchronous, maximum_failed_attempts: self.maximum_failed_attempts) check_proxies(protocol: protocol, proxy_type: proxy_type, mode: mode, maximum_failed_attempts: maximum_failed_attempts) update_proxies end |
#check_http_proxy(proxy, timeout = 60) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/http_utilities/proxies/proxy_checker.rb', line 90 def check_http_proxy(proxy, timeout = 60) document = nil valid_proxy = false = {method: :net_http, use_proxy: true, proxy: proxy.proxy_address, proxy_protocol: proxy.protocol, timeout: timeout, maximum_redirects: 1, disable_auth: true } Rails.logger.info "#{Time.now}: Fetching Google.com with proxy #{proxy.proxy_address}." response = self.client.retrieve_parsed_html("http://www.google.com/webhp?hl=en", ) if (response && response.parsed_body) title = response.parsed_body.css("title").first if (title && title.content) begin title = title.content.encode("UTF-8").strip.downcase body_content = response.parsed_body.content.to_s.encode("UTF-8").strip.downcase valid_proxy = (title.eql?("google") || !(body_content =~ /google home/i).nil?) Rails.logger.info "Title is: #{title}. Proxy #{proxy.proxy_address}" rescue Exception => e Rails.logger.error "Exception occured while trying to check proxy #{proxy.proxy_address}. Error Class: #{e.class}. Error Message: #{e.message}" valid_proxy = false end end end if (valid_proxy) Rails.logger.info "#{Time.now}: Proxy #{proxy.proxy_address} is working!" else Rails.logger.info "#{Time.now}: Proxy #{proxy.proxy_address} is not working!" end self.processed_proxies << {proxy: proxy, valid: valid_proxy} end |
#check_proxies(protocol: :all, proxy_type: :all, mode: :synchronous, maximum_failed_attempts: self.maximum_failed_attempts) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/http_utilities/proxies/proxy_checker.rb', line 27 def check_proxies(protocol: :all, proxy_type: :all, mode: :synchronous, maximum_failed_attempts: self.maximum_failed_attempts) proxies = Proxy.should_be_checked( protocol: protocol, proxy_type: proxy_type, date: Time.now, limit: self.limit, maximum_failed_attempts: maximum_failed_attempts ) if (proxies && proxies.any?) Rails.logger.info "Found #{proxies.size} #{proxy_type} proxies to check." proxies.each do |proxy| case mode when :synchronous check_proxy(proxy) when :resque Resque.enqueue(HttpUtilities::Jobs::Resque::Proxies::CheckProxyJob, proxy.id) when :sidekiq HttpUtilities::Jobs::Sidekiq::Proxies::CheckProxyJob.perform_async(proxy.id) end end else Rails.logger.info "Couldn't find any proxies to check!" end end |
#check_proxy(proxy) ⇒ Object
55 56 57 58 59 |
# File 'lib/http_utilities/proxies/proxy_checker.rb', line 55 def check_proxy(proxy) Rails.logger.info "#{Time.now}: Will check if proxy #{proxy.proxy_address} is working." self.send("check_#{proxy.protocol}_proxy", proxy) end |
#check_socks_proxy(proxy, test_host: "whois.verisign-grs.com", test_port: 43, test_query: "=google.com") ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/http_utilities/proxies/proxy_checker.rb', line 61 def check_socks_proxy(proxy, test_host: "whois.verisign-grs.com", test_port: 43, test_query: "=google.com") valid_proxy = false begin socks_proxy = Net::SSH::Proxy::SOCKS5.new(proxy.host, proxy.port, proxy.socks_proxy_credentials) client = socks_proxy.open(test_host, test_port) client.write("#{test_query}\r\n") response = client.read puts "SOCKS RESPONSE:\n" puts response valid_proxy = (response && response.present?) rescue StandardError => e Rails.logger.error "Exception occured while trying to check proxy #{proxy.proxy_address}. Error Class: #{e.class}. Error Message: #{e.message}" valid_proxy = false end if (valid_proxy) Rails.logger.info "#{Time.now}: Proxy #{proxy.proxy_address} is working!" else Rails.logger.info "#{Time.now}: Proxy #{proxy.proxy_address} is not working!" end self.processed_proxies << {proxy: proxy, valid: valid_proxy} end |
#update_proxies ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/http_utilities/proxies/proxy_checker.rb', line 135 def update_proxies columns = [:host, :port, :last_checked_at, :valid_proxy, :successful_attempts, :failed_attempts] values = [] Rails.logger.info "Updating/Importing #{self.processed_proxies.size} proxies" if (self.processed_proxies && self.processed_proxies.any?) self.processed_proxies.each do |value| proxy = value[:proxy] valid = value[:valid] successful_attempts = proxy.successful_attempts failed_attempts = proxy.failed_attempts if (valid) successful_attempts += 1 else failed_attempts += 1 end is_valid = (successful_attempts >= self.minimum_successful_attempts && failed_attempts < self.maximum_failed_attempts) value_arr = [proxy.host, proxy.port, Time.now, is_valid, successful_attempts, failed_attempts] values << value_arr end ::Proxy.import(columns, values, :on_duplicate_key_update => [:last_checked_at, :valid_proxy, :successful_attempts, :failed_attempts], :validate => false) end end |