You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
1.7 KiB
80 lines
1.7 KiB
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
// https://dd.phga.de/dd?usr=<username>&pw=<passwd>
|
|
func main() {
|
|
// cheap routing
|
|
http.HandleFunc("/dd", handleDynamicUpdate)
|
|
|
|
log.Fatalln(http.ListenAndServe(":"+config.WebPort, nil))
|
|
}
|
|
|
|
func handleDynamicUpdate(w http.ResponseWriter, r *http.Request) {
|
|
up := r.URL.Query()
|
|
ip := r.Header.Get("X-FORWARDED-FOR")
|
|
|
|
if len(ip) < 1 {
|
|
ip = strings.Split(r.RemoteAddr, ":")[0]
|
|
}
|
|
|
|
if len(up) != 2 {
|
|
log.Println("Attempted update for:", ip)
|
|
log.Println("Not enough arguments provided:", up)
|
|
return
|
|
}
|
|
|
|
for k, v := range config.DdUsers {
|
|
if k == up["usr"][0] {
|
|
if v["password"] == up["pwd"][0] {
|
|
log.Println("Starting update for:", ip, "(", v["domain"], ")")
|
|
// Do the update via nsupdate
|
|
err := updateDnsRecord(v["domain"], ip, v["hmac"], v["keyname"], v["secret"])
|
|
if err != nil {
|
|
log.Println("nsupdate failed:", err)
|
|
} else {
|
|
log.Println("nsupdate succeeded!")
|
|
}
|
|
} else {
|
|
log.Println("Wrong Secret provided!")
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func updateDnsRecord(domain string, ip string, hmac string, keyname string, secret string) error {
|
|
f, err := os.Create("/tmp/update.dns")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
|
|
update := fmt.Sprintf(`server 127.0.0.1
|
|
key %s:%s %s
|
|
update add %s %d a %s
|
|
send`, hmac, keyname, secret, domain, config.DnsTTL, ip)
|
|
|
|
_, err = io.WriteString(f, update)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = f.Sync()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
nsupdate := exec.Command("nsupdate", "/tmp/update.dns")
|
|
out, err := nsupdate.Output()
|
|
log.Println(string(out))
|
|
return err
|
|
}
|