/status: show HTTP request headers and client IP.
Show the HTTP request headers and the client IP on the /status page. This is for debugging request headers added by reverse proxies such as Cloudflare and Nginx, and for making sure the client IP is correctly set by the X-Forwarded-For header.
This commit is contained in:
@@ -2,7 +2,7 @@ class StatusController < ApplicationController
|
||||
respond_to :html, :json, :xml
|
||||
|
||||
def show
|
||||
@status = ServerStatus.new
|
||||
@status = ServerStatus.new(request)
|
||||
respond_with(@status)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,8 +3,16 @@ class ServerStatus
|
||||
include ActiveModel::Serializers::JSON
|
||||
include ActiveModel::Serializers::Xml
|
||||
|
||||
attr_reader :request
|
||||
|
||||
def initialize(request)
|
||||
@request = request
|
||||
end
|
||||
|
||||
def serializable_hash(options = {})
|
||||
{
|
||||
ip: request.remote_ip,
|
||||
headers: http_headers,
|
||||
status: {
|
||||
hostname: hostname,
|
||||
uptime: uptime,
|
||||
@@ -29,6 +37,14 @@ class ServerStatus
|
||||
end
|
||||
|
||||
concerning :InfoMethods do
|
||||
def http_headers
|
||||
headers = request.headers.env.select { |key| key.starts_with?("HTTP_") }
|
||||
headers = headers.transform_keys { |key| key.delete_prefix("HTTP_").titleize.tr(" ", "-") }
|
||||
headers = headers.except("Cookie")
|
||||
headers = headers.reject { |k, v| v.blank? }
|
||||
headers
|
||||
end
|
||||
|
||||
def hostname
|
||||
Socket.gethostname
|
||||
end
|
||||
|
||||
@@ -34,5 +34,14 @@
|
||||
|
||||
<%= render "list", hash: @status.serializable_hash[:redis][:info] %>
|
||||
</details>
|
||||
|
||||
<h2>Request</h2>
|
||||
|
||||
<details>
|
||||
<summary>
|
||||
IP: <%= @status.request.remote_ip %>
|
||||
</summary>
|
||||
<%= render "list", hash: @status.serializable_hash[:headers] %>
|
||||
</details>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user