hidemyass 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/hidemyass.rb CHANGED
@@ -1,5 +1,76 @@
1
+ require 'rubygems'
2
+ require 'nokogiri'
3
+ require 'open-uri'
4
+ require 'net/http'
5
+
1
6
  require "hidemyass/version"
2
7
 
3
8
  module Hidemyass
4
- # Your code goes here...
5
- end
9
+
10
+ HOST = 'hidemyass.com'
11
+ LOG_PREFIX = "** [hidemyass] "
12
+
13
+ HTTP_ERRORS = [Timeout::Error,
14
+ Errno::EINVAL,
15
+ Errno::ECONNRESET,
16
+ EOFError,
17
+ Net::HTTPBadResponse,
18
+ Net::HTTPHeaderSyntaxError,
19
+ Net::ProtocolError]
20
+
21
+ class << self
22
+ attr_reader :host, :port, :first_without_proxy, :proxies
23
+
24
+ def initialize(host = nil, port = nil, first_without_proxy = false, &block)
25
+ @host = host
26
+ @port = port
27
+ @first_without_proxy = first_without_proxy
28
+ end
29
+
30
+ def proxies
31
+ uri = URI.parse('http://%s/proxy-list/search-225729' % HOST)
32
+ dom = Nokogiri::HTML(open(uri))
33
+
34
+ @proxies ||= dom.xpath('//table[@id="listtable"]/tr').collect do |node|
35
+ { port: node.at_xpath('td[3]').content.strip,
36
+ host: node.at_xpath('td[2]/span').xpath('text() | *[not(contains(@style,"display:none"))]').
37
+ map(&:content).compact.join.to_s }
38
+ end
39
+
40
+ def request(host = @host, port = @port, first_without_proxy = @first_without_proxy)
41
+ return unless host && port
42
+
43
+ response = nil
44
+ if first_without_proxy
45
+ begin
46
+ response = Net::HTTP.start(host, port, &block)
47
+ rescue *HTTP_ERRORS => error
48
+ log :error, error
49
+ end
50
+ end
51
+
52
+ proxies.each do |proxy|
53
+ begin
54
+ response = Net::HTTP::Proxy(proxy[:host], proxy[:port]).start(host, port, &block)
55
+ break if response == Net::HTTPSuccess
56
+ rescue *HTTP_ERRORS => error
57
+ log :error, error
58
+ end
59
+ end
60
+
61
+ response
62
+ end
63
+
64
+ def logger
65
+ if defined?(Rails.logger)
66
+ Rails.logger
67
+ elsif defined?(RAILS_DEFAULT_LOGGER)
68
+ RAILS_DEFAULT_LOGGER
69
+ end
70
+ end
71
+
72
+ def log(level, message, response = nil)
73
+ logger.send level, LOG_PREFIX + message if logger
74
+ end
75
+ end
76
+ end
@@ -1,3 +1,3 @@
1
1
  module Hidemyass
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hidemyass
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: