68 lines
1.2 KiB
Go
68 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// check if net.ParseIP returns nil!!
|
|
func getIP(r *http.Request) net.IP {
|
|
addr := r.URL.Query().Get("myip")
|
|
if len(addr) > 0 {
|
|
return net.ParseIP(addr)
|
|
}
|
|
|
|
addr = r.Header.Get(http.CanonicalHeaderKey("X-Forwarded-For"))
|
|
if len(addr) > 0 {
|
|
end := strings.Index(addr, ", ")
|
|
if end == -1 {
|
|
end = len(addr)
|
|
}
|
|
return net.ParseIP(addr[:end])
|
|
}
|
|
|
|
addr = r.Header.Get(http.CanonicalHeaderKey("X-Real-IP"))
|
|
if len(addr) > 0 {
|
|
return net.ParseIP(addr)
|
|
}
|
|
|
|
addr, _, err := net.SplitHostPort(r.RemoteAddr)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return net.ParseIP(addr)
|
|
}
|
|
|
|
func handleRequest(w http.ResponseWriter, r *http.Request, data userData) {
|
|
msg := "good"
|
|
defer func() {
|
|
w.Write([]byte(msg))
|
|
}()
|
|
|
|
hostname := r.URL.Query().Get("hostname")
|
|
if len(hostname) <= 0 || hostname != data.hostname {
|
|
msg = "nohost"
|
|
return
|
|
}
|
|
|
|
ipaddr := getIP(r)
|
|
if ipaddr == nil {
|
|
//TODO:
|
|
msg = "error"
|
|
return
|
|
}
|
|
msg = "Hostname: " + hostname + " , IP: " + ipaddr.String()
|
|
|
|
updated, err := updateNameserver(data, ipaddr)
|
|
if err != nil {
|
|
msg = "dnserr"
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
if !updated {
|
|
msg = "nochg"
|
|
return
|
|
}
|
|
}
|