Added support for X-Forwarded-For header
This commit is contained in:
parent
c6bf995d81
commit
2c9a38bdc8
17
ddns.go
17
ddns.go
|
@ -7,6 +7,7 @@ import (
|
||||||
"github.com/pboehm/ddns/connection"
|
"github.com/pboehm/ddns/connection"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -98,7 +99,7 @@ func RunWebService() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ip, _, err := net.SplitHostPort(c.Req.RemoteAddr)
|
ip, err := GetRemoteAddr(c.Req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.String(500, "You sender IP address is not in the right format")
|
c.String(500, "You sender IP address is not in the right format")
|
||||||
}
|
}
|
||||||
|
@ -112,6 +113,20 @@ func RunWebService() {
|
||||||
r.Run(":8080")
|
r.Run(":8080")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get the Remote Address of the client. At First we try to get the
|
||||||
|
// X-Forwarded-For Header which holds the IP if we are behind a proxy,
|
||||||
|
// otherwise the RemoteAddr is used
|
||||||
|
func GetRemoteAddr(req *http.Request) (string, error) {
|
||||||
|
header_data, ok := req.Header["X-Forwarded-For"]
|
||||||
|
|
||||||
|
if ok {
|
||||||
|
return header_data[0], nil
|
||||||
|
} else {
|
||||||
|
ip, _, err := net.SplitHostPort(req.RemoteAddr)
|
||||||
|
return ip, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
if len(os.Args) < 2 {
|
if len(os.Args) < 2 {
|
||||||
|
|
Loading…
Reference in New Issue