Skip to content

Commit

Permalink
use headers, if provided
Browse files Browse the repository at this point in the history
this allows ipecho to run behind a reverse proxy
  • Loading branch information
lgrn authored Mar 14, 2024
1 parent e197403 commit 4009f8e
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,19 @@ func main() {
})

r.GET("/", func(c *gin.Context) {
ip := c.ClientIP() + "\n"
c.String(http.StatusOK, ip)
// primarily rely on header, if they are set
ip := c.GetHeader("X-Real-IP")

// if that's empty, try another one
if ip == "" {
ip = c.GetHeader("X-Forwarded-For")
}

// if that's also empty, go with actual IP seen
if ip == "" {
ip = c.ClientIP()
}
c.String(http.StatusOK, ip+"\n")
})

r.NoRoute(func(c *gin.Context) {
Expand Down

0 comments on commit 4009f8e

Please sign in to comment.