genericWebhookURLs as a list rather than a single endpoint.

This commit is contained in:
Michael Kaye 2022-02-01 13:13:37 +00:00
parent 8e001408d8
commit 4e3eeec92c
4 changed files with 32 additions and 22 deletions

View file

@ -1,7 +1,8 @@
## Generic webhook request ## Generic webhook request
If the configuration option `generic_webhook_url` is set, then a synchronous request to If the configuration option `generic_webhook_urls` is set, then an asynchronous request to
the endpoint will be sent after the incoming request is parsed and the files are uploaded. each endpoint listed will be sent in parallel, after the incoming request is parsed and the
files are uploaded.
The webhook is designed for notification or other tracking services, and does not contain The webhook is designed for notification or other tracking services, and does not contain
the original log files uploaded. the original log files uploaded.
@ -36,3 +37,4 @@ A sample JSON body is as follows:
``` ```
The log and other files can be individually downloaded by concatenating the `listing_url` and the `logs` or `files` name. The log and other files can be individually downloaded by concatenating the `listing_url` and the `logs` or `files` name.
You may need to provide a HTTP basic auth user/pass if configured on your rageshake server.

View file

@ -72,7 +72,7 @@ type config struct {
SMTPPassword string `yaml:"smtp_password"` SMTPPassword string `yaml:"smtp_password"`
GenericWebhookURL string `yaml:"generic_webhook_url"` GenericWebhookURLs []string `yaml:"generic_webhook_urls"`
} }
func basicAuth(handler http.Handler, username, password, realm string) http.Handler { func basicAuth(handler http.Handler, username, password, realm string) http.Handler {
@ -181,11 +181,11 @@ func main() {
} }
func configureGenericWebhookClient(cfg *config) (*http.Client) { func configureGenericWebhookClient(cfg *config) (*http.Client) {
if cfg.GenericWebhookURL == "" { if len(cfg.GenericWebhookURLs) == 0 {
fmt.Println("No generic_webhook_url configured.") fmt.Println("No generic_webhook_urls configured.")
return nil return nil
} }
fmt.Println("Will forward metadata of all requests to ", cfg.GenericWebhookURL) fmt.Println("Will forward metadata of all requests to ", cfg.GenericWebhookURLs)
return &http.Client{ return &http.Client{
Timeout: time.Second * 300, Timeout: time.Second * 300,
} }

View file

@ -50,3 +50,9 @@ email_from: Rageshake <rageshake@matrix.org>
smtp_server: localhost:25 smtp_server: localhost:25
smtp_username: myemailuser smtp_username: myemailuser
smtp_password: myemailpass smtp_password: myemailpass
# a list of webhook URLs, (see docs/generic_webhook.md)
generic_webhook_urls:
- https://server.example.com/your-server/api
- http://another-server.com/api

View file

@ -521,24 +521,26 @@ func (s *submitServer) submitGenericWebhook(p parsedPayload, listingURL string,
if s.genericWebhookClient == nil { if s.genericWebhookClient == nil {
return nil return nil
} }
url := s.cfg.GenericWebhookURL for _, url := range s.cfg.GenericWebhookURLs {
log.Println("Submitting json to URL", url) // Enrich the parsedPayload with a reportURL and listingURL, to convert a single struct
// Enrich the parsedPayload with a reportURL and listingURL, to convert a single struct // to JSON easily
// to JSON easily genericHookPayload := genericWebhookPayload{
genericHookPayload := genericWebhookPayload{ parsedPayload: p,
parsedPayload: p, ReportURL: reportURL,
ReportURL: reportURL, ListingURL: listingURL,
ListingURL: listingURL, }
}
payloadBuffer := new(bytes.Buffer) payloadBuffer := new(bytes.Buffer)
json.NewEncoder(payloadBuffer).Encode(genericHookPayload) json.NewEncoder(payloadBuffer).Encode(genericHookPayload)
req, err := http.NewRequest("POST", url, payloadBuffer) req, err := http.NewRequest("POST", url, payloadBuffer)
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
if err != nil { if err != nil {
return err log.Println("Unable to submit to URL ", url, " ", err)
return err
}
log.Println("Making generic webhook request to URL ", url)
go s.sendGenericWebhook(req)
} }
go s.sendGenericWebhook(req)
return nil return nil
} }