From 94c92948ae7ae669b6b6466ef4b7f00cc430f7ef Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Mon, 28 Jun 2021 11:08:15 -0700 Subject: added proxy wrapper with tor support --- cacheDatabase.go | 2 +- client.go | 34 +--------------------------------- main.go | 24 +++++++++++------------- outboxPost.go | 35 +++++++---------------------------- 4 files changed, 20 insertions(+), 75 deletions(-) diff --git a/cacheDatabase.go b/cacheDatabase.go index 5acead7..22a4a79 100644 --- a/cacheDatabase.go +++ b/cacheDatabase.go @@ -258,7 +258,7 @@ func WriteObjectReplyCache(db *sql.DB, obj ObjectBase) { } func WriteActorToCache(db *sql.DB, actorID string) { - actor := GetActor(actorID) + actor := FingerActor(actorID) collection := GetActorCollection(actor.Outbox) for _, e := range collection.OrderedItems { diff --git a/client.go b/client.go index 91f5f1f..b7f1938 100644 --- a/client.go +++ b/client.go @@ -9,8 +9,6 @@ import "strings" import "strconv" import "sort" import "regexp" -import "io/ioutil" -import "encoding/json" import "os" var Key *string = new(string) @@ -241,43 +239,13 @@ func PostGet(w http.ResponseWriter, r *http.Request, db *sql.DB){ t.ExecuteTemplate(w, "layout", returnData) } -func GetRemoteActor(id string) Actor { - - var respActor Actor - - id = StripTransferProtocol(id) - - req, err := http.NewRequest("GET", "http://" + id, nil) - - CheckError(err, "error with getting actor req") - - req.Header.Set("Accept", activitystreams) - - resp, err := http.DefaultClient.Do(req) - - if err != nil || resp.StatusCode != 200 { - fmt.Println("could not get actor from " + id) - return respActor - } - - defer resp.Body.Close() - - body, _ := ioutil.ReadAll(resp.Body) - - err = json.Unmarshal(body, &respActor) - - CheckError(err, "error getting actor from body") - - return respActor -} - func GetBoardCollection(db *sql.DB) []Board { var collection []Board for _, e := range FollowingBoards { var board Board boardActor := GetActorFromDB(db, e.Id) if boardActor.Id == "" { - boardActor = GetRemoteActor(e.Id) + boardActor = FingerActor(e.Id) } board.Name = "/" + boardActor.Name + "/" board.PrefName = boardActor.PreferredUsername diff --git a/main.go b/main.go index 4333d36..c632e53 100644 --- a/main.go +++ b/main.go @@ -35,9 +35,7 @@ var SiteEmailPassword = GetConfigValue("emailpass") var SiteEmailServer = GetConfigValue("emailserver") //mail.fchan.xyz var SiteEmailPort = GetConfigValue("emailport") //587 -var TorProxy = "127.0.0.1:9050" - -var ldjson = "application/ld+json" +var TorProxy = GetConfigValue("torproxy") //127.0.0.1:9050 var activitystreams = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"" @@ -370,7 +368,7 @@ func main() { req.Header.Set("Content-Type", we.FormDataContentType()) - resp, err := http.DefaultClient.Do(req) + resp, err := RouteProxy(req) CheckError(err, "error with post form resp") @@ -1502,7 +1500,7 @@ func CheckValidActivity(id string) (Collection, bool) { req.Header.Set("Accept", activitystreams) - resp, err := http.DefaultClient.Do(req) + resp, err := RouteProxy(req) if err != nil { fmt.Println("error with response") @@ -1542,7 +1540,7 @@ func GetActor(id string) Actor { req.Header.Set("Accept", activitystreams) - resp, err := http.DefaultClient.Do(req) + resp, err := RouteProxy(req) if err != nil { fmt.Println("error with getting actor resp " + id) @@ -1573,7 +1571,7 @@ func GetActorCollection(collection string) Collection { req.Header.Set("Accept", activitystreams) - resp, err := http.DefaultClient.Do(req) + resp, err := RouteProxy(req) if err != nil { fmt.Println("error with getting actor collection resp " + collection) @@ -1849,7 +1847,7 @@ func MakeActivityRequestOutbox(db *sql.DB, activity Activity) { req.Header.Set("Signature", signature) req.Host = instance - _, err = http.DefaultClient.Do(req) + _, err = RouteProxy(req) CheckError(err, "error with sending activity resp to") } @@ -1887,7 +1885,7 @@ func MakeActivityRequest(db *sql.DB, activity Activity) { req.Header.Set("Signature", signature) req.Host = instance - _, err = http.DefaultClient.Do(req) + _, err = RouteProxy(req) CheckError(err, "error with sending activity resp to") } @@ -1905,7 +1903,7 @@ func GetCollectionFromID(id string) Collection { req.Header.Set("Accept", activitystreams) - resp, err := http.DefaultClient.Do(req) + resp, err := RouteProxy(req) if err != nil { return nColl @@ -2162,7 +2160,7 @@ func GetActorCollectionReq(r *http.Request, collection string) Collection { req.Header.Set("Authorization", "Basic " + pass) - resp, err := http.DefaultClient.Do(req) + resp, err := RouteProxy(req) CheckError(err, "error with getting actor collection resp " + collection) @@ -2327,7 +2325,7 @@ func FingerRequest(actor string, instance string) (*http.Response){ CheckError(err, "could not get finger request from id req") - resp, err := http.DefaultClient.Do(req) + resp, err := RouteProxy(req) var finger Webfinger @@ -2354,7 +2352,7 @@ func FingerRequest(actor string, instance string) (*http.Response){ req.Header.Set("Accept", activitystreams) - resp, err := http.DefaultClient.Do(req) + resp, err := RouteProxy(req) return resp } } diff --git a/outboxPost.go b/outboxPost.go index 5772932..cc66677 100644 --- a/outboxPost.go +++ b/outboxPost.go @@ -366,8 +366,7 @@ func ObjectFromForm(r *http.Request, db *sql.DB, obj ObjectBase) ObjectBase { if originalPost.Id != "" { if !IsActivityLocal(db, activity) { - id := FingerActor(originalPost.Id).Id - actor := GetActor(id) + actor := FingerActor(originalPost.Id) if !IsInStringArray(obj.To, actor.Id) { obj.To = append(obj.To, actor.Id) } @@ -395,8 +394,7 @@ func ObjectFromForm(r *http.Request, db *sql.DB, obj ObjectBase) ObjectBase { activity.To = append(activity.To, e.Id) if !IsActivityLocal(db, activity) { - id := FingerActor(e.Id).Id - actor := GetActor(id) + actor := FingerActor(e.Id) if !IsInStringArray(obj.To, actor.Id) { obj.To = append(obj.To, actor.Id) } @@ -579,10 +577,14 @@ func ParseInboxRequest(w http.ResponseWriter, r *http.Request, db *sql.DB) { func MakeActivityFollowingReq(w http.ResponseWriter, r *http.Request, activity Activity) bool { actor := GetActor(activity.Object.Id) - resp, err := http.NewRequest("POST", actor.Inbox, nil) + req, err := http.NewRequest("POST", actor.Inbox, nil) CheckError(err, "Cannot make new get request to actor inbox for following req") + resp, err := RouteProxy(req) + + CheckError(err, "could not make remote actor auth resp") + defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) @@ -597,26 +599,3 @@ func MakeActivityFollowingReq(w http.ResponseWriter, r *http.Request, activity A return false } - -func RemoteActorHasAuth(actor string, code string) bool { - - if actor == "" || code == "" { - return false - } - - req, err := http.NewRequest("GET", actor + "/verification&code=" + code, nil) - - CheckError(err, "could not make remote actor auth req") - - resp, err := http.DefaultClient.Do(req) - - CheckError(err, "could not make remote actor auth resp") - - defer resp.Body.Close() - - if resp.StatusCode == 200 { - return true - } - - return false -} -- cgit v1.2.3