2023-01-25 05:47:06 +00:00
|
|
|
package nostr
|
|
|
|
|
|
|
|
import (
|
2023-02-03 04:56:15 +00:00
|
|
|
"encoding/json"
|
2023-01-25 05:47:06 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2023-02-03 04:56:15 +00:00
|
|
|
"git.minhas.io/asara/well-goknown/redis"
|
2023-01-25 05:47:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type nostrWellKnown struct {
|
|
|
|
Names map[string]string `json:"names"`
|
|
|
|
Relays map[string][]string `json:"relays,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type NostrRequest struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Key string `json:"key"`
|
|
|
|
Hostname string
|
2023-02-03 04:56:15 +00:00
|
|
|
Relays []string `json:"relays,omitempty"`
|
2023-01-25 05:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func AddNostrAddr(n NostrRequest) {
|
|
|
|
// transform request to well known struct
|
2023-02-03 04:56:15 +00:00
|
|
|
relays := make(map[string][]string)
|
|
|
|
user := nostrWellKnown{}
|
2023-01-25 05:47:06 +00:00
|
|
|
names := map[string]string{n.Name: n.Key}
|
2023-02-03 04:56:15 +00:00
|
|
|
if n.Relays != nil {
|
|
|
|
relays = map[string][]string{n.Key: n.Relays}
|
|
|
|
user = nostrWellKnown{Names: names, Relays: relays}
|
|
|
|
} else {
|
|
|
|
user = nostrWellKnown{Names: names}
|
|
|
|
}
|
2023-01-25 05:47:06 +00:00
|
|
|
jsonUser, _ := json.Marshal(user)
|
|
|
|
|
2023-02-03 04:56:15 +00:00
|
|
|
r, err := redis.New("localhost:6379", "", 0)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("FAILED")
|
|
|
|
}
|
2023-01-25 05:47:06 +00:00
|
|
|
nameKey := fmt.Sprintf("%s@%s", n.Name, n.Hostname)
|
2023-02-03 04:56:15 +00:00
|
|
|
err = r.Client.Set(nameKey, jsonUser, 0).Err()
|
2023-01-25 05:47:06 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("FAILED")
|
|
|
|
}
|
|
|
|
}
|
2023-02-03 04:56:15 +00:00
|
|
|
|
|
|
|
func RequestNostrAddr(w http.ResponseWriter, req *http.Request) {
|
|
|
|
}
|
2023-01-25 05:47:06 +00:00
|
|
|
|
|
|
|
func GetNostrAddr(w http.ResponseWriter, req *http.Request) {
|
|
|
|
// get query string for username
|
|
|
|
req.ParseForm()
|
|
|
|
|
|
|
|
// connect to redis
|
2023-02-03 04:56:15 +00:00
|
|
|
r, err := redis.New("localhost:6379", "", 0)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
2023-01-25 05:47:06 +00:00
|
|
|
|
|
|
|
requestedName := req.FormValue("name")
|
|
|
|
// search for user@domain
|
2023-02-03 04:56:15 +00:00
|
|
|
search := fmt.Sprintf("%s@%s", requestedName, getHostname(req.Host))
|
|
|
|
addr, err := r.Client.Get(search).Result()
|
2023-01-25 05:47:06 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
fmt.Fprintf(w, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getHostname(hostname string) string {
|
|
|
|
// remove port for local testing
|
|
|
|
if strings.Contains(hostname, ":") {
|
|
|
|
hostname = strings.Split(hostname, ":")[0]
|
|
|
|
}
|
|
|
|
return hostname
|
|
|
|
}
|