Skip to content

Commit

Permalink
Allow friendbot to include cloudflare derived IP address in request logs
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirms committed Jun 26, 2024
1 parent 132b84c commit fa6aec7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 9 additions & 3 deletions services/friendbot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions support/http/logging_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit fa6aec7

Please sign in to comment.