From bc9843dfa37dd0714c0c20c8f5f21234815535c3 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Thu, 23 Jan 2020 11:47:35 -0500 Subject: [PATCH 1/3] Add timeout on ActivityPub requests --- activitypub.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/activitypub.go b/activitypub.go index a18a636..62294cd 100644 --- a/activitypub.go +++ b/activitypub.go @@ -1,5 +1,5 @@ /* - * Copyright © 2018-2019 A Bunch Tell LLC. + * Copyright © 2018-2020 A Bunch Tell LLC. * * This file is part of WriteFreely. * @@ -62,6 +62,12 @@ func (ru *RemoteUser) AsPerson() *activitystreams.Person { } } +func activityPubClient() http.Client { + return http.Client{ + Timeout: 15 * time.Second, + } +} + func handleFetchCollectionActivities(app *App, w http.ResponseWriter, r *http.Request) error { w.Header().Set("Server", serverSoftware) @@ -502,7 +508,7 @@ func makeActivityPost(hostName string, p *activitystreams.Person, url string, m } } - resp, err := http.DefaultClient.Do(r) + resp, err := activityPubClient().Do(r) if err != nil { return err } @@ -538,7 +544,7 @@ func resolveIRI(hostName, url string) ([]byte, error) { } } - resp, err := http.DefaultClient.Do(r) + resp, err := activityPubClient().Do(r) if err != nil { return nil, err } From 8d3e755c8f5b0afe3b5bb2950dd3806a4af08f45 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Thu, 23 Jan 2020 12:03:23 -0500 Subject: [PATCH 2/3] Return pointer to http.Client in activityPubClient() --- activitypub.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/activitypub.go b/activitypub.go index 62294cd..b133f9d 100644 --- a/activitypub.go +++ b/activitypub.go @@ -62,8 +62,8 @@ func (ru *RemoteUser) AsPerson() *activitystreams.Person { } } -func activityPubClient() http.Client { - return http.Client{ +func activityPubClient() *http.Client { + return &http.Client{ Timeout: 15 * time.Second, } } From bf8dcff01eb26e0dcb9941fdf151d5c3f4af8317 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Mon, 27 Jan 2020 09:19:12 -0500 Subject: [PATCH 3/3] Quit AP goroutine early when there's no "to" Previously, we'd sleep for 2 seconds and then return for no reason. This fixes that. --- activitypub.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/activitypub.go b/activitypub.go index b133f9d..ba2bb27 100644 --- a/activitypub.go +++ b/activitypub.go @@ -388,6 +388,11 @@ func handleFetchCollectionInbox(app *App, w http.ResponseWriter, r *http.Request } go func() { + if to == nil { + log.Error("No to! %v", err) + return + } + time.Sleep(2 * time.Second) am, err := a.Serialize() if err != nil { @@ -396,10 +401,6 @@ func handleFetchCollectionInbox(app *App, w http.ResponseWriter, r *http.Request } am["@context"] = []string{activitystreams.Namespace} - if to == nil { - log.Error("No to! %v", err) - return - } err = makeActivityPost(app.cfg.App.Host, p, fullActor.Inbox, am) if err != nil { log.Error("Unable to make activity POST: %v", err)