Skip to content

Commit

Permalink
add flag for trusted proxies
Browse files Browse the repository at this point in the history
if you run ipecho behind a reverse proxy, you need to add the IP of
that reverse proxy as seen by ipecho with the -trusted-proxies flag:

ipecho -trusted-proxies="10.0.0.1"
  • Loading branch information
lgrn committed Mar 14, 2024
1 parent e197403 commit 4b1e16a
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
package main

import (
"flag"
"fmt"
"log"
"net/http"
"strings"

"github.com/gin-gonic/gin"
)

type stringSlice []string

func (i *stringSlice) String() string {
return strings.Join(*i, ",")
}

func (i *stringSlice) Set(value string) error {
*i = append(*i, value)
return nil
}

func main() {
var trustedProxies stringSlice
flag.Var(&trustedProxies, "trusted-proxies", "IP address of proxy trusted to provide correct headers (this flag can be repeated)")
flag.Parse()

gin.SetMode(gin.ReleaseMode)
r := gin.Default()
// do not trust proxies to provide "real ip" via header
r.SetTrustedProxies(nil)

if len(trustedProxies) > 0 {
if err := r.SetTrustedProxies(trustedProxies); err != nil {
log.Fatalf("Tried and failed to set trusted proxies: %v", err)
}
} else {
r.SetTrustedProxies(nil)
}

r.GET("/json", func(c *gin.Context) {
// c.Request.Header returns []string, use 'strings' to convert to string
Expand All @@ -24,8 +47,7 @@ func main() {
})

r.GET("/", func(c *gin.Context) {
ip := c.ClientIP() + "\n"
c.String(http.StatusOK, ip)
c.String(http.StatusOK, c.ClientIP()+"\n")
})

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

0 comments on commit 4b1e16a

Please sign in to comment.