Class: ProxyList

Inherits:
Array show all
Defined in:
lib/rhack/proxy/list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Array

#grabprlist

Constructor Details

#initialize(source = [], file = nil) ⇒ ProxyList

Returns a new instance of ProxyList.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rhack/proxy/list.rb', line 56

def initialize(source=[], file=nil)
  if source.is String
    super []
    load source
  elsif source.is Array
    raise ArgumentError, 'second arg must be a string' if file and !file.is String
    super source
    fix! if file
    @name	= sort.hash
    @name	= File.split(file)[1] if file and @name == 0
    @file	    = file || "tmp/#{'%0*x'%[12,@name]}.txt"
    rw @file, map {|i| i*':'+"\n"} if file and !empty?
  else raise TypeError, "can't create proxylist from #{source.class}"
  end
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



51
52
53
# File 'lib/rhack/proxy/list.rb', line 51

def file
  @file
end

#nameObject (readonly)

Returns the value of attribute name.



52
53
54
# File 'lib/rhack/proxy/list.rb', line 52

def name
  @name
end

Instance Method Details

#+(pl) ⇒ Object



88
89
90
# File 'lib/rhack/proxy/list.rb', line 88

def +(pl)
  ProxyList plus pl
end

#-(pl) ⇒ Object



84
85
86
# File 'lib/rhack/proxy/list.rb', line 84

def -(pl)
  ProxyList minus pl
end

#==(pl) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/rhack/proxy/list.rb', line 76

def ==(pl)
  if pl.is ProxyList
    pl.name == @name 
  else
    pl == self
  end
end

#_fixed(ip) ⇒ Object



116
117
118
# File 'lib/rhack/proxy/list.rb', line 116

def _fixed(ip)
  ip.is String and ip[/^\d+\.\d+\.\d+\.\d+$/] and !ip["\n"]
end

#find_allObject



188
189
190
# File 'lib/rhack/proxy/list.rb', line 188

def find_all
  super
end

#fixObject



102
103
104
# File 'lib/rhack/proxy/list.rb', line 102

def fix
  dup.fixprlist!
end

#fix!Object



92
93
94
95
96
97
98
99
100
# File 'lib/rhack/proxy/list.rb', line 92

def fix!
  map! {|i|
    if i.is Array and ip = i[0]
      port = i[1].to_i
      [(ip.gsub!(/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/) {|m| sprintf("%d.%d.%d.%d", *$~[1..4].map! {|d| d.to_i})} || ip), port > 0 ? port : i[1]]
    end
  }.compact!
  uniq! || self
end

#fix_ipsObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rhack/proxy/list.rb', line 141

def fix_ips
  ts = []
  (0...size).to_a.div(size/50).each {|d| ts << Thread.new {d.each {|i|
  
        if self[i][0].is Fixnum
          self[i][0] = self[i][0].to_ip
          next
        elsif self[i][1].to_i == 0 or self[i][0][/^\d+\.\d+\.\d+\.\d+$/]
          next
        end
        
        if (ip = self[i][0][/\d{1,3}[\.\-]\d+[\.\-]\d+[\.\-]\d{1,3}/])
          self[i][0] = ip.gsub('-', '.')
        elsif $unres_hosts.has ip
          self[i] = nil
        else
          ip = IPSocket.getaddress(self[i][0]) rescue($unres_hosts << self[i][0]; nil)
          ip and (self[i][0] = ip) and tick!
        end
        
  }}}
  ts.joins && ts.clear
  compact! || self
end

#glypesObject



132
133
134
# File 'lib/rhack/proxy/list.rb', line 132

def glypes
  reject	{|i| i[1].to_i > 0}
end

#inspectObject



106
107
108
109
110
# File 'lib/rhack/proxy/list.rb', line 106

def inspect
  @name.is(String) ?
    sprintf("<#ProxyList: %s (%d items)>", @name, size) :
    sprintf("<#ProxyList:%#0*x (%d items)>", 12, @name, size)
end

#ipsObject



120
121
122
123
124
125
126
# File 'lib/rhack/proxy/list.rb', line 120

def ips
  self[0].is(Array) ?
    self[0].size > 1 ?
      find_all	{|i| i[1].to_i > 0 and _fixed i[0]}.firsts :
      find_all	{|i| _fixed i[0]}.firsts :
    find_all	{|i| _fixed i}
end

#ips_fixedObject



136
137
138
139
# File 'lib/rhack/proxy/list.rb', line 136

def ips_fixed
  firsts.each {|ip| return false if !_fixed ip }
  true
end

#load(file = @file) ⇒ Object



177
178
179
180
181
182
183
184
185
186
# File 'lib/rhack/proxy/list.rb', line 177

def load(file=@file)
  if "\n".in file or "</".in file
    file.grabprlist self
  elsif File.file?(file)
    IO.read(file).grabprlist(self)
  else
    file.grabprlist self
  end
  self
end

#minusObject



53
# File 'lib/rhack/proxy/list.rb', line 53

alias :minus :-

#plusObject



54
# File 'lib/rhack/proxy/list.rb', line 54

alias :plus :+

#rehashObject



72
73
74
# File 'lib/rhack/proxy/list.rb', line 72

def rehash
  ProxyList self, @file
end

#rejectObject



192
193
194
# File 'lib/rhack/proxy/list.rb', line 192

def reject
  super
end

#save(mark = nil) ⇒ Object



171
172
173
174
175
# File 'lib/rhack/proxy/list.rb', line 171

def save(mark=nil)
  str = map {|i| i * ':' + "\n" if i.is Array}
  str = "#"*10+" #{puttime} - #{mark}\n#{str}"+"#"*20 if mark
  rw @file, str
end

#standartObject



128
129
130
# File 'lib/rhack/proxy/list.rb', line 128

def standart
  find_all	{|i| i[1].to_i > 0}
end

#to_plObject



112
113
114
# File 'lib/rhack/proxy/list.rb', line 112

def to_pl
  self
end

#validObject



166
167
168
169
# File 'lib/rhack/proxy/list.rb', line 166

def valid
  each {|i| return false if !i.is Array or i.nitems != 2 or !i[0].is String or !(i[1].is String or i[1].is Fixnum)}
  true
end