Add subscription to WebSub server
This commit is contained in:
parent
912e1322da
commit
63556bd9c7
97
cmd/server/incoming.go
Normal file
97
cmd/server/incoming.go
Normal file
|
@ -0,0 +1,97 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/hmac"
|
||||||
|
"crypto/sha1"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HubBackend handles information for the incoming handler
|
||||||
|
type HubBackend interface {
|
||||||
|
CreateFeed(url string) int64
|
||||||
|
GetSecret(id int64) string
|
||||||
|
}
|
||||||
|
|
||||||
|
type incomingHandler struct {
|
||||||
|
Backend HubBackend
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
urlRegex = regexp.MustCompile(`^/incoming/(\d+)$`)
|
||||||
|
)
|
||||||
|
|
||||||
|
func (h *incomingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
defer r.Body.Close()
|
||||||
|
if r.Method == http.MethodGet {
|
||||||
|
values := r.URL.Query()
|
||||||
|
|
||||||
|
// check
|
||||||
|
|
||||||
|
verify := values.Get("hub.challenge")
|
||||||
|
fmt.Fprint(w, verify)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.Error(w, "Method not allowed", 405)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// find feed
|
||||||
|
matches := urlRegex.FindStringSubmatch(r.URL.Path)
|
||||||
|
feed, err := strconv.ParseInt(matches[1], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprint(w, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// find secret
|
||||||
|
secret := h.Backend.GetSecret(feed)
|
||||||
|
if secret == "" {
|
||||||
|
http.Error(w, "Unknown", 400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// match signature
|
||||||
|
sig := r.Header.Get("X-Hub-Signature")
|
||||||
|
parts := strings.Split(sig, "=")
|
||||||
|
|
||||||
|
if len(parts) != 2 {
|
||||||
|
http.Error(w, "Signature format", 400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if sig != "sha1" {
|
||||||
|
http.Error(w, "Unknown signature format", 400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
feedContent, err := ioutil.ReadAll(r.Body)
|
||||||
|
|
||||||
|
// verification
|
||||||
|
mac := hmac.New(sha1.New, []byte(secret))
|
||||||
|
mac.Write(feedContent)
|
||||||
|
signature := mac.Sum(nil)
|
||||||
|
|
||||||
|
if fmt.Sprintf("%x", signature) != parts[1] {
|
||||||
|
http.Error(w, "Signature doesn't match", 400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ct := r.Header.Get("Content-Type")
|
||||||
|
if strings.HasPrefix(ct, "application/rss+xml") {
|
||||||
|
// RSS parsing
|
||||||
|
} else if strings.HasPrefix(ct, "application/atom+xml") {
|
||||||
|
// Atom parsing
|
||||||
|
} else if strings.HasPrefix(ct, "text/html") {
|
||||||
|
// h-entry parsing
|
||||||
|
} else {
|
||||||
|
http.Error(w, "Unknown format of body", 400)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
|
@ -22,7 +22,9 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -45,8 +47,68 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
type microsubHandler struct {
|
type microsubHandler struct {
|
||||||
Backend microsub.Microsub
|
Backend microsub.Microsub
|
||||||
Redis redis.Conn
|
HubIncomingBackend HubBackend
|
||||||
|
Redis redis.Conn
|
||||||
|
}
|
||||||
|
|
||||||
|
type hubIncomingBackend struct {
|
||||||
|
conn redis.Conn
|
||||||
|
}
|
||||||
|
|
||||||
|
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
|
||||||
|
func randStringBytes(n int) string {
|
||||||
|
b := make([]byte, n)
|
||||||
|
for i := range b {
|
||||||
|
b[i] = letterBytes[rand.Intn(len(letterBytes))]
|
||||||
|
}
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *hubIncomingBackend) GetSecret(id int64) string {
|
||||||
|
secret, err := redis.String(h.conn.Do("HGET", fmt.Sprintf("feed:%d", id), "secret"))
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return secret
|
||||||
|
}
|
||||||
|
|
||||||
|
var hubUrl = "https://hub.stuifzandapp.com/"
|
||||||
|
|
||||||
|
func (h *hubIncomingBackend) CreateFeed(topic string) int64 {
|
||||||
|
id, err := redis.Int64(h.conn.Do("INCR", "feed:next_id"))
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
h.conn.Do("HSET", fmt.Sprintf("feed:%d", id), "url", topic)
|
||||||
|
secret := randStringBytes(16)
|
||||||
|
h.conn.Do("HSET", fmt.Sprintf("feed:%d", id), "secret", secret)
|
||||||
|
|
||||||
|
hub, err := url.Parse(hubUrl)
|
||||||
|
q := hub.Query()
|
||||||
|
q.Add("hub.mode", "subscribe")
|
||||||
|
q.Add("hub.callback", fmt.Sprintf("https://microsub.stuifzandapp.com/incoming/%d", id))
|
||||||
|
q.Add("hub.topic", topic)
|
||||||
|
q.Add("hub.secret", secret)
|
||||||
|
hub.RawQuery = q.Encode()
|
||||||
|
|
||||||
|
req, err := http.NewRequest("GET", hub.String(), nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("new request: %s\n", err)
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
|
||||||
|
_, err = client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("subscription request: %s\n", err)
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
func simplify(itemType string, item map[string][]interface{}) map[string]interface{} {
|
func simplify(itemType string, item map[string][]interface{}) map[string]interface{} {
|
||||||
|
@ -345,7 +407,7 @@ func (h *microsubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
} else if action == "follow" {
|
} else if action == "follow" {
|
||||||
uid := values.Get("channel")
|
uid := values.Get("channel")
|
||||||
url := values.Get("url")
|
url := values.Get("url")
|
||||||
|
h.HubIncomingBackend.CreateFeed(url)
|
||||||
feed := h.Backend.FollowURL(uid, url)
|
feed := h.Backend.FollowURL(uid, url)
|
||||||
w.Header().Add("Content-Type", "application/json")
|
w.Header().Add("Content-Type", "application/json")
|
||||||
jw := json.NewEncoder(w)
|
jw := json.NewEncoder(w)
|
||||||
|
@ -432,6 +494,15 @@ func main() {
|
||||||
backend = loadMemoryBackend(conn)
|
backend = loadMemoryBackend(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
http.Handle("/microsub", µsubHandler{backend, nil})
|
hubBackend := hubIncomingBackend{conn}
|
||||||
|
|
||||||
|
http.Handle("/microsub", µsubHandler{
|
||||||
|
Backend: backend,
|
||||||
|
HubIncomingBackend: &hubBackend,
|
||||||
|
Redis: nil,
|
||||||
|
})
|
||||||
|
http.Handle("/incoming/", &incomingHandler{
|
||||||
|
Backend: &hubBackend,
|
||||||
|
})
|
||||||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user