Class: Ronin::Network::HTTP::Proxy

Inherits:
Struct
  • Object
show all
Defined in:
lib/ronin/network/http/proxy.rb

Overview

The Proxy class represents the information needed to connect to a HTTP Proxy. The Proxy class can also test the reliability of a HTTP proxy.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Proxy

Creates a new Proxy object that represents a proxy to connect to.

Options Hash (options):

  • :host (String)

    The host-name of the proxy.

  • :port (Integer)

    The port that the proxy is running on.

  • :user (String)

    The user-name to authenticate as.

  • :password (String)

    The password to authenticate with.


53
54
55
56
57
58
59
60
# File 'lib/ronin/network/http/proxy.rb', line 53

def initialize(options={})
  super(
    options[:host],
    options[:port],
    options[:user],
    options[:password]
  )
end

Instance Attribute Details

#hostObject

Returns the value of attribute host


31
32
33
# File 'lib/ronin/network/http/proxy.rb', line 31

def host
  @host
end

#passwordObject

Returns the value of attribute password


31
32
33
# File 'lib/ronin/network/http/proxy.rb', line 31

def password
  @password
end

#portObject

Returns the value of attribute port


31
32
33
# File 'lib/ronin/network/http/proxy.rb', line 31

def port
  @port
end

#userObject

Returns the value of attribute user


31
32
33
# File 'lib/ronin/network/http/proxy.rb', line 31

def user
  @user
end

Class Method Details

.create(proxy) ⇒ Proxy

Creates a new proxy.

Raises:


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/ronin/network/http/proxy.rb', line 119

def self.create(proxy)
  case proxy
  when Proxy
    proxy
  when URI::HTTP
    new(
      :host => proxy.host,
      :port => proxy.port,
      :user => proxy.user,
      :password => proxy.password
    )
  when Hash
    new(proxy)
  when String
    parse(proxy)
  when nil
    new
  else
    raise(ArgumentError,"argument must be either a #{self}, URI::HTTP, Hash or String")
  end
end

.parse(proxy) ⇒ Proxy

Parses a proxy URL.

Examples:

Proxy.parse('217.31.51.77:443')
Proxy.parse('joe:[email protected]:8080')
Proxy.parse('http://201.26.192.61:8080')

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ronin/network/http/proxy.rb', line 82

def self.parse(proxy)
  proxy = proxy.to_s.gsub(/^http(s)?:\/*/,'')

  if proxy.include?('@')
    auth, proxy = proxy.split('@',2)
    user, password = auth.split(':')
  else
    user = nil
    password = nil
  end

  host, port = proxy.split(':',2)
  port = port.to_i if port

  return new(
    :host => host,
    :port => port,
    :user => user,
    :password => password
  )
end

Instance Method Details

#anonymous?Boolean

Determines whether the proxy will hide our IP address.


225
226
227
# File 'lib/ronin/network/http/proxy.rb', line 225

def anonymous?
  !(transparent?)
end

#disable!Object

Disables the Proxy object.


234
235
236
237
238
239
240
241
# File 'lib/ronin/network/http/proxy.rb', line 234

def disable!
  self.host = nil
  self.port = nil
  self.user = nil
  self.password = nil

  return self
end

#enabled?Boolean

Specifies whether the proxy object is usable.


252
253
254
# File 'lib/ronin/network/http/proxy.rb', line 252

def enabled?
  !(host.nil?)
end

#inspectString

Inspects the proxy object.


303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/ronin/network/http/proxy.rb', line 303

def inspect
  unless host
    str = 'disabled'
  else
    str = ''
    
    str << host.to_s
    str << ":#{port}" if port

    if user
      auth_str = ''
      auth_str << user.to_s

      if password
        auth_str << ":#{password}"
      end

      str = "#{auth_str}@#{str}"
    end
  end

  return "#<#{self.class}: #{str}>"
end

#latencyFloat Also known as: lag

Measures the latency of the proxy.


169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/ronin/network/http/proxy.rb', line 169

def latency
  time = lambda { |proxy|
    t1 = Time.now
    Net.http_head(
      :url => 'http://www.example.com/',
      :proxy => proxy
    )
    t2 = Time.now

    (t2 - t1)
  }

  begin
    return (time.call(self) - time.call(nil))
  rescue Timeout::Error, StandardError
    return (1.0/0)
  end
end

#proxied_ipString

The IP address the proxy sends with proxied requests.


198
199
200
201
202
203
# File 'lib/ronin/network/http/proxy.rb', line 198

def proxied_ip
  IPAddr.extract(Net.http_get_body(
    :url => Network::IP_URL,
    :proxy => self
  )).first
end

#to_sString

Converts the proxy object to a String.


291
292
293
# File 'lib/ronin/network/http/proxy.rb', line 291

def to_s
  host.to_s
end

#transparent?Boolean

Determines whether the proxy forwards our IP address.


213
214
215
# File 'lib/ronin/network/http/proxy.rb', line 213

def transparent?
  Network.ip == proxied_ip
end

#urlURI::HTTP?

Builds a HTTP URI from the proxy information.


265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/ronin/network/http/proxy.rb', line 265

def url
  return nil unless enabled?

  userinfo = if user
               if password
                 "#{user}:#{password}"
               else
                 user
               end
             end
  
  return URI::HTTP.build(
    :userinfo => userinfo,
    :host => host,
    :port => port
  )
end

#valid?Boolean

Tests the proxy.


149
150
151
152
153
154
155
156
157
158
# File 'lib/ronin/network/http/proxy.rb', line 149

def valid?
  begin
    Net.http_get_body(
      :url => 'http://www.example.com/',
      :proxy => self
    ).include?('Example Web Page')
  rescue Timeout::Error, StandardError
    return false
  end
end