2019-09-04 12:33:32 +03:00
|
|
|
package activityserve
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-09-10 11:21:39 +03:00
|
|
|
"io/ioutil"
|
2019-09-04 12:33:32 +03:00
|
|
|
"net/http"
|
2019-09-10 11:21:39 +03:00
|
|
|
"strconv"
|
2019-09-04 12:33:32 +03:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gologme/log"
|
|
|
|
"github.com/gorilla/mux"
|
2019-10-29 06:14:36 +03:00
|
|
|
"github.com/writefreely/go-nodeinfo"
|
2024-05-16 22:31:02 +03:00
|
|
|
"github.com/go-fed/httpsig"
|
2019-09-04 12:33:32 +03:00
|
|
|
"encoding/json"
|
|
|
|
)
|
|
|
|
|
2019-09-20 16:56:00 +03:00
|
|
|
// ServeSingleActor just simplifies the call from main so
|
|
|
|
// that onboarding is as easy as possible
|
|
|
|
func ServeSingleActor(actor Actor) {
|
|
|
|
Serve(map[string]Actor{actor.Name: actor})
|
|
|
|
}
|
|
|
|
|
2019-09-10 11:21:39 +03:00
|
|
|
// Serve starts an http server with all the required handlers
|
2019-09-20 16:21:21 +03:00
|
|
|
func Serve(actors map[string]Actor) {
|
2019-09-04 12:33:32 +03:00
|
|
|
|
|
|
|
var webfingerHandler http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("content-type", "application/jrd+json; charset=utf-8")
|
|
|
|
account := r.URL.Query().Get("resource") // should be something like acct:user@example.com
|
|
|
|
account = strings.Replace(account, "acct:", "", 1) // remove acct:
|
|
|
|
server := strings.Split(baseURL, "://")[1] // remove protocol from baseURL. Should get example.com
|
|
|
|
server = strings.TrimSuffix(server, "/") // remove protocol from baseURL. Should get example.com
|
|
|
|
account = strings.Replace(account, "@"+server, "", 1) // remove server from handle. Should get user
|
|
|
|
actor, err := LoadActor(account)
|
|
|
|
// error out if this actor does not exist
|
|
|
|
if err != nil {
|
|
|
|
log.Info("No such actor")
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
fmt.Fprintf(w, "404 - actor not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// response := `{"subject":"acct:` + actor.name + `@` + server + `","aliases":["` + baseURL + actor.name + `","` + baseURL + actor.name + `"],"links":[{"href":"` + baseURL + `","type":"text/html","rel":"https://webfinger.net/rel/profile-page"},{"href":"` + baseURL + actor.name + `","type":"application/activity+json","rel":"self"}]}`
|
|
|
|
|
|
|
|
responseMap := make(map[string]interface{})
|
|
|
|
|
2019-09-20 16:21:21 +03:00
|
|
|
responseMap["subject"] = "acct:" + actor.Name + "@" + server
|
2019-09-10 11:21:39 +03:00
|
|
|
// links is a json array with a single element
|
|
|
|
var links [1]map[string]string
|
|
|
|
link1 := make(map[string]string)
|
|
|
|
link1["rel"] = "self"
|
|
|
|
link1["type"] = "application/activity+json"
|
2019-09-20 16:21:21 +03:00
|
|
|
link1["href"] = baseURL + actor.Name
|
2019-09-10 11:21:39 +03:00
|
|
|
links[0] = link1
|
2019-09-04 12:33:32 +03:00
|
|
|
responseMap["links"] = links
|
|
|
|
|
|
|
|
response, err := json.Marshal(responseMap)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("problem creating the webfinger response json")
|
|
|
|
}
|
2019-09-10 11:21:39 +03:00
|
|
|
PrettyPrintJSON(response)
|
2019-09-04 12:33:32 +03:00
|
|
|
w.Write([]byte(response))
|
|
|
|
}
|
|
|
|
|
|
|
|
var actorHandler http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("content-type", "application/activity+json; charset=utf-8")
|
2019-09-10 11:21:39 +03:00
|
|
|
log.Info("Remote server " + r.RemoteAddr + " just fetched our /actor endpoint")
|
2019-09-04 12:33:32 +03:00
|
|
|
username := mux.Vars(r)["actor"]
|
|
|
|
log.Info(username)
|
|
|
|
if username == ".well-known" || username == "favicon.ico" {
|
|
|
|
log.Info("well-known, skipping...")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
actor, err := LoadActor(username)
|
|
|
|
// error out if this actor does not exist (or there are dots or slashes in his name)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
fmt.Fprintf(w, "404 - page not found")
|
|
|
|
log.Info("Can't create local actor")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, actor.whoAmI())
|
2019-09-10 11:21:39 +03:00
|
|
|
|
|
|
|
// Show some debugging information
|
|
|
|
printer.Info("")
|
|
|
|
body, _ := ioutil.ReadAll(r.Body)
|
|
|
|
PrettyPrintJSON(body)
|
|
|
|
log.Info(FormatHeaders(r.Header))
|
|
|
|
printer.Info("")
|
2019-09-04 12:33:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var outboxHandler http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("content-type", "application/activity+json; charset=utf-8")
|
2019-09-11 21:17:21 +03:00
|
|
|
pageStr := r.URL.Query().Get("page") // get the page from the query string as string
|
|
|
|
username := mux.Vars(r)["actor"] // get the needed actor from the muxer (url variable {actor} below)
|
|
|
|
actor, err := LoadActor(username) // load the actor from disk
|
|
|
|
if err != nil { // either actor requested has illegal characters or
|
2019-09-04 12:33:32 +03:00
|
|
|
log.Info("Can't load local actor") // we don't have such actor
|
|
|
|
fmt.Fprintf(w, "404 - page not found")
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2019-09-13 20:21:54 +03:00
|
|
|
postsPerPage := 100
|
2019-09-11 21:17:21 +03:00
|
|
|
var response []byte
|
2019-09-20 16:21:21 +03:00
|
|
|
filename := storage + slash + "actors" + slash + actor.Name + slash + "outbox.txt"
|
2019-09-13 20:21:54 +03:00
|
|
|
totalLines, err := lineCounter(filename)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("Can't read outbox.txt")
|
|
|
|
log.Info(err)
|
|
|
|
return
|
|
|
|
}
|
2019-09-11 21:17:21 +03:00
|
|
|
if pageStr == "" {
|
2019-09-04 12:33:32 +03:00
|
|
|
//TODO fix total items
|
2019-09-11 21:17:21 +03:00
|
|
|
response = []byte(`{
|
2019-09-04 12:33:32 +03:00
|
|
|
"@context" : "https://www.w3.org/ns/activitystreams",
|
2019-09-20 16:21:21 +03:00
|
|
|
"first" : "` + baseURL + actor.Name + `/outbox?page=1",
|
|
|
|
"id" : "` + baseURL + actor.Name + `/outbox",
|
|
|
|
"last" : "` + baseURL + actor.Name + `/outbox?page=` + strconv.Itoa(totalLines/postsPerPage+1) + `",
|
2019-09-17 11:42:17 +03:00
|
|
|
"totalItems" : ` + strconv.Itoa(totalLines) + `,
|
2019-09-04 12:33:32 +03:00
|
|
|
"type" : "OrderedCollection"
|
2019-09-11 21:17:21 +03:00
|
|
|
}`)
|
2019-09-04 12:33:32 +03:00
|
|
|
} else {
|
2019-09-11 21:17:21 +03:00
|
|
|
page, err := strconv.Atoi(pageStr) // get page number from query string
|
|
|
|
if err != nil {
|
|
|
|
log.Info("Page number not a number, assuming 1")
|
|
|
|
page = 1
|
|
|
|
}
|
|
|
|
lines, err := ReadLines(filename, (page-1)*postsPerPage, page*(postsPerPage+1)-1)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("Can't read outbox file")
|
|
|
|
log.Info(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
responseMap := make(map[string]interface{})
|
|
|
|
responseMap["@context"] = context()
|
2019-09-20 16:21:21 +03:00
|
|
|
responseMap["id"] = baseURL + actor.Name + "/outbox?page=" + pageStr
|
2019-09-13 20:21:54 +03:00
|
|
|
|
2019-09-11 21:17:21 +03:00
|
|
|
if page*postsPerPage < totalLines {
|
2019-09-20 16:21:21 +03:00
|
|
|
responseMap["next"] = baseURL + actor.Name + "/outbox?page=" + strconv.Itoa(page+1)
|
2019-09-11 21:17:21 +03:00
|
|
|
}
|
|
|
|
if page > 1 {
|
2019-09-20 16:21:21 +03:00
|
|
|
responseMap["prev"] = baseURL + actor.Name + "/outbox?page=" + strconv.Itoa(page-1)
|
2019-09-11 21:17:21 +03:00
|
|
|
}
|
2019-09-20 16:21:21 +03:00
|
|
|
responseMap["partOf"] = baseURL + actor.Name + "/outbox"
|
2019-09-11 21:17:21 +03:00
|
|
|
responseMap["type"] = "OrderedCollectionPage"
|
|
|
|
|
2019-09-13 20:21:54 +03:00
|
|
|
orderedItems := make([]interface{}, 0, postsPerPage)
|
|
|
|
|
|
|
|
for _, item := range lines {
|
|
|
|
// split the line
|
|
|
|
parts := strings.Split(item, "/")
|
2019-09-11 21:17:21 +03:00
|
|
|
|
2019-09-13 20:21:54 +03:00
|
|
|
// keep the hash
|
|
|
|
hash := parts[len(parts)-1]
|
2019-09-11 21:17:21 +03:00
|
|
|
// build the filename
|
2019-09-20 16:21:21 +03:00
|
|
|
filename := storage + slash + "actors" + slash + actor.Name + slash + "items" + slash + hash + ".json"
|
2019-09-11 21:17:21 +03:00
|
|
|
// open the file
|
2019-09-13 20:21:54 +03:00
|
|
|
activityJSON, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("can't read activity")
|
|
|
|
log.Info(filename)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var temp map[string]interface{}
|
|
|
|
// put it into a map
|
|
|
|
json.Unmarshal(activityJSON, &temp)
|
2019-09-11 21:17:21 +03:00
|
|
|
// append to orderedItems
|
2019-09-13 20:21:54 +03:00
|
|
|
orderedItems = append(orderedItems, temp)
|
2019-09-11 21:17:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
responseMap["orderedItems"] = orderedItems
|
|
|
|
|
|
|
|
response, err = json.Marshal(responseMap)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("can't marshal map to json")
|
|
|
|
log.Info(err)
|
|
|
|
return
|
|
|
|
}
|
2019-09-04 12:33:32 +03:00
|
|
|
}
|
2019-09-11 21:17:21 +03:00
|
|
|
w.Write(response)
|
2019-09-04 12:33:32 +03:00
|
|
|
}
|
|
|
|
|
2019-09-10 11:21:39 +03:00
|
|
|
var inboxHandler http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
b, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
activity := make(map[string]interface{})
|
|
|
|
err = json.Unmarshal(b, &activity)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Probably this request didn't have (valid) JSON inside it")
|
|
|
|
return
|
|
|
|
}
|
2024-06-10 12:54:10 +03:00
|
|
|
actor,err := LoadActor(mux.Vars(r)["actor"])
|
|
|
|
if err != nil {
|
|
|
|
log.Error("No such actor")
|
|
|
|
}
|
2019-09-10 11:21:39 +03:00
|
|
|
// TODO check if it's actually an activity
|
2024-05-16 22:31:02 +03:00
|
|
|
verifier, err := httpsig.NewVerifier(r)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error then creating verifier")
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pubKeyId := verifier.KeyId()
|
|
|
|
algo := httpsig.RSA_SHA256
|
2024-06-10 12:54:10 +03:00
|
|
|
ra,err := NewRemoteActor(actor,pubKeyId)
|
2024-05-16 22:31:02 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Can't fetch remote actor")
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
verified := verifier.Verify(ra.publicKey, algo)
|
|
|
|
log.Info(verified)
|
|
|
|
if verified != nil {
|
|
|
|
log.Error("Signature mismatch")
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
2019-09-10 11:21:39 +03:00
|
|
|
// check if case is going to be an issue
|
|
|
|
switch activity["type"] {
|
|
|
|
case "Follow":
|
|
|
|
if err != nil {
|
|
|
|
log.Error("No such actor")
|
|
|
|
return
|
|
|
|
}
|
2019-09-13 20:21:54 +03:00
|
|
|
actor.OnFollow(activity)
|
2019-09-11 12:21:38 +03:00
|
|
|
case "Accept":
|
|
|
|
acceptor := activity["actor"].(string)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("No such actor")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-09-13 20:21:54 +03:00
|
|
|
// From here down this could be moved to Actor (TBD)
|
|
|
|
|
2019-09-11 12:21:38 +03:00
|
|
|
follow := activity["object"].(map[string]interface{})
|
|
|
|
id := follow["id"].(string)
|
|
|
|
|
|
|
|
// check if the object of the follow is us
|
2019-09-20 16:21:21 +03:00
|
|
|
if follow["actor"].(string) != baseURL+actor.Name {
|
2019-09-11 12:21:38 +03:00
|
|
|
log.Info("This is not for us, ignoring")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// try to get the hash only
|
2019-09-20 16:21:21 +03:00
|
|
|
hash := strings.Replace(id, baseURL+actor.Name+"/item/", "", 1)
|
2019-09-11 12:21:38 +03:00
|
|
|
// if there are still slashes in the result this means the
|
|
|
|
// above didn't work
|
|
|
|
if strings.ContainsAny(hash, "/") {
|
2019-09-20 16:21:21 +03:00
|
|
|
// log.Info(follow)
|
2019-09-11 12:21:38 +03:00
|
|
|
log.Info("The id of this follow is probably wrong")
|
2019-09-20 16:21:21 +03:00
|
|
|
// we could return here but pixelfed returns
|
|
|
|
// the id as http://domain.tld/actor instead of
|
|
|
|
// http://domain.tld/actor/item/hash so this chokes
|
|
|
|
// return
|
2019-09-11 12:21:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Have we already requested this follow or are we following anybody that
|
|
|
|
// sprays accepts?
|
2019-09-20 16:21:21 +03:00
|
|
|
|
|
|
|
// pixelfed doesn't return the original follow thus the id is wrong so we
|
|
|
|
// need to just check if we requested this actor
|
|
|
|
|
|
|
|
// pixelfed doesn't return the original follow thus the id is wrong so we
|
|
|
|
// need to just check if we requested this actor
|
|
|
|
if _, ok := actor.requested[acceptor]; !ok {
|
2019-11-08 17:01:18 +03:00
|
|
|
log.Info("We never requested this follow from " + acceptor +", ignoring the Accept")
|
2019-09-11 12:21:38 +03:00
|
|
|
return
|
|
|
|
}
|
2019-09-20 16:21:21 +03:00
|
|
|
// if pixelfed fixes https://github.com/pixelfed/pixelfed/issues/1710 we should uncomment
|
|
|
|
// hash is the _ from above
|
|
|
|
|
|
|
|
// if hash != id {
|
|
|
|
// log.Info("Id mismatch between Follow request and Accept")
|
|
|
|
// return
|
|
|
|
// }
|
2019-09-11 12:21:38 +03:00
|
|
|
actor.following[acceptor] = hash
|
2019-10-08 15:23:25 +03:00
|
|
|
PrettyPrint(activity)
|
2019-09-20 16:21:21 +03:00
|
|
|
delete(actor.requested, acceptor)
|
2019-09-11 12:21:38 +03:00
|
|
|
actor.save()
|
2019-09-17 11:42:17 +03:00
|
|
|
case "Reject":
|
|
|
|
rejector := activity["actor"].(string)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("No such actor")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// write the actor to the list of rejected follows so that
|
|
|
|
// we won't try following them again
|
|
|
|
actor.rejected[rejector] = ""
|
|
|
|
actor.save()
|
2019-09-20 16:21:21 +03:00
|
|
|
case "Create":
|
2019-10-21 19:44:10 +03:00
|
|
|
log.Info("Received the following activity from: " + r.UserAgent())
|
|
|
|
PrettyPrintJSON(b)
|
2019-09-20 16:21:21 +03:00
|
|
|
actor.OnReceiveContent(activity)
|
2019-09-10 11:21:39 +03:00
|
|
|
default:
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 11:12:15 +03:00
|
|
|
var peersHandler http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
|
2019-09-10 11:21:39 +03:00
|
|
|
w.Header().Set("content-type", "application/activity+json; charset=utf-8")
|
|
|
|
username := mux.Vars(r)["actor"]
|
2019-09-14 11:12:15 +03:00
|
|
|
collection := mux.Vars(r)["peers"]
|
|
|
|
if collection != "followers" && collection != "following" {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
w.Write([]byte("404 - No such collection"))
|
|
|
|
return
|
|
|
|
}
|
2019-09-10 11:21:39 +03:00
|
|
|
actor, err := LoadActor(username)
|
|
|
|
// error out if this actor does not exist
|
|
|
|
if err != nil {
|
|
|
|
log.Info("Can't create local actor")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var page int
|
|
|
|
pageS := r.URL.Query().Get("page")
|
|
|
|
if pageS == "" {
|
|
|
|
page = 0
|
|
|
|
} else {
|
|
|
|
page, _ = strconv.Atoi(pageS)
|
|
|
|
}
|
2019-09-14 11:12:15 +03:00
|
|
|
response, _ := actor.getPeers(page, collection)
|
2019-09-10 11:21:39 +03:00
|
|
|
w.Write(response)
|
|
|
|
}
|
|
|
|
|
2019-09-14 11:12:15 +03:00
|
|
|
var postHandler http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("content-type", "application/activity+json; charset=utf-8")
|
|
|
|
username := mux.Vars(r)["actor"]
|
|
|
|
hash := mux.Vars(r)["hash"]
|
|
|
|
actor, err := LoadActor(username)
|
|
|
|
// error out if this actor does not exist
|
|
|
|
if err != nil {
|
|
|
|
log.Info("Can't create local actor")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
post, err := actor.loadItem(hash)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
fmt.Fprintf(w, "404 - post not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
postJSON, err := json.Marshal(post)
|
2019-09-17 11:42:17 +03:00
|
|
|
if err != nil {
|
2019-09-14 11:12:15 +03:00
|
|
|
log.Info("failed to marshal json from item " + hash + " text")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write(postJSON)
|
|
|
|
}
|
|
|
|
|
2019-09-04 12:33:32 +03:00
|
|
|
// Add the handlers to a HTTP server
|
|
|
|
gorilla := mux.NewRouter()
|
2019-10-29 06:14:36 +03:00
|
|
|
niCfg := nodeInfoConfig(baseURL)
|
|
|
|
ni := nodeinfo.NewService(*niCfg, nodeInfoResolver{len(actors)})
|
|
|
|
gorilla.HandleFunc(nodeinfo.NodeInfoPath, http.HandlerFunc(ni.NodeInfoDiscover))
|
|
|
|
gorilla.HandleFunc(niCfg.InfoURL, http.HandlerFunc(ni.NodeInfo))
|
2019-09-04 12:33:32 +03:00
|
|
|
gorilla.HandleFunc("/.well-known/webfinger", webfingerHandler)
|
2019-09-14 11:12:15 +03:00
|
|
|
gorilla.HandleFunc("/{actor}/peers/{peers}", peersHandler)
|
2019-09-04 12:33:32 +03:00
|
|
|
gorilla.HandleFunc("/{actor}/outbox", outboxHandler)
|
|
|
|
gorilla.HandleFunc("/{actor}/outbox/", outboxHandler)
|
2019-09-10 11:21:39 +03:00
|
|
|
gorilla.HandleFunc("/{actor}/inbox", inboxHandler)
|
|
|
|
gorilla.HandleFunc("/{actor}/inbox/", inboxHandler)
|
2019-09-04 12:33:32 +03:00
|
|
|
gorilla.HandleFunc("/{actor}/", actorHandler)
|
2024-05-31 20:41:29 +03:00
|
|
|
gorilla.HandleFunc("/.well-known/apgateway/{actor}/actor", actorHandler)
|
2019-09-14 11:12:15 +03:00
|
|
|
gorilla.HandleFunc("/{actor}/item/{hash}", postHandler)
|
2019-09-04 12:33:32 +03:00
|
|
|
http.Handle("/", gorilla)
|
|
|
|
|
|
|
|
log.Fatal(http.ListenAndServe(":8081", nil))
|
|
|
|
}
|