-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
208 lines (171 loc) · 4.13 KB
/
main.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"encoding/json"
"fmt"
"net"
"net/http"
"os"
"regexp"
"sort"
"sync"
"time"
"github.com/alecthomas/kong"
"github.com/sergiogarciadev/ctmon/db"
"github.com/sergiogarciadev/ctmon/logclient"
"github.com/sergiogarciadev/ctmon/logger"
)
func saveState() {
data, err := json.MarshalIndent(logclient.Logs, "", " ")
logclient.PanicOnError(err)
logclient.PanicOnError(os.WriteFile("state.json", data, 0600))
}
func loadState() {
data, err := os.ReadFile("state.json")
logclient.PanicOnError(err)
json.Unmarshal(data, &logclient.Logs)
}
func getHeads() {
var wg sync.WaitGroup
for _, log := range logclient.Logs {
wg.Add(1)
go func(log *logclient.Log) {
sth, err := log.GetSTH()
logclient.PanicOnError(err)
if sth.Timestamp > uint64(log.Timestamp) {
log.Timestamp = int64(sth.Timestamp)
log.TreeSize = int64(sth.TreeSize)
}
wg.Done()
}(log)
}
wg.Wait()
}
func printHeads() {
for {
fmt.Print("\033[H\033[2J")
logNames := make([]string, 0, len(logclient.Logs))
for logName := range logclient.Logs {
logNames = append(logNames, logName)
}
sort.Strings(logNames)
fmt.Println("Log Head Timestamp Tree Size Downloaded Remaining")
for _, logName := range logNames {
log := logclient.Logs[logName]
timestamp := time.UnixMilli(int64(log.Timestamp))
fmt.Printf("%-15s: %s %15d %15d %12d\n", log.Name, timestamp.Format(time.DateTime), log.TreeSize, log.LastEntry, log.TreeSize-log.LastEntry)
}
println("\n=================================================================================")
time.Sleep(10 * time.Second)
}
}
type DownloadCmd struct {
Save bool `help:"Save certificates to database."`
SaveBulk bool `help:"Save certificates to database using BulkInsert"`
IPs []string `name:"ip" help:"IPs to user." type:"string"`
Regex string `help:"Print certificates matching this regex to stdout"`
}
func (cmd *DownloadCmd) Run(cli *CliContext) error {
if cmd.Save || cmd.SaveBulk {
db.Open()
defer db.Close()
}
defer logger.Close()
for _, ip := range cmd.IPs {
localAddr, err := net.ResolveIPAddr("ip", ip)
if err != nil {
panic(err)
}
localTCPAddr := net.TCPAddr{
IP: localAddr.IP,
}
client := http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
LocalAddr: &localTCPAddr,
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
}
logclient.AddHttpClient(client)
}
loadState()
if cli.ShowStats {
go printHeads()
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
for {
getHeads()
saveState()
time.Sleep(1 * time.Minute)
}
}()
entries := make(chan logclient.Entry, 8)
go func() {
for _, log := range logclient.Logs {
wg.Add(1)
go func(log *logclient.Log) {
stop := make(chan bool, 1)
logEntries := log.StreamEntries(stop)
for entry := range logEntries {
entries <- entry
}
wg.Done()
}(log)
}
}()
bulkEntries := make([]*logclient.Entry, 10_000)
bulkIndex := 0
var re *regexp.Regexp
if cmd.Regex != "" {
var err error
re, err = regexp.Compile(cmd.Regex)
if err != nil {
return err
}
}
for entry := range entries {
if cmd.SaveBulk {
bulkEntries[bulkIndex] = &entry
bulkIndex++
if bulkIndex == 10_000 {
db.BulkInsert(bulkEntries)
bulkIndex = 0
}
} else if cmd.Save {
err := db.Insert(&entry)
if err != nil {
println(err.Error())
}
}
if entry.Certificate != nil && re != nil {
for _, name := range entry.Certificate.DNSNames {
if re.MatchString(name) {
println(name)
}
}
}
}
wg.Wait()
return nil
}
type CliContext struct {
ShowStats bool
}
var cli struct {
ShowStats bool `help:"Show application status."`
Download DownloadCmd `cmd:"" help:"Download certicates."`
}
func main() {
ctx := kong.Parse(&cli)
err := ctx.Run(&CliContext{ShowStats: cli.ShowStats})
ctx.FatalIfErrorf(err)
}