mirror of
https://github.com/fiatjaf/khatru.git
synced 2025-09-17 16:53:32 +02:00
fix closing of closed ws.Authed channel when client AUTHs twice.
This commit is contained in:
@@ -50,7 +50,6 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
|
|||||||
conn: conn,
|
conn: conn,
|
||||||
Request: r,
|
Request: r,
|
||||||
Challenge: hex.EncodeToString(challenge),
|
Challenge: hex.EncodeToString(challenge),
|
||||||
Authed: make(chan struct{}),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(
|
ctx, cancel := context.WithCancel(
|
||||||
@@ -204,7 +203,11 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
|
|||||||
wsBaseUrl := strings.Replace(rl.ServiceURL, "http", "ws", 1)
|
wsBaseUrl := strings.Replace(rl.ServiceURL, "http", "ws", 1)
|
||||||
if pubkey, ok := nip42.ValidateAuthEvent(&env.Event, ws.Challenge, wsBaseUrl); ok {
|
if pubkey, ok := nip42.ValidateAuthEvent(&env.Event, ws.Challenge, wsBaseUrl); ok {
|
||||||
ws.AuthedPublicKey = pubkey
|
ws.AuthedPublicKey = pubkey
|
||||||
close(ws.Authed)
|
ws.authLock.Lock()
|
||||||
|
if ws.Authed != nil {
|
||||||
|
close(ws.Authed)
|
||||||
|
}
|
||||||
|
ws.authLock.Unlock()
|
||||||
ws.WriteJSON(nostr.OKEnvelope{EventID: env.Event.ID, OK: true})
|
ws.WriteJSON(nostr.OKEnvelope{EventID: env.Event.ID, OK: true})
|
||||||
} else {
|
} else {
|
||||||
ws.WriteJSON(nostr.OKEnvelope{EventID: env.Event.ID, OK: false, Reason: "error: failed to authenticate"})
|
ws.WriteJSON(nostr.OKEnvelope{EventID: env.Event.ID, OK: false, Reason: "error: failed to authenticate"})
|
||||||
|
3
utils.go
3
utils.go
@@ -14,6 +14,9 @@ const (
|
|||||||
|
|
||||||
func RequestAuth(ctx context.Context) {
|
func RequestAuth(ctx context.Context) {
|
||||||
ws := GetConnection(ctx)
|
ws := GetConnection(ctx)
|
||||||
|
ws.authLock.Lock()
|
||||||
|
ws.Authed = make(chan struct{})
|
||||||
|
ws.authLock.Unlock()
|
||||||
ws.WriteJSON(nostr.AuthEnvelope{Challenge: &ws.Challenge})
|
ws.WriteJSON(nostr.AuthEnvelope{Challenge: &ws.Challenge})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -18,6 +18,8 @@ type WebSocket struct {
|
|||||||
Challenge string
|
Challenge string
|
||||||
AuthedPublicKey string
|
AuthedPublicKey string
|
||||||
Authed chan struct{}
|
Authed chan struct{}
|
||||||
|
|
||||||
|
authLock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ws *WebSocket) WriteJSON(any any) error {
|
func (ws *WebSocket) WriteJSON(any any) error {
|
||||||
|
Reference in New Issue
Block a user