1 // Package relayinfo reads a relay's NIP-11 document: what it is called, who runs it, and which
2 // back-end keys it authorises.
3 //
4 // That last field is the contact-free part of joining a relay. A relay publishes the gateways it
5 // admits, so a gateway can ask a candidate relay "do you already list me" without an operator
6 // exchange, and an operator can see which gateways a relay would accept before deciding to add
7 // one. The list is public by construction: these are the keys the relay will accept publishes
8 // from, and the listings they sign are public too.
9 package relayinfo
10 11 import (
12 "context"
13 "encoding/json"
14 "fmt"
15 "io"
16 "net/http"
17 "net/url"
18 "strings"
19 "time"
20 )
21 22 // Document is the part of NIP-11 this protocol reads. Unknown fields are ignored, which is what
23 // lets the relay advertise more later without breaking older gateways.
24 type Document struct {
25 Name string `json:"name"`
26 PubKey string `json:"pubkey,omitempty"`
27 Contact string `json:"contact"`
28 Software string `json:"software"`
29 Version string `json:"version"`
30 // Gateways is the back-end keys the relay authorises, as hex. Empty means the relay
31 // advertises none, which is not the same as refusing every gateway: an older relay has no
32 // field at all.
33 Gateways []string `json:"gateways,omitempty"`
34 }
35 36 // HTTPURL turns a relay WebSocket URL into the HTTP URL that serves its NIP-11 document.
37 func HTTPURL(relayURL string) (string, error) {
38 parsed, err := url.Parse(relayURL)
39 40 if err != nil {
41 return "", fmt.Errorf("relayinfo: %w", err)
42 }
43 44 switch parsed.Scheme {
45 case "wss":
46 parsed.Scheme = "https"
47 case "ws":
48 parsed.Scheme = "http"
49 case "https", "http":
50 default:
51 return "", fmt.Errorf("relayinfo: unsupported relay scheme %q", parsed.Scheme)
52 }
53 54 parsed.Path = "/"
55 parsed.RawQuery = ""
56 57 return parsed.String(), nil
58 }
59 60 // Fetch reads and decodes the document. A relay that serves something else, or nothing, is
61 // reported as an error rather than as a relay with no gateways: the two mean different things.
62 func Fetch(ctx context.Context, client *http.Client, relayURL string) (Document, error) {
63 target, err := HTTPURL(relayURL)
64 65 if err != nil {
66 return Document{}, err
67 }
68 69 if client == nil {
70 client = &http.Client{Timeout: 10 * time.Second}
71 }
72 73 request, err := http.NewRequestWithContext(ctx, http.MethodGet, target, nil)
74 75 if err != nil {
76 return Document{}, fmt.Errorf("relayinfo: %w", err)
77 }
78 79 request.Header.Set("Accept", "application/nostr+json")
80 81 response, err := client.Do(request)
82 83 if err != nil {
84 return Document{}, fmt.Errorf("relayinfo: %s: %w", target, err)
85 }
86 87 defer response.Body.Close()
88 89 if response.StatusCode != http.StatusOK {
90 return Document{}, fmt.Errorf("relayinfo: %s: HTTP %d", target, response.StatusCode)
91 }
92 93 body, err := io.ReadAll(io.LimitReader(response.Body, 1<<20))
94 95 if err != nil {
96 return Document{}, fmt.Errorf("relayinfo: %s: %w", target, err)
97 }
98 99 var doc Document
100 101 if err := json.Unmarshal(body, &doc); err != nil {
102 return Document{}, fmt.Errorf("relayinfo: %s does not serve NIP-11 JSON: %w", target, err)
103 }
104 105 return doc, nil
106 }
107 108 // Authorizes reports whether this relay's advertised list contains key, case-insensitively.
109 func (d Document) Authorizes(key string) bool {
110 want := strings.ToLower(strings.TrimSpace(key))
111 112 for _, gateway := range d.Gateways {
113 if strings.ToLower(strings.TrimSpace(gateway)) == want {
114 return true
115 }
116 }
117 118 return false
119 }
120