Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set webhook context w/ 5s timeout and disable SSL verify #81

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading