From 93628b37bdd639934e302d4317b6b5656aa7b722 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Fri, 19 Feb 2016 01:08:38 +0100 Subject: Initial functionality --- check.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 check.go (limited to 'check.go') 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 +} -- cgit v1.2.3