Skip to content

Commit bde0ce4

Browse files
author
Kirill Platonov
committed
fix issue #1 - remove tcp ping and add http-checking of proxy
1 parent f4e9846 commit bde0ce4

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

lib/proxy_manager/proxy.rb

+44-5
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,35 @@ def initialize(proxies)
1313
end
1414
end
1515

16-
def connectable?(proxy)
17-
proxy = proxy.chomp.split(':') if proxy.is_a? String
18-
Net::Ping::TCP.new(proxy[0], proxy[1].to_i).ping
16+
def get(count = 1)
17+
raise 'List is empty' if @list.empty?
18+
19+
items = []
20+
new_list = @list.clone
21+
22+
@list.each_with_index do |proxy, key|
23+
new_list.shift
24+
new_list << proxy
25+
26+
if count == 1
27+
items = proxy
28+
break
29+
else
30+
items << proxy
31+
break if items.size == count
32+
end
33+
end
34+
35+
@list = new_list
36+
37+
raise 'There are no available proxy' if items.empty?
38+
39+
save_to_file(@list_file, @list) if @list_file
40+
41+
items
1942
end
2043

21-
def get(count = 1)
44+
def get!(count = 1)
2245
raise 'List is empty' if @list.empty?
2346

2447
items = []
@@ -27,7 +50,7 @@ def get(count = 1)
2750
@list.each_with_index do |proxy, key|
2851
new_list.shift
2952

30-
if connectable? proxy
53+
if self.class.connectable? proxy
3154
new_list << proxy
3255

3356
if count == 1
@@ -49,6 +72,22 @@ def get(count = 1)
4972
items
5073
end
5174

75+
def self.connectable?(proxy)
76+
proxy = proxy.chomp.split(':') if proxy.is_a? String
77+
ip, port = proxy
78+
connection = Net::HTTP.new("http://google.com", nil, ip, port)
79+
connection.open_timeout = 3
80+
connection.read_timeout = 3
81+
82+
connection.start do |http|
83+
return true if http.get('/')
84+
end
85+
86+
false
87+
rescue Exception => e
88+
false
89+
end
90+
5291
private
5392

5493
def load_list_from_array(proxies)

0 commit comments

Comments
 (0)