summaryrefslogtreecommitdiff
path: root/check.go
blob: f66813ec5930de0adfaebdc15bf8d5139dde8edc (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
package main

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

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

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 fmt.Errorf("%v down", uri)
	}
	return nil
}