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 +++++++++++++++++++++++++++++++++++++++++++ check_test.go | 8 ++++++++ config.yml | 8 ++++---- main.go | 16 +++++++++++++--- 4 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 check.go create mode 100644 check_test.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 +} diff --git a/check_test.go b/check_test.go new file mode 100644 index 0000000..702c48a --- /dev/null +++ b/check_test.go @@ -0,0 +1,8 @@ +package main + +import "testing" + +func TestKey(t *testing.T) { + k := Key() + t.Log(k) +} diff --git a/config.yml b/config.yml index f26c500..fa5374c 100644 --- a/config.yml +++ b/config.yml @@ -26,9 +26,9 @@ check: - http://www.berlin-mauer.de/livewatch/ - http://www.schwabenkinder.eu/livewatch - http://cl.zdf.de/livewatch/ - - http://letztespur.moccu.com/api/livewatch +# - http://letztespur.moccu.com/api/livewatch - http://liveblog.zdf.de/livewatch/ - - http://muellermeter.zdf.de/livewatch +# - http://muellermeter.zdf.de/livewatch - https://tippcenter.zdf.de/livewatch/ - - http://w1.webapp.wettendass.de/livewatch/celery/ - - http://w1.webapp.wettendass.de/livewatch/default/ +# - http://w1.webapp.wettendass.de/livewatch/celery/ +# - http://w1.webapp.wettendass.de/livewatch/default/ diff --git a/main.go b/main.go index 2bb53ba..b517c49 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,21 @@ package main -import "log" +import ( + "flag" + "log" +) + +var conf = flag.String("conf", "config.yml", "configuration file") func main() { - c, err := LoadConfig("config.yml") + c, err := LoadConfig(*conf) if err != nil { log.Fatal(err) } - log.Printf("%+v\n", c) + for _, uri := range c.Check { + err := Check(uri) + if err != nil { + log.Println(uri, err) + } + } } -- cgit v1.2.3