Translate ActivityPub pushes (Mastodon/Pleroma messages) into Webhooks
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.3 KiB

package main
import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
func mustGetEnv(env string) (val string) {
val, ok := os.LookupEnv(env)
if !ok {
log.Fatalf("required env var %s not found", env)
}
return
}
var host string
var fullhost string
var prefix *string
func main() {
prefix = flag.String("prefix", "AW_", "Environment var prefix")
proto := flag.String("proto", "https", "Protocol under which this server is going to be exposed")
flag.Parse()
bind, ok := os.LookupEnv(*prefix + "BIND")
if !ok {
bind = ":8080" // Default to *:8080
}
host = strings.ToLower(mustGetEnv(*prefix + "HOST"))
fullhost = fmt.Sprintf("%s://%s", *proto, host)
discoverApps()
http.HandleFunc("/.well-known/host-meta", hostMetaHandler)
http.HandleFunc("/.well-known/webfinger", handlerWebFinger)
http.HandleFunc("/u/", handlerUserPage)
http.HandleFunc("/inbox", func(w http.ResponseWriter, r *http.Request) {
byt, _ := ioutil.ReadAll(r.Body)
fmt.Println(string(byt))
})
if err := http.ListenAndServe(bind, nil); err != nil {
log.Fatalf("error while listening: %f", err)
}
}
func discoverApps() {
// Find app definitions
envs := os.Environ()
envAppPrefix := *prefix + "APP_"
envAppPrefixLen := len(envAppPrefix)
apps = make(map[string]appDef)
for _, envName := range envs {
// Filter for APP_* vars
if !strings.HasPrefix(envName, envAppPrefix) {
continue
}
// Retrieve app name
nextSep := strings.Index(envName[envAppPrefixLen:], "_")
if nextSep < 0 {
nextSep = 0
}
name := envName[envAppPrefixLen : envAppPrefixLen+nextSep]
appName := strings.ToLower(name)
// Skip if we processed this app already
if _, ok := apps[appName]; ok {
continue
}
// Retrieve info for app
var pubkey *rsa.PublicKey = nil
if key, ok := os.LookupEnv(envAppPrefix + name + "_PEM"); ok {
key, _ := pem.Decode([]byte(key))
pub, err := x509.ParsePKCS1PublicKey(key.Bytes)
if err != nil {
log.Fatalf("Error while parsing PEM/PKCS1 block for %s: %s", envAppPrefix+name+"_PEM", err.Error())
}
pubkey = pub
}
apps[appName] = appDef{
Name: mustGetEnv(envAppPrefix + name + "_NAME"),
WebhookURL: mustGetEnv(envAppPrefix + name + "_URL"),
PublicKey: pubkey,
}
log.Printf("app registered: %s", appName)
}
}