2020-11-24 00:00:04 -05:00
package main
import (
2020-12-17 04:23:25 +00:00
"encoding/json"
2020-11-24 00:00:04 -05:00
"fmt"
2020-12-17 04:23:25 +00:00
"io/ioutil"
2020-11-24 00:00:04 -05:00
"log"
"net/http"
"os"
2020-12-29 20:06:18 +00:00
"strings"
2020-11-24 00:00:04 -05:00
)
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 20:20:02 +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
}
2022-01-09 21:16:34 -05:00
// CreateObject - Used by post web receiver
type AnnounceObject struct {
ID string ` json:"id" `
Actor string ` json:"actor" `
To [ ] string ` json:"to" `
Type string ` json:"type" `
Object string ` json:"object" `
}
2020-12-18 06:06:32 +00:00
// RelayBase - The base object used by web receiver
type RelayBase struct {
2020-12-29 20:20:02 +00:00
Actor string ` json:"actor" `
Cc [ ] string ` json:"cc" `
2020-12-17 04:23:25 +00:00
Object json . RawMessage ` json:"Object" `
ID string ` json:"id" `
Published string ` json:"published" `
To [ ] string ` json:"to" `
Type string ` json:"type" `
2020-11-24 00:00:04 -05:00
}
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 {
2020-11-24 00:00:04 -05:00
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
}
}
2020-12-22 20:36:37 +00:00
func inboxHandler ( ) http . HandlerFunc {
2020-12-16 02:41:45 +00:00
return func ( w http . ResponseWriter , r * http . Request ) {
2020-12-17 01:15:17 +00:00
2020-12-16 02:41:45 +00:00
body , err := ioutil . ReadAll ( r . Body )
2020-12-14 21:50:31 +00:00
if err != nil {
2020-12-16 02:41:45 +00:00
fmt . Println ( err )
return
2020-12-14 21:50:31 +00:00
}
2022-01-09 21:16:34 -05:00
defer r . Body . Close ( )
2020-12-14 21:50:31 +00:00
2020-12-18 06:06:32 +00:00
var findtype RelayBase
2020-12-16 02:41:45 +00:00
err = json . Unmarshal ( body , & findtype )
2021-01-02 07:00:21 +00:00
if err != nil {
2022-01-09 21:16:34 -05:00
logWarn ( "Unable to unmarshal here, exiting..." )
return
2021-01-02 07:00:21 +00:00
}
2020-12-16 02:41:45 +00:00
switch findtype . Type {
case "Create" :
var createobject CreateObject
2022-01-09 21:16:34 -05:00
err = json . Unmarshal ( body , & createobject )
2020-12-16 02:41:45 +00:00
if err != nil {
2020-12-17 01:15:17 +00:00
return
2020-12-16 02:41:45 +00:00
}
2021-02-01 12:52:42 +00:00
go check_activity ( createobject . ID )
2020-12-29 20:06:18 +00:00
slashend := strings . Index ( createobject . ID [ 8 : ] , "/" )
2020-12-29 20:20:02 +00:00
newinstance := createobject . ID [ 8 : 8 + slashend ]
2020-12-29 20:06:18 +00:00
2021-01-10 05:31:51 +00:00
go CheckInstance ( newinstance , "" )
2020-12-29 20:06:18 +00:00
2022-01-09 21:16:34 -05:00
case "Update" :
case "Reject" :
case "Add" :
case "Remove" :
2022-01-13 17:14:22 -05:00
case "Follow" :
case "Accept" :
2020-12-16 02:41:45 +00:00
case "Like" :
2022-01-09 21:16:34 -05:00
case "Announce" :
var announceobject AnnounceObject
err = json . Unmarshal ( body , & announceobject )
if err != nil {
fmt . Println ( err . Error ( ) )
return
}
check_activity ( announceobject . Object )
matchset := re . FindStringSubmatch ( announceobject . Object )
if matchset != nil {
newinstance := matchset [ 1 ]
go CheckInstance ( newinstance , "" )
}
2020-12-16 02:41:45 +00:00
case "Delete" :
case "Undo" :
default :
2022-01-13 17:14:22 -05:00
logWarn ( "Unknown ActivityPub request:" , findtype . Type )
2020-12-16 02:41:45 +00:00
}
2022-01-09 21:16:34 -05:00
w . Header ( ) . Set ( "Content-Type" , "application/json" )
fmt . Fprintf ( w , "{}" )
2020-12-16 02:41:45 +00:00
}
2020-11-24 00:00:04 -05:00
}
2022-01-09 21:16:34 -05:00
func internalFetch ( w http . ResponseWriter , r * http . Request ) {
publicKeyBytes , err := ioutil . ReadFile ( "keys/public.pem" )
publicKeyString := string ( publicKeyBytes )
publicKeyString = strings . Replace ( publicKeyString , "\n" , "\\n" , - 11 )
if err != nil {
log . Fatal ( err )
}
staticjson := fmt . Sprintf ( ` { "@context":["https://www.w3.org/ns/activitystreams","https://%[1]s/schemas/litepub-0.1.jsonld", { "@language":"und"}],"endpoints": { "oauthAuthorizationEndpoint":"https://%[1]s/oauth/authorize","oauthRegistrationEndpoint":"https://%[1]s/api/v1/apps","oauthTokenEndpoint":"https://%[1]s/oauth/token","sharedInbox":"https://%[1]s/inbox","uploadMedia":"https://%[1]s/api/ap/upload_media"},"followers":"https://%[1]s/internal/fetch/followers","following":"https://%[1]s/internal/fetch/following","id":"https://%[1]s/internal/fetch","inbox":"https://%[1]s/internal/fetch/inbox","invisible":true,"manuallyApprovesFollowers":false,"name":"Pleroma","preferredUsername":"fedilogue","publicKey": { "id":"https://%[1]s/internal/fetch#main-key","owner":"https://%[1]s/internal/fetch","publicKeyPem":"%[2]s"},"summary":"Fedilogue Key or something.","type":"Application","url":"https://%[1]s/internal/fetch"} ` , r . Host , publicKeyString )
fmt . Fprintf ( w , staticjson )
}
func relay ( w http . ResponseWriter , r * http . Request ) {
2022-01-11 00:44:01 -05:00
fmt . Println ( "Someone out there requested /relay" )
2022-01-09 21:16:34 -05:00
publicKeyBytes , err := ioutil . ReadFile ( "keys/public.pem" )
publicKeyString := string ( publicKeyBytes )
publicKeyString = strings . Replace ( publicKeyString , "\n" , "\\n" , - 11 )
if err != nil {
log . Fatal ( err )
}
staticjson := fmt . Sprintf ( ` { "@context":["https://www.w3.org/ns/activitystreams","https://%[1]s/schemas/litepub-0.1.jsonld", { "@language":"und"}],"alsoKnownAs":[],"attachment":[],"capabilities": { },"discoverable":false,"endpoints": { "oauthAuthorizationEndpoint":"https://%[1]s/oauth/authorize","oauthRegistrationEndpoint":"https://%[1]s/api/v1/apps","oauthTokenEndpoint":"https://%[1]s/oauth/token","sharedInbox":"https://%[1]s/inbox","uploadMedia":"https://%[1]s/api/ap/upload_media"},"featured":"https://%[1]s/relay/collections/featured","followers":"https://%[1]s/relay/followers","following":"https://%[1]s/relay/following","id":"https://%[1]s/relay","inbox":"https://%[1]s/relay/inbox","manuallyApprovesFollowers":false,"name":null,"outbox":"https://%[1]s/relay/outbox","preferredUsername":"relay","publicKey": { "id":"https://%[1]s/relay#main-key","owner":"https://%[1]s/relay","publicKeyPem":"%[2]s"},"summary":"","tag":[],"type":"Person","url":"https://%[1]s/relay"} ` , r . Host , publicKeyString )
fmt . Fprintf ( w , staticjson )
}
2020-12-17 04:23:25 +00:00
func usersFedilogueFollowers ( w http . ResponseWriter , r * http . Request ) {
2020-11-24 00:00:04 -05:00
fmt . Println ( "PATH --> " , r . URL . Path )
host := r . Host
2020-12-17 04:23:25 +00:00
contextlist := map [ string ] string { "@language" : "und" }
2020-11-24 00:00:04 -05:00
2020-12-17 04:23:25 +00:00
context := [ ] interface { } { "https://www.w3.org/ns/activitystreams" , "https://" + host + "/schemas/litepub-0.1.jsonld" , contextlist }
2020-11-24 00:00:04 -05:00
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 ) {
2020-11-24 00:00:04 -05:00
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 ) {
2020-11-24 00:00:04 -05:00
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 }
2020-11-24 00:00:04 -05:00
capabilities := map [ string ] bool { }
tag := [ ] string { }
2020-12-17 04:23:25 +00:00
contextlist := map [ string ] string { "@language" : "und" }
2020-11-24 00:00:04 -05:00
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-11-24 00:00:04 -05:00
2020-12-17 04:23:25 +00:00
context := [ ] interface { } { "https://www.w3.org/ns/activitystreams" , "https://" + host + "/schemas/litepub-0.1.jsonld" , contextlist }
2021-02-01 12:52:42 +00:00
actorjsonmap := make ( map [ string ] interface { } )
actorjsonmap [ "@context" ] = context
actorjsonmap [ "attachment" ] = attachment
actorjsonmap [ "capabilities" ] = capabilities
actorjsonmap [ "discoverable" ] = false
actorjsonmap [ "endpoints" ] = endpoints
actorjsonmap [ "followers" ] = "https://" + host + "/users/fedilogue/followers"
actorjsonmap [ "following" ] = "https://" + host + "/users/fedilogue/following"
actorjsonmap [ "id" ] = "https://" + host + "/users/fedilogue"
actorjsonmap [ "inbox" ] = "https://" + host + "/users/fedilogue/inbox"
actorjsonmap [ "manuallyApprovesFollowers" ] = false
actorjsonmap [ "name" ] = "Fedilogue Mass Follower"
actorjsonmap [ "outbox" ] = "https://" + host + "/users/fedilogue/outbox"
actorjsonmap [ "preferredUsername" ] = "fedilogue"
actorjsonmap [ "publicKey" ] = publicKey
actorjsonmap [ "summary" ] = ""
actorjsonmap [ "tag" ] = tag
2022-01-09 21:16:34 -05:00
actorjsonmap [ "type" ] = "Application"
2021-02-01 12:52:42 +00:00
actorjsonmap [ "uri" ] = "https://" + host + "/users/fedilogue"
actorjsonbin , err := json . Marshal ( actorjsonmap )
2020-11-24 00:00:04 -05:00
if err != nil {
fmt . Println ( err . Error ( ) )
return
}
2021-02-01 12:52:42 +00:00
actorjsonstr := string ( actorjsonbin )
2020-11-24 00:00:04 -05:00
w . Header ( ) . Set ( "Content-Type" , "application/activity+json; charset=utf-8" )
2021-02-01 12:52:42 +00:00
fmt . Fprintf ( w , actorjsonstr )
2020-11-24 00:00:04 -05:00
}
func errorHandler ( w http . ResponseWriter , r * http . Request ) {
2022-01-09 21:16:34 -05:00
for name , headers := range r . Header {
fmt . Println ( "ROW: " , name , " " , headers )
for _ , h := range headers {
fmt . Fprintf ( w , "%v: %v\n" , name , h )
}
}
reqBody , err := ioutil . ReadAll ( r . Body )
if err != nil {
log . Fatal ( err )
}
fmt . Printf ( "POST DATA: %s\n" , reqBody )
2020-11-24 00:00:04 -05:00
}
2020-12-22 20:36:37 +00:00
func webmain ( ) {
2022-01-13 17:09:12 -05:00
webreceiver := http . NewServeMux ( )
webreceiver . HandleFunc ( "/.well-known/webfinger" , webfinger )
webreceiver . HandleFunc ( "/.well-known/host-meta" , hostmeta )
webreceiver . HandleFunc ( "/inbox" , inboxHandler ( ) )
webreceiver . HandleFunc ( "/internal/fetch" , internalFetch )
webreceiver . HandleFunc ( "/relay" , relay )
webreceiver . HandleFunc ( "/users/fedilogue" , usersFedilogue )
webreceiver . HandleFunc ( "/users/fedilogue/followers" , usersFedilogueFollowers )
webreceiver . HandleFunc ( "/users/fedilogue/following" , usersFedilogueFollowing )
webreceiver . HandleFunc ( "/" , errorHandler )
2022-01-09 21:16:34 -05:00
log . Print ( "Starting HTTP inbox on port 127.0.0.1:8042" )
2022-01-13 17:09:12 -05:00
log . Fatal ( http . ListenAndServe ( "127.0.0.1:8042" , webreceiver ) )
2020-11-24 00:00:04 -05:00
}