2024-09-17 01:01:42 +00:00
|
|
|
package khatru
|
|
|
|
|
|
|
|
import (
|
2024-10-29 00:11:29 +00:00
|
|
|
"context"
|
2024-09-17 01:01:42 +00:00
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/fasthttp/websocket"
|
2024-10-29 00:11:29 +00:00
|
|
|
"github.com/puzpuzpuz/xsync/v3"
|
2024-09-17 01:01:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type WebSocket struct {
|
|
|
|
conn *websocket.Conn
|
|
|
|
mutex sync.Mutex
|
|
|
|
|
|
|
|
// original request
|
|
|
|
Request *http.Request
|
|
|
|
|
2024-10-29 00:11:29 +00:00
|
|
|
// this Context will be canceled whenever the connection is closed from the client side or server-side.
|
|
|
|
Context context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
|
2024-09-17 01:01:42 +00:00
|
|
|
// nip42
|
|
|
|
Challenge string
|
|
|
|
AuthedPublicKey string
|
|
|
|
Authed chan struct{}
|
|
|
|
|
2024-10-29 00:11:29 +00:00
|
|
|
// nip77
|
|
|
|
negentropySessions *xsync.MapOf[string, *NegentropySession]
|
|
|
|
|
2024-09-17 01:01:42 +00:00
|
|
|
authLock sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *WebSocket) WriteJSON(any any) error {
|
|
|
|
ws.mutex.Lock()
|
|
|
|
defer ws.mutex.Unlock()
|
|
|
|
return ws.conn.WriteJSON(any)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ws *WebSocket) WriteMessage(t int, b []byte) error {
|
|
|
|
ws.mutex.Lock()
|
|
|
|
defer ws.mutex.Unlock()
|
|
|
|
return ws.conn.WriteMessage(t, b)
|
|
|
|
}
|