2023-08-10 14:32:11 -03:00
|
|
|
package khatru
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-12-09 08:19:37 -03:00
|
|
|
|
|
|
|
"github.com/nbd-wtf/go-nostr"
|
|
|
|
"github.com/sebest/xff"
|
2023-08-10 14:32:11 -03:00
|
|
|
)
|
|
|
|
|
2023-12-25 09:14:09 -03:00
|
|
|
const (
|
|
|
|
wsKey = iota
|
|
|
|
subscriptionIdKey
|
|
|
|
)
|
|
|
|
|
2023-12-25 09:30:13 -03:00
|
|
|
func RequestAuth(ctx context.Context) {
|
|
|
|
ws := GetConnection(ctx)
|
2023-12-27 12:30:23 -03:00
|
|
|
ws.authLock.Lock()
|
|
|
|
ws.Authed = make(chan struct{})
|
|
|
|
ws.authLock.Unlock()
|
2023-12-25 09:30:13 -03:00
|
|
|
ws.WriteJSON(nostr.AuthEnvelope{Challenge: &ws.Challenge})
|
|
|
|
}
|
|
|
|
|
2023-08-10 14:32:11 -03:00
|
|
|
func GetConnection(ctx context.Context) *WebSocket {
|
2023-12-25 09:14:09 -03:00
|
|
|
return ctx.Value(wsKey).(*WebSocket)
|
2023-08-10 14:32:11 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetAuthed(ctx context.Context) string {
|
2023-12-09 08:19:37 -03:00
|
|
|
return GetConnection(ctx).AuthedPublicKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetIP(ctx context.Context) string {
|
|
|
|
return xff.GetRemoteAddr(GetConnection(ctx).Request)
|
|
|
|
}
|
|
|
|
|
2023-12-25 09:14:09 -03:00
|
|
|
func GetSubscriptionID(ctx context.Context) string {
|
|
|
|
return ctx.Value(subscriptionIdKey).(string)
|
|
|
|
}
|
|
|
|
|
2023-12-09 08:19:37 -03:00
|
|
|
func GetOpenSubscriptions(ctx context.Context) []nostr.Filter {
|
2023-12-09 14:41:54 -03:00
|
|
|
if subs, ok := listeners.Load(GetConnection(ctx)); ok {
|
2023-12-09 08:19:37 -03:00
|
|
|
res := make([]nostr.Filter, 0, listeners.Size()*2)
|
2023-12-09 14:41:54 -03:00
|
|
|
subs.Range(func(_ string, sub *Listener) bool {
|
|
|
|
res = append(res, sub.filters...)
|
2023-12-09 08:19:37 -03:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
return res
|
2023-08-10 14:32:11 -03:00
|
|
|
}
|
2023-12-09 08:19:37 -03:00
|
|
|
return nil
|
2023-08-10 14:32:11 -03:00
|
|
|
}
|