2023-08-10 14:32:11 -03:00
|
|
|
package khatru
|
2021-01-13 23:46:06 -03:00
|
|
|
|
|
|
|
import (
|
2023-05-03 09:40:25 -07:00
|
|
|
"context"
|
2023-01-15 22:38:24 -03:00
|
|
|
"crypto/rand"
|
2021-01-13 23:46:06 -03:00
|
|
|
"encoding/hex"
|
2023-12-22 19:51:35 -03:00
|
|
|
"errors"
|
2021-01-13 23:46:06 -03:00
|
|
|
"net/http"
|
2023-11-11 21:08:39 -03:00
|
|
|
"strings"
|
2023-08-10 14:32:11 -03:00
|
|
|
"sync"
|
2021-01-13 23:46:06 -03:00
|
|
|
"time"
|
2025-03-12 00:53:19 -03:00
|
|
|
"unsafe"
|
2021-01-13 23:46:06 -03:00
|
|
|
|
2024-10-26 23:41:39 -03:00
|
|
|
"github.com/bep/debounce"
|
2023-06-23 07:10:03 -03:00
|
|
|
"github.com/fasthttp/websocket"
|
2022-11-11 09:48:04 -03:00
|
|
|
"github.com/nbd-wtf/go-nostr"
|
2023-01-15 22:38:24 -03:00
|
|
|
"github.com/nbd-wtf/go-nostr/nip42"
|
2024-12-07 00:26:22 -03:00
|
|
|
"github.com/nbd-wtf/go-nostr/nip45"
|
|
|
|
"github.com/nbd-wtf/go-nostr/nip45/hyperloglog"
|
2024-10-26 23:41:39 -03:00
|
|
|
"github.com/nbd-wtf/go-nostr/nip77"
|
|
|
|
"github.com/nbd-wtf/go-nostr/nip77/negentropy"
|
|
|
|
"github.com/puzpuzpuz/xsync/v3"
|
2023-11-20 08:44:45 -03:00
|
|
|
"github.com/rs/cors"
|
2021-01-13 23:46:06 -03:00
|
|
|
)
|
|
|
|
|
2023-08-10 14:32:11 -03:00
|
|
|
// ServeHTTP implements http.Handler interface.
|
|
|
|
func (rl *Relay) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2024-11-05 18:37:12 +00:00
|
|
|
corsMiddleware := cors.New(cors.Options{
|
|
|
|
AllowedOrigins: []string{"*"},
|
|
|
|
AllowedMethods: []string{
|
|
|
|
http.MethodHead,
|
|
|
|
http.MethodGet,
|
|
|
|
http.MethodPost,
|
|
|
|
http.MethodPut,
|
|
|
|
http.MethodPatch,
|
|
|
|
http.MethodDelete,
|
|
|
|
},
|
|
|
|
AllowedHeaders: []string{"Authorization", "*"},
|
|
|
|
MaxAge: 86400,
|
|
|
|
})
|
|
|
|
|
2023-08-10 14:32:11 -03:00
|
|
|
if r.Header.Get("Upgrade") == "websocket" {
|
|
|
|
rl.HandleWebsocket(w, r)
|
|
|
|
} else if r.Header.Get("Accept") == "application/nostr+json" {
|
2024-11-05 18:37:12 +00:00
|
|
|
corsMiddleware.Handler(http.HandlerFunc(rl.HandleNIP11)).ServeHTTP(w, r)
|
2024-07-11 15:36:25 -03:00
|
|
|
} else if r.Header.Get("Content-Type") == "application/nostr+json+rpc" {
|
2024-11-05 18:37:12 +00:00
|
|
|
corsMiddleware.Handler(http.HandlerFunc(rl.HandleNIP86)).ServeHTTP(w, r)
|
2023-08-10 14:32:11 -03:00
|
|
|
} else {
|
2024-11-05 18:37:12 +00:00
|
|
|
corsMiddleware.Handler(rl.serveMux).ServeHTTP(w, r)
|
2023-08-10 14:32:11 -03:00
|
|
|
}
|
2021-01-13 23:46:06 -03:00
|
|
|
}
|
|
|
|
|
2023-08-10 14:32:11 -03:00
|
|
|
func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
|
2024-05-12 20:37:00 -03:00
|
|
|
for _, reject := range rl.RejectConnection {
|
|
|
|
if reject(r) {
|
2024-05-17 20:58:17 -03:00
|
|
|
w.WriteHeader(429) // Too many requests
|
2024-05-12 20:37:00 -03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-10 14:32:11 -03:00
|
|
|
conn, err := rl.upgrader.Upgrade(w, r, nil)
|
start: introduce Server type and Shutdown (breaking change)
the main motivation for this change is to be able to run tests.
before this commit, Start, Router and Log operated on global variables,
making automated testing unreasonably hard.
this commit puts all that a server needs in a new Server type,
which also made it possible for a Server.Shutdown - see ShutdownAware
doc comments.
BREAKING CHANGES:
- Relay.OnInitialized takes one argument now, *relayer.Server.
- relayer.Router is now replaced by relayer.Server.Router().
package users can still hook into the router from OnInitialized
for custom HTTP routing.
- relayer.Log is gone. apart from another global var, imho this was
a too opinionated choice for a framework to build a custom relay upon.
this commit introduces a Logger interface which package users can implement
for zerolog to make it log like before. see Server.Log for details.
other notable changes: finally added a couple basic tests, for start up
and shutdown. doc comments now explain most of the essentials,
hopefully making it more approachable for newcomers and easier to understand
the relayer package.
the changes in handlers.go are minimal, although git diff goes crazy.
this is because most of the lines are simply shifted indentation back by one
due to go fmt.
before this commit:
func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request)
func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request)
after:
func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request)
func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
|
|
|
if err != nil {
|
2023-08-10 14:32:11 -03:00
|
|
|
rl.Log.Printf("failed to upgrade websocket: %v\n", err)
|
start: introduce Server type and Shutdown (breaking change)
the main motivation for this change is to be able to run tests.
before this commit, Start, Router and Log operated on global variables,
making automated testing unreasonably hard.
this commit puts all that a server needs in a new Server type,
which also made it possible for a Server.Shutdown - see ShutdownAware
doc comments.
BREAKING CHANGES:
- Relay.OnInitialized takes one argument now, *relayer.Server.
- relayer.Router is now replaced by relayer.Server.Router().
package users can still hook into the router from OnInitialized
for custom HTTP routing.
- relayer.Log is gone. apart from another global var, imho this was
a too opinionated choice for a framework to build a custom relay upon.
this commit introduces a Logger interface which package users can implement
for zerolog to make it log like before. see Server.Log for details.
other notable changes: finally added a couple basic tests, for start up
and shutdown. doc comments now explain most of the essentials,
hopefully making it more approachable for newcomers and easier to understand
the relayer package.
the changes in handlers.go are minimal, although git diff goes crazy.
this is because most of the lines are simply shifted indentation back by one
due to go fmt.
before this commit:
func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request)
func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request)
after:
func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request)
func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
|
|
|
return
|
|
|
|
}
|
2024-07-30 09:43:04 -03:00
|
|
|
|
2023-08-10 14:32:11 -03:00
|
|
|
ticker := time.NewTicker(rl.PingPeriod)
|
start: introduce Server type and Shutdown (breaking change)
the main motivation for this change is to be able to run tests.
before this commit, Start, Router and Log operated on global variables,
making automated testing unreasonably hard.
this commit puts all that a server needs in a new Server type,
which also made it possible for a Server.Shutdown - see ShutdownAware
doc comments.
BREAKING CHANGES:
- Relay.OnInitialized takes one argument now, *relayer.Server.
- relayer.Router is now replaced by relayer.Server.Router().
package users can still hook into the router from OnInitialized
for custom HTTP routing.
- relayer.Log is gone. apart from another global var, imho this was
a too opinionated choice for a framework to build a custom relay upon.
this commit introduces a Logger interface which package users can implement
for zerolog to make it log like before. see Server.Log for details.
other notable changes: finally added a couple basic tests, for start up
and shutdown. doc comments now explain most of the essentials,
hopefully making it more approachable for newcomers and easier to understand
the relayer package.
the changes in handlers.go are minimal, although git diff goes crazy.
this is because most of the lines are simply shifted indentation back by one
due to go fmt.
before this commit:
func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request)
func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request)
after:
func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request)
func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
|
|
|
|
2023-01-16 01:24:35 -05:00
|
|
|
// NIP-42 challenge
|
2023-01-15 22:38:24 -03:00
|
|
|
challenge := make([]byte, 8)
|
|
|
|
rand.Read(challenge)
|
|
|
|
|
|
|
|
ws := &WebSocket{
|
2024-10-26 23:41:39 -03:00
|
|
|
conn: conn,
|
|
|
|
Request: r,
|
|
|
|
Challenge: hex.EncodeToString(challenge),
|
|
|
|
negentropySessions: xsync.NewMapOf[string, *NegentropySession](),
|
2023-01-15 22:38:24 -03:00
|
|
|
}
|
2024-10-16 15:06:47 -03:00
|
|
|
ws.Context, ws.cancel = context.WithCancel(context.Background())
|
start: introduce Server type and Shutdown (breaking change)
the main motivation for this change is to be able to run tests.
before this commit, Start, Router and Log operated on global variables,
making automated testing unreasonably hard.
this commit puts all that a server needs in a new Server type,
which also made it possible for a Server.Shutdown - see ShutdownAware
doc comments.
BREAKING CHANGES:
- Relay.OnInitialized takes one argument now, *relayer.Server.
- relayer.Router is now replaced by relayer.Server.Router().
package users can still hook into the router from OnInitialized
for custom HTTP routing.
- relayer.Log is gone. apart from another global var, imho this was
a too opinionated choice for a framework to build a custom relay upon.
this commit introduces a Logger interface which package users can implement
for zerolog to make it log like before. see Server.Log for details.
other notable changes: finally added a couple basic tests, for start up
and shutdown. doc comments now explain most of the essentials,
hopefully making it more approachable for newcomers and easier to understand
the relayer package.
the changes in handlers.go are minimal, although git diff goes crazy.
this is because most of the lines are simply shifted indentation back by one
due to go fmt.
before this commit:
func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request)
func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request)
after:
func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request)
func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
|
|
|
|
2024-07-30 09:43:04 -03:00
|
|
|
rl.clientsMutex.Lock()
|
|
|
|
rl.clients[ws] = make([]listenerSpec, 0, 2)
|
|
|
|
rl.clientsMutex.Unlock()
|
|
|
|
|
2023-12-09 00:00:20 -03:00
|
|
|
ctx, cancel := context.WithCancel(
|
|
|
|
context.WithValue(
|
|
|
|
context.Background(),
|
2023-12-25 09:14:09 -03:00
|
|
|
wsKey, ws,
|
2023-12-09 00:00:20 -03:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
kill := func() {
|
2023-12-09 09:00:11 -03:00
|
|
|
for _, ondisconnect := range rl.OnDisconnect {
|
|
|
|
ondisconnect(ctx)
|
|
|
|
}
|
|
|
|
|
2023-12-09 00:00:20 -03:00
|
|
|
ticker.Stop()
|
|
|
|
cancel()
|
2024-10-16 15:06:47 -03:00
|
|
|
ws.cancel()
|
|
|
|
ws.conn.Close()
|
2024-07-30 09:43:04 -03:00
|
|
|
|
2024-08-01 15:36:14 -03:00
|
|
|
rl.removeClientAndListeners(ws)
|
2023-12-09 00:00:20 -03:00
|
|
|
}
|
|
|
|
|
start: introduce Server type and Shutdown (breaking change)
the main motivation for this change is to be able to run tests.
before this commit, Start, Router and Log operated on global variables,
making automated testing unreasonably hard.
this commit puts all that a server needs in a new Server type,
which also made it possible for a Server.Shutdown - see ShutdownAware
doc comments.
BREAKING CHANGES:
- Relay.OnInitialized takes one argument now, *relayer.Server.
- relayer.Router is now replaced by relayer.Server.Router().
package users can still hook into the router from OnInitialized
for custom HTTP routing.
- relayer.Log is gone. apart from another global var, imho this was
a too opinionated choice for a framework to build a custom relay upon.
this commit introduces a Logger interface which package users can implement
for zerolog to make it log like before. see Server.Log for details.
other notable changes: finally added a couple basic tests, for start up
and shutdown. doc comments now explain most of the essentials,
hopefully making it more approachable for newcomers and easier to understand
the relayer package.
the changes in handlers.go are minimal, although git diff goes crazy.
this is because most of the lines are simply shifted indentation back by one
due to go fmt.
before this commit:
func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request)
func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request)
after:
func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request)
func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
|
|
|
go func() {
|
2023-12-09 00:00:20 -03:00
|
|
|
defer kill()
|
2021-01-13 23:46:06 -03:00
|
|
|
|
2024-10-16 15:06:47 -03:00
|
|
|
ws.conn.SetReadLimit(rl.MaxMessageSize)
|
|
|
|
ws.conn.SetReadDeadline(time.Now().Add(rl.PongWait))
|
|
|
|
ws.conn.SetPongHandler(func(string) error {
|
|
|
|
ws.conn.SetReadDeadline(time.Now().Add(rl.PongWait))
|
start: introduce Server type and Shutdown (breaking change)
the main motivation for this change is to be able to run tests.
before this commit, Start, Router and Log operated on global variables,
making automated testing unreasonably hard.
this commit puts all that a server needs in a new Server type,
which also made it possible for a Server.Shutdown - see ShutdownAware
doc comments.
BREAKING CHANGES:
- Relay.OnInitialized takes one argument now, *relayer.Server.
- relayer.Router is now replaced by relayer.Server.Router().
package users can still hook into the router from OnInitialized
for custom HTTP routing.
- relayer.Log is gone. apart from another global var, imho this was
a too opinionated choice for a framework to build a custom relay upon.
this commit introduces a Logger interface which package users can implement
for zerolog to make it log like before. see Server.Log for details.
other notable changes: finally added a couple basic tests, for start up
and shutdown. doc comments now explain most of the essentials,
hopefully making it more approachable for newcomers and easier to understand
the relayer package.
the changes in handlers.go are minimal, although git diff goes crazy.
this is because most of the lines are simply shifted indentation back by one
due to go fmt.
before this commit:
func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request)
func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request)
after:
func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request)
func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2023-08-10 14:32:11 -03:00
|
|
|
for _, onconnect := range rl.OnConnect {
|
2023-12-08 22:51:00 -03:00
|
|
|
onconnect(ctx)
|
2023-01-15 22:38:24 -03:00
|
|
|
}
|
|
|
|
|
2025-03-11 17:32:04 -03:00
|
|
|
smp := nostr.NewMessageParser()
|
2025-03-07 21:47:34 -03:00
|
|
|
|
start: introduce Server type and Shutdown (breaking change)
the main motivation for this change is to be able to run tests.
before this commit, Start, Router and Log operated on global variables,
making automated testing unreasonably hard.
this commit puts all that a server needs in a new Server type,
which also made it possible for a Server.Shutdown - see ShutdownAware
doc comments.
BREAKING CHANGES:
- Relay.OnInitialized takes one argument now, *relayer.Server.
- relayer.Router is now replaced by relayer.Server.Router().
package users can still hook into the router from OnInitialized
for custom HTTP routing.
- relayer.Log is gone. apart from another global var, imho this was
a too opinionated choice for a framework to build a custom relay upon.
this commit introduces a Logger interface which package users can implement
for zerolog to make it log like before. see Server.Log for details.
other notable changes: finally added a couple basic tests, for start up
and shutdown. doc comments now explain most of the essentials,
hopefully making it more approachable for newcomers and easier to understand
the relayer package.
the changes in handlers.go are minimal, although git diff goes crazy.
this is because most of the lines are simply shifted indentation back by one
due to go fmt.
before this commit:
func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request)
func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request)
after:
func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request)
func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
|
|
|
for {
|
2025-03-12 00:53:19 -03:00
|
|
|
typ, msgb, err := ws.conn.ReadMessage()
|
start: introduce Server type and Shutdown (breaking change)
the main motivation for this change is to be able to run tests.
before this commit, Start, Router and Log operated on global variables,
making automated testing unreasonably hard.
this commit puts all that a server needs in a new Server type,
which also made it possible for a Server.Shutdown - see ShutdownAware
doc comments.
BREAKING CHANGES:
- Relay.OnInitialized takes one argument now, *relayer.Server.
- relayer.Router is now replaced by relayer.Server.Router().
package users can still hook into the router from OnInitialized
for custom HTTP routing.
- relayer.Log is gone. apart from another global var, imho this was
a too opinionated choice for a framework to build a custom relay upon.
this commit introduces a Logger interface which package users can implement
for zerolog to make it log like before. see Server.Log for details.
other notable changes: finally added a couple basic tests, for start up
and shutdown. doc comments now explain most of the essentials,
hopefully making it more approachable for newcomers and easier to understand
the relayer package.
the changes in handlers.go are minimal, although git diff goes crazy.
this is because most of the lines are simply shifted indentation back by one
due to go fmt.
before this commit:
func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request)
func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request)
after:
func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request)
func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
|
|
|
if err != nil {
|
|
|
|
if websocket.IsUnexpectedCloseError(
|
|
|
|
err,
|
2023-11-19 08:30:06 -03:00
|
|
|
websocket.CloseNormalClosure, // 1000
|
start: introduce Server type and Shutdown (breaking change)
the main motivation for this change is to be able to run tests.
before this commit, Start, Router and Log operated on global variables,
making automated testing unreasonably hard.
this commit puts all that a server needs in a new Server type,
which also made it possible for a Server.Shutdown - see ShutdownAware
doc comments.
BREAKING CHANGES:
- Relay.OnInitialized takes one argument now, *relayer.Server.
- relayer.Router is now replaced by relayer.Server.Router().
package users can still hook into the router from OnInitialized
for custom HTTP routing.
- relayer.Log is gone. apart from another global var, imho this was
a too opinionated choice for a framework to build a custom relay upon.
this commit introduces a Logger interface which package users can implement
for zerolog to make it log like before. see Server.Log for details.
other notable changes: finally added a couple basic tests, for start up
and shutdown. doc comments now explain most of the essentials,
hopefully making it more approachable for newcomers and easier to understand
the relayer package.
the changes in handlers.go are minimal, although git diff goes crazy.
this is because most of the lines are simply shifted indentation back by one
due to go fmt.
before this commit:
func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request)
func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request)
after:
func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request)
func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
|
|
|
websocket.CloseGoingAway, // 1001
|
|
|
|
websocket.CloseNoStatusReceived, // 1005
|
|
|
|
websocket.CloseAbnormalClosure, // 1006
|
2024-01-05 20:55:24 -03:00
|
|
|
4537, // some client seems to send many of these
|
start: introduce Server type and Shutdown (breaking change)
the main motivation for this change is to be able to run tests.
before this commit, Start, Router and Log operated on global variables,
making automated testing unreasonably hard.
this commit puts all that a server needs in a new Server type,
which also made it possible for a Server.Shutdown - see ShutdownAware
doc comments.
BREAKING CHANGES:
- Relay.OnInitialized takes one argument now, *relayer.Server.
- relayer.Router is now replaced by relayer.Server.Router().
package users can still hook into the router from OnInitialized
for custom HTTP routing.
- relayer.Log is gone. apart from another global var, imho this was
a too opinionated choice for a framework to build a custom relay upon.
this commit introduces a Logger interface which package users can implement
for zerolog to make it log like before. see Server.Log for details.
other notable changes: finally added a couple basic tests, for start up
and shutdown. doc comments now explain most of the essentials,
hopefully making it more approachable for newcomers and easier to understand
the relayer package.
the changes in handlers.go are minimal, although git diff goes crazy.
this is because most of the lines are simply shifted indentation back by one
due to go fmt.
before this commit:
func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request)
func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request)
after:
func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request)
func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
|
|
|
) {
|
2025-02-23 18:20:54 -03:00
|
|
|
rl.Log.Printf("unexpected close error from %s: %v\n", GetIPFromRequest(r), err)
|
start: introduce Server type and Shutdown (breaking change)
the main motivation for this change is to be able to run tests.
before this commit, Start, Router and Log operated on global variables,
making automated testing unreasonably hard.
this commit puts all that a server needs in a new Server type,
which also made it possible for a Server.Shutdown - see ShutdownAware
doc comments.
BREAKING CHANGES:
- Relay.OnInitialized takes one argument now, *relayer.Server.
- relayer.Router is now replaced by relayer.Server.Router().
package users can still hook into the router from OnInitialized
for custom HTTP routing.
- relayer.Log is gone. apart from another global var, imho this was
a too opinionated choice for a framework to build a custom relay upon.
this commit introduces a Logger interface which package users can implement
for zerolog to make it log like before. see Server.Log for details.
other notable changes: finally added a couple basic tests, for start up
and shutdown. doc comments now explain most of the essentials,
hopefully making it more approachable for newcomers and easier to understand
the relayer package.
the changes in handlers.go are minimal, although git diff goes crazy.
this is because most of the lines are simply shifted indentation back by one
due to go fmt.
before this commit:
func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request)
func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request)
after:
func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request)
func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
|
|
|
}
|
2024-10-16 15:06:47 -03:00
|
|
|
ws.cancel()
|
2023-12-09 00:14:08 -03:00
|
|
|
return
|
start: introduce Server type and Shutdown (breaking change)
the main motivation for this change is to be able to run tests.
before this commit, Start, Router and Log operated on global variables,
making automated testing unreasonably hard.
this commit puts all that a server needs in a new Server type,
which also made it possible for a Server.Shutdown - see ShutdownAware
doc comments.
BREAKING CHANGES:
- Relay.OnInitialized takes one argument now, *relayer.Server.
- relayer.Router is now replaced by relayer.Server.Router().
package users can still hook into the router from OnInitialized
for custom HTTP routing.
- relayer.Log is gone. apart from another global var, imho this was
a too opinionated choice for a framework to build a custom relay upon.
this commit introduces a Logger interface which package users can implement
for zerolog to make it log like before. see Server.Log for details.
other notable changes: finally added a couple basic tests, for start up
and shutdown. doc comments now explain most of the essentials,
hopefully making it more approachable for newcomers and easier to understand
the relayer package.
the changes in handlers.go are minimal, although git diff goes crazy.
this is because most of the lines are simply shifted indentation back by one
due to go fmt.
before this commit:
func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request)
func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request)
after:
func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request)
func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
|
|
|
}
|
2021-01-13 23:46:06 -03:00
|
|
|
|
start: introduce Server type and Shutdown (breaking change)
the main motivation for this change is to be able to run tests.
before this commit, Start, Router and Log operated on global variables,
making automated testing unreasonably hard.
this commit puts all that a server needs in a new Server type,
which also made it possible for a Server.Shutdown - see ShutdownAware
doc comments.
BREAKING CHANGES:
- Relay.OnInitialized takes one argument now, *relayer.Server.
- relayer.Router is now replaced by relayer.Server.Router().
package users can still hook into the router from OnInitialized
for custom HTTP routing.
- relayer.Log is gone. apart from another global var, imho this was
a too opinionated choice for a framework to build a custom relay upon.
this commit introduces a Logger interface which package users can implement
for zerolog to make it log like before. see Server.Log for details.
other notable changes: finally added a couple basic tests, for start up
and shutdown. doc comments now explain most of the essentials,
hopefully making it more approachable for newcomers and easier to understand
the relayer package.
the changes in handlers.go are minimal, although git diff goes crazy.
this is because most of the lines are simply shifted indentation back by one
due to go fmt.
before this commit:
func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request)
func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request)
after:
func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request)
func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
|
|
|
if typ == websocket.PingMessage {
|
|
|
|
ws.WriteMessage(websocket.PongMessage, nil)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2025-03-12 00:53:19 -03:00
|
|
|
message := unsafe.String(unsafe.SliceData(msgb), len(msgb))
|
|
|
|
|
|
|
|
// parse messages sequentially otherwise sonic breaks
|
2025-03-07 21:47:34 -03:00
|
|
|
envelope, err := smp.ParseMessage(message)
|
|
|
|
|
|
|
|
// then delegate to the goroutine
|
2025-03-12 00:53:19 -03:00
|
|
|
go func(message string) {
|
2025-03-07 21:47:34 -03:00
|
|
|
if err != nil {
|
|
|
|
if err == nostr.UnknownLabel && rl.Negentropy {
|
|
|
|
envelope = nip77.ParseNegMessage(message)
|
2024-10-26 23:41:39 -03:00
|
|
|
}
|
|
|
|
if envelope == nil {
|
2025-03-07 21:47:34 -03:00
|
|
|
ws.WriteJSON(nostr.NoticeEnvelope("failed to parse envelope: " + err.Error()))
|
2024-10-26 23:41:39 -03:00
|
|
|
return
|
|
|
|
}
|
2021-02-17 16:59:56 -03:00
|
|
|
}
|
2021-01-13 23:46:06 -03:00
|
|
|
|
2023-11-28 22:43:06 -03:00
|
|
|
switch env := envelope.(type) {
|
|
|
|
case *nostr.EventEnvelope:
|
2023-11-19 16:40:29 -03:00
|
|
|
// check id
|
2024-09-25 14:33:43 -03:00
|
|
|
if !env.Event.CheckID() {
|
2023-11-28 22:43:06 -03:00
|
|
|
ws.WriteJSON(nostr.OKEnvelope{EventID: env.Event.ID, OK: false, Reason: "invalid: id is computed incorrectly"})
|
2023-11-22 17:30:34 -03:00
|
|
|
return
|
2023-11-19 16:40:29 -03:00
|
|
|
}
|
2021-02-14 21:08:02 -03:00
|
|
|
|
2023-11-20 08:44:45 -03:00
|
|
|
// check signature
|
2023-11-28 22:43:06 -03:00
|
|
|
if ok, err := env.Event.CheckSignature(); err != nil {
|
|
|
|
ws.WriteJSON(nostr.OKEnvelope{EventID: env.Event.ID, OK: false, Reason: "error: failed to verify signature"})
|
start: introduce Server type and Shutdown (breaking change)
the main motivation for this change is to be able to run tests.
before this commit, Start, Router and Log operated on global variables,
making automated testing unreasonably hard.
this commit puts all that a server needs in a new Server type,
which also made it possible for a Server.Shutdown - see ShutdownAware
doc comments.
BREAKING CHANGES:
- Relay.OnInitialized takes one argument now, *relayer.Server.
- relayer.Router is now replaced by relayer.Server.Router().
package users can still hook into the router from OnInitialized
for custom HTTP routing.
- relayer.Log is gone. apart from another global var, imho this was
a too opinionated choice for a framework to build a custom relay upon.
this commit introduces a Logger interface which package users can implement
for zerolog to make it log like before. see Server.Log for details.
other notable changes: finally added a couple basic tests, for start up
and shutdown. doc comments now explain most of the essentials,
hopefully making it more approachable for newcomers and easier to understand
the relayer package.
the changes in handlers.go are minimal, although git diff goes crazy.
this is because most of the lines are simply shifted indentation back by one
due to go fmt.
before this commit:
func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request)
func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request)
after:
func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request)
func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
|
|
|
return
|
|
|
|
} else if !ok {
|
2023-11-28 22:43:06 -03:00
|
|
|
ws.WriteJSON(nostr.OKEnvelope{EventID: env.Event.ID, OK: false, Reason: "invalid: signature is invalid"})
|
start: introduce Server type and Shutdown (breaking change)
the main motivation for this change is to be able to run tests.
before this commit, Start, Router and Log operated on global variables,
making automated testing unreasonably hard.
this commit puts all that a server needs in a new Server type,
which also made it possible for a Server.Shutdown - see ShutdownAware
doc comments.
BREAKING CHANGES:
- Relay.OnInitialized takes one argument now, *relayer.Server.
- relayer.Router is now replaced by relayer.Server.Router().
package users can still hook into the router from OnInitialized
for custom HTTP routing.
- relayer.Log is gone. apart from another global var, imho this was
a too opinionated choice for a framework to build a custom relay upon.
this commit introduces a Logger interface which package users can implement
for zerolog to make it log like before. see Server.Log for details.
other notable changes: finally added a couple basic tests, for start up
and shutdown. doc comments now explain most of the essentials,
hopefully making it more approachable for newcomers and easier to understand
the relayer package.
the changes in handlers.go are minimal, although git diff goes crazy.
this is because most of the lines are simply shifted indentation back by one
due to go fmt.
before this commit:
func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request)
func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request)
after:
func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request)
func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
|
|
|
return
|
|
|
|
}
|
2021-02-14 21:08:02 -03:00
|
|
|
|
2024-02-13 12:22:15 -03:00
|
|
|
// check NIP-70 protected
|
|
|
|
for _, v := range env.Event.Tags {
|
|
|
|
if len(v) == 1 && v[0] == "-" {
|
|
|
|
msg := "must be published by event author"
|
|
|
|
authed := GetAuthed(ctx)
|
|
|
|
if authed == "" {
|
|
|
|
RequestAuth(ctx)
|
|
|
|
ws.WriteJSON(nostr.OKEnvelope{
|
|
|
|
EventID: env.Event.ID,
|
|
|
|
OK: false,
|
|
|
|
Reason: "auth-required: " + msg,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if authed != env.Event.PubKey {
|
|
|
|
ws.WriteJSON(nostr.OKEnvelope{
|
|
|
|
EventID: env.Event.ID,
|
|
|
|
OK: false,
|
|
|
|
Reason: "blocked: " + msg,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-30 09:43:04 -03:00
|
|
|
srl := rl
|
|
|
|
if rl.getSubRelayFromEvent != nil {
|
|
|
|
srl = rl.getSubRelayFromEvent(&env.Event)
|
|
|
|
}
|
|
|
|
|
2023-08-10 14:32:11 -03:00
|
|
|
var ok bool
|
2023-12-22 19:51:35 -03:00
|
|
|
var writeErr error
|
2024-04-26 14:56:50 -03:00
|
|
|
var skipBroadcast bool
|
2024-09-05 14:38:59 -03:00
|
|
|
|
2023-11-28 22:43:06 -03:00
|
|
|
if env.Event.Kind == 5 {
|
2023-12-22 19:51:35 -03:00
|
|
|
// this always returns "blocked: " whenever it returns an error
|
2024-07-30 09:43:04 -03:00
|
|
|
writeErr = srl.handleDeleteRequest(ctx, &env.Event)
|
2023-08-10 14:32:11 -03:00
|
|
|
} else {
|
2023-12-22 19:51:35 -03:00
|
|
|
// this will also always return a prefixed reason
|
2024-07-30 09:43:04 -03:00
|
|
|
skipBroadcast, writeErr = srl.AddEvent(ctx, &env.Event)
|
start: introduce Server type and Shutdown (breaking change)
the main motivation for this change is to be able to run tests.
before this commit, Start, Router and Log operated on global variables,
making automated testing unreasonably hard.
this commit puts all that a server needs in a new Server type,
which also made it possible for a Server.Shutdown - see ShutdownAware
doc comments.
BREAKING CHANGES:
- Relay.OnInitialized takes one argument now, *relayer.Server.
- relayer.Router is now replaced by relayer.Server.Router().
package users can still hook into the router from OnInitialized
for custom HTTP routing.
- relayer.Log is gone. apart from another global var, imho this was
a too opinionated choice for a framework to build a custom relay upon.
this commit introduces a Logger interface which package users can implement
for zerolog to make it log like before. see Server.Log for details.
other notable changes: finally added a couple basic tests, for start up
and shutdown. doc comments now explain most of the essentials,
hopefully making it more approachable for newcomers and easier to understand
the relayer package.
the changes in handlers.go are minimal, although git diff goes crazy.
this is because most of the lines are simply shifted indentation back by one
due to go fmt.
before this commit:
func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request)
func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request)
after:
func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request)
func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
|
|
|
}
|
2023-11-04 17:35:47 -03:00
|
|
|
|
2023-12-22 22:35:44 -03:00
|
|
|
var reason string
|
2023-12-22 19:51:35 -03:00
|
|
|
if writeErr == nil {
|
2023-08-10 14:32:11 -03:00
|
|
|
ok = true
|
2024-07-30 09:43:04 -03:00
|
|
|
for _, ovw := range srl.OverwriteResponseEvent {
|
2024-01-18 18:20:24 -03:00
|
|
|
ovw(ctx, &env.Event)
|
|
|
|
}
|
2024-04-26 14:56:50 -03:00
|
|
|
if !skipBroadcast {
|
2024-08-01 12:41:27 -03:00
|
|
|
srl.notifyListeners(&env.Event)
|
2024-04-26 14:56:50 -03:00
|
|
|
}
|
2023-08-10 14:32:11 -03:00
|
|
|
} else {
|
2023-12-22 22:35:44 -03:00
|
|
|
reason = writeErr.Error()
|
2023-12-09 14:41:54 -03:00
|
|
|
if strings.HasPrefix(reason, "auth-required:") {
|
2023-12-25 09:30:13 -03:00
|
|
|
RequestAuth(ctx)
|
2023-12-06 12:14:58 -03:00
|
|
|
}
|
2023-06-16 17:38:32 -03:00
|
|
|
}
|
2023-11-28 22:43:06 -03:00
|
|
|
ws.WriteJSON(nostr.OKEnvelope{EventID: env.Event.ID, OK: ok, Reason: reason})
|
|
|
|
case *nostr.CountEnvelope:
|
2024-12-07 00:26:22 -03:00
|
|
|
if rl.CountEvents == nil && rl.CountEventsHLL == nil {
|
2023-11-28 22:43:06 -03:00
|
|
|
ws.WriteJSON(nostr.ClosedEnvelope{SubscriptionID: env.SubscriptionID, Reason: "unsupported: this relay does not support NIP-45"})
|
2023-05-17 19:54:56 +09:00
|
|
|
return
|
|
|
|
}
|
2024-07-30 09:43:04 -03:00
|
|
|
|
2023-08-10 14:32:11 -03:00
|
|
|
var total int64
|
2024-12-07 00:26:22 -03:00
|
|
|
var hll *hyperloglog.HyperLogLog
|
|
|
|
uneligibleForHLL := false
|
|
|
|
|
2025-03-07 10:11:58 -03:00
|
|
|
srl := rl
|
|
|
|
if rl.getSubRelayFromFilter != nil {
|
|
|
|
srl = rl.getSubRelayFromFilter(env.Filter)
|
|
|
|
}
|
2024-12-07 00:26:22 -03:00
|
|
|
|
2025-03-07 10:11:58 -03:00
|
|
|
if offset := nip45.HyperLogLogEventPubkeyOffsetForFilter(env.Filter); offset != -1 && !uneligibleForHLL {
|
|
|
|
total, hll = srl.handleCountRequestWithHLL(ctx, ws, env.Filter, offset)
|
|
|
|
} else {
|
|
|
|
total = srl.handleCountRequest(ctx, ws, env.Filter)
|
2023-05-17 19:54:56 +09:00
|
|
|
}
|
2024-12-07 00:26:22 -03:00
|
|
|
|
|
|
|
resp := nostr.CountEnvelope{
|
|
|
|
SubscriptionID: env.SubscriptionID,
|
|
|
|
Count: &total,
|
|
|
|
}
|
|
|
|
if hll != nil {
|
|
|
|
resp.HyperLogLog = hll.GetRegisters()
|
|
|
|
}
|
|
|
|
|
|
|
|
ws.WriteJSON(resp)
|
|
|
|
|
2023-11-28 22:43:06 -03:00
|
|
|
case *nostr.ReqEnvelope:
|
2023-08-10 14:32:11 -03:00
|
|
|
eose := sync.WaitGroup{}
|
2023-11-28 22:43:06 -03:00
|
|
|
eose.Add(len(env.Filters))
|
2023-08-10 14:32:11 -03:00
|
|
|
|
2023-12-08 23:48:30 -03:00
|
|
|
// a context just for the "stored events" request handler
|
|
|
|
reqCtx, cancelReqCtx := context.WithCancelCause(ctx)
|
2023-12-08 22:51:00 -03:00
|
|
|
|
2023-12-25 09:14:09 -03:00
|
|
|
// expose subscription id in the context
|
|
|
|
reqCtx = context.WithValue(reqCtx, subscriptionIdKey, env.SubscriptionID)
|
|
|
|
|
2023-12-08 23:48:30 -03:00
|
|
|
// handle each filter separately -- dispatching events as they're loaded from databases
|
2023-11-28 22:43:06 -03:00
|
|
|
for _, filter := range env.Filters {
|
2024-07-30 09:43:04 -03:00
|
|
|
srl := rl
|
|
|
|
if rl.getSubRelayFromFilter != nil {
|
|
|
|
srl = rl.getSubRelayFromFilter(filter)
|
|
|
|
}
|
|
|
|
err := srl.handleRequest(reqCtx, env.SubscriptionID, &eose, ws, filter)
|
2023-12-06 21:32:48 -03:00
|
|
|
if err != nil {
|
2023-12-08 23:48:30 -03:00
|
|
|
// fail everything if any filter is rejected
|
2023-12-09 14:41:54 -03:00
|
|
|
reason := err.Error()
|
|
|
|
if strings.HasPrefix(reason, "auth-required:") {
|
2023-12-25 09:30:13 -03:00
|
|
|
RequestAuth(ctx)
|
2023-12-06 12:14:58 -03:00
|
|
|
}
|
2023-12-06 11:56:56 -03:00
|
|
|
ws.WriteJSON(nostr.ClosedEnvelope{SubscriptionID: env.SubscriptionID, Reason: reason})
|
2023-12-22 19:51:35 -03:00
|
|
|
cancelReqCtx(errors.New("filter rejected"))
|
2023-12-06 11:56:56 -03:00
|
|
|
return
|
2024-07-30 09:43:04 -03:00
|
|
|
} else {
|
|
|
|
rl.addListener(ws, env.SubscriptionID, srl, filter, cancelReqCtx)
|
2021-12-25 21:22:40 -03:00
|
|
|
}
|
2023-11-28 22:43:06 -03:00
|
|
|
}
|
2023-05-01 19:21:09 -03:00
|
|
|
|
2023-08-10 14:32:11 -03:00
|
|
|
go func() {
|
2025-02-09 20:46:53 -03:00
|
|
|
// when all events have been loaded from databases and dispatched we can fire the EOSE message
|
2023-08-10 14:32:11 -03:00
|
|
|
eose.Wait()
|
2023-11-28 22:43:06 -03:00
|
|
|
ws.WriteJSON(nostr.EOSEEnvelope(env.SubscriptionID))
|
2023-08-10 14:32:11 -03:00
|
|
|
}()
|
2023-11-28 22:43:06 -03:00
|
|
|
case *nostr.CloseEnvelope:
|
2024-07-30 09:43:04 -03:00
|
|
|
id := string(*env)
|
|
|
|
rl.removeListenerId(ws, id)
|
2023-11-28 22:43:06 -03:00
|
|
|
case *nostr.AuthEnvelope:
|
2024-12-31 22:15:06 -03:00
|
|
|
wsBaseUrl := strings.Replace(rl.getBaseURL(r), "http", "ws", 1)
|
2023-12-06 15:03:53 -03:00
|
|
|
if pubkey, ok := nip42.ValidateAuthEvent(&env.Event, ws.Challenge, wsBaseUrl); ok {
|
|
|
|
ws.AuthedPublicKey = pubkey
|
2023-12-27 12:30:23 -03:00
|
|
|
ws.authLock.Lock()
|
|
|
|
if ws.Authed != nil {
|
|
|
|
close(ws.Authed)
|
2023-12-27 12:55:05 -03:00
|
|
|
ws.Authed = nil
|
2023-12-27 12:30:23 -03:00
|
|
|
}
|
|
|
|
ws.authLock.Unlock()
|
2023-12-06 15:03:53 -03:00
|
|
|
ws.WriteJSON(nostr.OKEnvelope{EventID: env.Event.ID, OK: true})
|
|
|
|
} else {
|
|
|
|
ws.WriteJSON(nostr.OKEnvelope{EventID: env.Event.ID, OK: false, Reason: "error: failed to authenticate"})
|
2023-01-15 22:38:24 -03:00
|
|
|
}
|
2024-10-26 23:41:39 -03:00
|
|
|
case *nip77.OpenEnvelope:
|
|
|
|
srl := rl
|
|
|
|
if rl.getSubRelayFromFilter != nil {
|
|
|
|
srl = rl.getSubRelayFromFilter(env.Filter)
|
|
|
|
if !srl.Negentropy {
|
|
|
|
// ignore
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vec, err := srl.startNegentropySession(ctx, env.Filter)
|
|
|
|
if err != nil {
|
|
|
|
// fail everything if any filter is rejected
|
|
|
|
reason := err.Error()
|
|
|
|
if strings.HasPrefix(reason, "auth-required:") {
|
|
|
|
RequestAuth(ctx)
|
|
|
|
}
|
|
|
|
ws.WriteJSON(nip77.ErrorEnvelope{SubscriptionID: env.SubscriptionID, Reason: reason})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// reconcile to get the next message and return it
|
|
|
|
neg := negentropy.New(vec, 1024*1024)
|
|
|
|
out, err := neg.Reconcile(env.Message)
|
|
|
|
if err != nil {
|
|
|
|
ws.WriteJSON(nip77.ErrorEnvelope{SubscriptionID: env.SubscriptionID, Reason: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ws.WriteJSON(nip77.MessageEnvelope{SubscriptionID: env.SubscriptionID, Message: out})
|
|
|
|
|
|
|
|
// if the message is not empty that means we'll probably have more reconciliation sessions, so store this
|
|
|
|
if out != "" {
|
|
|
|
deb := debounce.New(time.Second * 7)
|
|
|
|
negSession := &NegentropySession{
|
|
|
|
neg: neg,
|
|
|
|
postponeClose: func() {
|
|
|
|
deb(func() {
|
|
|
|
ws.negentropySessions.Delete(env.SubscriptionID)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
negSession.postponeClose()
|
|
|
|
|
|
|
|
ws.negentropySessions.Store(env.SubscriptionID, negSession)
|
|
|
|
}
|
|
|
|
case *nip77.MessageEnvelope:
|
|
|
|
negSession, ok := ws.negentropySessions.Load(env.SubscriptionID)
|
|
|
|
if !ok {
|
|
|
|
// bad luck, your request was destroyed
|
|
|
|
ws.WriteJSON(nip77.ErrorEnvelope{SubscriptionID: env.SubscriptionID, Reason: "CLOSED"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// reconcile to get the next message and return it
|
|
|
|
out, err := negSession.neg.Reconcile(env.Message)
|
|
|
|
if err != nil {
|
|
|
|
ws.WriteJSON(nip77.ErrorEnvelope{SubscriptionID: env.SubscriptionID, Reason: err.Error()})
|
|
|
|
ws.negentropySessions.Delete(env.SubscriptionID)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ws.WriteJSON(nip77.MessageEnvelope{SubscriptionID: env.SubscriptionID, Message: out})
|
|
|
|
|
|
|
|
// if there is more reconciliation to do, postpone this
|
|
|
|
if out != "" {
|
|
|
|
negSession.postponeClose()
|
|
|
|
} else {
|
|
|
|
// otherwise we can just close it
|
|
|
|
ws.negentropySessions.Delete(env.SubscriptionID)
|
|
|
|
}
|
|
|
|
case *nip77.CloseEnvelope:
|
|
|
|
ws.negentropySessions.Delete(env.SubscriptionID)
|
2021-12-25 21:22:40 -03:00
|
|
|
}
|
start: introduce Server type and Shutdown (breaking change)
the main motivation for this change is to be able to run tests.
before this commit, Start, Router and Log operated on global variables,
making automated testing unreasonably hard.
this commit puts all that a server needs in a new Server type,
which also made it possible for a Server.Shutdown - see ShutdownAware
doc comments.
BREAKING CHANGES:
- Relay.OnInitialized takes one argument now, *relayer.Server.
- relayer.Router is now replaced by relayer.Server.Router().
package users can still hook into the router from OnInitialized
for custom HTTP routing.
- relayer.Log is gone. apart from another global var, imho this was
a too opinionated choice for a framework to build a custom relay upon.
this commit introduces a Logger interface which package users can implement
for zerolog to make it log like before. see Server.Log for details.
other notable changes: finally added a couple basic tests, for start up
and shutdown. doc comments now explain most of the essentials,
hopefully making it more approachable for newcomers and easier to understand
the relayer package.
the changes in handlers.go are minimal, although git diff goes crazy.
this is because most of the lines are simply shifted indentation back by one
due to go fmt.
before this commit:
func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request)
func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request)
after:
func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request)
func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
|
|
|
}(message)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2023-12-09 00:00:20 -03:00
|
|
|
defer kill()
|
2022-07-11 16:00:21 -03:00
|
|
|
|
start: introduce Server type and Shutdown (breaking change)
the main motivation for this change is to be able to run tests.
before this commit, Start, Router and Log operated on global variables,
making automated testing unreasonably hard.
this commit puts all that a server needs in a new Server type,
which also made it possible for a Server.Shutdown - see ShutdownAware
doc comments.
BREAKING CHANGES:
- Relay.OnInitialized takes one argument now, *relayer.Server.
- relayer.Router is now replaced by relayer.Server.Router().
package users can still hook into the router from OnInitialized
for custom HTTP routing.
- relayer.Log is gone. apart from another global var, imho this was
a too opinionated choice for a framework to build a custom relay upon.
this commit introduces a Logger interface which package users can implement
for zerolog to make it log like before. see Server.Log for details.
other notable changes: finally added a couple basic tests, for start up
and shutdown. doc comments now explain most of the essentials,
hopefully making it more approachable for newcomers and easier to understand
the relayer package.
the changes in handlers.go are minimal, although git diff goes crazy.
this is because most of the lines are simply shifted indentation back by one
due to go fmt.
before this commit:
func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request)
func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request)
after:
func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request)
func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
|
|
|
for {
|
|
|
|
select {
|
2023-12-09 00:00:20 -03:00
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
start: introduce Server type and Shutdown (breaking change)
the main motivation for this change is to be able to run tests.
before this commit, Start, Router and Log operated on global variables,
making automated testing unreasonably hard.
this commit puts all that a server needs in a new Server type,
which also made it possible for a Server.Shutdown - see ShutdownAware
doc comments.
BREAKING CHANGES:
- Relay.OnInitialized takes one argument now, *relayer.Server.
- relayer.Router is now replaced by relayer.Server.Router().
package users can still hook into the router from OnInitialized
for custom HTTP routing.
- relayer.Log is gone. apart from another global var, imho this was
a too opinionated choice for a framework to build a custom relay upon.
this commit introduces a Logger interface which package users can implement
for zerolog to make it log like before. see Server.Log for details.
other notable changes: finally added a couple basic tests, for start up
and shutdown. doc comments now explain most of the essentials,
hopefully making it more approachable for newcomers and easier to understand
the relayer package.
the changes in handlers.go are minimal, although git diff goes crazy.
this is because most of the lines are simply shifted indentation back by one
due to go fmt.
before this commit:
func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request)
func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request)
after:
func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request)
func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
|
|
|
case <-ticker.C:
|
|
|
|
err := ws.WriteMessage(websocket.PingMessage, nil)
|
|
|
|
if err != nil {
|
2023-11-11 21:08:39 -03:00
|
|
|
if !strings.HasSuffix(err.Error(), "use of closed network connection") {
|
2023-11-07 22:55:24 -03:00
|
|
|
rl.Log.Printf("error writing ping: %v; closing websocket\n", err)
|
|
|
|
}
|
start: introduce Server type and Shutdown (breaking change)
the main motivation for this change is to be able to run tests.
before this commit, Start, Router and Log operated on global variables,
making automated testing unreasonably hard.
this commit puts all that a server needs in a new Server type,
which also made it possible for a Server.Shutdown - see ShutdownAware
doc comments.
BREAKING CHANGES:
- Relay.OnInitialized takes one argument now, *relayer.Server.
- relayer.Router is now replaced by relayer.Server.Router().
package users can still hook into the router from OnInitialized
for custom HTTP routing.
- relayer.Log is gone. apart from another global var, imho this was
a too opinionated choice for a framework to build a custom relay upon.
this commit introduces a Logger interface which package users can implement
for zerolog to make it log like before. see Server.Log for details.
other notable changes: finally added a couple basic tests, for start up
and shutdown. doc comments now explain most of the essentials,
hopefully making it more approachable for newcomers and easier to understand
the relayer package.
the changes in handlers.go are minimal, although git diff goes crazy.
this is because most of the lines are simply shifted indentation back by one
due to go fmt.
before this commit:
func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request)
func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request)
after:
func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request)
func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2022-07-11 16:00:21 -03:00
|
|
|
}
|
start: introduce Server type and Shutdown (breaking change)
the main motivation for this change is to be able to run tests.
before this commit, Start, Router and Log operated on global variables,
making automated testing unreasonably hard.
this commit puts all that a server needs in a new Server type,
which also made it possible for a Server.Shutdown - see ShutdownAware
doc comments.
BREAKING CHANGES:
- Relay.OnInitialized takes one argument now, *relayer.Server.
- relayer.Router is now replaced by relayer.Server.Router().
package users can still hook into the router from OnInitialized
for custom HTTP routing.
- relayer.Log is gone. apart from another global var, imho this was
a too opinionated choice for a framework to build a custom relay upon.
this commit introduces a Logger interface which package users can implement
for zerolog to make it log like before. see Server.Log for details.
other notable changes: finally added a couple basic tests, for start up
and shutdown. doc comments now explain most of the essentials,
hopefully making it more approachable for newcomers and easier to understand
the relayer package.
the changes in handlers.go are minimal, although git diff goes crazy.
this is because most of the lines are simply shifted indentation back by one
due to go fmt.
before this commit:
func handleWebsocket(relay Relay) func(http.ResponseWriter, *http.Request)
func handleNIP11(relay Relay) func(http.ResponseWriter, *http.Request)
after:
func (s *Server) handleWebsocket(w http.ResponseWriter, r *http.Request)
func (s *Server) handleNIP11(w http.ResponseWriter, r *http.Request)
2022-12-24 22:21:26 +01:00
|
|
|
}()
|
|
|
|
}
|