@@ -13,12 +13,35 @@ def initialize(proxies)
13
13
end
14
14
end
15
15
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
19
42
end
20
43
21
- def get ( count = 1 )
44
+ def get! ( count = 1 )
22
45
raise 'List is empty' if @list . empty?
23
46
24
47
items = [ ]
@@ -27,7 +50,7 @@ def get(count = 1)
27
50
@list . each_with_index do |proxy , key |
28
51
new_list . shift
29
52
30
- if connectable? proxy
53
+ if self . class . connectable? proxy
31
54
new_list << proxy
32
55
33
56
if count == 1
@@ -49,6 +72,22 @@ def get(count = 1)
49
72
items
50
73
end
51
74
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
+
52
91
private
53
92
54
93
def load_list_from_array ( proxies )
0 commit comments