Class: Net::HTTP::Paranoid

Inherits:
Object
  • Object
show all
Defined in:
lib/net/http/paranoid.rb

Defined Under Namespace

Classes: NotAllowedHostError, ParanoidError

Constant Summary collapse

VERSION =
"0.0.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Paranoid

Returns a new instance of Paranoid.



14
15
16
17
18
# File 'lib/net/http/paranoid.rb', line 14

def initialize(opts={})
	opts = OpenStruct.new(opts)
	@blacklist = opts.blacklist || []
	@whitelist = opts.whitelist || []
end

Instance Attribute Details

#blacklistObject

Returns the value of attribute blacklist.



11
12
13
# File 'lib/net/http/paranoid.rb', line 11

def blacklist
  @blacklist
end

#whitelistObject

Returns the value of attribute whitelist.



12
13
14
# File 'lib/net/http/paranoid.rb', line 12

def whitelist
  @whitelist
end

Instance Method Details

#allow?(address) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/net/http/paranoid.rb', line 27

def allow?(address)
	name, _, _, ip = TCPSocket.gethostbyname(address)

	[
		[@whitelist, true], [@blacklist, false]
	].each do |list, ret|
		(list || []).each do |a|
			return ret if a === address

			return ret if a === name
			return ret if a === ip
		end
	end

	!lan?(address)
end

#wrap(http) ⇒ Object



20
21
22
23
24
25
# File 'lib/net/http/paranoid.rb', line 20

def wrap(http)
	unless allow?(http.address)
		raise NotAllowedHostError, "#{http.address} is not allowed host"
	end
	http
end