allow using a custom http client.

fixes nostr:nevent1qvzqqqqx25pzqm8ksn7p6aak225sed38vlzngtuwl50tf0e8ahzuzkhpmuahzgzdqyd8wumn8ghj7cmpvd5x2v3wwpexjmtpdshxuet59amrzqg4waehxw309aex2mrp0yhxgctdw4eju6t09uqzq8r9r4par63whq6px0af5uxtkkx0psydtamq6rdcva248l27l2szensns3
This commit is contained in:
fiatjaf
2025-12-18 12:01:30 -03:00
parent 4d7f7ce25d
commit 97424e363a
4 changed files with 26 additions and 54 deletions

View File

@@ -3,10 +3,11 @@ package nostr
import (
"bytes"
"context"
"crypto/tls"
"errors"
"fmt"
"io"
"net/http"
"net/textproto"
"sync/atomic"
"time"
@@ -20,7 +21,7 @@ type writeRequest struct {
answer chan error
}
func (r *Relay) newConnection(ctx context.Context, tlsConfig *tls.Config) error {
func (r *Relay) newConnection(ctx context.Context, httpClient *http.Client) error {
debugLogf("{%s} connecting!\n", r.URL)
dialCtx := ctx
@@ -29,7 +30,18 @@ func (r *Relay) newConnection(ctx context.Context, tlsConfig *tls.Config) error
dialCtx, _ = context.WithTimeoutCause(ctx, 7*time.Second, errors.New("connection took too long"))
}
c, _, err := ws.Dial(dialCtx, r.URL, getConnectionOptions(r.requestHeader, tlsConfig))
dialOpts := &ws.DialOptions{
HTTPHeader: http.Header{
textproto.CanonicalMIMEHeaderKey("User-Agent"): {"fiatjaf.com/nostr"},
},
CompressionMode: ws.CompressionContextTakeover,
HTTPClient: httpClient,
}
for k, v := range r.requestHeader {
dialOpts.HTTPHeader[k] = v
}
c, _, err := ws.Dial(dialCtx, r.URL, dialOpts)
if err != nil {
return err
}