-
Notifications
You must be signed in to change notification settings - Fork 1
/
dump.rb
137 lines (115 loc) · 2.92 KB
/
dump.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
require 'uri'
require 'faraday'
require 'faraday/detailed_logger'
require 'pp'
require 'timeout'
require 'json'
require 'pry'
def dump(method, params, request, response)
data = {
method: method,
url: request.url,
headers: extract_headers(request),
params: params.to_h,
}
timestamp = Time.now.strftime("%Y%m%d-%H%M%S-%N")
random = "%010d" % rand(10**10)
basename = File.join("dump", "#{timestamp}-#{random}")
if response
data[:response] = {
status: response.status,
headers: response.headers,
}
if response.body
data[:response][:length] = response.body.length
File.open("#{basename}.response", "w") do |fh|
fh.write(response.body)
end
end
end
File.open("#{basename}.log", "w") do |fh|
fh.puts(data.pretty_inspect)
end
return 404
end
REMOVE_HEADERS = %w(
version
x-real-ip
x-forwarded-for
x-forwarded-proto
)
def extract_headers(request)
return request.env.map do |key, value|
next if value.nil?
next unless key =~ /^HTTP_/
name = $'.downcase.gsub('_', '-')
next if REMOVE_HEADERS.include?(name)
[name, value]
end.compact.to_h
end
$http_cache = {}
BLACKLIST = %w(
localhost
janus.
maki.
192.168.
127.
)
def faraday_for(uri)
cache_key = [uri.scheme, uri.host, uri.port]
return $http_cache[cache_key] || make_faraday_for(uri)
end
def make_faraday_for(uri)
Faraday.new("#{uri.scheme}://#{uri.host}:#{uri.port}") #do |faraday|
#faraday.request :url_encoder
#faraday.response :detailed_logger
#faraday.adapter Faraday.default_adapter
#end
end
def proxy_forward(method, request)
uri = URI.parse(request.url)
headers = extract_headers(request)
#headers['x-rblgsdp'] = '1'
# avoid accidental loops
if BLACKLIST.any? { |prefix| uri.host.start_with?(prefix) }
return nil
elsif request.env['HTTP_X_RBLGSDP'] == '1'
raise "Rejecting looped request"
end
Timeout.timeout(10) do
http = faraday_for(uri)
if method == :get
return http.get(uri) do |req|
req.headers = headers
end
elsif method == :post
request.body.rewind
return http.post(uri) do |req|
req.body = request.body.read
p req.body
req.headers = headers
end
else
raise "Unknown method: #{method.inspect}"
end
end
rescue Timeout::Error
return nil
end
def response_headers(response)
headers = response.headers.map { |k, v| [k.downcase, v] }.to_h
headers.delete('transfer-encoding') if headers['transfer-encoding'] == 'chunked'
return headers
end
get '/rest/*' do
response = proxy_forward(:get, request)
dump(:get, params, request, response)
return 404 unless response
return [response.status, response_headers(response), response.body]
end
post '/rest/*' do
response = proxy_forward(:post, request)
dump(:post, params, request, response)
return 404 unless response
return [response.status, response_headers(response), response.body]
end