-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
439 lines (368 loc) · 9.17 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package main
import (
"bufio"
"bytes"
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"sync"
"time"
badger "github.com/dgraph-io/badger"
"github.com/ditashi/jsbeautifier-go/jsbeautifier"
"github.com/pmezard/go-difflib/difflib"
"github.com/gosimple/slug"
"github.com/urfave/cli/v2"
)
const TIMEOUT = 8 * time.Second
const RESPONSE_BODY_LIMIT = 1024 * 1024 * 3 //3MB
const BUCKET_URLS = "urls"
const DB_FILE = "my.db"
// this headers will be included in the response which is stored
var headerToInclude = []string{
"Host",
"Content-Length",
"Content-Type",
"Location",
"Access-Control-Allow-Origin",
"Access-Control-Allow-Methods",
"Access-Control-Expose-Headers",
"Access-Control-Allow-Credentials",
"Allow",
"Content-Security-Policy",
"Proxy-Authenticate",
"Server",
"WWW-Authenticate",
"X-Frame-Options",
"X-Powered-By",
}
type Bits uint8
const MODE_HEADERS_ONLY Bits = 1 << iota
func Set(b, flag Bits) Bits { return b | flag }
func Clear(b, flag Bits) Bits { return b &^ flag }
func Toggle(b, flag Bits) Bits { return b ^ flag }
func Has(b, flag Bits) bool { return b&flag != 0 }
func addUrl(db *badger.DB, url string, useStdin bool, headersOnly bool) error {
var mode Bits = 0
if headersOnly {
mode = MODE_HEADERS_ONLY
}
return db.Update(func(tx *badger.Txn) error {
if useStdin {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
url = scanner.Text()
e := badger.NewEntry([]byte(url), []byte("")).WithMeta(byte(mode))
err := tx.SetEntry(e)
if err != nil {
return err
}
fmt.Printf("+ %v\n", url)
}
return nil
}
fmt.Printf("+ %v\n", url)
e := badger.NewEntry([]byte(url), []byte("")).WithMeta(byte(mode))
return tx.SetEntry(e)
})
}
func removeUrl(db *badger.DB, url string) error {
return db.Update(func(tx *badger.Txn) error {
return tx.Delete([]byte(url))
})
}
func list(db *badger.DB) error {
return db.View(func(tx *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchSize = 10
it := tx.NewIterator(opts)
defer it.Close()
for it.Rewind(); it.Valid(); it.Next() {
item := it.Item()
k := item.Key()
mode := ""
if Has(Bits(item.UserMeta()), MODE_HEADERS_ONLY) {
mode = ", ONLY_HEADERS"
}
err := item.Value(func(v []byte) error {
fmt.Printf("%v, %vB%v\n", string(k), len(v), mode)
return nil
})
if err != nil {
return err
}
}
return nil
})
}
func getUrl(db *badger.DB, url string) error {
return db.View(func(tx *badger.Txn) error {
item, err := tx.Get([]byte(url))
if err != nil {
return err
}
valCopy, err := item.ValueCopy(nil)
fmt.Println(string(valCopy))
return nil
})
}
func retrieve(url string) (*http.Response, error) {
transport := &http.Transport{
MaxIdleConnsPerHost: -1,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
DisableKeepAlives: true,
}
var client = &http.Client{
Timeout: TIMEOUT,
Transport: transport,
CheckRedirect: func(redirectedRequest *http.Request, previousRequest []*http.Request) error {
return http.ErrUseLastResponse
},
}
return client.Get(url)
}
func Abs(x int) int {
if x < 0 {
return -x
}
return x
}
func beautifyJs(s string) string {
opts := jsbeautifier.DefaultOptions()
splitted := strings.SplitN(s, "\n\n", 2)
if len(splitted) != 2 {
return s
}
beautified, err := jsbeautifier.Beautify(&splitted[1], opts)
if err != nil {
return s
}
return fmt.Sprintf("%v\n\n%v", splitted[0], beautified)
}
func handleDiff(url, bodyOld, bodyNew, outDir string, beautify bool) {
diffLen := Abs(len(bodyOld) - len(bodyNew))
if beautify {
bodyOld = beautifyJs(bodyOld)
bodyNew = beautifyJs(bodyNew)
}
diff := difflib.UnifiedDiff{
A: difflib.SplitLines(bodyOld),
B: difflib.SplitLines(bodyNew),
FromFile: "Original",
ToFile: "Current",
Context: 3,
}
text, _ := difflib.GetUnifiedDiffString(diff)
if outDir == "" {
fmt.Printf("[%v] %vb diff:\n", url, diffLen)
fmt.Printf("%v", text)
} else {
filename := fmt.Sprintf("%v/%v_%v.diff", outDir, time.Now().Format("20060201-150405"), slug.Make(url))
data := []byte(text)
err := ioutil.WriteFile(filename, data, 0644)
if err != nil {
log.Printf("error saving output to %v: %v", filename, err)
}
}
}
// only leave a few interesting headers in the response
func minifyResponse(resp *http.Response, onlyHeaders bool) ([]byte, error) {
defer resp.Body.Close()
var b bytes.Buffer
b.WriteString(fmt.Sprintf("%v %v\n", resp.Proto, resp.Status))
for _, header := range headerToInclude {
if onlyHeaders && header == "Content-Length" {
continue
}
v := resp.Header.Get(header)
if v != "" {
b.WriteString(fmt.Sprintf("%v: %v\n", header, v))
}
}
if !onlyHeaders {
limitedReader := &io.LimitedReader{R: resp.Body, N: RESPONSE_BODY_LIMIT}
b.WriteString("\n")
io.Copy(&b, limitedReader)
}
return b.Bytes(), nil
}
func retrieveAndCompare(db *badger.DB, url, outDir string, save bool, bodyOld []byte, beautify bool, onlyHeaders bool, wg *sync.WaitGroup) {
defer wg.Done()
resp, err := retrieve(url)
if err != nil {
log.Printf("err retrieving %v: %v", url, err)
return
}
bodyNew, err := minifyResponse(resp, onlyHeaders)
if err != nil {
log.Printf("err minifying resp: %v", err)
return
}
if bytes.Compare(bodyOld, bodyNew) == 0 {
return
}
if beautify && strings.Contains(resp.Header.Get("Content-Type"), "javascript") {
beautify = true
} else {
beautify = false
}
handleDiff(url, string(bodyOld), string(bodyNew), outDir, beautify)
if save {
err := db.Update(func(tx *badger.Txn) error {
return tx.Set([]byte(url), bodyNew)
})
if err != nil {
log.Printf("err updating body: %v", err)
}
}
}
func monitor(db *badger.DB, save bool, outDir string, beautify bool, worker int) error {
var wg sync.WaitGroup
var sem = make(chan int, worker)
err := db.Update(func(tx *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchSize = 10
it := tx.NewIterator(opts)
defer it.Close()
for it.Rewind(); it.Valid(); it.Next() {
sem <- 1
item := it.Item()
url := string(item.Key())
onlyHeaders := Has(Bits(item.UserMeta()), MODE_HEADERS_ONLY)
bodyOld, err := item.ValueCopy(nil)
if err != nil {
return err
}
wg.Add(1)
go func(db *badger.DB, url, outDIr string, save bool, bodyOld []byte, onlyHeaders bool, wg *sync.WaitGroup) {
retrieveAndCompare(db, string(url), outDir, save, bodyOld, beautify, onlyHeaders, wg)
<-sem
}(db, string(url), outDir, save, bodyOld, onlyHeaders, &wg)
}
return nil
})
wg.Wait()
return err
}
func initDb(path string) (*badger.DB, error) {
options := badger.DefaultOptions(path)
options.Logger = nil
return badger.Open(options)
}
func main() {
db, err := initDb(DB_FILE)
if err != nil {
log.Fatal(err)
}
defer db.Close()
app := &cli.App{
Name: "wonitor",
Usage: "web monitor",
Commands: []*cli.Command{
{
Name: "add",
Aliases: []string{"a"},
Usage: "add endpoint to monitor",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "url",
Usage: "url to add",
},
&cli.BoolFlag{
Name: "stdin",
Usage: "read urls from stdin, line by line",
},
&cli.BoolFlag{
Name: "headersOnly",
Usage: "only retrieve headers and discard body",
Value: false,
},
},
Action: func(c *cli.Context) error {
if c.String("url") == "" && !c.Bool("stdin") {
fmt.Println("please use --url or --stdin")
os.Exit(1)
}
return addUrl(db, c.String("url"), c.Bool("stdin"), c.Bool("headersOnly"))
},
},
{
Name: "delete",
Aliases: []string{"d"},
Usage: "deletes an endpoint",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "url",
Usage: "url to delete",
Required: true,
},
},
Action: func(c *cli.Context) error {
return removeUrl(db, c.String("url"))
},
},
{
Name: "get",
Aliases: []string{"g"},
Usage: "get endpoint body",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "url",
Usage: "url to get from store",
Required: true,
},
},
Action: func(c *cli.Context) error {
return getUrl(db, c.String("url"))
},
},
{
Name: "list",
Aliases: []string{"l"},
Usage: "list all monitored endpoints and their body size in bytes",
Action: func(c *cli.Context) error {
return list(db)
},
},
{
Name: "monitor",
Aliases: []string{"m"},
Usage: "retrieve all urls and compare them",
Action: func(c *cli.Context) error {
return monitor(db, c.Bool("save"), c.String("outDir"), c.Bool("jsbeautify"), c.Int("worker"))
},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "save",
Usage: "save updates to store",
Value: false,
},
&cli.StringFlag{
Name: "outDir",
Usage: "save diffs as html to folder",
},
&cli.IntFlag{
Name: "worker",
Usage: "numbers of worker to retrieve data",
Value: 20,
},
&cli.BoolFlag{
Name: "jsbeautify",
Usage: "beautify javascript if found",
Value: true,
},
},
},
},
}
err = app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}