feat(webhooks): add support for webhook proxy
This commit is contained in:
parent
b38360c9a5
commit
831aa4a014
@ -175,6 +175,8 @@ const (
|
|||||||
|
|
||||||
WebhooksEnabled Key = `webhooks.enabled`
|
WebhooksEnabled Key = `webhooks.enabled`
|
||||||
WebhooksTimeoutSeconds Key = `webhooks.timeoutseconds`
|
WebhooksTimeoutSeconds Key = `webhooks.timeoutseconds`
|
||||||
|
WebhooksProxyURL Key = `webhooks.proxyurl`
|
||||||
|
WebhooksProxyPassword Key = `webhooks.proxypassword`
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetString returns a string config value
|
// GetString returns a string config value
|
||||||
|
@ -26,9 +26,11 @@ import (
|
|||||||
"code.vikunja.io/web"
|
"code.vikunja.io/web"
|
||||||
"crypto/hmac"
|
"crypto/hmac"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
|
"encoding/base64"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"sort"
|
"sort"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@ -139,6 +141,27 @@ func (w *Webhook) Delete(s *xorm.Session, a web.Auth) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getWebhookHTTPClient() (client *http.Client) {
|
||||||
|
client = http.DefaultClient
|
||||||
|
client.Timeout = time.Duration(config.WebhooksTimeoutSeconds.GetInt()) * time.Second
|
||||||
|
|
||||||
|
if config.WebhooksProxyURL.GetString() == "" || config.WebhooksProxyPassword.GetString() == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
proxyURL, _ := url.Parse(config.WebhooksProxyURL.GetString())
|
||||||
|
|
||||||
|
client.Transport = &http.Transport{
|
||||||
|
Proxy: http.ProxyURL(proxyURL),
|
||||||
|
ProxyConnectHeader: http.Header{
|
||||||
|
"Proxy-Authorization": []string{"Basic " + base64.StdEncoding.EncodeToString([]byte("vikunja:"+config.WebhooksProxyPassword.GetString()))},
|
||||||
|
"User-Agent": []string{"Vikunja/" + version.Version},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (w *Webhook) sendWebhookPayload(p *WebhookPayload) (err error) {
|
func (w *Webhook) sendWebhookPayload(p *WebhookPayload) (err error) {
|
||||||
payload, err := json.Marshal(p)
|
payload, err := json.Marshal(p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -162,9 +185,11 @@ func (w *Webhook) sendWebhookPayload(p *WebhookPayload) (err error) {
|
|||||||
|
|
||||||
req.Header.Add("User-Agent", "Vikunja/"+version.Version)
|
req.Header.Add("User-Agent", "Vikunja/"+version.Version)
|
||||||
|
|
||||||
client := http.DefaultClient
|
if config.WebhooksProxyURL.GetString() != "" && config.WebhooksProxyPassword.GetString() != "" {
|
||||||
client.Timeout = time.Duration(config.WebhooksTimeoutSeconds.GetInt()) * time.Second
|
req.Header.Add("Proxy-Authorization", base64.StdEncoding.EncodeToString([]byte(config.WebhooksProxyPassword.GetString())))
|
||||||
|
}
|
||||||
|
|
||||||
|
client := getWebhookHTTPClient()
|
||||||
_, err = client.Do(req)
|
_, err = client.Do(req)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
log.Debugf("Sent webhook payload for webhook %d for event %s", w.ID, p.EventName)
|
log.Debugf("Sent webhook payload for webhook %d for event %s", w.ID, p.EventName)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user