Skip to content

Commit 5282b21

Browse files
committedDec 17, 2023
webhooks
1 parent 9275034 commit 5282b21

File tree

5 files changed

+67
-8
lines changed

5 files changed

+67
-8
lines changed
 

‎README.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# uqw - urlquery-worker (v0.2)
22

3-
A worker to submit and retrive reports from urlquery.net
3+
A worker for submitting URLs and retrive reports from http://urlquery.net
44

5-
Monitor files for input (URLs), and are submitted to urlquery.net for analysis.
5+
It monitors files for input (URLs) which are submitted to urlquery.net for analysis.
66

7-
Report data (screenshot, JSON) can be saved to a output directory further handling / analysis.
7+
Report data (screenshot, JSON) can be retrived and saved to a output directory for further handling / analysis.
88
Supports retrival of report data via both webhooks and REST API.
99

10+
Get a APIKEY by creating a account at:
11+
https://urlquery.net/user/signup
12+
13+
1014
## Usage example
1115

1216
Simple config
@@ -18,6 +22,7 @@ webhooks:
1822
enabled: false
1923
listen: 0.0.0.0:8080
2024

25+
# Setup webhook listeners for report events
2126
reports:
2227
# Alerts from users YARA signatures
2328
alerted:
@@ -40,7 +45,7 @@ submit:
4045
enabled: true
4146

4247
settings:
43-
access: public
48+
access: public # public, restricted, private
4449
tags: []
4550

4651
# alternative to webhooks

‎config.go

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type AppConfig struct {
1616
type WebhooksSettings struct {
1717
Enabled bool `yaml:"enabled"`
1818
Listen string `yaml:"listen"`
19+
1920
Reports struct {
2021
Alerted ReportOutput `yaml:"alerted"`
2122
Submitted ReportOutput `yaml:"submitted"`

‎config.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ apikey:
33

44
webhooks:
55
enabled: false
6-
listen: 0.0.0.0:0
6+
# IP this server should listen
7+
listen: 0.0.0.0:0
78

89
reports:
910
# Alerts from users YARA signatures

‎main.go

+52-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package main
22

33
import (
4+
"context"
45
"flag"
56
"fmt"
67
"log"
8+
"net/http"
79
"os"
810
"os/signal"
911
"strings"
@@ -31,13 +33,28 @@ func init() {
3133
}
3234
urlquery.SetDefaultKey(cfg.APIKey)
3335

36+
if cfg.Webhooks.Enabled {
37+
fmt.Println("Using Webhooks")
38+
39+
usr, _ := urlquery.GetUser()
40+
if err == nil {
41+
fmt.Println(" NB: Make sure the configured Webhook is correct (requests originate from urlquery.net)")
42+
fmt.Println(" Webhook URL:", usr.Notify.Webhook.URL)
43+
44+
if usr.Notify.Webhook.Enabled == false {
45+
fmt.Println(" WARNING - Webhooks is not enabled")
46+
}
47+
}
48+
}
49+
3450
}
3551

3652
func main() {
53+
var srv *http.Server
3754

3855
// Start webhook server
3956
if cfg.Webhooks.Enabled {
40-
StartWebhookServer()
57+
srv = StartWebhookServer()
4158
}
4259

4360
// Start submission workers
@@ -47,11 +64,44 @@ func main() {
4764
}
4865
}
4966

50-
// TODO: add shutdown
67+
waitForQuitSignal()
68+
err := shutdownHttpServer(srv, 60)
69+
if err != nil {
70+
fmt.Println("Shutdown timedout")
71+
os.Exit(1)
72+
}
73+
74+
}
75+
76+
// waitForQuitSignal waits for CRTL-C, forces exit if its pressed 2 more times
77+
func waitForQuitSignal() {
5178
// listen for OS interrupt signals
5279
quit := make(chan os.Signal, 1)
5380
signal.Notify(quit, os.Interrupt)
5481
<-quit
82+
83+
go func() {
84+
for force_quit := 2; force_quit != 0; force_quit-- {
85+
fmt.Printf("Hit Ctrl-C %d more times to force quit..\n", force_quit)
86+
signal.Notify(quit, os.Interrupt)
87+
<-quit
88+
}
89+
os.Exit(1)
90+
}()
91+
}
92+
93+
func shutdownHttpServer(srv *http.Server, timeout_seconds uint) error {
94+
if srv == nil {
95+
return nil
96+
}
97+
98+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout_seconds)*time.Second)
99+
defer cancel()
100+
if err := srv.Shutdown(ctx); err != nil {
101+
return err
102+
}
103+
104+
return nil
55105
}
56106

57107
func SubmitWorker(cfg SubmitterSettings) {

‎webhook.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type webhookServer struct {
1515
cfg WebhooksSettings
1616
}
1717

18-
func StartWebhookServer() {
18+
func StartWebhookServer() *http.Server {
1919
srv := CreateWebhookServer(cfg.Webhooks)
2020
http.Handle("/", srv.router)
2121

@@ -31,6 +31,8 @@ func StartWebhookServer() {
3131
log.Fatal(err)
3232
}
3333
}()
34+
35+
return web
3436
}
3537

3638
func CreateWebhookServer(cfg WebhooksSettings) *webhookServer {

0 commit comments

Comments
 (0)
Please sign in to comment.