Comparison

plugins/mod_http.lua @ 8594:b4a0bc46c82d

mod_http: Set request.ip on all HTTP requests (moves code out of mod_bosh) (fixes #540)
author Kim Alvefur <zash@zash.se>
date Thu, 15 Mar 2018 17:22:49 +0100
parent 7868:11fcdef5022f
child 8596:71da54c7f797
comparison
equal deleted inserted replaced
8593:c4222e36333c 8594:b4a0bc46c82d
148 function module.unload() 148 function module.unload()
149 server.remove_host(host); 149 server.remove_host(host);
150 end 150 end
151 end 151 end
152 152
153 local trusted_proxies = module:get_option_set("trusted_proxies", { "127.0.0.1", "::1" })._items;
154
155 local function get_ip_from_request(request)
156 local ip = request.conn:ip();
157 local forwarded_for = request.headers.x_forwarded_for;
158 if forwarded_for then
159 forwarded_for = forwarded_for..", "..ip;
160 for forwarded_ip in forwarded_for:gmatch("[^%s,]+") do
161 if not trusted_proxies[forwarded_ip] then
162 ip = forwarded_ip;
163 end
164 end
165 end
166 return ip;
167 end
168
169 module:wrap_object_event(server, false, function (handlers, event_name, event_data)
170 local request = event_data.request;
171 if request then
172 -- Not included in eg http-error events
173 request.ip = get_ip_from_request(request);
174 end
175 return handlers(event_name, event_data);
176 end);
177
153 module:provides("net", { 178 module:provides("net", {
154 name = "http"; 179 name = "http";
155 listener = server.listener; 180 listener = server.listener;
156 default_port = 5280; 181 default_port = 5280;
157 multiplex = { 182 multiplex = {