forked from polera/tlskit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtlskit.go
74 lines (61 loc) · 1.46 KB
/
tlskit.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package tlskit
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
)
type TLSRequest []struct {
Server string `json:"server"`
Port int32 `json:"port"`
}
type TLSResponse []struct {
Response
}
type Response struct {
Server string `json:"server"`
ValidSince string `json:"valid_since"`
AtAlertThreshold bool `json:"at_alert_threshold"`
ExpirationDate string `json:"expiration_date"`
DaysValid int32 `json:"days_valid"`
Expired bool `json:"expired"`
DaysToExpiration int32 `json:"days_to_expiration"`
Bits int32 `json:"bits"`
Port int32 `json:"port"`
}
func (r Response) String() string {
if r.Expired {
return fmt.Sprintf("%s has expired.", r.Server)
}
return fmt.Sprintf("%s is still valid.", r.Server)
}
const (
VERSION = "1.0.0"
API_HOST = "tlskit.com"
API_PATH = "/api"
)
func Lookup(request TLSRequest) ([]Response, error) {
jsonRequest, _ := json.Marshal(request)
url := fmt.Sprintf("http://%s%s", API_HOST, API_PATH)
req, err := http.Post(url,
"application/json",
bytes.NewReader(jsonRequest))
if err != nil {
return nil, err
}
defer req.Body.Close()
if req.StatusCode != http.StatusOK {
return nil, errors.New(req.Status)
}
r := new(TLSResponse)
err = json.NewDecoder(req.Body).Decode(r)
if err != nil {
return nil, err
}
responses := make([]Response, len(*r))
for i, child := range *r {
responses[i] = child.Response
}
return responses, nil
}