mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
refactor(http): implement shared transport and clients (#1288)
* fix(http): flip to a shared transport and clients * nice threads * that is terrible * fake uri for magnet * lazy locking * why bother with r's * flip magic params to struct * refactor(http-clients): use separate clients with shared transport * refactor(http-clients): add missing license header * refactor(http-clients): defer and fix errors --------- Co-authored-by: ze0s <ze0s@riseup.net>
This commit is contained in:
parent
2a4fb7750b
commit
3234f0d919
48 changed files with 537 additions and 391 deletions
|
@ -6,7 +6,6 @@ package action
|
|||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
@ -197,14 +196,6 @@ func (s *service) webhook(ctx context.Context, action *domain.Action, release do
|
|||
s.log.Trace().Msgf("webhook action '%s' - host: %s data: %s", action.Name, action.WebhookHost, action.WebhookData)
|
||||
}
|
||||
|
||||
t := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
},
|
||||
}
|
||||
|
||||
client := http.Client{Transport: t, Timeout: 120 * time.Second}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, action.WebhookHost, bytes.NewBufferString(action.WebhookData))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not build request for webhook")
|
||||
|
@ -214,8 +205,7 @@ func (s *service) webhook(ctx context.Context, action *domain.Action, release do
|
|||
req.Header.Set("User-Agent", "autobrr")
|
||||
|
||||
start := time.Now()
|
||||
|
||||
res, err := client.Do(req)
|
||||
res, err := s.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not make request for webhook")
|
||||
}
|
||||
|
|
|
@ -6,10 +6,13 @@ package action
|
|||
import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/internal/download_client"
|
||||
"github.com/autobrr/autobrr/internal/logger"
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
|
||||
"github.com/asaskevich/EventBus"
|
||||
"github.com/dcarbone/zadapters/zstdlog"
|
||||
|
@ -34,6 +37,8 @@ type service struct {
|
|||
repo domain.ActionRepo
|
||||
clientSvc download_client.Service
|
||||
bus EventBus.Bus
|
||||
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
func NewService(log logger.Logger, repo domain.ActionRepo, clientSvc download_client.Service, bus EventBus.Bus) Service {
|
||||
|
@ -42,6 +47,11 @@ func NewService(log logger.Logger, repo domain.ActionRepo, clientSvc download_cl
|
|||
repo: repo,
|
||||
clientSvc: clientSvc,
|
||||
bus: bus,
|
||||
|
||||
httpClient: &http.Client{
|
||||
Timeout: time.Second * 120,
|
||||
Transport: sharedhttp.TransportTLSInsecure,
|
||||
},
|
||||
}
|
||||
|
||||
s.subLogger = zstdlog.NewStdLoggerWithLevel(s.log.With().Logger(), zerolog.TraceLevel)
|
||||
|
|
|
@ -1,114 +0,0 @@
|
|||
// Copyright (c) 2021 - 2023, Ludvig Lundgren and the autobrr contributors.
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
package client
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
|
||||
"github.com/anacrolix/torrent/metainfo"
|
||||
"github.com/avast/retry-go"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type DownloadFileResponse struct {
|
||||
Body *io.ReadCloser
|
||||
FileName string
|
||||
}
|
||||
|
||||
type DownloadTorrentFileResponse struct {
|
||||
MetaInfo *metainfo.MetaInfo
|
||||
TmpFileName string
|
||||
}
|
||||
|
||||
type HttpClient struct {
|
||||
http *http.Client
|
||||
}
|
||||
|
||||
func NewHttpClient() *HttpClient {
|
||||
httpClient := &http.Client{
|
||||
Timeout: time.Second * 10,
|
||||
}
|
||||
return &HttpClient{
|
||||
http: httpClient,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *HttpClient) DownloadTorrentFile(url string, opts map[string]string) (*DownloadTorrentFileResponse, error) {
|
||||
if url == "" {
|
||||
return nil, errors.New("download_file: url can't be empty")
|
||||
}
|
||||
|
||||
// Create tmp file
|
||||
tmpFile, err := os.CreateTemp("", "autobrr-")
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("error creating temp file")
|
||||
return nil, err
|
||||
}
|
||||
defer tmpFile.Close()
|
||||
|
||||
res := &DownloadTorrentFileResponse{}
|
||||
// try request and if fail run 3 retries
|
||||
err = retry.Do(func() error {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return errors.New("error downloading file: %q", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return errors.New("error downloading file bad status: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
nuke := func() {
|
||||
tmpFile.Seek(0, io.SeekStart)
|
||||
tmpFile.Truncate(0)
|
||||
}
|
||||
|
||||
// Write the body to file
|
||||
_, err = io.Copy(tmpFile, resp.Body)
|
||||
if err != nil {
|
||||
nuke()
|
||||
return errors.New("error writing downloaded file: %v | %q", tmpFile.Name(), err)
|
||||
}
|
||||
|
||||
meta, err := metainfo.Load(resp.Body)
|
||||
if err != nil {
|
||||
nuke()
|
||||
return errors.New("metainfo could not load file contents: %v | %q", tmpFile.Name(), err)
|
||||
}
|
||||
|
||||
res = &DownloadTorrentFileResponse{
|
||||
MetaInfo: meta,
|
||||
TmpFileName: tmpFile.Name(),
|
||||
}
|
||||
|
||||
if res.TmpFileName == "" || res.MetaInfo == nil {
|
||||
nuke()
|
||||
return errors.New("tmp file error - empty body")
|
||||
}
|
||||
|
||||
if len(res.MetaInfo.InfoBytes) < 1 {
|
||||
nuke()
|
||||
return errors.New("could not read infohash")
|
||||
}
|
||||
|
||||
log.Debug().Msgf("successfully downloaded file: %v", tmpFile.Name())
|
||||
return nil
|
||||
},
|
||||
//retry.OnRetry(func(n uint, err error) { c.log.Printf("%q: attempt %d - %v\n", err, n, url) }),
|
||||
retry.Delay(time.Second*5),
|
||||
retry.Attempts(3),
|
||||
retry.MaxJitter(time.Second*1))
|
||||
|
||||
if err != nil {
|
||||
res = nil
|
||||
}
|
||||
|
||||
return res, err
|
||||
}
|
|
@ -6,7 +6,6 @@ package domain
|
|||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"html"
|
||||
"io"
|
||||
|
@ -19,6 +18,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
|
||||
"github.com/anacrolix/torrent/bencode"
|
||||
"github.com/anacrolix/torrent/metainfo"
|
||||
|
@ -397,13 +397,6 @@ func (r *Release) downloadTorrentFile(ctx context.Context) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
customTransport := http.DefaultTransport.(*http.Transport).Clone()
|
||||
customTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||||
client := &http.Client{
|
||||
Transport: customTransport,
|
||||
Timeout: time.Second * 45,
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, r.DownloadURL, nil)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error downloading file")
|
||||
|
@ -411,6 +404,11 @@ func (r *Release) downloadTorrentFile(ctx context.Context) error {
|
|||
|
||||
req.Header.Set("User-Agent", "autobrr")
|
||||
|
||||
client := http.Client{
|
||||
Timeout: time.Second * 60,
|
||||
Transport: sharedhttp.TransportTLSInsecure,
|
||||
}
|
||||
|
||||
if r.RawCookie != "" {
|
||||
jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
|
||||
if err != nil {
|
||||
|
@ -573,11 +571,6 @@ func (r *Release) ResolveMagnetUri(ctx context.Context) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
client := http.Client{
|
||||
Transport: &magnetRoundTripper{},
|
||||
Timeout: time.Second * 60,
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, r.MagnetURI, nil)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not build request to resolve magnet uri")
|
||||
|
@ -586,6 +579,11 @@ func (r *Release) ResolveMagnetUri(ctx context.Context) error {
|
|||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("User-Agent", "autobrr")
|
||||
|
||||
client := &http.Client{
|
||||
Timeout: time.Second * 45,
|
||||
Transport: sharedhttp.MagnetTransport,
|
||||
}
|
||||
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "could not make request to resolve magnet uri")
|
||||
|
|
|
@ -5,11 +5,12 @@ package feed
|
|||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"time"
|
||||
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
|
||||
"github.com/mmcdole/gofeed"
|
||||
"golang.org/x/net/publicsuffix"
|
||||
)
|
||||
|
@ -22,16 +23,16 @@ type RSSParser struct {
|
|||
|
||||
// NewFeedParser wraps the gofeed.Parser using our own http client for full control
|
||||
func NewFeedParser(timeout time.Duration, cookie string) *RSSParser {
|
||||
//store cookies in jar
|
||||
jarOptions := &cookiejar.Options{PublicSuffixList: publicsuffix.List}
|
||||
jar, _ := cookiejar.New(jarOptions)
|
||||
|
||||
customTransport := http.DefaultTransport.(*http.Transport).Clone()
|
||||
customTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||||
httpClient := &http.Client{
|
||||
Timeout: time.Second * 60,
|
||||
Transport: customTransport,
|
||||
Jar: jar,
|
||||
Transport: sharedhttp.TransportTLSInsecure,
|
||||
}
|
||||
|
||||
if cookie != "" {
|
||||
//store cookies in jar
|
||||
jarOptions := &cookiejar.Options{PublicSuffixList: publicsuffix.List}
|
||||
jar, _ := cookiejar.New(jarOptions)
|
||||
httpClient.Jar = jar
|
||||
}
|
||||
|
||||
c := &RSSParser{
|
||||
|
|
|
@ -6,7 +6,6 @@ package filter
|
|||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
@ -22,6 +21,7 @@ import (
|
|||
"github.com/autobrr/autobrr/internal/logger"
|
||||
"github.com/autobrr/autobrr/internal/utils"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
|
||||
"github.com/avast/retry-go/v4"
|
||||
"github.com/mattn/go-shellwords"
|
||||
|
@ -52,6 +52,8 @@ type service struct {
|
|||
releaseRepo domain.ReleaseRepo
|
||||
indexerSvc indexer.Service
|
||||
apiService indexer.APIService
|
||||
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
func NewService(log logger.Logger, repo domain.FilterRepo, actionRepo domain.ActionRepo, releaseRepo domain.ReleaseRepo, apiService indexer.APIService, indexerSvc indexer.Service) Service {
|
||||
|
@ -62,6 +64,10 @@ func NewService(log logger.Logger, repo domain.FilterRepo, actionRepo domain.Act
|
|||
releaseRepo: releaseRepo,
|
||||
apiService: apiService,
|
||||
indexerSvc: indexerSvc,
|
||||
httpClient: &http.Client{
|
||||
Timeout: time.Second * 120,
|
||||
Transport: sharedhttp.TransportTLSInsecure,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -659,14 +665,6 @@ func (s *service) webhook(ctx context.Context, external domain.FilterExternal, r
|
|||
|
||||
s.log.Trace().Msgf("sending %s to external webhook filter: (%s) payload: (%s)", external.WebhookMethod, external.WebhookHost, external.WebhookData)
|
||||
|
||||
t := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
},
|
||||
}
|
||||
|
||||
client := http.Client{Transport: t, Timeout: 120 * time.Second}
|
||||
|
||||
method := http.MethodPost
|
||||
if external.WebhookMethod != "" {
|
||||
method = external.WebhookMethod
|
||||
|
@ -720,7 +718,7 @@ func (s *service) webhook(ctx context.Context, external domain.FilterExternal, r
|
|||
if external.WebhookData != "" && dataArgs != "" {
|
||||
clonereq.Body = io.NopCloser(bytes.NewBufferString(dataArgs))
|
||||
}
|
||||
res, err := client.Do(clonereq)
|
||||
res, err := s.httpClient.Do(clonereq)
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "could not make request for webhook")
|
||||
}
|
||||
|
|
|
@ -91,6 +91,8 @@ func setupAuthHandler() {
|
|||
}
|
||||
|
||||
func TestAuthHandlerLogin(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
logger := zerolog.Nop()
|
||||
encoder := encoder{}
|
||||
cookieStore := sessions.NewCookieStore([]byte("test"))
|
||||
|
@ -141,6 +143,8 @@ func TestAuthHandlerLogin(t *testing.T) {
|
|||
log.Fatalf("Error occurred: %v", err)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
// check for response, here we'll just check for 204 NoContent
|
||||
if status := resp.StatusCode; status != http.StatusNoContent {
|
||||
t.Errorf("login: handler returned wrong status code: got %v want %v", status, http.StatusNoContent)
|
||||
|
@ -152,6 +156,8 @@ func TestAuthHandlerLogin(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestAuthHandlerValidateOK(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
logger := zerolog.Nop()
|
||||
encoder := encoder{}
|
||||
cookieStore := sessions.NewCookieStore([]byte("test"))
|
||||
|
@ -202,6 +208,8 @@ func TestAuthHandlerValidateOK(t *testing.T) {
|
|||
log.Fatalf("Error occurred: %v", err)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
// check for response, here we'll just check for 204 NoContent
|
||||
if status := resp.StatusCode; status != http.StatusNoContent {
|
||||
t.Errorf("login: handler returned wrong status code: got %v want %v", status, http.StatusNoContent)
|
||||
|
@ -217,12 +225,16 @@ func TestAuthHandlerValidateOK(t *testing.T) {
|
|||
log.Fatalf("Error occurred: %v", err)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
if status := resp.StatusCode; status != http.StatusNoContent {
|
||||
t.Errorf("validate: handler returned wrong status code: got %v want %v", status, http.StatusNoContent)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthHandlerValidateBad(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
logger := zerolog.Nop()
|
||||
encoder := encoder{}
|
||||
cookieStore := sessions.NewCookieStore([]byte("test"))
|
||||
|
@ -264,12 +276,16 @@ func TestAuthHandlerValidateBad(t *testing.T) {
|
|||
log.Fatalf("Error occurred: %v", err)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
if status := resp.StatusCode; status != http.StatusUnauthorized {
|
||||
t.Errorf("validate: handler returned wrong status code: got %v want %v", status, http.StatusUnauthorized)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthHandlerLoginBad(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
logger := zerolog.Nop()
|
||||
encoder := encoder{}
|
||||
cookieStore := sessions.NewCookieStore([]byte("test"))
|
||||
|
@ -310,6 +326,8 @@ func TestAuthHandlerLoginBad(t *testing.T) {
|
|||
log.Fatalf("Error occurred: %v", err)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
// check for response, here we'll just check for 204 NoContent
|
||||
if status := resp.StatusCode; status != http.StatusUnauthorized {
|
||||
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusUnauthorized)
|
||||
|
@ -317,6 +335,8 @@ func TestAuthHandlerLoginBad(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestAuthHandlerLogout(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
logger := zerolog.Nop()
|
||||
encoder := encoder{}
|
||||
cookieStore := sessions.NewCookieStore([]byte("test"))
|
||||
|
@ -367,6 +387,8 @@ func TestAuthHandlerLogout(t *testing.T) {
|
|||
log.Fatalf("Error occurred: %v", err)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
// check for response, here we'll just check for 204 NoContent
|
||||
if status := resp.StatusCode; status != http.StatusNoContent {
|
||||
t.Errorf("login: handler returned wrong status code: got %v want %v", status, http.StatusNoContent)
|
||||
|
@ -382,6 +404,8 @@ func TestAuthHandlerLogout(t *testing.T) {
|
|||
log.Fatalf("Error occurred: %v", err)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
if status := resp.StatusCode; status != http.StatusNoContent {
|
||||
t.Errorf("validate: handler returned wrong status code: got %v want %v", status, http.StatusNoContent)
|
||||
}
|
||||
|
@ -392,6 +416,8 @@ func TestAuthHandlerLogout(t *testing.T) {
|
|||
log.Fatalf("Error occurred: %v", err)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
if status := resp.StatusCode; status != http.StatusNoContent {
|
||||
t.Errorf("validate: handler returned wrong status code: got %v want %v", status, http.StatusNoContent)
|
||||
}
|
||||
|
|
|
@ -92,7 +92,7 @@ func (s *apiService) AddClient(indexer string, settings map[string]string) error
|
|||
if !ok || key == "" {
|
||||
return errors.New("api.Service.AddClient: could not initialize btn client: missing var 'api_key'")
|
||||
}
|
||||
s.apiClients[indexer] = btn.NewClient("", key)
|
||||
s.apiClients[indexer] = btn.NewClient(key)
|
||||
|
||||
case "ptp":
|
||||
user, ok := settings["api_user"]
|
||||
|
@ -128,7 +128,7 @@ func (s *apiService) AddClient(indexer string, settings map[string]string) error
|
|||
s.apiClients[indexer] = ops.NewClient(key)
|
||||
|
||||
case "mock":
|
||||
s.apiClients[indexer] = mock.NewMockClient("", "mock")
|
||||
s.apiClients[indexer] = mock.NewMockClient("mock")
|
||||
|
||||
default:
|
||||
return errors.New("api.Service.AddClient: could not initialize client: unsupported indexer: %s", indexer)
|
||||
|
@ -154,7 +154,7 @@ func (s *apiService) getClientForTest(req domain.IndexerTestApiRequest) (apiClie
|
|||
if req.ApiKey == "" {
|
||||
return nil, errors.New("api.Service.AddClient: could not initialize btn client: missing var 'api_key'")
|
||||
}
|
||||
return btn.NewClient("", req.ApiKey), nil
|
||||
return btn.NewClient(req.ApiKey), nil
|
||||
|
||||
case "ptp":
|
||||
if req.ApiUser == "" {
|
||||
|
@ -185,7 +185,7 @@ func (s *apiService) getClientForTest(req domain.IndexerTestApiRequest) (apiClie
|
|||
return ops.NewClient(req.ApiKey), nil
|
||||
|
||||
case "mock":
|
||||
return mock.NewMockClient("", "mock"), nil
|
||||
return mock.NewMockClient("mock"), nil
|
||||
|
||||
default:
|
||||
return nil, errors.New("api.Service.AddClient: could not initialize client: unsupported indexer: %s", req.Identifier)
|
||||
|
|
|
@ -16,16 +16,28 @@ type IndexerApiClient interface {
|
|||
}
|
||||
|
||||
type IndexerClient struct {
|
||||
URL string
|
||||
url string
|
||||
APIKey string
|
||||
}
|
||||
|
||||
func NewMockClient(url string, apiKey string) IndexerApiClient {
|
||||
type OptFunc func(client *IndexerClient)
|
||||
|
||||
func WithUrl(url string) OptFunc {
|
||||
return func(c *IndexerClient) {
|
||||
c.url = url
|
||||
}
|
||||
}
|
||||
|
||||
func NewMockClient(apiKey string, opts ...OptFunc) IndexerApiClient {
|
||||
c := &IndexerClient{
|
||||
URL: url,
|
||||
url: "",
|
||||
APIKey: apiKey,
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(c)
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,6 @@ package notification
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
|
@ -15,6 +14,7 @@ import (
|
|||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
"github.com/rs/zerolog"
|
||||
|
@ -50,12 +50,18 @@ const (
|
|||
type discordSender struct {
|
||||
log zerolog.Logger
|
||||
Settings domain.Notification
|
||||
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
func NewDiscordSender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
|
||||
return &discordSender{
|
||||
log: log.With().Str("sender", "discord").Logger(),
|
||||
Settings: settings,
|
||||
httpClient: &http.Client{
|
||||
Timeout: time.Second * 30,
|
||||
Transport: sharedhttp.Transport,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -80,27 +86,20 @@ func (a *discordSender) Send(event domain.NotificationEvent, payload domain.Noti
|
|||
req.Header.Set("Content-Type", "application/json")
|
||||
//req.Header.Set("User-Agent", "autobrr")
|
||||
|
||||
t := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
},
|
||||
}
|
||||
|
||||
client := http.Client{Transport: t, Timeout: 30 * time.Second}
|
||||
res, err := client.Do(req)
|
||||
res, err := a.httpClient.Do(req)
|
||||
if err != nil {
|
||||
a.log.Error().Err(err).Msgf("discord client request error: %v", event)
|
||||
return errors.Wrap(err, "could not make request: %+v", req)
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
a.log.Error().Err(err).Msgf("discord client request error: %v", event)
|
||||
return errors.Wrap(err, "could not read data")
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
a.log.Trace().Msgf("discord status: %v response: %v", res.StatusCode, string(body))
|
||||
|
||||
// discord responds with 204, Notifiarr with 204 so lets take all 200 as ok
|
||||
|
|
|
@ -13,6 +13,7 @@ import (
|
|||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
@ -26,6 +27,8 @@ type gotifySender struct {
|
|||
log zerolog.Logger
|
||||
Settings domain.Notification
|
||||
builder NotificationBuilderPlainText
|
||||
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
func NewGotifySender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
|
||||
|
@ -33,6 +36,10 @@ func NewGotifySender(log zerolog.Logger, settings domain.Notification) domain.No
|
|||
log: log.With().Str("sender", "gotify").Logger(),
|
||||
Settings: settings,
|
||||
builder: NotificationBuilderPlainText{},
|
||||
httpClient: &http.Client{
|
||||
Timeout: time.Second * 30,
|
||||
Transport: sharedhttp.Transport,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -56,21 +63,20 @@ func (s *gotifySender) Send(event domain.NotificationEvent, payload domain.Notif
|
|||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
req.Header.Set("User-Agent", "autobrr")
|
||||
|
||||
client := http.Client{Timeout: 30 * time.Second}
|
||||
res, err := client.Do(req)
|
||||
res, err := s.httpClient.Do(req)
|
||||
if err != nil {
|
||||
s.log.Error().Err(err).Msgf("gotify client request error: %v", event)
|
||||
return errors.Wrap(err, "could not make request: %+v", req)
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
s.log.Error().Err(err).Msgf("gotify client request error: %v", event)
|
||||
return errors.Wrap(err, "could not read data")
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
s.log.Trace().Msgf("gotify status: %v response: %v", res.StatusCode, string(body))
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
@ -26,6 +27,8 @@ type lunaSeaSender struct {
|
|||
log zerolog.Logger
|
||||
Settings domain.Notification
|
||||
builder NotificationBuilderPlainText
|
||||
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
func (s *lunaSeaSender) rewriteWebhookURL(url string) string {
|
||||
|
@ -38,6 +41,10 @@ func NewLunaSeaSender(log zerolog.Logger, settings domain.Notification) domain.N
|
|||
log: log.With().Str("sender", "lunasea").Logger(),
|
||||
Settings: settings,
|
||||
builder: NotificationBuilderPlainText{},
|
||||
httpClient: &http.Client{
|
||||
Timeout: time.Second * 30,
|
||||
Transport: sharedhttp.Transport,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -64,8 +71,7 @@ func (s *lunaSeaSender) Send(event domain.NotificationEvent, payload domain.Noti
|
|||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
client := &http.Client{Timeout: 30 * time.Second}
|
||||
res, err := client.Do(req)
|
||||
res, err := s.httpClient.Do(req)
|
||||
if err != nil {
|
||||
s.log.Error().Err(err).Msg("lunasea client request error")
|
||||
return errors.Wrap(err, "could not make request")
|
||||
|
|
|
@ -5,7 +5,6 @@ package notification
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
|
@ -13,6 +12,7 @@ import (
|
|||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
@ -45,6 +45,8 @@ type notifiarrSender struct {
|
|||
log zerolog.Logger
|
||||
Settings domain.Notification
|
||||
baseUrl string
|
||||
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
func NewNotifiarrSender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
|
||||
|
@ -52,6 +54,10 @@ func NewNotifiarrSender(log zerolog.Logger, settings domain.Notification) domain
|
|||
log: log.With().Str("sender", "notifiarr").Logger(),
|
||||
Settings: settings,
|
||||
baseUrl: "https://notifiarr.com/api/v1/notification/autobrr",
|
||||
httpClient: &http.Client{
|
||||
Timeout: time.Second * 30,
|
||||
Transport: sharedhttp.Transport,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -77,27 +83,20 @@ func (s *notifiarrSender) Send(event domain.NotificationEvent, payload domain.No
|
|||
req.Header.Set("User-Agent", "autobrr")
|
||||
req.Header.Set("X-API-Key", s.Settings.APIKey)
|
||||
|
||||
t := &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
},
|
||||
}
|
||||
|
||||
client := http.Client{Transport: t, Timeout: 30 * time.Second}
|
||||
res, err := client.Do(req)
|
||||
res, err := s.httpClient.Do(req)
|
||||
if err != nil {
|
||||
s.log.Error().Err(err).Msgf("notifiarr client request error: %v", event)
|
||||
return errors.Wrap(err, "could not make request: %+v", req)
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
s.log.Error().Err(err).Msgf("notifiarr client request error: %v", event)
|
||||
return errors.Wrap(err, "could not read data")
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
s.log.Trace().Msgf("notifiarr status: %v response: %v", res.StatusCode, string(body))
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
|
|
|
@ -14,6 +14,7 @@ import (
|
|||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
@ -33,6 +34,8 @@ type pushoverSender struct {
|
|||
Settings domain.Notification
|
||||
baseUrl string
|
||||
builder NotificationBuilderPlainText
|
||||
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
func NewPushoverSender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
|
||||
|
@ -40,6 +43,11 @@ func NewPushoverSender(log zerolog.Logger, settings domain.Notification) domain.
|
|||
log: log.With().Str("sender", "pushover").Logger(),
|
||||
Settings: settings,
|
||||
baseUrl: "https://api.pushover.net/1/messages.json",
|
||||
builder: NotificationBuilderPlainText{},
|
||||
httpClient: &http.Client{
|
||||
Timeout: time.Second * 30,
|
||||
Transport: sharedhttp.Transport,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -81,21 +89,20 @@ func (s *pushoverSender) Send(event domain.NotificationEvent, payload domain.Not
|
|||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
req.Header.Set("User-Agent", "autobrr")
|
||||
|
||||
client := http.Client{Timeout: 30 * time.Second}
|
||||
res, err := client.Do(req)
|
||||
res, err := s.httpClient.Do(req)
|
||||
if err != nil {
|
||||
s.log.Error().Err(err).Msgf("pushover client request error: %v", event)
|
||||
return errors.Wrap(err, "could not make request: %+v", req)
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
s.log.Error().Err(err).Msgf("pushover client request error: %v", event)
|
||||
return errors.Wrap(err, "could not read data")
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
s.log.Trace().Msgf("pushover status: %v response: %v", res.StatusCode, string(body))
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
|
|
|
@ -14,11 +14,12 @@ import (
|
|||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/pkg/errors"
|
||||
"github.com/autobrr/autobrr/pkg/sharedhttp"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
// Reference: https://core.telegram.org/bots/api#sendmessage
|
||||
// TelegramMessage Reference: https://core.telegram.org/bots/api#sendmessage
|
||||
type TelegramMessage struct {
|
||||
ChatID string `json:"chat_id"`
|
||||
Text string `json:"text"`
|
||||
|
@ -31,6 +32,8 @@ type telegramSender struct {
|
|||
Settings domain.Notification
|
||||
ThreadID int
|
||||
builder NotificationBuilderPlainText
|
||||
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
func NewTelegramSender(log zerolog.Logger, settings domain.Notification) domain.NotificationSender {
|
||||
|
@ -46,6 +49,11 @@ func NewTelegramSender(log zerolog.Logger, settings domain.Notification) domain.
|
|||
log: log.With().Str("sender", "telegram").Logger(),
|
||||
Settings: settings,
|
||||
ThreadID: threadID,
|
||||
builder: NotificationBuilderPlainText{},
|
||||
httpClient: &http.Client{
|
||||
Timeout: time.Second * 30,
|
||||
Transport: sharedhttp.Transport,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -76,21 +84,20 @@ func (s *telegramSender) Send(event domain.NotificationEvent, payload domain.Not
|
|||
req.Header.Set("Content-Type", "application/json")
|
||||
//req.Header.Set("User-Agent", "autobrr")
|
||||
|
||||
client := http.Client{Timeout: 30 * time.Second}
|
||||
res, err := client.Do(req)
|
||||
res, err := s.httpClient.Do(req)
|
||||
if err != nil {
|
||||
s.log.Error().Err(err).Msgf("telegram client request error: %v", event)
|
||||
return errors.Wrap(err, "could not make request: %+v", req)
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
s.log.Error().Err(err).Msgf("telegram client request error: %v", event)
|
||||
return errors.Wrap(err, "could not read data")
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
s.log.Trace().Msgf("telegram status: %v response: %v", res.StatusCode, string(body))
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue