-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathct.go
104 lines (89 loc) · 2.58 KB
/
ct.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"context"
"log/slog"
"net/http"
"github.com/go-resty/resty/v2"
ct "github.com/google/certificate-transparency-go"
ctClient "github.com/google/certificate-transparency-go/client"
"github.com/google/certificate-transparency-go/jsonclient"
ctLog "github.com/google/certificate-transparency-go/loglist3"
"github.com/google/certificate-transparency-go/x509"
"github.com/hashicorp/go-retryablehttp"
)
type CertData struct {
LeafInput string `json:"leaf_input"`
ExtraData string `json:"extra_data"`
}
type CertLog struct {
Entries []CertData
}
func GetX509CertLogEntries(client *ctClient.LogClient, start, end int64) []ct.LogEntry {
ctx := context.Background()
resp, err := client.GetRawEntries(ctx, start, end)
if err != nil {
slog.Error("Failed get entries ", "log", client.BaseURI(), "startEntry", start, "endEntry", end, "err", err)
return nil
}
entries := make([]ct.LogEntry, len(resp.Entries))
for i, entry := range resp.Entries {
index := start + int64(i)
logEntry, err := ct.LogEntryFromLeaf(index, &entry)
if x509.IsFatal(err) {
slog.Error("Failed to parse entry", "log", client.BaseURI(), "entry", entry, "err", err)
continue
}
entries[i] = *logEntry
}
return entries
}
func GetLogList() *ctLog.LogList {
logListURL := ctLog.LogListURL
client := resty.New()
resp, err := client.R().
EnableTrace().
Get(logListURL)
if err != nil {
Abort(err.Error())
}
logList, err := ctLog.NewFromJSON(resp.Body())
if err != nil {
Abort(err.Error())
}
return logList
}
func CreateLogClient(log *ctLog.Log) *ctClient.LogClient {
httpClient := retryablehttp.NewClient()
httpClient.Logger = slog.Default()
httpClient.RetryMax = 10
httpClient.CheckRetry = func(c context.Context, resp *http.Response, err error) (bool, error) {
if err != nil {
slog.Error("", "err", err)
bodyReader := resp.Body
body := make([]byte, 1024)
bodyReader.Read(body)
slog.Error("", "body", string(body))
return true, nil
}
if resp.StatusCode == 429 {
// TODO: *Actually* increase sleep?
slog.Warn("Got HTTP 429, increasing sleep for", "log", log.URL, "headers", resp.Header)
return true, nil
}
if resp.StatusCode == 400 {
slog.Warn("Got 400")
bodyReader := resp.Body
body := make([]byte, 1024)
bodyReader.Read(body)
slog.Warn("", "body", string(body))
}
// All other go to default policy
return retryablehttp.DefaultRetryPolicy(c, resp, err)
}
stdHttpClient := httpClient.StandardClient()
client, err := ctClient.New(log.URL, stdHttpClient, jsonclient.Options{})
if err != nil {
Abort(err.Error())
}
return client
}