2021-12-25 21:22:40 -03:00
|
|
|
package relayer
|
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
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2022-07-11 16:21:47 -03:00
|
|
|
"fmt"
|
2021-01-13 23:46:06 -03:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
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"
|
|
|
|
"github.com/nbd-wtf/go-nostr/nip11"
|
2023-01-15 22:38:24 -03:00
|
|
|
"github.com/nbd-wtf/go-nostr/nip42"
|
|
|
|
"golang.org/x/exp/slices"
|
2021-01-13 23:46:06 -03:00
|
|
|
)
|
|
|
|
|
2023-01-15 23:33:53 -05:00
|
|
|
// TODO: consider moving these to Server as config params
|
2021-01-13 23:46:06 -03:00
|
|
|
const (
|
|
|
|
// Time allowed to write a message to the peer.
|
|
|
|
writeWait = 10 * time.Second
|
|
|
|
|
|
|
|
// Time allowed to read the next pong message from the peer.
|
|
|
|
pongWait = 60 * time.Second
|
|
|
|
|
|
|
|
// Send pings to peer with this period. Must be less than pongWait.
|
|
|
|
pingPeriod = pongWait / 2
|
|
|
|
|
|
|
|
// Maximum message size allowed from peer.
|
|
|
|
maxMessageSize = 512000
|
|
|
|
)
|
|
|
|
|
2023-01-15 23:33:53 -05:00
|
|
|
// TODO: consider moving these to Server as config params
|
2021-01-13 23:46:06 -03:00
|
|
|
var upgrader = websocket.Upgrader{
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
WriteBufferSize: 1024,
|
|
|
|
CheckOrigin: func(r *http.Request) bool { return true },
|
|
|
|
}
|
|
|
|
|
2023-05-01 19:21:09 -03:00
|
|
|
func (s *Server) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
store := s.relay.Storage(ctx)
|
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
|
|
|
advancedDeleter, _ := store.(AdvancedDeleter)
|
2022-07-24 16:55:59 -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
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
s.Log.Errorf("failed to upgrade websocket: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.clientsMu.Lock()
|
|
|
|
defer s.clientsMu.Unlock()
|
|
|
|
s.clients[conn] = struct{}{}
|
|
|
|
ticker := time.NewTicker(pingPeriod)
|
|
|
|
|
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{
|
|
|
|
conn: conn,
|
|
|
|
challenge: hex.EncodeToString(challenge),
|
|
|
|
}
|
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
|
|
|
|
|
|
|
// reader
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
ticker.Stop()
|
|
|
|
s.clientsMu.Lock()
|
|
|
|
if _, ok := s.clients[conn]; ok {
|
2021-12-25 21:22:40 -03:00
|
|
|
conn.Close()
|
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
|
|
|
delete(s.clients, conn)
|
2022-12-29 15:02:59 +01:00
|
|
|
removeListener(ws)
|
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
|
|
|
}
|
|
|
|
s.clientsMu.Unlock()
|
|
|
|
}()
|
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
|
|
|
conn.SetReadLimit(maxMessageSize)
|
|
|
|
conn.SetReadDeadline(time.Now().Add(pongWait))
|
|
|
|
conn.SetPongHandler(func(string) error {
|
2021-01-13 23:46:06 -03:00
|
|
|
conn.SetReadDeadline(time.Now().Add(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-01-16 01:24:35 -05:00
|
|
|
// NIP-42 auth challenge
|
2023-01-15 22:38:24 -03:00
|
|
|
if _, ok := s.relay.(Auther); ok {
|
2023-06-16 17:38:32 -03:00
|
|
|
ws.WriteJSON(nostr.AuthEnvelope{Challenge: &ws.challenge})
|
2023-01-15 22:38:24 -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 {
|
|
|
|
typ, message, err := conn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
if websocket.IsUnexpectedCloseError(
|
|
|
|
err,
|
|
|
|
websocket.CloseGoingAway, // 1001
|
|
|
|
websocket.CloseNoStatusReceived, // 1005
|
|
|
|
websocket.CloseAbnormalClosure, // 1006
|
|
|
|
) {
|
|
|
|
s.Log.Warningf("unexpected close error from %s: %v", r.Header.Get("X-Forwarded-For"), err)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
go func(message []byte) {
|
2023-05-03 09:40:25 -07:00
|
|
|
ctx = 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
|
|
|
var notice string
|
|
|
|
defer func() {
|
|
|
|
if notice != "" {
|
2023-06-16 17:38:32 -03:00
|
|
|
ws.WriteJSON(nostr.NoticeEnvelope(notice))
|
2021-02-14 21:08:02 -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
|
|
|
}()
|
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
|
|
|
var request []json.RawMessage
|
|
|
|
if err := json.Unmarshal(message, &request); err != nil {
|
|
|
|
// stop silently
|
|
|
|
return
|
2021-02-17 16:59:56 -03: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 len(request) < 2 {
|
|
|
|
notice = "request has less than 2 parameters"
|
|
|
|
return
|
|
|
|
}
|
2021-02-14 21:08:02 -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
|
|
|
var typ string
|
|
|
|
json.Unmarshal(request[0], &typ)
|
2021-12-27 11:14:29 -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
|
|
|
switch typ {
|
|
|
|
case "EVENT":
|
|
|
|
// it's a new event
|
|
|
|
var evt nostr.Event
|
|
|
|
if err := json.Unmarshal(request[1], &evt); err != nil {
|
|
|
|
notice = "failed to decode event: " + err.Error()
|
2021-02-14 21:08:02 -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
|
|
|
// check serialization
|
|
|
|
serialized := evt.Serialize()
|
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
|
|
|
// assign ID
|
|
|
|
hash := sha256.Sum256(serialized)
|
|
|
|
evt.ID = hex.EncodeToString(hash[:])
|
2021-02-14 21:08:02 -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
|
|
|
// check signature (requires the ID to be set)
|
|
|
|
if ok, err := evt.CheckSignature(); err != nil {
|
2023-06-16 17:38:32 -03:00
|
|
|
reason := "error: failed to verify signature"
|
|
|
|
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: false, Reason: &reason})
|
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-06-16 17:38:32 -03:00
|
|
|
reason := "invalid: signature is invalid"
|
|
|
|
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: false, Reason: &reason})
|
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
|
|
|
|
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 evt.Kind == 5 {
|
|
|
|
// event deletion -- nip09
|
|
|
|
for _, tag := range evt.Tags {
|
|
|
|
if len(tag) >= 2 && tag[0] == "e" {
|
|
|
|
if advancedDeleter != nil {
|
2023-05-01 19:21:09 -03:00
|
|
|
advancedDeleter.BeforeDelete(ctx, tag[1], evt.PubKey)
|
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-02-14 21:08:02 -03:00
|
|
|
|
2023-05-01 19:21:09 -03:00
|
|
|
if err := store.DeleteEvent(ctx, tag[1], evt.PubKey); err != nil {
|
2023-06-16 17:38:32 -03:00
|
|
|
reason := fmt.Sprintf("error: %s", err.Error())
|
|
|
|
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: false, Reason: &reason})
|
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-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 advancedDeleter != nil {
|
|
|
|
advancedDeleter.AfterDelete(tag[1], evt.PubKey)
|
2022-07-11 16:21:47 -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
|
|
|
return
|
|
|
|
}
|
2022-07-11 16:21:47 -03:00
|
|
|
|
2023-06-12 08:19:07 -03:00
|
|
|
ok, message := AddEvent(ctx, s.relay, &evt)
|
2023-06-16 17:38:32 -03:00
|
|
|
var reason *string
|
|
|
|
if message != "" {
|
|
|
|
reason = &message
|
|
|
|
}
|
|
|
|
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: ok, Reason: reason})
|
2023-05-17 19:54:56 +09:00
|
|
|
case "COUNT":
|
|
|
|
counter, ok := store.(EventCounter)
|
|
|
|
if !ok {
|
|
|
|
notice = "restricted: this relay does not support NIP-45"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var id string
|
|
|
|
json.Unmarshal(request[1], &id)
|
|
|
|
if id == "" {
|
|
|
|
notice = "COUNT has no <id>"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
total := int64(0)
|
|
|
|
filters := make(nostr.Filters, len(request)-2)
|
|
|
|
for i, filterReq := range request[2:] {
|
|
|
|
if err := json.Unmarshal(filterReq, &filters[i]); err != nil {
|
|
|
|
notice = "failed to decode filter"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
filter := &filters[i]
|
|
|
|
|
|
|
|
// prevent kind-4 events from being returned to unauthed users,
|
|
|
|
// only when authentication is a thing
|
|
|
|
if _, ok := s.relay.(Auther); ok {
|
|
|
|
if slices.Contains(filter.Kinds, 4) {
|
|
|
|
senders := filter.Authors
|
|
|
|
receivers, _ := filter.Tags["p"]
|
|
|
|
switch {
|
|
|
|
case ws.authed == "":
|
|
|
|
// not authenticated
|
|
|
|
notice = "restricted: this relay does not serve kind-4 to unauthenticated users, does your client implement NIP-42?"
|
|
|
|
return
|
|
|
|
case len(senders) == 1 && len(receivers) < 2 && (senders[0] == ws.authed):
|
|
|
|
// allowed filter: ws.authed is sole sender (filter specifies one or all receivers)
|
|
|
|
case len(receivers) == 1 && len(senders) < 2 && (receivers[0] == ws.authed):
|
|
|
|
// allowed filter: ws.authed is sole receiver (filter specifies one or all senders)
|
|
|
|
default:
|
|
|
|
// restricted filter: do not return any events,
|
|
|
|
// even if other elements in filters array were not restricted).
|
|
|
|
// client should know better.
|
|
|
|
notice = "restricted: authenticated user does not have authorization for requested filters."
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err := counter.CountEvents(ctx, filter)
|
|
|
|
if err != nil {
|
|
|
|
s.Log.Errorf("store: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
total += count
|
|
|
|
}
|
|
|
|
|
|
|
|
ws.WriteJSON([]interface{}{"COUNT", id, map[string]int64{"count": total}})
|
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 "REQ":
|
|
|
|
var id string
|
|
|
|
json.Unmarshal(request[1], &id)
|
|
|
|
if id == "" {
|
|
|
|
notice = "REQ has no <id>"
|
|
|
|
return
|
|
|
|
}
|
2022-07-24 16:55:59 -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
|
|
|
filters := make(nostr.Filters, len(request)-2)
|
|
|
|
for i, filterReq := range request[2:] {
|
|
|
|
if err := json.Unmarshal(
|
|
|
|
filterReq,
|
|
|
|
&filters[i],
|
|
|
|
); err != nil {
|
|
|
|
notice = "failed to decode filter"
|
2021-12-25 21:22:40 -03:00
|
|
|
return
|
|
|
|
}
|
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
|
|
|
filter := &filters[i]
|
2022-07-24 19:58:34 -03:00
|
|
|
|
2023-01-15 22:38:24 -03:00
|
|
|
// prevent kind-4 events from being returned to unauthed users,
|
|
|
|
// only when authentication is a thing
|
|
|
|
if _, ok := s.relay.(Auther); ok {
|
|
|
|
if slices.Contains(filter.Kinds, 4) {
|
|
|
|
senders := filter.Authors
|
2023-01-16 21:29:35 -05:00
|
|
|
receivers, _ := filter.Tags["p"]
|
|
|
|
switch {
|
|
|
|
case ws.authed == "":
|
|
|
|
// not authenticated
|
|
|
|
notice = "restricted: this relay does not serve kind-4 to unauthenticated users, does your client implement NIP-42?"
|
2023-01-15 22:38:24 -03:00
|
|
|
return
|
2023-01-16 21:29:35 -05:00
|
|
|
case len(senders) == 1 && len(receivers) < 2 && (senders[0] == ws.authed):
|
|
|
|
// allowed filter: ws.authed is sole sender (filter specifies one or all receivers)
|
|
|
|
case len(receivers) == 1 && len(senders) < 2 && (receivers[0] == ws.authed):
|
|
|
|
// allowed filter: ws.authed is sole receiver (filter specifies one or all senders)
|
|
|
|
default:
|
|
|
|
// restricted filter: do not return any events,
|
|
|
|
// even if other elements in filters array were not restricted).
|
|
|
|
// client should know better.
|
|
|
|
notice = "restricted: authenticated user does not have authorization for requested filters."
|
2023-01-15 22:38:24 -03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-01 19:21:09 -03:00
|
|
|
events, err := store.QueryEvents(ctx, filter)
|
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 {
|
|
|
|
s.Log.Errorf("store: %v", err)
|
|
|
|
continue
|
2021-12-25 21:22:40 -03:00
|
|
|
}
|
2021-01-13 23:46:06 -03:00
|
|
|
|
2023-05-01 19:21:09 -03:00
|
|
|
// ensures the client won't be bombarded with events in case Storage doesn't do limits right
|
|
|
|
if filter.Limit == 0 {
|
|
|
|
filter.Limit = 9999999999
|
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-05-01 19:21:09 -03:00
|
|
|
i := 0
|
|
|
|
for event := range events {
|
2023-06-16 17:38:32 -03:00
|
|
|
ws.WriteJSON(nostr.EventEnvelope{SubscriptionID: &id, Event: *event})
|
2023-05-01 19:21:09 -03:00
|
|
|
i++
|
|
|
|
if i > filter.Limit {
|
|
|
|
break
|
|
|
|
}
|
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-05-01 19:40:16 -03:00
|
|
|
|
|
|
|
// exhaust the channel (in case we broke out of it early) so it is closed by the storage
|
|
|
|
for range events {
|
|
|
|
}
|
2021-12-25 21:22:40 -03:00
|
|
|
}
|
2023-05-01 19:21:09 -03:00
|
|
|
|
2023-06-16 17:38:32 -03:00
|
|
|
ws.WriteJSON(nostr.EOSEEnvelope(id))
|
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
|
|
|
setListener(id, ws, filters)
|
|
|
|
case "CLOSE":
|
|
|
|
var id string
|
|
|
|
json.Unmarshal(request[1], &id)
|
|
|
|
if id == "" {
|
|
|
|
notice = "CLOSE has no <id>"
|
2021-12-25 21:22:40 -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
|
|
|
|
2022-12-29 15:02:59 +01:00
|
|
|
removeListenerId(ws, id)
|
2023-01-15 22:38:24 -03:00
|
|
|
case "AUTH":
|
|
|
|
if auther, ok := s.relay.(Auther); ok {
|
|
|
|
var evt nostr.Event
|
|
|
|
if err := json.Unmarshal(request[1], &evt); err != nil {
|
|
|
|
notice = "failed to decode auth event: " + err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if pubkey, ok := nip42.ValidateAuthEvent(&evt, ws.challenge, auther.ServiceURL()); ok {
|
|
|
|
ws.authed = pubkey
|
2023-06-26 20:05:06 -03:00
|
|
|
ctx = context.WithValue(ctx, AUTH_CONTEXT_KEY, pubkey)
|
2023-06-16 17:38:32 -03:00
|
|
|
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: true})
|
2023-01-16 01:24:35 -05:00
|
|
|
} else {
|
2023-06-16 17:38:32 -03:00
|
|
|
reason := "error: failed to authenticate"
|
|
|
|
ws.WriteJSON(nostr.OKEnvelope{EventID: evt.ID, OK: false, Reason: &reason})
|
2023-01-15 22:38:24 -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
|
|
|
default:
|
|
|
|
if cwh, ok := s.relay.(CustomWebSocketHandler); ok {
|
|
|
|
cwh.HandleUnknownType(ws, typ, request)
|
|
|
|
} else {
|
|
|
|
notice = "unknown message type " + typ
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// writer
|
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
ticker.Stop()
|
|
|
|
conn.Close()
|
2021-12-25 21:22:40 -03:00
|
|
|
}()
|
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 {
|
|
|
|
case <-ticker.C:
|
|
|
|
err := ws.WriteMessage(websocket.PingMessage, nil)
|
|
|
|
if err != nil {
|
|
|
|
s.Log.Errorf("error writing ping: %v; closing websocket", err)
|
|
|
|
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
|
|
|
}()
|
|
|
|
}
|
2022-07-11 16:00:21 -03:00
|
|
|
|
2023-05-01 19:21:09 -03:00
|
|
|
func (s *Server) HandleNIP11(w http.ResponseWriter, r *http.Request) {
|
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
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
2023-03-31 10:03:35 -04:00
|
|
|
supportedNIPs := []int{9, 11, 12, 15, 16, 20, 33}
|
2023-01-15 22:38:24 -03:00
|
|
|
if _, ok := s.relay.(Auther); ok {
|
|
|
|
supportedNIPs = append(supportedNIPs, 42)
|
|
|
|
}
|
2023-05-17 19:54:56 +09:00
|
|
|
if storage, ok := s.relay.(Storage); ok && storage != nil {
|
|
|
|
if _, ok = storage.(EventCounter); ok {
|
|
|
|
supportedNIPs = append(supportedNIPs, 45)
|
|
|
|
}
|
|
|
|
}
|
2023-01-15 22:38:24 -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
|
|
|
info := nip11.RelayInformationDocument{
|
|
|
|
Name: s.relay.Name(),
|
|
|
|
Description: "relay powered by the relayer framework",
|
|
|
|
PubKey: "~",
|
|
|
|
Contact: "~",
|
2023-01-15 22:38:24 -03:00
|
|
|
SupportedNIPs: supportedNIPs,
|
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
|
|
|
Software: "https://github.com/fiatjaf/relayer",
|
|
|
|
Version: "~",
|
|
|
|
}
|
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
|
|
|
if ifmer, ok := s.relay.(Informationer); ok {
|
|
|
|
info = ifmer.GetNIP11InformationDocument()
|
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
|
|
|
|
|
|
|
json.NewEncoder(w).Encode(info)
|
2022-07-11 16:00:21 -03:00
|
|
|
}
|