fix closing of closed ws.Authed channel when client AUTHs twice.

This commit is contained in:
fiatjaf
2023-12-27 12:30:23 -03:00
parent 5b17786273
commit 21b08cb044
3 changed files with 10 additions and 2 deletions

View File

@@ -50,7 +50,6 @@ func (rl *Relay) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
conn: conn,
Request: r,
Challenge: hex.EncodeToString(challenge),
Authed: make(chan struct{}),
}
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)
if pubkey, ok := nip42.ValidateAuthEvent(&env.Event, ws.Challenge, wsBaseUrl); ok {
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})
} else {
ws.WriteJSON(nostr.OKEnvelope{EventID: env.Event.ID, OK: false, Reason: "error: failed to authenticate"})

View File

@@ -14,6 +14,9 @@ const (
func RequestAuth(ctx context.Context) {
ws := GetConnection(ctx)
ws.authLock.Lock()
ws.Authed = make(chan struct{})
ws.authLock.Unlock()
ws.WriteJSON(nostr.AuthEnvelope{Challenge: &ws.Challenge})
}

View File

@@ -18,6 +18,8 @@ type WebSocket struct {
Challenge string
AuthedPublicKey string
Authed chan struct{}
authLock sync.Mutex
}
func (ws *WebSocket) WriteJSON(any any) error {