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