Class: ProxiesScanner::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/proxies-scanner/proxy.rb

Constant Summary collapse

CHECK_URL =
'http://www.google.fr'
TIMEOUT =
10
STATUS_NOT_TESTED =
"N.TESTED"
STATUS_OPEN =
"OPEN"
STATUS_FILTERED =
"FILTERED"
STATUS_TIMEOUT =
"TIME OUT"
STATUS_SOCKERROR =
"CLOSED"
STATUS_STR_MAX_LENGTH =
STATUS_NOT_TESTED.length

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip, port, area = '--', status = STATUS_NOT_TESTED) ⇒ Proxy

Returns a new instance of Proxy.



17
18
19
20
# File 'lib/proxies-scanner/proxy.rb', line 17

def initialize(ip, port, area='--', status=STATUS_NOT_TESTED)
  @ip, @port, @area, @status = ip, port, area, status
  @response_time = -1
end

Instance Attribute Details

#areaObject

Returns the value of attribute area.



4
5
6
# File 'lib/proxies-scanner/proxy.rb', line 4

def area
  @area
end

#ipObject

Returns the value of attribute ip.



4
5
6
# File 'lib/proxies-scanner/proxy.rb', line 4

def ip
  @ip
end

#portObject

Returns the value of attribute port.



4
5
6
# File 'lib/proxies-scanner/proxy.rb', line 4

def port
  @port
end

#response_timeObject

Returns the value of attribute response_time.



4
5
6
# File 'lib/proxies-scanner/proxy.rb', line 4

def response_time
  @response_time
end

#statusObject

Returns the value of attribute status.



4
5
6
# File 'lib/proxies-scanner/proxy.rb', line 4

def status
  @status
end

Instance Method Details

#checkObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/proxies-scanner/proxy.rb', line 30

def check
  $logger.debug "check proxy: #{self}"

  uri = URI.parse(CHECK_URL)
  Timeout::timeout(TIMEOUT) do

    beginning_time = Time.now
    http = Net::HTTP::Proxy @ip, @port
    body = http.get_response(uri).body
    end_time = Time.now

    @response_time = (end_time - beginning_time)*1000

    t = Nokogiri::HTML(body).search('title')
    if !t.nil? && !t[0].nil? && t[0].text.downcase.include?('google')
      $logger.debug "Proxy TITLE: #{t[0].text}"
      $logger.debug 'Proxy open'
      @status = Proxy::STATUS_OPEN
    else
      $logger.debug 'Proxy filtered'
      @status = Proxy::STATUS_FILTERED
    end
  end

rescue SignalException => e
  raise e
rescue Timeout::Error => e
  @status = Proxy::STATUS_TIMEOUT
  $logger.info "** Proxies.check: timeout: #{self}"
rescue Exception => e
  @status = Proxy::STATUS_SOCKERROR
  $logger.error "** Proxies.check: #{e.message} proxy=#{self}"
end

#formatObject



26
27
28
# File 'lib/proxies-scanner/proxy.rb', line 26

def format
  ProxyFormater.fetch(self)
end

#to_sObject



22
23
24
# File 'lib/proxies-scanner/proxy.rb', line 22

def to_s
  "#{@ip}:#{@port} [#{@area}] [#{@status}]" 
end