-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclient.go
More file actions
481 lines (410 loc) · 12.9 KB
/
Copy pathclient.go
File metadata and controls
481 lines (410 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package client
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io"
"net"
"net/http"
"time"
"git.sr.ht/~mariusor/cache"
"git.sr.ht/~mariusor/lw"
vocab "github.com/go-ap/activitypub"
"github.com/go-ap/client/debug"
"github.com/go-ap/client/internal/requests"
"github.com/go-ap/errors"
"github.com/go-ap/jsonld"
"golang.org/x/oauth2"
)
type Ctx = lw.Ctx
type RequestSignFn func(*http.Request) error
type CtxLogFn func(...Ctx) LogFn
type LogFn func(string, ...any)
type Basic interface {
LoadIRI(vocab.IRI) (vocab.Item, error)
CtxLoadIRI(context.Context, vocab.IRI) (vocab.Item, error)
ToCollection(vocab.Item, ...vocab.IRI) (vocab.IRI, vocab.Item, error)
CtxToCollection(context.Context, vocab.Item, ...vocab.IRI) (vocab.IRI, vocab.Item, error)
}
// NOTE(marius): these are exported to allow calling code to use them.
const (
ContentTypeJsonLD = jsonld.ContentType
// ContentTypeJsonActivity This specification registers the application/activity+json MIME Media Type
// specifically for identifying documents conforming to the Activity Streams 2.0 format.
//
// https://www.w3.org/TR/activitystreams-core/#media-type
ContentTypeJsonActivity = requests.ContentTypeJsonActivity
ContentTypeJson = "application/json;q=0.9"
)
// UserAgent value that the client uses when performing requests
var UserAgent = "GoAP-Client (+https://github.com/go-ap)"
var nilLogger = lw.Nil()
type httpClient interface {
Do(*http.Request) (*http.Response, error)
}
type C struct {
c httpClient
l lw.Logger
ua string
authFns []func(*http.Request) error
proxyURL vocab.IRI
}
// WithHTTPClient sets the http client
func WithHTTPClient(h *http.Client) OptionFn {
return func(c *C) {
c.c = h
}
}
func WithAuthorizationFn(fns ...func(*http.Request) error) OptionFn {
return func(c *C) {
c.authFns = append(c.authFns, fns...)
}
}
func WithLogger(l lw.Logger) OptionFn {
return func(c *C) {
c.l = l
}
}
func getTransportWithTLSValidation(rt http.RoundTripper, skip bool) http.RoundTripper {
if rt == nil {
rt = defaultTransport
}
switch tr := rt.(type) {
case *http.Transport:
if tr.TLSClientConfig == nil {
tr.TLSClientConfig = new(tls.Config)
}
tr.TLSClientConfig.InsecureSkipVerify = skip
case *debug.Transport:
tr.Base = getTransportWithTLSValidation(tr.Base, skip)
case *oauth2.Transport:
tr.Base = getTransportWithTLSValidation(tr.Base, skip)
case *cache.Transport:
tr.Base = getTransportWithTLSValidation(tr.Base, skip)
}
return rt
}
// SkipTLSValidation sets the flag for skipping TLS validation on the default HTTP transport.
func SkipTLSValidation(skip bool) OptionFn {
return func(c *C) {
if cl, ok := c.c.(*http.Client); ok {
getTransportWithTLSValidation(cl.Transport, skip)
}
}
}
// WithUserAgent explicitly sets the UserAgent set by the client
func WithUserAgent(ua string) OptionFn {
return func(c *C) {
c.ua = ua
}
}
// WithProxyURL explicitly sets a ProxyURL to be used in order for fetch Social API requests
// to be proxied through the ActivityPub server it designates.
func WithProxyURL(u vocab.IRI) OptionFn {
return func(c *C) {
c.proxyURL = u
}
}
// OptionFn is the type designating setup functions accepted by the [client.New] initializer.
type OptionFn func(s *C)
const MByte = 1024 * 1024 * 1024
var (
defaultClient = &http.Client{
Timeout: 10 * time.Second,
Transport: cache.Shared(defaultTransport, cache.Mem(MByte)),
}
// This is the TCP connect timeout in this instance.
longTimeout = 2500 * time.Millisecond
defaultTransport http.RoundTripper = &http.Transport{
Proxy: http.ProxyFromEnvironment,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
MaxIdleConnsPerHost: 20,
DialContext: (&net.Dialer{Timeout: longTimeout}).DialContext,
TLSClientConfig: &tls.Config{InsecureSkipVerify: DefaultInsecureSkipVerify},
TLSHandshakeTimeout: longTimeout,
}
)
func New(o ...OptionFn) *C {
c := &C{c: defaultClient, ua: UserAgent, l: nilLogger}
for _, fn := range o {
fn(c)
}
return c
}
var TimeNow = func() time.Time { return time.Now().Truncate(time.Millisecond).UTC() }
func (c C) loadCtx(ctx context.Context, id vocab.IRI) (vocab.Item, error) {
errCtx := Ctx{"IRI": id}
st := TimeNow()
if len(id) == 0 {
return nil, errf("invalid nil IRI")
}
if _, err := id.URL(); err != nil {
return nil, errf("trying to load an invalid IRI").iri(id).annotate(err)
}
var obj vocab.Item
resp, err := c.CtxGet(ctx, id.String())
if err != nil {
c.l.WithContext(errCtx, Ctx{"err": err.Error()}).Errorf("failed to load IRI")
return obj, err
}
defer func() {
_ = resp.Body.Close()
}()
errCtx["duration"] = time.Since(st)
errCtx["status"] = resp.StatusCode
if val := resp.Header.Get("Signature-Input"); val != "" {
errCtx["sig-input"] = val
}
if val := resp.Header.Get("Signature"); val != "" {
errCtx["sig"] = val
}
if val := resp.Header.Get("Authorization"); val != "" {
errCtx["auth"] = val
}
if val := resp.Header.Get("ETag"); val != "" {
errCtx["etag"] = val
}
if val := resp.Header.Get("User-Agent"); val != "" {
errCtx["ua"] = val
}
body, err := io.ReadAll(resp.Body)
if err != nil {
c.l.WithContext(errCtx, Ctx{"err": err}).Errorf("unable to read response body")
return obj, err
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusGone {
c.l.WithContext(errCtx).Errorf("error response received")
errb, _ := errors.UnmarshalJSON(body)
if len(errb) > 0 {
err = errf("invalid status received").status(resp.StatusCode).iri(id).annotate(errors.Join(errb...))
} else {
// NOTE(marius): treat the body as a wrapped error
err = errf("invalid status received").status(resp.StatusCode).iri(id).annotate(fmt.Errorf("%s", body[:min(512, len(body))]))
}
return obj, err
}
it, err := vocab.UnmarshalJSON(body)
if err != nil {
return nil, errf("invalid ActivityPub object returned").iri(id).annotate(err)
}
if it != nil {
// NOTE(marius): success
return it, nil
}
// NOTE(marius): the body didn't have a recognizable ActivityPub document,
// maybe it's an error due to being deleted
if resp.StatusCode == http.StatusGone {
e, err := errors.UnmarshalJSON(body)
if err != nil || len(e) == 0 {
return it, errf("").iri(id).annotate(errors.Gonef("gone"))
}
return it, errf("unable to load IRI").iri(id).annotate(errors.NewGone(errors.Join(e...), ""))
}
return nil, errf("invalid response from ActivityPub server").annotate(errors.NotImplementedf("not a document and not an error")).iri(id)
}
// CtxLoadIRI tries to dereference an IRI and load the full ActivityPub object it represents
func (c C) CtxLoadIRI(ctx context.Context, id vocab.IRI) (vocab.Item, error) {
return c.loadCtx(ctx, id)
}
// LoadIRI tries to dereference an IRI and load the full ActivityPub object it represents
func (c C) LoadIRI(id vocab.IRI) (vocab.Item, error) {
return c.loadCtx(context.Background(), id)
}
func (c C) FetchRequest(ctx context.Context, url string) (*http.Request, error) {
return FetchRequest(ctx, url, http.MethodGet)
}
func (c C) PostRequest(ctx context.Context, url, contentType string, body io.Reader) (*http.Request, error) {
return ActivityPubRequest(ctx, url, contentType, body)
}
var ErrRetry = errors.Newf("retry")
// cloneRequest returns a clone of the provided *http.Request.
// The clone is a shallow copy of the struct and its Header map.
func cloneRequest(r *http.Request, last bool) *http.Request {
r2 := r.Clone(r.Context())
if r.Body != nil {
ob := r.Body
buff, err := io.ReadAll(r.Body)
if err == nil && buff != nil {
if !last {
// NOTE(marius): this is the last try,
// so we don't need the original request to have a valid body
r.Body = io.NopCloser(bytes.NewReader(buff))
}
r2.Body = io.NopCloser(bytes.NewReader(buff))
}
// NOTE(marius): we close the old request body
_ = ob.Close()
}
return r2
}
func (c C) Do(req *http.Request) (*http.Response, error) {
if c.c == nil {
c.c = defaultClient
}
if ua := req.Header.Get("User-Agent"); len(ua) == 0 && len(c.ua) > 0 {
req.Header.Set("User-Agent", c.ua)
}
if len(c.authFns) > 0 {
return c.doRetry(req)
}
// NOTE(marius): try without a signing function
return c.c.Do(req)
}
func (c C) doRetry(req *http.Request) (res *http.Response, err error) {
try := 0
roundTripFn := func(req *http.Request) (*http.Response, error) {
lc := lw.Ctx{}
lc["host"] = req.URL.Hostname()
if try > 0 {
lc["retry"] = try
}
res, err := c.c.Do(req)
// NOTE(marius): the client failed for some reason, or we tried with all signing functions.
if try == len(c.authFns)-1 || err != nil {
return res, err
}
try++
switch res.StatusCode {
case http.StatusServiceUnavailable:
// NOTE(marius): this is a hack for mastoart.social
// which returns a 503 if it encountered previous errors.
fallthrough
case http.StatusBadRequest:
// NOTE(marius): this is a hack for tags.pub that doesn't
// return a 403 or 401 error status on failing signatures
// See https://todo.sr.ht/~mariusor/go-activitypub/473
fallthrough
case http.StatusNotFound:
// NOTE(marius): many services, among which the GoActivityPub ones, return not found
// for resources that are actually forbidden.
fallthrough
case http.StatusUnauthorized, http.StatusForbidden:
// NOTE(marius): Not an acceptable response status, so we want to try again.
lc["status"] = res.StatusCode
c.l.WithContext(lc).Errorf("error response from remote server")
_, _ = io.Copy(io.Discard, res.Body)
_ = res.Body.Close()
return nil, ErrRetry
default:
// NOTE(marius): some kind of success
return res, nil
}
}
for i, signFn := range c.authFns {
r2 := cloneRequest(req, i == len(c.authFns)-1)
if err = signFn(r2); err != nil {
continue
}
res, err = roundTripFn(r2)
if err == nil || !errors.Is(err, ErrRetry) {
break
}
}
return res, err
}
// CtxGet wrapper over the functionality offered by the default http.Client object
func (c C) CtxGet(ctx context.Context, url string) (*http.Response, error) {
req, err := FetchRequest(ctx, url, http.MethodGet)
if err != nil {
return nil, err
}
return c.Do(req)
}
func (c C) toCollections(ctx context.Context, act vocab.Item, colIRI ...vocab.IRI) (vocab.IRI, vocab.Item, error) {
result := make(vocab.ItemCollection, 0, len(colIRI))
actIRIs := make(vocab.IRIs, 0, len(colIRI))
for _, iri := range colIRI {
actIRI, it, err := c.toCollection(ctx, act, iri)
if err != nil {
return "", result, err
}
if !vocab.IsNil(it) {
_ = result.Append(it)
}
if !actIRI.Equal("") {
_ = actIRIs.Append(actIRI)
}
}
var it vocab.Item
var iri vocab.IRI
// NOTE(marius): currently I don't know how to return multiple IRIs if we have multiple actors,
// so we currently do the wrong thing for len(iris) > 1 and return only the IRI of the first activity.
if len(actIRIs) >= 1 {
iri = actIRIs[0]
}
// NOTE(marius): we return the created object if there was only one actor, otherwise a collection of them.
if len(result) == 1 {
it = result[0]
} else if len(result) > 1 {
it = result
}
return iri, it, nil
}
func (c C) toCollection(ctx context.Context, act vocab.Item, colIRI vocab.IRI) (vocab.IRI, vocab.Item, error) {
if len(colIRI) == 0 {
return "", nil, errf("invalid IRI to POST to")
}
cont, err := jsonld.WithContext(jsonld.IRI(vocab.ActivityBaseURI), jsonld.IRI(vocab.SecurityContextURI)).Marshal(act)
if err != nil {
return "", nil, errf("unable to marshal activity").iri(colIRI)
}
req, err := ActivityPubRequest(ctx, string(colIRI), requests.ContentTypeJsonActivity, bytes.NewReader(cont))
if err != nil {
return "", nil, err
}
resp, err := c.Do(req)
if err != nil {
return "", nil, err
}
resultIRI := vocab.IRI(resp.Header.Get("Location"))
if resp.StatusCode >= http.StatusBadRequest && resp.StatusCode != http.StatusGone {
if err = errors.FromResponse(resp); err == nil {
err = errf("invalid status received: %d", resp.StatusCode).iri(resultIRI)
} else {
err = errf("invalid status received: %d", resp.StatusCode).iri(resultIRI).annotate(err)
}
return resultIRI, nil, err
}
defer func() {
_ = resp.Body.Close()
}()
body, err := io.ReadAll(resp.Body)
if err != nil {
c.l.WithContext(Ctx{"iri": colIRI, "status": resp.Status, "err": err}).Errorf("failed to read response body")
return resultIRI, nil, err
}
if len(body) == 0 {
return resultIRI, nil, nil
}
it, err := vocab.UnmarshalJSON(body)
if err != nil {
return resultIRI, nil, err
}
return resultIRI, it, nil
}
// ToCollection
func (c C) ToCollection(a vocab.Item, url ...vocab.IRI) (vocab.IRI, vocab.Item, error) {
return c.toCollections(context.Background(), a, url...)
}
// CtxToCollection
func (c C) CtxToCollection(ctx context.Context, a vocab.Item, url ...vocab.IRI) (vocab.IRI, vocab.Item, error) {
return c.toCollections(ctx, a, url...)
}
func HTTPClient(c httpClient) *http.Client {
if c == nil {
return nil
}
switch httpC := c.(type) {
case *C:
if httpC == nil {
return nil
}
return HTTPClient(httpC.c)
case *http.Client:
return httpC
default:
return nil
}
}