Compare commits

..

4 Commits

Author SHA1 Message Date
fiatjaf
3f73a9690a GetConnection() and GetAuthed() may return empty. 2024-07-03 22:16:44 -03:00
fiatjaf
2a8b704299 returning a nil chan from QueryEvents causes an immediate eose from that source. 2024-05-29 07:51:48 -03:00
fiatjaf
746f030f46 I'm a teapot -> Too many requests 2024-05-17 20:58:17 -03:00
fiatjaf
81ad56e85c simplify RestrictToSpecifiedKinds() 2024-05-12 20:54:53 -03:00
4 changed files with 14 additions and 24 deletions

View File

@@ -36,7 +36,7 @@ func (rl *Relay) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
for _, reject := range rl.RejectConnection {
if reject(r) {
w.WriteHeader(418) // I'm a teapot
w.WriteHeader(429) // Too many requests
return
}
}

View File

@@ -67,31 +67,10 @@ func PreventLargeTags(maxTagValueLen int) func(context.Context, *nostr.Event) (b
// RestrictToSpecifiedKinds returns a function that can be used as a RejectFilter that will reject
// any events with kinds different than the specified ones.
func RestrictToSpecifiedKinds(kinds ...uint16) func(context.Context, *nostr.Event) (bool, string) {
max := 0
min := 0
for _, kind := range kinds {
if int(kind) > max {
max = int(kind)
}
if int(kind) < min {
min = int(kind)
}
}
// sort the kinds in increasing order
slices.Sort(kinds)
return func(ctx context.Context, event *nostr.Event) (reject bool, msg string) {
// these are cheap and very questionable optimizations, but they exist for a reason:
// we would have to ensure that the kind number is within the bounds of a uint16 anyway
if event.Kind > max {
return true, fmt.Sprintf("event kind not allowed (it should be lower than %d)", max)
}
if event.Kind < min {
return true, fmt.Sprintf("event kind not allowed (it should be higher than %d)", min)
}
// hopefully this map of uint16s is very fast
if _, allowed := slices.BinarySearch(kinds, uint16(event.Kind)); allowed {
return false, ""
}

View File

@@ -41,6 +41,9 @@ func (rl *Relay) handleRequest(ctx context.Context, id string, eose *sync.WaitGr
ws.WriteJSON(nostr.NoticeEnvelope(err.Error()))
eose.Done()
continue
} else if ch == nil {
eose.Done()
continue
}
go func(ch chan *nostr.Event) {

View File

@@ -22,11 +22,19 @@ func RequestAuth(ctx context.Context) {
}
func GetConnection(ctx context.Context) *WebSocket {
return ctx.Value(wsKey).(*WebSocket)
wsi := ctx.Value(wsKey)
if wsi != nil {
return wsi.(*WebSocket)
}
return nil
}
func GetAuthed(ctx context.Context) string {
return GetConnection(ctx).AuthedPublicKey
conn := GetConnection(ctx)
if conn != nil {
return conn.AuthedPublicKey
}
return ""
}
func GetIP(ctx context.Context) string {