-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
153 lines (125 loc) · 3.22 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
package main
import (
"context"
"errors"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"
"urlquery/public/uqw/webhook"
"github.com/hpcloud/tail"
"github.com/urlquery/urlquery-api-go"
)
var appcfg *AppConfig
func init() {
var filename string
var err error
flag.StringVar(&filename, "config", "", "config file")
flag.Parse()
appcfg, err = LoadConfig(filename)
if err != nil {
log.Fatal("load config", err)
}
if appcfg.APIKey == "" || len(appcfg.APIKey) != 32 {
log.Fatal("API key needed")
}
urlquery.SetDefaultKey(appcfg.APIKey)
if appcfg.Webhooks.Enabled {
fmt.Println("Using Webhooks")
usr, _ := urlquery.GetUser()
if err == nil {
fmt.Println(" NB: Make sure the configured Webhook is correct (requests originate from urlquery.net)")
fmt.Println(" Webhook URL:", usr.Notify.Webhook.URL)
if usr.Notify.Webhook.Enabled == false {
fmt.Println(" WARNING - Webhooks is not enabled")
}
}
}
}
func main() {
var srv *webhook.WebhookServer
// Start webhook server
if appcfg.Webhooks.Enabled {
fmt.Println("Starting Webhook server:", appcfg.Webhooks.Listen)
srv = webhook.CreateWebhookServer(appcfg.Webhooks.Listen)
go func() {
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
}()
srv.RegisterCallbackReportCompleted(appcfg.Webhooks.Reports.Submitted.WriteReportData)
srv.RegisterCallbackAlertedReport(appcfg.Webhooks.Reports.Alerted.WriteReportData)
}
// Start submission workers
for _, submitter := range appcfg.Submit {
if submitter.Enabled {
go SubmitWorker(submitter)
}
}
waitForQuitSignal()
err := shutdownHttpServer(srv.Server, 60)
if err != nil {
fmt.Println("Shutdown timedout")
os.Exit(1)
}
}
// waitForQuitSignal waits for CRTL-C, forces exit if its pressed 2 more times
func waitForQuitSignal() {
// listen for OS interrupt signals
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
go func() {
for force_quit := 2; force_quit != 0; force_quit-- {
fmt.Printf("Hit Ctrl-C %d more times to force quit..\n", force_quit)
signal.Notify(quit, os.Interrupt)
<-quit
}
os.Exit(1)
}()
}
func shutdownHttpServer(srv *http.Server, timeout_seconds uint) error {
if srv == nil {
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout_seconds)*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
return err
}
return nil
}
func SubmitWorker(cfg SubmitterSettings) {
file, _ := os.OpenFile(cfg.File, os.O_RDWR|os.O_CREATE, 0644)
file.Close()
t, err := tail.TailFile(cfg.File, tail.Config{
ReOpen: true,
MustExist: false,
Poll: true,
Follow: true,
Logger: tail.DiscardingLogger,
Location: &tail.SeekInfo{Offset: 0, Whence: 2},
})
if err != nil {
log.Fatal(err)
}
for line := range t.Lines {
url := urlquery.SubmitJob{
Access: cfg.Settings.Access,
Tags: cfg.Settings.Tags,
}
url.Url = line.Text
submission, err := urlquery.Submit(url)
if err != nil {
fmt.Println("submission failed", url.Url)
continue
}
fmt.Println("submitted url", submission.QueueID, url.Url)
if cfg.Output.Enabled {
go cfg.Output.GrabQueuedReport(submission.QueueID)
}
}
}