basecamp / thruster

MIT License
672 stars 16 forks source link

Set X-Forwarded-* headers #29

Closed 3v0k4 closed 2 months ago

3v0k4 commented 2 months ago

Fixes #25

For context, you may want to read:

Manual test:

cd ~/code/thruster
cd cmd/thrust
make build
cd ~/code
rails new myapp
cd myapp
bin/rails g controller home index
mv ../thruster/bin/thrust .
HTTP_PORT=8080 DEBUG=1 ./thrust bin/rails server
class HomeController < ApplicationController
  def index
    puts ["Host", request.headers["Host"]].inspect
    puts ["XFor", request.headers["X-Forwarded-For"]].inspect
    puts ["XHost", request.headers["X-Forwarded-Host"]].inspect
    puts ["XProto", request.headers["X-Forwarded-Proto"]].inspect
    head :ok
  end
end
open http://127.0.0.1:3000/home/index
# ["Host", "127.0.0.1:3000"]
# ["XFor", nil]
# ["XHost", nil]
# ["XProto", nil]

http://127.0.0.1:8080/home/index
# ["Host", "127.0.0.1:8080"]
# ["XFor", "127.0.0.1"]
# ["XHost", "127.0.0.1:8080"]
# ["XProto", "http"]
# config/environments/development.rb
config.hosts << "example.com"
curl -H "Host: example.com" -H "X-Forwarded-For: 1.2.3.4" http://localhost:8080/home/index   
# ["Host", "example.com"]
# ["XFor", "1.2.3.4, 127.0.0.1"]
# ["XHost", "example.com"]
# ["XProto", "http"]
kevinmcconnell commented 2 months ago

This looks perfect, @3v0k4! Thanks again for tackling it! 🙏