2019-02-12 08:16:33 +09:00
|
|
|
package pub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto"
|
|
|
|
"fmt"
|
|
|
|
"github.com/go-fed/httpsig"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2019-02-15 05:51:57 +09:00
|
|
|
"strings"
|
2019-02-14 06:37:29 +09:00
|
|
|
"sync"
|
2019-02-12 08:16:33 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// acceptHeaderValue is the Accept header value indicating that the
|
|
|
|
// response should contain an ActivityStreams object.
|
|
|
|
acceptHeaderValue = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
|
|
|
|
)
|
|
|
|
|
2019-05-04 17:02:35 +09:00
|
|
|
// isSuccess returns true if the HTTP status code is either OK, Created, or
|
|
|
|
// Accepted.
|
|
|
|
func isSuccess(code int) bool {
|
|
|
|
return code == http.StatusOK ||
|
|
|
|
code == http.StatusCreated ||
|
|
|
|
code == http.StatusAccepted
|
|
|
|
}
|
|
|
|
|
2019-02-16 05:03:43 +09:00
|
|
|
// Transport makes ActivityStreams calls to other servers in order to send or
|
|
|
|
// receive ActivityStreams data.
|
|
|
|
//
|
|
|
|
// It is responsible for setting the appropriate request headers, signing the
|
|
|
|
// requests if needed, and facilitating the traffic between this server and
|
|
|
|
// another.
|
|
|
|
//
|
|
|
|
// The transport is exclusively used to issue requests on behalf of an actor,
|
|
|
|
// and is never sending requests on behalf of the server in general.
|
2019-02-14 06:37:29 +09:00
|
|
|
//
|
|
|
|
// It may be reused multiple times, but never concurrently.
|
2019-02-12 08:16:33 +09:00
|
|
|
type Transport interface {
|
|
|
|
// Dereference fetches the ActivityStreams object located at this IRI
|
|
|
|
// with a GET request.
|
|
|
|
Dereference(c context.Context, iri *url.URL) ([]byte, error)
|
|
|
|
// Deliver sends an ActivityStreams object.
|
|
|
|
Deliver(c context.Context, b []byte, to *url.URL) error
|
2019-02-14 06:37:29 +09:00
|
|
|
// BatchDeliver sends an ActivityStreams object to multiple recipients.
|
|
|
|
BatchDeliver(c context.Context, b []byte, recipients []*url.URL) error
|
2019-02-12 08:16:33 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// Transport must be implemented by HttpSigTransport.
|
|
|
|
var _ Transport = &HttpSigTransport{}
|
|
|
|
|
|
|
|
// HttpSigTransport makes a dereference call using HTTP signatures to
|
|
|
|
// authenticate the request on behalf of a particular actor.
|
|
|
|
//
|
|
|
|
// No rate limiting is applied.
|
|
|
|
//
|
|
|
|
// Only one request is tried per call.
|
|
|
|
type HttpSigTransport struct {
|
2019-05-11 18:33:55 +09:00
|
|
|
client HttpClient
|
|
|
|
appAgent string
|
|
|
|
gofedAgent string
|
|
|
|
clock Clock
|
|
|
|
getSigner httpsig.Signer
|
|
|
|
getSignerMu *sync.Mutex
|
|
|
|
postSigner httpsig.Signer
|
|
|
|
postSignerMu *sync.Mutex
|
|
|
|
pubKeyId string
|
|
|
|
privKey crypto.PrivateKey
|
2019-02-12 08:16:33 +09:00
|
|
|
}
|
|
|
|
|
2019-02-16 05:03:43 +09:00
|
|
|
// NewHttpSigTransport returns a new Transport.
|
|
|
|
//
|
|
|
|
// It sends requests specifically on behalf of a specific actor on this server.
|
|
|
|
// The actor's credentials are used to add an HTTP Signature to requests, which
|
|
|
|
// requires an actor's private key, a unique identifier for their public key,
|
|
|
|
// and an HTTP Signature signing algorithm.
|
|
|
|
//
|
|
|
|
// The client lets users issue requests through any HTTP client, including the
|
|
|
|
// standard library's HTTP client.
|
|
|
|
//
|
|
|
|
// The appAgent uniquely identifies the calling application's requests, so peers
|
|
|
|
// may aid debugging the requests incoming from this server. Note that the
|
|
|
|
// agent string will also include one for go-fed, so at minimum peer servers can
|
|
|
|
// reach out to the go-fed library to aid in notifying implementors of malformed
|
|
|
|
// or unsupported requests.
|
2019-02-12 08:16:33 +09:00
|
|
|
func NewHttpSigTransport(
|
|
|
|
client HttpClient,
|
2019-02-16 05:03:43 +09:00
|
|
|
appAgent string,
|
2019-02-12 08:16:33 +09:00
|
|
|
clock Clock,
|
2019-04-05 04:38:08 +09:00
|
|
|
getSigner, postSigner httpsig.Signer,
|
2019-02-12 08:16:33 +09:00
|
|
|
pubKeyId string,
|
|
|
|
privKey crypto.PrivateKey) *HttpSigTransport {
|
|
|
|
return &HttpSigTransport{
|
2019-05-11 18:33:55 +09:00
|
|
|
client: client,
|
|
|
|
appAgent: appAgent,
|
|
|
|
gofedAgent: goFedUserAgent(),
|
|
|
|
clock: clock,
|
|
|
|
getSigner: getSigner,
|
|
|
|
getSignerMu: &sync.Mutex{},
|
|
|
|
postSigner: postSigner,
|
|
|
|
postSignerMu: &sync.Mutex{},
|
|
|
|
pubKeyId: pubKeyId,
|
|
|
|
privKey: privKey,
|
2019-02-12 08:16:33 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-16 05:03:43 +09:00
|
|
|
// Dereference sends a GET request signed with an HTTP Signature to obtain an
|
|
|
|
// ActivityStreams value.
|
2019-02-12 08:16:33 +09:00
|
|
|
func (h HttpSigTransport) Dereference(c context.Context, iri *url.URL) ([]byte, error) {
|
|
|
|
req, err := http.NewRequest("GET", iri.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.WithContext(c)
|
|
|
|
req.Header.Add(acceptHeader, acceptHeaderValue)
|
|
|
|
req.Header.Add("Accept-Charset", "utf-8")
|
|
|
|
req.Header.Add("Date", h.clock.Now().UTC().Format("Mon, 02 Jan 2006 15:04:05")+" GMT")
|
|
|
|
req.Header.Add("User-Agent", fmt.Sprintf("%s %s", h.appAgent, h.gofedAgent))
|
2019-05-11 18:33:55 +09:00
|
|
|
h.getSignerMu.Lock()
|
2019-09-13 19:18:49 +09:00
|
|
|
err = h.getSigner.SignRequest(h.privKey, h.pubKeyId, req, nil)
|
2019-05-11 18:33:55 +09:00
|
|
|
h.getSignerMu.Unlock()
|
2019-02-12 08:16:33 +09:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resp, err := h.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return nil, fmt.Errorf("GET request to %s failed (%d): %s", iri.String(), resp.StatusCode, resp.Status)
|
|
|
|
}
|
|
|
|
return ioutil.ReadAll(resp.Body)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deliver sends a POST request with an HTTP Signature.
|
|
|
|
func (h HttpSigTransport) Deliver(c context.Context, b []byte, to *url.URL) error {
|
|
|
|
byteCopy := make([]byte, len(b))
|
|
|
|
copy(byteCopy, b)
|
|
|
|
buf := bytes.NewBuffer(byteCopy)
|
|
|
|
req, err := http.NewRequest("POST", to.String(), buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.WithContext(c)
|
|
|
|
req.Header.Add(contentTypeHeader, contentTypeHeaderValue)
|
|
|
|
req.Header.Add("Accept-Charset", "utf-8")
|
|
|
|
req.Header.Add("Date", h.clock.Now().UTC().Format("Mon, 02 Jan 2006 15:04:05")+" GMT")
|
|
|
|
req.Header.Add("User-Agent", fmt.Sprintf("%s %s", h.appAgent, h.gofedAgent))
|
2019-05-11 18:33:55 +09:00
|
|
|
h.postSignerMu.Lock()
|
2019-09-13 19:18:49 +09:00
|
|
|
err = h.postSigner.SignRequest(h.privKey, h.pubKeyId, req, b)
|
2019-05-11 18:33:55 +09:00
|
|
|
h.postSignerMu.Unlock()
|
2019-02-12 08:16:33 +09:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp, err := h.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2019-05-04 17:02:35 +09:00
|
|
|
if !isSuccess(resp.StatusCode) {
|
2019-02-12 08:16:33 +09:00
|
|
|
return fmt.Errorf("POST request to %s failed (%d): %s", to.String(), resp.StatusCode, resp.Status)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-14 06:37:29 +09:00
|
|
|
// BatchDeliver sends concurrent POST requests. Returns an error if any of the
|
|
|
|
// requests had an error.
|
|
|
|
func (h HttpSigTransport) BatchDeliver(c context.Context, b []byte, recipients []*url.URL) error {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
errCh := make(chan error, len(recipients))
|
|
|
|
for _, recipient := range recipients {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(r *url.URL) {
|
|
|
|
defer wg.Done()
|
|
|
|
if err := h.Deliver(c, b, r); err != nil {
|
|
|
|
errCh <- err
|
|
|
|
}
|
|
|
|
}(recipient)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
errs := make([]string, 0, len(recipients))
|
2019-05-04 04:44:18 +09:00
|
|
|
outer:
|
2019-02-14 06:37:29 +09:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case e := <-errCh:
|
|
|
|
errs = append(errs, e.Error())
|
|
|
|
default:
|
2019-05-04 04:44:18 +09:00
|
|
|
break outer
|
2019-02-14 06:37:29 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(errs) > 0 {
|
|
|
|
return fmt.Errorf("batch deliver had at least one failure: %s", strings.Join(errs, "; "))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-12 08:16:33 +09:00
|
|
|
// HttpClient sends http requests, and is an abstraction only needed by the
|
|
|
|
// HttpSigTransport. The standard library's Client satisfies this interface.
|
|
|
|
type HttpClient interface {
|
|
|
|
Do(req *http.Request) (*http.Response, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// HttpClient must be implemented by http.Client.
|
|
|
|
var _ HttpClient = &http.Client{}
|