Moved function to generate sessions Ids from websocket to Utils
This commit is contained in:
@@ -6,7 +6,9 @@ package utils
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base32"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
@@ -18,3 +20,14 @@ func HashMAC(message, key []byte) string {
|
||||
|
||||
return hex.EncodeToString(expected)
|
||||
}
|
||||
|
||||
// Generate a new random Hash
|
||||
func RandomHash() string {
|
||||
b := make([]byte, 25)
|
||||
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
panic("websockets: Could not generate a random session ID")
|
||||
}
|
||||
|
||||
return base32.StdEncoding.EncodeToString(b)
|
||||
}
|
||||
|
||||
+1
-14
@@ -5,8 +5,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base32"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -225,7 +223,7 @@ func Websocket(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
sessionID := randomSessionID()
|
||||
sessionID := utils.RandomHash()
|
||||
|
||||
if err := onOpen(conn, w, r, sessionID, app); err != nil {
|
||||
emitWSError(err, conn)
|
||||
@@ -235,17 +233,6 @@ func Websocket(w http.ResponseWriter, r *http.Request) {
|
||||
onMessage(conn, w, r, sessionID, app)
|
||||
}
|
||||
|
||||
// Generate a new random sessionID
|
||||
func randomSessionID() string {
|
||||
b := make([]byte, 25)
|
||||
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
panic("websockets: Could not generate a random session ID")
|
||||
}
|
||||
|
||||
return base32.StdEncoding.EncodeToString(b)
|
||||
}
|
||||
|
||||
// Emit an Websocket ErrorEvent
|
||||
func emitWSError(err WebsocketError, conn *websocket.Conn) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user