fedilogue/web.go

243 lines
8.5 KiB
Go
Raw Normal View History

package main
import (
// "crypto/sha1"
2020-12-17 04:23:25 +00:00
"encoding/json"
"fmt"
// "html"
2020-12-17 04:23:25 +00:00
"io/ioutil"
"log"
"net/http"
"os"
"strings"
// "time"
)
2020-12-17 04:23:25 +00:00
// CreateObject - Used by post web receiver
2020-12-14 21:50:31 +00:00
type CreateObject struct {
2020-12-29 17:30:26 +00:00
ID string `json:"id"`
2020-12-17 04:23:25 +00:00
Actor string `json:"actor"`
Cc []string `json:"cc"`
Content string `json:"content"`
To []string `json:"to"`
Type string `json:"type"`
2020-12-14 21:50:31 +00:00
}
2020-12-18 06:06:32 +00:00
// RelayBase - The base object used by web receiver
type RelayBase struct {
2020-12-17 04:23:25 +00:00
Actor string `json:"actor"`
Cc []string `json:"cc"`
Object json.RawMessage `json:"Object"`
ID string `json:"id"`
Published string `json:"published"`
To []string `json:"to"`
Type string `json:"type"`
}
func hostmeta(w http.ResponseWriter, r *http.Request) {
fmt.Println("PATH --> ", r.URL.Path)
host := r.Host
xml := "<?xml version=\"1.0\" encoding=\"UTF-8\"?><XRD xmlns=\"http://docs.oasis-open.org/ns/xri/xrd-1.0\"><Link rel=\"lrdd\" template=\"https://" + host + "/.well-known/webfinger?resource={uri}\" type=\"application/xrd+xml\" /></XRD>"
w.Header().Set("Content-Type", "application/xrd+xml")
fmt.Fprintf(w, xml)
}
func webfinger(w http.ResponseWriter, r *http.Request) {
fmt.Println("PATH --> ", r.URL.Path)
host := r.Host
webfingermap := make(map[string]interface{})
webfingermap["subject"] = "acct:fedilogue@" + host
webfingermap["aliases"] = []string{"https://" + host + "/users/fedilogue"}
link0 := make(map[string]string)
link0["rel"] = "http://webfinger.net/rel/profile-page"
link0["type"] = "text/html"
link0["href"] = "https://" + host + "/users/fedilogue"
link1 := make(map[string]string)
link1["rel"] = "self"
link1["type"] = "application/activity+json"
link1["href"] = "https://" + host + "/users/fedilogue"
link2 := make(map[string]string)
link2["rel"] = "self"
link2["type"] = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""
link2["href"] = "https://" + host + "/users/fedilogue"
link3 := make(map[string]string)
link3["rel"] = "http://ostatus.org/schema/1.0/subscribe"
link3["template"] = "https://" + host + "/ostatus_subscribe?acct={uri}"
links := []map[string]string{link0, link1, link2, link3}
webfingermap["links"] = links
webfingerbin, err := json.Marshal(webfingermap)
if err != nil {
fmt.Println(err.Error())
return
}
webfingerstr := string(webfingerbin)
query := r.URL.Query()
resourceRaw, exists := query["resource"]
if exists {
resource := resourceRaw[0]
2020-12-17 04:23:25 +00:00
if resource != "acct:fedilogue@"+host {
fmt.Println("Writes properly but wrong acct")
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprintf(w, webfingerstr)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprintf(w, webfingerstr)
} else {
fmt.Println(query)
w.WriteHeader(http.StatusNotFound)
return
}
}
func inboxHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
2020-12-14 21:50:31 +00:00
if err != nil {
fmt.Println(err)
return
2020-12-14 21:50:31 +00:00
}
2020-12-18 06:06:32 +00:00
var findtype RelayBase
err = json.Unmarshal(body, &findtype)
switch findtype.Type {
case "Create":
var createobject CreateObject
err = json.Unmarshal(findtype.Object, &createobject)
if err != nil {
return
}
2020-12-29 17:30:26 +00:00
go check_post(createobject.ID)
slashend := strings.Index(createobject.ID[8:], "/")
newinstance := createobject.ID[8:8+slashend]
log.Print("The at sign is: ", newinstance)
// Only done if we are crawling
if settings.Crawl == true && stringexists(newinstance, settings.Banned) == false {
// Skip over this if its the same as the endpoint
ri_mutex.Lock()
o, exists := runninginstances[newinstance]
if exists == false || o.Status == KEEPALIVE {
m := RunningInstance{}
runninginstances[newinstance] = m
go StartInstance(newinstance)
}
ri_mutex.Unlock()
}
case "Like":
case "Announcement":
case "Delete":
case "Undo":
default:
fmt.Println("Others --> " + findtype.Type)
}
}
}
2020-12-17 04:23:25 +00:00
func usersFedilogueFollowers(w http.ResponseWriter, r *http.Request) {
fmt.Println("PATH --> ", r.URL.Path)
host := r.Host
2020-12-17 04:23:25 +00:00
contextlist := map[string]string{"@language": "und"}
2020-12-17 04:23:25 +00:00
context := []interface{}{"https://www.w3.org/ns/activitystreams", "https://" + host + "/schemas/litepub-0.1.jsonld", contextlist}
followersmap := make(map[string]interface{})
followersmap["@context"] = context
staticjson := "{\"@context\":[\"https://www.w3.org/ns/activitystreams\",\"https://" + host + "/schemas/litepub-0.1.jsonld\",{\"@language\":\"und\"}],\"first\":{\"id\":\"https://" + host + "/users/fedilogue/followers?page=1\",\"next\":\"https://" + host + "/users/fedilogue/followers?page=2\",\"orderedItems\":[\"https://mastodon.host/users/federationbot\"],\"partOf\":\"https://" + host + "/users/fedilogue/followers\",\"totalItems\":1,\"type\":\"OrderedCollectionPage\"},\"id\":\"https://" + host + "/users/fedilogue/followers\",\"totalItems\":1,\"type\":\"OrderedCollection\"}"
w.Header().Set("Content-Type", "application/activity+json; charset=utf-8")
fmt.Fprintf(w, staticjson)
}
2020-12-17 04:23:25 +00:00
func usersFedilogueFollowing(w http.ResponseWriter, r *http.Request) {
host := r.Host
fmt.Println("PATH --> ", r.URL.Path)
staticjson := "{\"@context\": [\"https://www.w3.org/ns/activitystreams\", \"https://" + host + "/schemas/litepub-0.1.jsonld\", {\"@language\": \"und\"}], \"first\": {\"id\": \"https://" + host + "/users/fedilogue/following?page=1\", \"orderedItems\": [], \"partOf\": \"https://" + host + "/users/fedilogue/following\", \"totalItems\": 0, \"type\": \"OrderedCollectionPage\"}, \"id\": \"https://" + host + "/users/fedilogue/following\", \"totalItems\": 0, \"type\": \"OrderedCollection\"}"
w.Header().Set("Content-Type", "application/activity+json; charset=utf-8")
fmt.Fprintf(w, staticjson)
}
2020-12-17 04:23:25 +00:00
func usersFedilogue(w http.ResponseWriter, r *http.Request) {
fmt.Println("PATH --> ", r.URL.Path)
host := r.Host
fmt.Println(r.Host)
fmt.Println(r.Header["Accept"])
publickeybin, err := ioutil.ReadFile("keys/public.pem")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
publickeypemstr := string(publickeybin)
2020-12-17 04:23:25 +00:00
publicKey := map[string]string{"id": "https://" + host + "/users/fedilogue#main-key", "owner": "https://" + host + "/users/fedilogue", "publicKeyPem": publickeypemstr}
capabilities := map[string]bool{}
tag := []string{}
2020-12-17 04:23:25 +00:00
contextlist := map[string]string{"@language": "und"}
attachment := []string{}
2020-12-17 04:23:25 +00:00
endpoints := map[string]string{"oauthAuthorizationEndpoint": "https://" + host + "/oauth/authorize", "oauthRegistrationEndpoint": "https://" + host + "/api/v1/apps", "oauthTokenEndpoint": "https://" + host + "/oauth/token", "sharedInbox": "https://" + host + "/inbox", "uploadMedia": "https://" + host + "/api/ap/upload_media"}
2020-12-17 04:23:25 +00:00
context := []interface{}{"https://www.w3.org/ns/activitystreams", "https://" + host + "/schemas/litepub-0.1.jsonld", contextlist}
userjsonmap := make(map[string]interface{})
userjsonmap["@context"] = context
userjsonmap["attachment"] = attachment
userjsonmap["capabilities"] = capabilities
userjsonmap["discoverable"] = false
userjsonmap["endpoints"] = endpoints
userjsonmap["followers"] = "https://" + host + "/users/fedilogue/followers"
userjsonmap["following"] = "https://" + host + "/users/fedilogue/following"
userjsonmap["id"] = "https://" + host + "/users/fedilogue"
userjsonmap["inbox"] = "https://" + host + "/users/fedilogue/inbox"
userjsonmap["manuallyApprovesFollowers"] = false
userjsonmap["name"] = "Fedilogue Mass Follower"
userjsonmap["outbox"] = "https://" + host + "/users/fedilogue/outbox"
userjsonmap["preferredUsername"] = "fedilogue"
userjsonmap["publicKey"] = publicKey
userjsonmap["summary"] = ""
userjsonmap["tag"] = tag
userjsonmap["type"] = "Person"
2020-12-07 15:47:44 -05:00
userjsonmap["uri"] = "https://" + host + "/users/fedilogue"
userjsonbin, err := json.Marshal(userjsonmap)
if err != nil {
fmt.Println(err.Error())
return
}
userjsonstr := string(userjsonbin)
w.Header().Set("Content-Type", "application/activity+json; charset=utf-8")
fmt.Fprintf(w, userjsonstr)
}
func errorHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("404 --> ", r.URL.Path)
}
func webmain() {
http.HandleFunc("/.well-known/webfinger", webfinger)
http.HandleFunc("/.well-known/host-meta", hostmeta)
http.HandleFunc("/inbox", inboxHandler())
2020-12-17 04:23:25 +00:00
http.HandleFunc("/users/fedilogue", usersFedilogue)
http.HandleFunc("/users/fedilogue/followers", usersFedilogueFollowers)
http.HandleFunc("/users/fedilogue/following", usersFedilogueFollowing)
http.HandleFunc("/", errorHandler)
2020-12-13 23:33:38 +00:00
log.Print("Starting HTTP inbox on port 8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}