initial commit

This commit is contained in:
ston1th 2016-09-07 22:55:59 +02:00
commit 44d4a91bfa
3 changed files with 43 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
ddns
KEY

39
ddns.go Normal file
View file

@ -0,0 +1,39 @@
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))
}

2
make.sh Executable file
View file

@ -0,0 +1,2 @@
#!/bin/sh
go build -ldflags="-X main.key=$(cat KEY) -s -w"