intial import
This commit is contained in:
commit
2e90d32ce2
5 changed files with 264 additions and 0 deletions
68
backend.go
Normal file
68
backend.go
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
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
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue