summaryrefslogtreecommitdiff
path: root/check.go
diff options
context:
space:
mode:
Diffstat (limited to 'check.go')
-rw-r--r--check.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/check.go b/check.go
new file mode 100644
index 0000000..b61bb66
--- /dev/null
+++ b/check.go
@@ -0,0 +1,43 @@
+package main
+
+import (
+ "bytes"
+ "encoding/hex"
+ "errors"
+ "io/ioutil"
+ "math/rand"
+ "net/http"
+ "time"
+)
+
+func init() {
+ rand.Seed(time.Now().UnixNano())
+}
+
+var errDiffer = errors.New("key differs")
+
+func Key() string {
+ b := make([]byte, 16)
+ for i := range b {
+ b[i] = byte(rand.Int())
+ }
+ return hex.EncodeToString(b[:])
+}
+
+func Check(uri string) error {
+ key := Key()
+ resp, err := http.Get(uri + "?key=" + key)
+ if err != nil {
+ return err
+ }
+ defer resp.Body.Close()
+ body, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ return err
+ }
+ body = bytes.TrimSpace(body)
+ if string(body) != key {
+ return errDiffer
+ }
+ return nil
+}