Class: ProxyServer

Inherits:
Goliath::API
  • Object
show all
Defined in:
lib/proxy/proxy_server.rb

Constant Summary collapse

DEFAULT_PORT =
8080

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ProxyServer

Returns a new instance of ProxyServer.



9
10
11
12
13
14
15
16
17
# File 'lib/proxy/proxy_server.rb', line 9

def initialize(options = {})
  @upstream_proxy = URI.parse(options[:proxy]) if options[:proxy]

  @port = options.fetch(:port, DEFAULT_PORT)

  @substitute_requests = {}
  @requests = []
  @track_requests = []
end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port.



5
6
7
# File 'lib/proxy/proxy_server.rb', line 5

def port
  @port
end

#requestsObject (readonly)

Returns the value of attribute requests.



6
7
8
# File 'lib/proxy/proxy_server.rb', line 6

def requests
  @requests
end

#substitute_requestsObject (readonly)

Returns the value of attribute substitute_requests.



6
7
8
# File 'lib/proxy/proxy_server.rb', line 6

def substitute_requests
  @substitute_requests
end

#track_requestsObject (readonly)

Returns the value of attribute track_requests.



6
7
8
# File 'lib/proxy/proxy_server.rb', line 6

def track_requests
  @track_requests
end

#upstream_proxyObject (readonly)

Returns the value of attribute upstream_proxy.



5
6
7
# File 'lib/proxy/proxy_server.rb', line 5

def upstream_proxy
  @upstream_proxy
end

Instance Method Details

#get_proxy(options) ⇒ Object



71
72
73
# File 'lib/proxy/proxy_server.rb', line 71

def get_proxy(options)
  options[:proxy] = {:type => 'http', :host => @upstream_proxy.host, :port => @upstream_proxy.port} if @upstream_proxy
end

#get_substituted_response(options) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/proxy/proxy_server.rb', line 75

def get_substituted_response(options)
  if options[:body]
    [200, {}, [options[:body]]]
  elsif options[:url]
    request_uri options[:url]
  end
end

#get_uri(env) ⇒ Object



57
58
59
# File 'lib/proxy/proxy_server.rb', line 57

def get_uri(env)
  env['REQUEST_URI'] || "http://#{env['SERVER_NAME']}#{env['PATH_INFO']}#{env['QUERY_STRING'].length > 0 ? '?'+env['QUERY_STRING'] : ''}"
end

#has_substitute?(uri) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_substitute?(uri)
  @substitute_requests.each do |pattern, options|
    if Regexp.new(pattern) =~ uri
      return true
    end
  end
  false
end

#request_uri(uri) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/proxy/proxy_server.rb', line 61

def request_uri(uri)
  options = {
      :redirects => 1
  }
  get_proxy(options)
  client= EM::HttpRequest.new(uri, options)
  request = client.get
  [request.response_header.status, request.response_header, request.response]
end

#resetObject



44
45
46
47
48
# File 'lib/proxy/proxy_server.rb', line 44

def reset
  @requests.clear
  @track_requests.clear
  @substitute_requests.clear
end

#response(env) ⇒ Object



50
51
52
53
54
55
# File 'lib/proxy/proxy_server.rb', line 50

def response(env)
  uri = get_uri env
  track uri if tracking_enabled? uri
  return substitute uri if has_substitute? uri
  request_uri uri
end

#startObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/proxy/proxy_server.rb', line 19

def start
  Thread.abort_on_exception = true
  require 'log4r'
  @proxy_thread = Thread.new do
    Goliath.env = :development
    server = Goliath::Server.new('0.0.0.0', self.port)
    server.app = Goliath::Rack::Builder.build(self.class, self)
    server.logger = Log4r::Logger.new 'proxy-server'

    server.options = {
        :daemonize => false,
        :verbose => true,
        :log_stdout => true,
        :env => :development
    }
    server.start do
      p "Proxy started"
    end
  end
end

#stopObject



40
41
42
# File 'lib/proxy/proxy_server.rb', line 40

def stop
  Thread.kill @proxy_thread if @proxy_thread
end

#substitute(uri) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/proxy/proxy_server.rb', line 83

def substitute(uri)
  @substitute_requests.each do |pattern, options|
    if Regexp.new(pattern) =~ uri
      return get_substituted_response(options)
    end
  end
  nil
end

#substitute_request(pattern, options) ⇒ Object



101
102
103
# File 'lib/proxy/proxy_server.rb', line 101

def substitute_request(pattern, options)
  @substitute_requests[pattern] = options
end

#track(uri) ⇒ Object



109
110
111
112
113
114
# File 'lib/proxy/proxy_server.rb', line 109

def track(uri)
  @requests << uri
  @track_requests.each do |pattern|
    @requests << uri if Regexp.new(pattern) =~ uri
  end
end

#track_request(pattern) ⇒ Object



105
106
107
# File 'lib/proxy/proxy_server.rb', line 105

def track_request(pattern)
  @track_requests << pattern
end

#tracking_enabled?(uri) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
119
120
121
# File 'lib/proxy/proxy_server.rb', line 116

def tracking_enabled?(uri)
  @track_requests.each do |pattern|
    return true if Regexp.new(pattern) =~ uri
  end
  false
end