mirror of
https://github.com/fiatjaf/khatru.git
synced 2026-04-08 06:26:52 +02:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3f73a9690a | ||
|
|
2a8b704299 | ||
|
|
746f030f46 | ||
|
|
81ad56e85c |
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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, ""
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
12
utils.go
12
utils.go
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user