From 44d4a91bfa29b3d2c5aba887acecb523b89ec90a Mon Sep 17 00:00:00 2001 From: ston1th Date: Wed, 7 Sep 2016 22:55:59 +0200 Subject: [PATCH] initial commit --- .gitignore | 2 ++ ddns.go | 39 +++++++++++++++++++++++++++++++++++++++ make.sh | 2 ++ 3 files changed, 43 insertions(+) create mode 100644 .gitignore create mode 100644 ddns.go create mode 100755 make.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..61aa9bf --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +ddns +KEY diff --git a/ddns.go b/ddns.go new file mode 100644 index 0000000..f6fd994 --- /dev/null +++ b/ddns.go @@ -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)) +} diff --git a/make.sh b/make.sh new file mode 100755 index 0000000..6f46763 --- /dev/null +++ b/make.sh @@ -0,0 +1,2 @@ +#!/bin/sh +go build -ldflags="-X main.key=$(cat KEY) -s -w"