Add a header to change the which header we look for the remote IP

closes #3
This commit is contained in:
George Shammas 2021-03-29 08:29:19 -04:00
parent 45d46592cf
commit 23a10b7039
1 changed files with 23 additions and 19 deletions

12
main.go
View File

@ -15,6 +15,7 @@ type Configuration struct {
hostname string // Displayed Hostname
host string // Listened Host
port string // HTTP Port
ipheader string // Header to overwrite the remote IP
tls bool // TLS enabled
tlscert string // TLS Cert Path
tlskey string // TLS Cert Key Path
@ -29,6 +30,9 @@ func init() {
host := getEnvWithDefault("HOST", "")
port := getEnvWithDefault("PORT", "8080")
// Most common alternative would be X-Forwarded-For
ipheader := getEnvWithDefault("FORWARD_IP_HEADER", "CF-Connecting-IP")
tlsenabled := getEnvWithDefault("TLS", "0")
tlsport := getEnvWithDefault("TLSPORT", "8443")
tlscert := getEnvWithDefault("TLSCERT", "/opt/ifconfig/.cf/ifconfig.io.crt")
@ -38,6 +42,7 @@ func init() {
hostname: hostname,
host: host,
port: port,
ipheader: ipheader,
tls: tlsenabled == "1",
tlscert: tlscert,
tlskey: tlskey,
@ -71,9 +76,9 @@ func mainHandler(c *gin.Context) {
c.Abort()
}
cfIP := net.ParseIP(c.Request.Header.Get("CF-Connecting-IP"))
if cfIP != nil {
ip.IP = cfIP
header_ip := net.ParseIP(c.Request.Header.Get(configuration.ipheader))
if header_ip != nil {
ip.IP = header_ip
}
if fields[0] == "porttest" {
@ -192,7 +197,6 @@ func main() {
}
}(errc)
go func(errc chan error) {
errc <- r.Run(fmt.Sprintf("%s:%s", configuration.host, configuration.port))
}(errc)