@@ -14,34 +14,45 @@ def initialize(proxies)
14
14
end
15
15
16
16
def get ( count = 1 )
17
- raise 'List is empty' if @list . empty?
17
+ get_proxy ( count )
18
+ end
18
19
19
- items = [ ]
20
- new_list = @list . clone
20
+ def get! ( count = 1 )
21
+ get_proxy ( count , true )
22
+ end
21
23
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
24
+ def self . connectable? ( proxy )
25
+ proxy = proxy . chomp . split ( ':' ) if proxy . is_a? String
26
+ ip , port = proxy
27
+ connection = Net ::HTTP . new ( "http://google.com" , nil , ip , port )
28
+ connection . open_timeout = 3
29
+ connection . read_timeout = 3
30
+
31
+ connection . start do |http |
32
+ return true if http . get ( '/' )
33
33
end
34
34
35
- @list = new_list
35
+ false
36
+ rescue Exception => e
37
+ false
38
+ end
36
39
37
- raise 'There are no available proxy' if items . empty?
40
+ private
38
41
39
- save_to_file ( @list_file , @list ) if @list_file
42
+ def load_list_from_array ( proxies )
43
+ @list = proxies . map { |arg | [ arg . split ( ':' ) [ 0 ] , arg . split ( ':' ) [ 1 ] . to_i ] }
44
+ end
40
45
41
- items
46
+ def load_from_file ( file )
47
+ result = [ ]
48
+ IO . readlines ( file ) . each do |line |
49
+ ip , port = line . chomp . split ( ':' )
50
+ result << [ ip , port . to_i ] if ip . is_a? ( String ) && port . is_a? ( String )
51
+ end
52
+ result
42
53
end
43
54
44
- def get! ( count = 1 )
55
+ def get_proxy ( count , check_connection = false )
45
56
raise 'List is empty' if @list . empty?
46
57
47
58
items = [ ]
@@ -50,7 +61,7 @@ def get!(count = 1)
50
61
@list . each_with_index do |proxy , key |
51
62
new_list . shift
52
63
53
- if self . class . connectable? proxy
64
+ if ! check_connection || self . class . connectable? ( proxy )
54
65
new_list << proxy
55
66
56
67
if count == 1
@@ -72,37 +83,6 @@ def get!(count = 1)
72
83
items
73
84
end
74
85
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
-
91
- private
92
-
93
- def load_list_from_array ( proxies )
94
- @list = proxies . map { |arg | [ arg . split ( ':' ) [ 0 ] , arg . split ( ':' ) [ 1 ] . to_i ] }
95
- end
96
-
97
- def load_from_file ( file )
98
- result = [ ]
99
- IO . readlines ( file ) . each do |line |
100
- ip , port = line . chomp . split ( ':' )
101
- result << [ ip , port . to_i ] if ip . is_a? ( String ) && port . is_a? ( String )
102
- end
103
- result
104
- end
105
-
106
86
def save_to_file ( file , list )
107
87
source = ''
108
88
list . each { |p | source << "#{ p [ 0 ] } :#{ p [ 1 ] } \n " }
0 commit comments