ddns/ddns.go
2016-09-07 22:55:59 +02:00

39 lines
773 B
Go

package main
import (
"crypto/subtle"
"net/http"
"sync"
)
var (
mu sync.RWMutex
ip string
key string
)
func main() {
http.HandleFunc("/get", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
mu.RLock()
defer mu.RUnlock()
w.Write([]byte(ip))
return
}
http.Error(w, "bad request", http.StatusBadRequest)
})
http.HandleFunc("/set", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
r.ParseForm()
if subtle.ConstantTimeCompare([]byte(key), []byte(r.FormValue("token"))) == 1 {
mu.Lock()
defer mu.Unlock()
ip = r.FormValue("ip")
w.Write([]byte("ok"))
return
}
}
http.Error(w, "bad request", http.StatusBadRequest)
})
panic(http.ListenAndServe("127.0.0.1:9090", nil))
}