diff --git a/services/friendbot/main.go b/services/friendbot/main.go index 22a04b0bed..d8f0b79b01 100644 --- a/services/friendbot/main.go +++ b/services/friendbot/main.go @@ -8,6 +8,7 @@ import ( "github.com/go-chi/chi" "github.com/spf13/cobra" + "github.com/stellar/go/services/friendbot/internal" "github.com/stellar/go/support/app" "github.com/stellar/go/support/config" @@ -29,6 +30,7 @@ type Config struct { BaseFee int64 `toml:"base_fee" valid:"optional"` MinionBatchSize int `toml:"minion_batch_size" valid:"optional"` SubmitTxRetriesAllowed int `toml:"submit_tx_retries_allowed" valid:"optional"` + UseCloudflareIP bool `toml:"use_cloudflare_ip" valid:"optional"` } func main() { @@ -68,7 +70,7 @@ func run(cmd *cobra.Command, args []string) { log.Error(err) os.Exit(1) } - router := initRouter(fb) + router := initRouter(cfg, fb) registerProblems() addr := fmt.Sprintf("0.0.0.0:%d", cfg.Port) @@ -84,8 +86,12 @@ func run(cmd *cobra.Command, args []string) { }) } -func initRouter(fb *internal.Bot) *chi.Mux { - mux := http.NewAPIMux(log.DefaultLogger) +func initRouter(cfg Config, fb *internal.Bot) *chi.Mux { + mux := chi.NewRouter() + // first apply XFFMiddleware so we can have the real ip in the subsequent + // middlewares + mux.Use(http.XFFMiddleware(http.XFFMiddlewareConfig{BehindCloudflare: cfg.UseCloudflareIP})) + mux.Use(http.NewAPIMux(log.DefaultLogger).Middlewares()...) handler := &internal.FriendbotHandler{Friendbot: fb} mux.Get("/", handler.Handle) diff --git a/support/http/logging_middleware.go b/support/http/logging_middleware.go index 2cc957ac68..540dbd1243 100644 --- a/support/http/logging_middleware.go +++ b/support/http/logging_middleware.go @@ -8,6 +8,7 @@ import ( "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" + "github.com/stellar/go/support/http/mutil" "github.com/stellar/go/support/log" ) @@ -136,6 +137,7 @@ func logEndOfRequest( "subsys": "http", "path": r.URL.String(), "method": r.Method, + "ip": r.RemoteAddr, "status": mw.Status(), "bytes": mw.BytesWritten(), "duration": duration,