2021-08-25 13:34:33 +00:00
|
|
|
/*
|
|
|
|
GoToSocial
|
2023-01-05 11:43:00 +00:00
|
|
|
Copyright (C) 2021-2023 GoToSocial Authors admin@gotosocial.org
|
2021-08-25 13:34:33 +00:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-05-17 17:06:58 +00:00
|
|
|
package transport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto"
|
2022-05-15 09:16:43 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"errors"
|
2022-11-23 21:40:07 +00:00
|
|
|
"fmt"
|
2022-01-16 17:52:55 +00:00
|
|
|
"io"
|
2022-05-15 09:16:43 +00:00
|
|
|
"net/http"
|
2021-05-17 17:06:58 +00:00
|
|
|
"net/url"
|
2023-02-10 20:16:01 +00:00
|
|
|
"strconv"
|
2022-05-15 09:16:43 +00:00
|
|
|
"strings"
|
2021-05-17 17:06:58 +00:00
|
|
|
"sync"
|
2022-05-15 09:16:43 +00:00
|
|
|
"time"
|
2021-05-17 17:06:58 +00:00
|
|
|
|
2022-05-15 09:16:43 +00:00
|
|
|
errorsv2 "codeberg.org/gruf/go-errors/v2"
|
2022-07-19 08:47:55 +00:00
|
|
|
"codeberg.org/gruf/go-kv"
|
2021-05-17 17:06:58 +00:00
|
|
|
"github.com/go-fed/httpsig"
|
2021-06-27 14:52:18 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
2022-05-26 11:38:41 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/httpclient"
|
2022-07-19 08:47:55 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/log"
|
2021-05-17 17:06:58 +00:00
|
|
|
)
|
|
|
|
|
2022-11-23 21:40:07 +00:00
|
|
|
// Transport implements the pub.Transport interface with some additional functionality for fetching remote media.
|
2022-03-15 14:01:19 +00:00
|
|
|
//
|
|
|
|
// Since the transport has the concept of 'shortcuts' for fetching data locally rather than remotely, it is
|
|
|
|
// not *always* the case that calling a Transport function does an http call, but it usually will for remote
|
|
|
|
// hosts or resources for which a shortcut isn't provided by the transport controller (also in this package).
|
2022-11-23 21:40:07 +00:00
|
|
|
//
|
|
|
|
// For any of the transport functions, if a Fastfail context is passed in as the first parameter, the function
|
|
|
|
// will return after the first transport failure, instead of retrying + backing off.
|
2021-05-17 17:06:58 +00:00
|
|
|
type Transport interface {
|
2022-11-23 21:40:07 +00:00
|
|
|
/*
|
|
|
|
POST functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Deliver sends an ActivityStreams object.
|
|
|
|
Deliver(ctx context.Context, b []byte, to *url.URL) error
|
|
|
|
// BatchDeliver sends an ActivityStreams object to multiple recipients.
|
|
|
|
BatchDeliver(ctx context.Context, b []byte, recipients []*url.URL) error
|
|
|
|
|
|
|
|
/*
|
|
|
|
GET functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Dereference fetches the ActivityStreams object located at this IRI with a GET request.
|
|
|
|
Dereference(ctx context.Context, iri *url.URL) ([]byte, error)
|
2022-01-23 13:41:58 +00:00
|
|
|
// DereferenceMedia fetches the given media attachment IRI, returning the reader and filesize.
|
2022-09-29 20:50:43 +00:00
|
|
|
DereferenceMedia(ctx context.Context, iri *url.URL) (io.ReadCloser, int64, error)
|
2021-06-27 14:52:18 +00:00
|
|
|
// DereferenceInstance dereferences remote instance information, first by checking /api/v1/instance, and then by checking /.well-known/nodeinfo.
|
2021-08-25 13:34:33 +00:00
|
|
|
DereferenceInstance(ctx context.Context, iri *url.URL) (*gtsmodel.Instance, error)
|
2021-05-29 17:36:54 +00:00
|
|
|
// Finger performs a webfinger request with the given username and domain, and returns the bytes from the response body.
|
2022-11-23 21:40:07 +00:00
|
|
|
Finger(ctx context.Context, targetUsername string, targetDomain string) ([]byte, error)
|
2021-05-17 17:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// transport implements the Transport interface
|
|
|
|
type transport struct {
|
2022-05-15 09:16:43 +00:00
|
|
|
controller *controller
|
|
|
|
pubKeyID string
|
|
|
|
privkey crypto.PrivateKey
|
|
|
|
|
|
|
|
signerExp time.Time
|
|
|
|
getSigner httpsig.Signer
|
|
|
|
postSigner httpsig.Signer
|
|
|
|
signerMu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET will perform given http request using transport client, retrying on certain preset errors, or if status code is among retryOn.
|
2023-02-10 20:16:01 +00:00
|
|
|
func (t *transport) GET(r *http.Request) (*http.Response, error) {
|
2022-05-15 09:16:43 +00:00
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
return nil, errors.New("must be GET request")
|
|
|
|
}
|
|
|
|
return t.do(r, func(r *http.Request) error {
|
|
|
|
return t.signGET(r)
|
2023-02-10 20:16:01 +00:00
|
|
|
})
|
2022-05-15 09:16:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// POST will perform given http request using transport client, retrying on certain preset errors, or if status code is among retryOn.
|
2023-02-10 20:16:01 +00:00
|
|
|
func (t *transport) POST(r *http.Request, body []byte) (*http.Response, error) {
|
2022-05-15 09:16:43 +00:00
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
return nil, errors.New("must be POST request")
|
|
|
|
}
|
|
|
|
return t.do(r, func(r *http.Request) error {
|
|
|
|
return t.signPOST(r, body)
|
2023-02-10 20:16:01 +00:00
|
|
|
})
|
2022-05-15 09:16:43 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 20:16:01 +00:00
|
|
|
func (t *transport) do(r *http.Request, signer func(*http.Request) error) (*http.Response, error) {
|
|
|
|
const (
|
|
|
|
// max no. attempts
|
|
|
|
maxRetries = 5
|
2022-10-08 11:50:16 +00:00
|
|
|
|
2023-02-10 20:16:01 +00:00
|
|
|
// starting backoff duration.
|
|
|
|
baseBackoff = 2 * time.Second
|
2022-10-08 11:50:16 +00:00
|
|
|
)
|
|
|
|
|
2023-02-10 20:16:01 +00:00
|
|
|
// Get request hostname
|
|
|
|
host := r.URL.Hostname()
|
|
|
|
|
2022-10-08 11:50:16 +00:00
|
|
|
// Check if recently reached max retries for this host
|
|
|
|
// so we don't need to bother reattempting it. The only
|
|
|
|
// errors that are retried upon are server failure and
|
|
|
|
// domain resolution type errors, so this cached result
|
|
|
|
// indicates this server is likely having issues.
|
|
|
|
if t.controller.badHosts.Has(host) {
|
|
|
|
return nil, errors.New("too many failed attempts")
|
|
|
|
}
|
2022-05-15 09:16:43 +00:00
|
|
|
|
2022-11-23 21:40:07 +00:00
|
|
|
// Check whether request should fast fail, we check this
|
|
|
|
// before loop as each context.Value() requires mutex lock.
|
|
|
|
fastFail := IsFastfail(r.Context())
|
|
|
|
|
2022-05-15 09:16:43 +00:00
|
|
|
// Start a log entry for this request
|
2023-02-17 11:02:29 +00:00
|
|
|
l := log.WithContext(r.Context()).
|
|
|
|
WithFields(kv.Fields{
|
|
|
|
{"pubKeyID", t.pubKeyID},
|
|
|
|
{"method", r.Method},
|
|
|
|
{"url", r.URL.String()},
|
|
|
|
}...)
|
2022-05-15 09:16:43 +00:00
|
|
|
|
2022-11-26 20:19:42 +00:00
|
|
|
r.Header.Set("User-Agent", t.controller.userAgent)
|
|
|
|
|
2022-05-15 09:16:43 +00:00
|
|
|
for i := 0; i < maxRetries; i++ {
|
2023-02-10 20:16:01 +00:00
|
|
|
var backoff time.Duration
|
|
|
|
|
2022-05-15 09:16:43 +00:00
|
|
|
// Reset signing header fields
|
|
|
|
now := t.controller.clock.Now().UTC()
|
|
|
|
r.Header.Set("Date", now.Format("Mon, 02 Jan 2006 15:04:05")+" GMT")
|
|
|
|
r.Header.Del("Signature")
|
|
|
|
r.Header.Del("Digest")
|
|
|
|
|
|
|
|
// Perform request signing
|
|
|
|
if err := signer(r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
l.Infof("performing request")
|
|
|
|
|
|
|
|
// Attempt to perform request
|
|
|
|
rsp, err := t.controller.client.Do(r)
|
2023-02-10 20:16:01 +00:00
|
|
|
if err == nil { //nolint:gocritic
|
2022-05-15 09:16:43 +00:00
|
|
|
// TooManyRequest means we need to slow
|
|
|
|
// down and retry our request. Codes over
|
|
|
|
// 500 generally indicate temp. outages.
|
|
|
|
if code := rsp.StatusCode; code < 500 &&
|
2023-02-10 20:16:01 +00:00
|
|
|
code != http.StatusTooManyRequests {
|
2022-05-15 09:16:43 +00:00
|
|
|
return rsp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate error from status code for logging
|
|
|
|
err = errors.New(`http response "` + rsp.Status + `"`)
|
2023-02-10 20:16:01 +00:00
|
|
|
|
|
|
|
// Search for a provided "Retry-After" header value.
|
|
|
|
if after := rsp.Header.Get("Retry-After"); after != "" {
|
|
|
|
|
|
|
|
if u, _ := strconv.ParseUint(after, 10, 32); u != 0 {
|
|
|
|
// An integer number of backoff seconds was provided.
|
|
|
|
backoff = time.Duration(u) * time.Second
|
|
|
|
} else if at, _ := http.ParseTime(after); !at.Before(now) {
|
|
|
|
// An HTTP formatted future date-time was provided.
|
|
|
|
backoff = at.Sub(now)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't let their provided backoff exceed our max.
|
|
|
|
if max := baseBackoff * maxRetries; backoff > max {
|
|
|
|
backoff = max
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-26 11:38:41 +00:00
|
|
|
} else if errorsv2.Is(err,
|
|
|
|
context.DeadlineExceeded,
|
|
|
|
context.Canceled,
|
|
|
|
httpclient.ErrInvalidRequest,
|
|
|
|
httpclient.ErrBodyTooLarge,
|
|
|
|
httpclient.ErrReservedAddr,
|
|
|
|
) {
|
|
|
|
// Return on non-retryable errors
|
2022-05-15 09:16:43 +00:00
|
|
|
return nil, err
|
|
|
|
} else if strings.Contains(err.Error(), "stopped after 10 redirects") {
|
|
|
|
// Don't bother if net/http returned after too many redirects
|
|
|
|
return nil, err
|
|
|
|
} else if errors.As(err, &x509.UnknownAuthorityError{}) {
|
|
|
|
// Unknown authority errors we do NOT recover from
|
|
|
|
return nil, err
|
2023-02-10 20:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if fastFail {
|
2022-11-23 21:40:07 +00:00
|
|
|
// on fast-fail, don't bother backoff/retry
|
|
|
|
return nil, fmt.Errorf("%w (fast fail)", err)
|
2022-05-15 09:16:43 +00:00
|
|
|
}
|
|
|
|
|
2023-02-10 20:16:01 +00:00
|
|
|
if backoff == 0 {
|
|
|
|
// No retry-after found, set our predefined backoff.
|
|
|
|
backoff = time.Duration(i) * baseBackoff
|
|
|
|
}
|
|
|
|
|
2022-05-15 09:16:43 +00:00
|
|
|
l.Errorf("backing off for %s after http request error: %v", backoff.String(), err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
// Request ctx cancelled
|
|
|
|
case <-r.Context().Done():
|
|
|
|
return nil, r.Context().Err()
|
|
|
|
|
|
|
|
// Backoff for some time
|
|
|
|
case <-time.After(backoff):
|
|
|
|
backoff *= 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-08 11:50:16 +00:00
|
|
|
// Add "bad" entry for this host
|
|
|
|
t.controller.badHosts.Set(host, struct{}{})
|
|
|
|
|
2022-05-15 09:16:43 +00:00
|
|
|
return nil, errors.New("transport reached max retries")
|
|
|
|
}
|
|
|
|
|
|
|
|
// signGET will safely sign an HTTP GET request.
|
|
|
|
func (t *transport) signGET(r *http.Request) (err error) {
|
|
|
|
t.safesign(func() {
|
|
|
|
err = t.getSigner.SignRequest(t.privkey, t.pubKeyID, r, nil)
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// signPOST will safely sign an HTTP POST request for given body.
|
|
|
|
func (t *transport) signPOST(r *http.Request, body []byte) (err error) {
|
|
|
|
t.safesign(func() {
|
|
|
|
err = t.postSigner.SignRequest(t.privkey, t.pubKeyID, r, body)
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// safesign will perform sign function within mutex protection,
|
|
|
|
// and ensured that httpsig.Signers are up-to-date.
|
|
|
|
func (t *transport) safesign(sign func()) {
|
|
|
|
// Perform within mu safety
|
|
|
|
t.signerMu.Lock()
|
|
|
|
defer t.signerMu.Unlock()
|
|
|
|
|
|
|
|
if now := time.Now(); now.After(t.signerExp) {
|
|
|
|
const expiry = 120
|
|
|
|
|
|
|
|
// Signers have expired and require renewal
|
|
|
|
t.getSigner, _ = NewGETSigner(expiry)
|
|
|
|
t.postSigner, _ = NewPOSTSigner(expiry)
|
|
|
|
t.signerExp = now.Add(time.Second * expiry)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform signing
|
|
|
|
sign()
|
2022-03-15 14:01:19 +00:00
|
|
|
}
|