/
proxy.rb
74 lines (66 loc) · 2.07 KB
/
proxy.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
require 'rubygems'
require 'eventmachine'
require 'base64'
require 'uri'
require 'webrick/httputils'
require 'active_support/core_ext'
require 'proxy/http_client'
require 'proxy/http_request'
require 'proxy/http_response'
require 'proxy/authorizer'
require 'proxy/filters'
require 'policy/client'
Proxy::Authorizer.use_auth = true
class ServerHandler < EventMachine::Connection
#TODO: Include Proxy
def finalize(http_response)
send_data(http_response.respond)
close_connection_after_writing
end
def receive_data(input)
# Get the peer's details
port, ip = Socket.unpack_sockaddr_in(get_peername)
puts input
# TODO: Handle BadRequest
# TODO: Why aren't we using evma_httpserver here?? Maybe the limited response options? Check it out anyway
request = Proxy::HTTPRequest.new(input, :client_ip => ip, :client_port => port)
# Policy Error
request.errback { |options|
result = case options[:reason]
when 301 then Proxy::HTTPResponse.redirect(options[:to])
when 403 then Proxy::HTTPResponse.access_denied(options[:message])
when 407 then Proxy::HTTPResponse.auth_required(options[:realm])
end
finalize(result)
}
request.callback { |request|
client = Proxy::HTTPClient.new(request)
client.callback { |response|
result = Proxy::HTTPResponse.new(response.status) do |http|
http.headers = response.headers
http.content = response.content
end
finalize(result)
}
# HTTP Error
client.errback { |message|
puts "Client ERROR: #{message}" # TODO: Log this?
result = Proxy::HTTPResponse.new(200) do |http|
# TODO: Handle specific errors like host not found or time out
http.content = $!
http.headers['content_type'] = "text/plain"
end
finalize(result)
}
client.perform_request
}
# Do it
request.perform_request
end
end
EM.run{
_host = "0.0.0.0"
_port = 8080
EventMachine::start_server _host, _port, ServerHandler
puts "Started EchoServer on #{_host}:#{_port}…"
}