Module: NetHTTPOverride

Defined in:
lib/evervault/http/request_intercept.rb

Constant Summary collapse

@@api_key =
nil
@@relay_url =
nil
@@relay_port =
nil
@@cert =
nil
@@get_decryption_domains_func =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_get_decryption_domains_func(get_decryption_domains_func) ⇒ Object



32
33
34
# File 'lib/evervault/http/request_intercept.rb', line 32

def self.add_get_decryption_domains_func(get_decryption_domains_func)
  @@get_decryption_domains_func = get_decryption_domains_func
end

.set_api_key(value) ⇒ Object



18
19
20
# File 'lib/evervault/http/request_intercept.rb', line 18

def self.set_api_key(value)
  @@api_key = value
end

.set_cert(value) ⇒ Object



28
29
30
# File 'lib/evervault/http/request_intercept.rb', line 28

def self.set_cert(value)
  @@cert = value
end

.set_relay_url(value) ⇒ Object



22
23
24
25
26
# File 'lib/evervault/http/request_intercept.rb', line 22

def self.set_relay_url(value)
  relay_address_and_port = value.gsub(%r{(^\w+:|^)//}, '').split(':')
  @@relay_url = relay_address_and_port[0]
  @@relay_port = relay_address_and_port[1]
end

.should_decrypt(domain) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/evervault/http/request_intercept.rb', line 36

def self.should_decrypt(domain)
  if @@get_decryption_domains_func.nil?
    false
  else
    decryption_domains = @@get_decryption_domains_func.call
    decryption_domains.any? do |decryption_domain|
      if decryption_domain.start_with?('*')
        domain.end_with?(decryption_domain[1..])
      else
        domain == decryption_domain
      end
    end
  end
end

Instance Method Details

#connect_with_interceptObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/evervault/http/request_intercept.rb', line 51

def connect_with_intercept
  if NetHTTPOverride.should_decrypt(conn_address)
    @cert_store = OpenSSL::X509::Store.new
    @cert_store.add_cert(@@cert)
    @proxy_from_env = false
    @proxy_address = @@relay_url
    @proxy_port = @@relay_port
  end
  connect_without_intercept
end

#request_with_intercept(req, body = nil, &block) ⇒ Object



62
63
64
65
66
# File 'lib/evervault/http/request_intercept.rb', line 62

def request_with_intercept(req, body = nil, &block)
  should_decrypt = NetHTTPOverride.should_decrypt(@address)
  req['Proxy-Authorization'] = @@api_key if should_decrypt
  request_without_intercept(req, body, &block)
end