proxies-scanner 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in proxies-scanner.gemspec
4
+ gemspec
5
+
6
+ gem 'work_queue'
7
+ gem 'nokogiri'
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Sam
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Proxies::Scanner
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'proxies-scanner'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install proxies-scanner
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,81 @@
1
+ #!/usr/bin/env ruby
2
+ #encoding: utf-8
3
+
4
+ require 'logger'
5
+
6
+ $:.unshift(File.dirname(__FILE__) + "/../lib")
7
+ require 'proxies-scanner'
8
+
9
+ include ProxiesScanner
10
+
11
+ $logger = Logger.new(STDOUT)
12
+ # $logger.level = Logger::DEBUG
13
+ # $logger.level = Logger::INFO
14
+ # $logger.level = Logger::ERROR
15
+ $logger.level = Logger::FATAL
16
+
17
+ QUEUE_SIZE_PROXIES = 15
18
+ QUEUE_SIZE_HTTP = 3
19
+
20
+ def scan_areas(areas)
21
+ proxies = []
22
+ wq = WorkQueue.new(QUEUE_SIZE_HTTP)
23
+
24
+ beginning_time = Time.now
25
+
26
+ i = 0
27
+ areas.each do |area|
28
+ wq.enqueue_b do
29
+ i += 1
30
+ begin
31
+ s_pos = " [#{i}/#{areas.size}]"
32
+ print "\033[36m ** HTTP/GET #{area.upcase} proxies #{s_pos.rjust(52, '.')}\033[0m\n"
33
+ proxies += Proxies.find_by_area(area)
34
+ rescue SignalException => e
35
+ raise e
36
+ rescue Exception => e
37
+ print "\033[31m~ Error: #{e.message}\033[0m\n"
38
+ end
39
+ end
40
+ end
41
+ wq.join
42
+ end_time = Time.now
43
+
44
+ print "\033[36m ** #{areas.size} areas scanned in #{((end_time - beginning_time)*1000).round(2)} ms \033[0m\n"
45
+ puts
46
+
47
+ return proxies
48
+ end
49
+
50
+
51
+ begin
52
+ proxies = []
53
+ if ARGV.size > 0
54
+ if ARGV[0] = '-f'
55
+ proxies = Proxies.from_file(ARGV[1])
56
+ else
57
+ proxies = scan_areas(ARGV)
58
+ end
59
+ else
60
+ proxies = scan_areas(Proxies::ALL_AREAS)
61
+ end
62
+
63
+ print "\033[32m ** #{proxies.size} proxies founded\033[0m\n"
64
+
65
+ wq = WorkQueue.new(QUEUE_SIZE_PROXIES)
66
+ proxies.each do |p|
67
+ wq.enqueue_b do
68
+ p.check
69
+ print p.format
70
+ end
71
+ end
72
+ wq.join
73
+
74
+ rescue SignalException => e
75
+ rescue Exception => e
76
+ puts 'Error:'
77
+ puts e.message
78
+ puts e.backtrace
79
+ end
80
+ puts
81
+ $logger.close
@@ -0,0 +1,68 @@
1
+
2
+
3
+ module ProxiesScanner
4
+ class Proxies
5
+
6
+ ALL_AREAS = [
7
+ 'us', 'fr', 'gb', 'au', 'ca', 'it', 'es', 'hu', 'ng', 'ch', 'se', 'br', 'id',
8
+ 'cn', 'fi', 'be', 'co', 'hr', 'ge', 'gr', 'ie', 'mx', 'mt', 'nl', 'pa', 'ph',
9
+ 'ro', 'sc', 'tw', 'tn', 'tr', 'ua', 've'
10
+ ]
11
+
12
+ def self.find_by_area(area, page=0)
13
+ $logger.info "Proxies.extract area=#{area}, page=#{page}"
14
+ proxies = []
15
+
16
+ doc = Nokogiri::HTML(open("http://www.proxylists.net/#{area.downcase}_#{page}.html"))
17
+ table = doc.search('table')[1]
18
+
19
+ table.css('tr').each do |tr|
20
+ td1 = tr.css('td')[0]
21
+ td2 = tr.css('td')[1]
22
+
23
+ unless td1.nil?
24
+ # extract host
25
+ js = td1.text.scan(/cape\('(.*)'\)\);/)[0]
26
+ unless js.nil?
27
+ js1 = js[0]
28
+ js2 = URI.decode(js1) # self.document.writeln(\"81.199.34.100\");
29
+ $logger.debug "Proxies.extract js2: #{js2}"
30
+ ip = js2.to_s.scan(/ln\("(.*)"\);/)[0][0].to_s
31
+ $logger.debug "Proxies.extract ip: #{ip}"
32
+ end
33
+
34
+ # extract port
35
+ unless td2.nil?
36
+ port = td2.text.strip.to_s.to_i || 8080
37
+ end
38
+
39
+ proxies << Proxy.new(ip, port, area) unless ip.nil?
40
+ end
41
+ end
42
+ return proxies
43
+ end
44
+
45
+ def self.from_file(filename)
46
+ proxies = []
47
+ file = nil
48
+ begin
49
+ file = File.open(filename, 'r+')
50
+ file.each_line do |line|
51
+ next if line[0] == '#'
52
+ s_ip = line.split(':')[0]
53
+ s_port = line.split(':')[1]
54
+ s_area = line.split(':')[2] || '--'
55
+ $logger.debug "From file extract s_ip: #{s_ip}"
56
+ $logger.debug "From file extract s_port: #{s_port}"
57
+ $logger.debug "From file extract s_area: #{s_area}"
58
+ proxies << Proxy.new(s_ip.strip, s_port.strip.to_i, s_area.strip)
59
+ end
60
+ rescue Exception => e
61
+ $logger.error "From file error: #{e.message}"
62
+ end
63
+ file.close unless file.nil?
64
+ return proxies
65
+ end
66
+ end
67
+
68
+ end
@@ -0,0 +1,84 @@
1
+
2
+ module ProxiesScanner
3
+ class Proxy
4
+ attr_accessor :ip, :port, :area, :status, :response_time
5
+
6
+ CHECK_URL = 'http://www.google.fr'
7
+ TIMEOUT = 10
8
+
9
+ STATUS_NOT_TESTED = "N.TESTED"
10
+ STATUS_OPEN = "OPEN"
11
+ STATUS_FILTERED = "FILTERED"
12
+ STATUS_TIMEOUT = "TIME OUT"
13
+ STATUS_SOCKERROR = "CLOSED"
14
+
15
+ STATUS_STR_MAX_LENGTH = STATUS_NOT_TESTED.length
16
+
17
+ def initialize(ip, port, area='--', status=STATUS_NOT_TESTED)
18
+ @ip, @port, @area, @status = ip, port, area, status
19
+ @response_time = -1
20
+ end
21
+
22
+ def to_s
23
+ "#{@ip}:#{@port} [#{@area}] [#{@status}]"
24
+ end
25
+
26
+ def format
27
+ ProxyFormater.fetch(self)
28
+ end
29
+
30
+ def check
31
+ $logger.debug "check proxy: #{self}"
32
+
33
+ uri = URI.parse(CHECK_URL)
34
+ Timeout::timeout(TIMEOUT) do
35
+
36
+ beginning_time = Time.now
37
+ http = Net::HTTP::Proxy @ip, @port
38
+ body = http.get_response(uri).body
39
+ end_time = Time.now
40
+
41
+ @response_time = (end_time - beginning_time)*1000
42
+
43
+ t = Nokogiri::HTML(body).search('title')
44
+ if !t.nil? && !t[0].nil? && t[0].text.downcase.include?('google')
45
+ $logger.debug "Proxy TITLE: #{t[0].text}"
46
+ $logger.debug 'Proxy open'
47
+ @status = Proxy::STATUS_OPEN
48
+ else
49
+ $logger.debug 'Proxy filtered'
50
+ @status = Proxy::STATUS_FILTERED
51
+ end
52
+ end
53
+
54
+ rescue SignalException => e
55
+ raise e
56
+ rescue Timeout::Error => e
57
+ @status = Proxy::STATUS_TIMEOUT
58
+ $logger.info "** Proxies.check: timeout: #{self}"
59
+ rescue Exception => e
60
+ @status = Proxy::STATUS_SOCKERROR
61
+ $logger.error "** Proxies.check: #{e.message} proxy=#{self}"
62
+ end
63
+
64
+ end
65
+
66
+ class ProxyFormater
67
+ SEPARATOR = ' '
68
+ COL_IP_SIZE = '255.255.255.255'.size
69
+ COL_PORT_SIZE = '99999'.size
70
+ COL_STATUS_SIZE = Proxy::STATUS_STR_MAX_LENGTH
71
+
72
+ def self.fetch(proxy)
73
+ s_color = "\033[31m"
74
+ s_color = "\033[32m" if proxy.status == Proxy::STATUS_OPEN
75
+ s_color = "\033[35m" if proxy.status == Proxy::STATUS_FILTERED
76
+ s_color = "\033[33m" if proxy.status == Proxy::STATUS_TIMEOUT
77
+
78
+ s_time = ""
79
+ s_time = "response-time: #{(proxy.response_time.round(2)).to_s[0..8].rjust(9)} (ms)" if proxy.response_time > 0
80
+ "%color #{proxy.ip.rjust(COL_IP_SIZE)}#{SEPARATOR}#{proxy.port.to_s.ljust(COL_PORT_SIZE)}#{SEPARATOR}[#{proxy.area.upcase}]#{SEPARATOR}[#{proxy.status.center(COL_STATUS_SIZE)}]#{SEPARATOR}#{s_time}\033[0m\n".gsub(/%color/, s_color)
81
+ end
82
+ end
83
+
84
+ end
@@ -0,0 +1,3 @@
1
+ module ProxiesScanner
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,15 @@
1
+
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'cgi'
5
+ require 'timeout'
6
+ require 'nokogiri'
7
+ require 'open-uri'
8
+ require 'work_queue'
9
+ require 'logger'
10
+
11
+ require "proxies-scanner/version"
12
+ require "proxies-scanner/proxies"
13
+ require "proxies-scanner/proxy"
14
+
15
+
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/proxies-scanner/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Sam"]
6
+ gem.email = ["samuel@pagedegeek.com"]
7
+ gem.description = %q{scan proxies list and check if they are valid}
8
+ gem.summary = %q{scan proxies list and check if they are valid}
9
+ gem.homepage = "http://www.github.com/pagedegeek/proxies-scanner"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "proxies-scanner"
15
+ gem.require_paths = ["lib", "bin"]
16
+ gem.version = ProxiesScanner::VERSION
17
+
18
+ gem.add_dependency "nokogiri"
19
+ gem.add_dependency "work_queue"
20
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: proxies-scanner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sam
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: work_queue
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: scan proxies list and check if they are valid
47
+ email:
48
+ - samuel@pagedegeek.com
49
+ executables:
50
+ - proxies-scanner
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - LICENSE
57
+ - README.md
58
+ - Rakefile
59
+ - bin/proxies-scanner
60
+ - lib/proxies-scanner.rb
61
+ - lib/proxies-scanner/proxies.rb
62
+ - lib/proxies-scanner/proxy.rb
63
+ - lib/proxies-scanner/version.rb
64
+ - proxies-scanner.gemspec
65
+ homepage: http://www.github.com/pagedegeek/proxies-scanner
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ - bin
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 1.8.24
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: scan proxies list and check if they are valid
90
+ test_files: []