2022-11-06 21:15:42 -03:00
|
|
|
package nostr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
s "github.com/SaveTheRbtz/generic-sync-map-go"
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Status int
|
|
|
|
|
|
|
|
const (
|
|
|
|
PublishStatusSent Status = 0
|
|
|
|
PublishStatusFailed Status = -1
|
|
|
|
PublishStatusSucceeded Status = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s Status) String() string {
|
|
|
|
switch s {
|
|
|
|
case PublishStatusSent:
|
|
|
|
return "sent"
|
|
|
|
case PublishStatusFailed:
|
|
|
|
return "failed"
|
|
|
|
case PublishStatusSucceeded:
|
|
|
|
return "success"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
type Relay struct {
|
|
|
|
URL string
|
|
|
|
|
|
|
|
Connection *Connection
|
|
|
|
subscriptions s.MapOf[string, *Subscription]
|
|
|
|
|
2022-11-15 07:53:50 -03:00
|
|
|
Notices chan string
|
|
|
|
ConnectionError chan error
|
|
|
|
|
2022-11-12 21:49:57 -03:00
|
|
|
statusChans s.MapOf[string, chan Status]
|
2022-11-06 21:15:42 -03:00
|
|
|
}
|
|
|
|
|
2022-11-15 07:53:50 -03:00
|
|
|
func RelayConnect(url string) (*Relay, error) {
|
|
|
|
r := &Relay{URL: NormalizeURL(url)}
|
|
|
|
err := r.Connect()
|
|
|
|
return r, err
|
2022-11-06 21:15:42 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Relay) Connect() error {
|
|
|
|
if r.URL == "" {
|
|
|
|
return fmt.Errorf("invalid relay URL '%s'", r.URL)
|
|
|
|
}
|
|
|
|
|
|
|
|
socket, _, err := websocket.DefaultDialer.Dial(r.URL, nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error opening websocket to '%s': %w", r.URL, err)
|
|
|
|
}
|
|
|
|
|
2022-11-15 07:53:50 -03:00
|
|
|
r.Notices = make(chan string)
|
|
|
|
r.ConnectionError = make(chan error)
|
|
|
|
|
2022-11-06 21:15:42 -03:00
|
|
|
conn := NewConnection(socket)
|
2022-11-14 19:48:02 -03:00
|
|
|
r.Connection = conn
|
2022-11-06 21:15:42 -03:00
|
|
|
|
2022-11-14 19:48:02 -03:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
typ, message, err := conn.socket.ReadMessage()
|
|
|
|
if err != nil {
|
2022-11-15 07:53:50 -03:00
|
|
|
r.ConnectionError <- err
|
|
|
|
break
|
2022-11-14 19:48:02 -03:00
|
|
|
}
|
2022-11-15 07:53:50 -03:00
|
|
|
|
2022-11-14 19:48:02 -03:00
|
|
|
if typ == websocket.PingMessage {
|
|
|
|
conn.WriteMessage(websocket.PongMessage, nil)
|
|
|
|
continue
|
|
|
|
}
|
2022-11-06 21:15:42 -03:00
|
|
|
|
2022-11-14 19:48:02 -03:00
|
|
|
if typ != websocket.TextMessage || len(message) == 0 || message[0] != '[' {
|
|
|
|
continue
|
|
|
|
}
|
2022-11-06 21:15:42 -03:00
|
|
|
|
2022-11-14 19:48:02 -03:00
|
|
|
var jsonMessage []json.RawMessage
|
|
|
|
err = json.Unmarshal(message, &jsonMessage)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-11-06 21:15:42 -03:00
|
|
|
|
2022-11-14 19:48:02 -03:00
|
|
|
if len(jsonMessage) < 2 {
|
2022-11-06 21:15:42 -03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-11-14 19:48:02 -03:00
|
|
|
var label string
|
|
|
|
json.Unmarshal(jsonMessage[0], &label)
|
|
|
|
|
|
|
|
switch label {
|
|
|
|
case "NOTICE":
|
|
|
|
var content string
|
|
|
|
json.Unmarshal(jsonMessage[1], &content)
|
|
|
|
r.Notices <- content
|
|
|
|
case "EVENT":
|
|
|
|
if len(jsonMessage) < 3 {
|
2022-11-06 21:15:42 -03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-11-14 19:48:02 -03:00
|
|
|
var channel string
|
|
|
|
json.Unmarshal(jsonMessage[1], &channel)
|
|
|
|
if subscription, ok := r.subscriptions.Load(channel); ok {
|
|
|
|
var event Event
|
|
|
|
json.Unmarshal(jsonMessage[2], &event)
|
|
|
|
|
|
|
|
// check signature of all received events, ignore invalid
|
|
|
|
ok, err := event.CheckSignature()
|
|
|
|
if !ok {
|
|
|
|
errmsg := ""
|
|
|
|
if err != nil {
|
|
|
|
errmsg = err.Error()
|
|
|
|
}
|
|
|
|
log.Printf("bad signature: %s", errmsg)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the event matches the desired filter, ignore otherwise
|
|
|
|
if !subscription.filters.Match(&event) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !subscription.stopped {
|
|
|
|
subscription.Events <- event
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case "EOSE":
|
|
|
|
if len(jsonMessage) < 2 {
|
2022-11-06 21:15:42 -03:00
|
|
|
continue
|
|
|
|
}
|
2022-11-14 19:48:02 -03:00
|
|
|
var channel string
|
|
|
|
json.Unmarshal(jsonMessage[1], &channel)
|
|
|
|
if subscription, ok := r.subscriptions.Load(channel); ok {
|
|
|
|
subscription.EndOfStoredEvents <- struct{}{}
|
2022-11-06 21:15:42 -03:00
|
|
|
}
|
2022-11-14 19:48:02 -03:00
|
|
|
case "OK":
|
|
|
|
if len(jsonMessage) < 3 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
eventId string
|
|
|
|
ok bool
|
|
|
|
)
|
|
|
|
json.Unmarshal(jsonMessage[1], &eventId)
|
|
|
|
json.Unmarshal(jsonMessage[2], &ok)
|
|
|
|
|
|
|
|
if statusChan, ok := r.statusChans.Load(eventId); ok {
|
|
|
|
if ok {
|
|
|
|
statusChan <- PublishStatusSucceeded
|
|
|
|
} else {
|
|
|
|
statusChan <- PublishStatusFailed
|
|
|
|
}
|
2022-11-12 21:49:57 -03:00
|
|
|
}
|
|
|
|
}
|
2022-11-06 21:15:42 -03:00
|
|
|
}
|
2022-11-14 19:48:02 -03:00
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
2022-11-06 21:15:42 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r Relay) Publish(event Event) chan Status {
|
|
|
|
statusChan := make(chan Status)
|
|
|
|
|
|
|
|
go func() {
|
2022-11-12 21:49:57 -03:00
|
|
|
// we keep track of this so the OK message can be used to close it
|
|
|
|
r.statusChans.Store(event.ID, statusChan)
|
|
|
|
defer r.statusChans.Delete(event.ID)
|
|
|
|
|
2022-11-06 21:15:42 -03:00
|
|
|
err := r.Connection.WriteJSON([]interface{}{"EVENT", event})
|
|
|
|
if err != nil {
|
|
|
|
statusChan <- PublishStatusFailed
|
|
|
|
close(statusChan)
|
|
|
|
}
|
|
|
|
statusChan <- PublishStatusSent
|
|
|
|
|
|
|
|
sub := r.Subscribe(Filters{Filter{IDs: []string{event.ID}}})
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case receivedEvent := <-sub.Events:
|
|
|
|
if receivedEvent.ID == event.ID {
|
|
|
|
statusChan <- PublishStatusSucceeded
|
|
|
|
close(statusChan)
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
close(statusChan)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return statusChan
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Relay) Subscribe(filters Filters) *Subscription {
|
2022-11-14 19:48:02 -03:00
|
|
|
if r.Connection == nil {
|
|
|
|
panic(fmt.Errorf("must call .Connect() first before calling .Subscribe()"))
|
|
|
|
}
|
|
|
|
|
2022-11-06 21:15:42 -03:00
|
|
|
random := make([]byte, 7)
|
|
|
|
rand.Read(random)
|
|
|
|
id := hex.EncodeToString(random)
|
|
|
|
return r.subscribe(id, filters)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Relay) subscribe(id string, filters Filters) *Subscription {
|
2022-11-14 19:48:02 -03:00
|
|
|
sub := Subscription{
|
|
|
|
conn: r.Connection,
|
|
|
|
id: id,
|
|
|
|
Events: make(chan Event),
|
|
|
|
EndOfStoredEvents: make(chan struct{}),
|
|
|
|
}
|
2022-11-06 21:15:42 -03:00
|
|
|
|
|
|
|
r.subscriptions.Store(sub.id, &sub)
|
|
|
|
|
|
|
|
sub.Sub(filters)
|
|
|
|
return &sub
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Relay) Close() error {
|
|
|
|
return r.Connection.Close()
|
|
|
|
}
|