Class: Proxied::Checker
- Inherits:
-
Object
- Object
- Proxied::Checker
- Defined in:
- lib/proxied/checker.rb
Instance Attribute Summary collapse
-
#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.
-
#mode ⇒ Object
Returns the value of attribute mode.
Instance Method Summary collapse
- #check_http_proxy(proxy, test_url: ::Proxied.configuration.http_test[:url], evaluate: ::Proxied.configuration.http_test[:evaluate], timeout: ::Proxied.configuration.http_test[:timeout], update: true) ⇒ Object
- #check_proxies(protocol: :all, proxy_type: :all, category: nil, update: true) ⇒ Object
- #check_proxy(proxy, update: true) ⇒ Object
- #check_socks_proxy(proxy, test_host: ::Proxied.configuration.socks_test[:hostname], test_port: ::Proxied.configuration.socks_test[:port], test_query: ::Proxied.configuration.socks_test[:query], timeout: ::Proxied.configuration.socks_test[:timeout], update: true) ⇒ Object
-
#initialize(mode: :synchronous, minimum_successful_attempts: ::Proxied.configuration.minimum_successful_attempts, maximum_failed_attempts: ::Proxied.configuration.maximum_failed_attempts, limit: nil) ⇒ Checker
constructor
A new instance of Checker.
- #update_proxy(proxy, valid, response = nil) ⇒ Object
Constructor Details
#initialize(mode: :synchronous, minimum_successful_attempts: ::Proxied.configuration.minimum_successful_attempts, maximum_failed_attempts: ::Proxied.configuration.maximum_failed_attempts, limit: nil) ⇒ Checker
Returns a new instance of Checker.
7 8 9 10 11 12 |
# File 'lib/proxied/checker.rb', line 7 def initialize(mode: :synchronous, minimum_successful_attempts: ::Proxied.configuration.minimum_successful_attempts, maximum_failed_attempts: ::Proxied.configuration.maximum_failed_attempts, limit: nil) self.mode = mode self.minimum_successful_attempts = minimum_successful_attempts self.maximum_failed_attempts = maximum_failed_attempts self.limit = limit end |
Instance Attribute Details
#limit ⇒ Object
Returns the value of attribute limit.
5 6 7 |
# File 'lib/proxied/checker.rb', line 5 def limit @limit end |
#maximum_failed_attempts ⇒ Object
Returns the value of attribute maximum_failed_attempts.
4 5 6 |
# File 'lib/proxied/checker.rb', line 4 def maximum_failed_attempts @maximum_failed_attempts end |
#minimum_successful_attempts ⇒ Object
Returns the value of attribute minimum_successful_attempts.
4 5 6 |
# File 'lib/proxied/checker.rb', line 4 def minimum_successful_attempts @minimum_successful_attempts end |
#mode ⇒ Object
Returns the value of attribute mode.
3 4 5 |
# File 'lib/proxied/checker.rb', line 3 def mode @mode end |
Instance Method Details
#check_http_proxy(proxy, test_url: ::Proxied.configuration.http_test[:url], evaluate: ::Proxied.configuration.http_test[:evaluate], timeout: ::Proxied.configuration.http_test[:timeout], update: true) ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/proxied/checker.rb', line 72 def check_http_proxy(proxy, test_url: ::Proxied.configuration.http_test[:url], evaluate: ::Proxied.configuration.http_test[:evaluate], timeout: ::Proxied.configuration.http_test[:timeout], update: true) ::Proxied::Logger.log "#{Time.now}: Fetching #{::Proxied.configuration.http_test[:url]} with proxy #{proxy.proxy_address} (#{proxy.ip_address})." response = request(test_url, proxy, options: {timeout: timeout}) valid_proxy = evaluate.call(proxy, response) update_proxy(proxy, valid_proxy, response) if update return valid_proxy end |
#check_proxies(protocol: :all, proxy_type: :all, category: nil, update: true) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/proxied/checker.rb', line 14 def check_proxies(protocol: :all, proxy_type: :all, category: nil, update: true) proxies = ::Proxied.configuration.proxy_class.constantize.should_be_checked( mode: self.mode.to_sym, protocol: protocol, proxy_type: proxy_type, category: category, date: Time.now, limit: self.limit, maximum_failed_attempts: self.maximum_failed_attempts ) if proxies&.any? ::Proxied::Logger.log "Found #{proxies.size} #{proxy_type} proxies to check." proxies.each do |proxy| case self.mode.to_sym when :synchronous check_proxy(proxy, update: update) when :asynchronous ::Proxied::Jobs::CheckProxyJob.perform_async(proxy.id.to_s) end end else ::Proxied::Logger.log "Couldn't find any proxies to check!" end end |
#check_proxy(proxy, update: true) ⇒ Object
42 43 44 45 46 |
# File 'lib/proxied/checker.rb', line 42 def check_proxy(proxy, update: true) ::Proxied::Logger.log "#{Time.now}: Will check if proxy #{proxy.proxy_address} is working." self.send("check_#{proxy.protocol}_proxy", proxy, update: update) end |
#check_socks_proxy(proxy, test_host: ::Proxied.configuration.socks_test[:hostname], test_port: ::Proxied.configuration.socks_test[:port], test_query: ::Proxied.configuration.socks_test[:query], timeout: ::Proxied.configuration.socks_test[:timeout], update: true) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/proxied/checker.rb', line 48 def check_socks_proxy(proxy, test_host: ::Proxied.configuration.socks_test[:hostname], test_port: ::Proxied.configuration.socks_test[:port], test_query: ::Proxied.configuration.socks_test[:query], timeout: ::Proxied.configuration.socks_test[:timeout], update: true) 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, {timeout: timeout}) client.write("#{test_query}\r\n") response = client.read valid_proxy = !response.to_s.empty? client.close rescue StandardError => e ::Proxied::Logger.log "Exception occured while trying to check proxy #{proxy.proxy_address}. Error Class: #{e.class}. Error Message: #{e.message}" valid_proxy = false end update_proxy(proxy, valid_proxy) if update return valid_proxy end |
#update_proxy(proxy, valid, response = nil) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/proxied/checker.rb', line 83 def update_proxy(proxy, valid, response = nil) ::Proxied::Logger.info "#{Time.now}: Proxy #{proxy.proxy_address} (#{proxy.ip_address}) is #{valid ? "working" : "not working"}!" ::Proxied::Logger.debug "Response: #{response}" if !valid && response successful_attempts = proxy.successful_attempts || 0 failed_attempts = proxy.failed_attempts || 0 if valid successful_attempts += 1 else failed_attempts += 1 end is_valid = (successful_attempts >= self.minimum_successful_attempts && failed_attempts < self.maximum_failed_attempts) proxy.valid_proxy = is_valid proxy.successful_attempts = successful_attempts proxy.failed_attempts = failed_attempts proxy.last_checked_at = Time.now proxy.save end |