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

42
main.go
View File

@ -12,13 +12,14 @@ import (
) )
type Configuration struct { 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
tls bool // TLS enabled ipheader string // Header to overwrite the remote IP
tlscert string // TLS Cert Path tls bool // TLS enabled
tlskey string // TLS Cert Key Path tlscert string // TLS Cert Path
tlsport string // HTTPS Port tlskey string // TLS Cert Key Path
tlsport string // HTTPS Port
} }
var configuration = Configuration{} var configuration = Configuration{}
@ -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")
@ -36,12 +40,13 @@ func init() {
configuration = Configuration{ configuration = Configuration{
hostname: hostname, hostname: hostname,
host: host, host: host,
port: port, port: port,
tls: tlsenabled == "1", ipheader: ipheader,
tlscert: tlscert, tls: tlsenabled == "1",
tlskey: tlskey, tlscert: tlscert,
tlsport: tlsport, tlskey: tlskey,
tlsport: tlsport,
} }
} }
@ -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)
@ -200,8 +204,8 @@ func main() {
if configuration.tls { if configuration.tls {
go func(errc chan error) { go func(errc chan error) {
errc <- r.RunTLS( errc <- r.RunTLS(
fmt.Sprintf("%s:%s", configuration.host, configuration.tlsport), fmt.Sprintf("%s:%s", configuration.host, configuration.tlsport),
configuration.tlscert, configuration.tlskey) configuration.tlscert, configuration.tlskey)
}(errc) }(errc)
} }