Skip to content

Commit

Permalink
Merge pull request #81 from blinklabs-io/fix/webhook-context-ssl
Browse files Browse the repository at this point in the history
fix: set webhook context w/ 5s timeout and disable SSL verify
  • Loading branch information
wolf31o2 authored Sep 13, 2023
2 parents dd7dd30 + a2e43a3 commit f7e3d1c
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions output/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ package webhook

import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"time"

"github.com/blinklabs-io/snek/event"
"github.com/blinklabs-io/snek/input/chainsync"
Expand Down Expand Up @@ -60,7 +63,6 @@ func (w *WebhookOutput) Start() error {
if payload == nil {
panic(fmt.Errorf("ERROR: %v", payload))
}
logger.Infof("debug: type: %s", evt.Type)
switch evt.Type {
case "chainsync.block":
be := payload.(chainsync.BlockEvent)
Expand Down Expand Up @@ -93,14 +95,28 @@ func SendWebhook(e *event.Event, url string) error {
return fmt.Errorf("%s", err)
}
// Setup request
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(data))
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(data))
if err != nil {
return fmt.Errorf("%s", err)
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("User-Agent", fmt.Sprintf("Snek/%s", version.GetVersionString()))
// Setup custom transport to ignore self-signed SSL
defaultTransport := http.DefaultTransport.(*http.Transport)
customTransport := &http.Transport{
Proxy: defaultTransport.Proxy,
DialContext: defaultTransport.DialContext,
MaxIdleConns: defaultTransport.MaxIdleConns,
IdleConnTimeout: defaultTransport.IdleConnTimeout,
ExpectContinueTimeout: defaultTransport.ExpectContinueTimeout,
TLSHandshakeTimeout: defaultTransport.TLSHandshakeTimeout,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: customTransport}
// Send payload
resp, err := http.DefaultClient.Do(req)
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("%s", err)
}
Expand Down

0 comments on commit f7e3d1c

Please sign in to comment.