summaryrefslogtreecommitdiff
path: root/check.go
blob: be329e735816fbb559b69851c383506074935f65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main

import (
	"bytes"
	"encoding/hex"
	"fmt"
	"io/ioutil"
	"math/rand"
	"net/http"
	"time"
)

func init() {
	rand.Seed(time.Now().UnixNano())
}

func Keys() chan string {
	keys := make(chan string)
	go func() {
		for {
			keys <- Key()
		}
	}()
	return keys
}

func Key() string {
	b := make([]byte, 16)
	for i := range b {
		b[i] = byte(rand.Int())
	}
	return hex.EncodeToString(b[:])
}

func Check(uri, key string) error {
	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 fmt.Errorf("%v down", uri)
	}
	return nil
}