diff --git a/wshandler/api_handler_test.go b/wshandler/api_handler_test.go index 33d4b77..18f0378 100644 --- a/wshandler/api_handler_test.go +++ b/wshandler/api_handler_test.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "math" "reflect" "testing" @@ -105,3 +106,13 @@ func TestPeerApiBroker(t *testing.T) { func1args, _ = json.Marshal([]any{float64(111.1), []int{99, 98}}) ws.Call(peer, "test.ApiFunc3", bytes.NewBuffer(func1args)) } + +func TestOverflow(t *testing.T) { + var x uint32 + x = math.MaxUint32 + + var y uint32 + y = x + 1 + + fmt.Printf("%x, %x", x, y) +} diff --git a/wshandler/wshandler_peer.go b/wshandler/wshandler_peer.go index d71aa86..80a49b6 100644 --- a/wshandler/wshandler_peer.go +++ b/wshandler/wshandler_peer.go @@ -1,11 +1,12 @@ package wshandler import ( - "encoding/base64" "encoding/hex" "io" + "math/rand" "net/http" "strings" + "time" "go.mongodb.org/mongo-driver/bson/primitive" "repositories.action2quare.com/ayo/gocommon" @@ -37,7 +38,7 @@ func (ws *WebsocketPeerHandler) RegisterHandlers(serveMux *http.ServeMux, prefix return nil } -func (ws *WebsocketPeerHandler) upgrade_core(conn *websocket.Conn, accid primitive.ObjectID, alias string) { +func (ws *WebsocketPeerHandler) upgrade_core(conn *websocket.Conn, accid primitive.ObjectID, nonce uint32) { go func(c *websocket.Conn, accid primitive.ObjectID) { peer := ws.CreatePeer(accid) ws.ClientConnected(peer, c) @@ -109,15 +110,18 @@ func (ws *WebsocketPeerHandler) upgrade_nosession(w http.ResponseWriter, r *http return } - var alias string - if v := r.Header.Get("AS-X-ALIAS"); len(v) > 0 { - vt, _ := base64.StdEncoding.DecodeString(v) - alias = string(vt) - } else { - alias = accid.Hex() - } + // var alias string + // if v := r.Header.Get("AS-X-ALIAS"); len(v) > 0 { + // vt, _ := base64.StdEncoding.DecodeString(v) + // alias = string(vt) + // } else { + // alias = accid.Hex() + // } - ws.upgrade_core(conn, accid, alias) + nonce := rand.New(rand.NewSource(time.Now().UnixNano())).Uint32() + ws.upgrade_core(conn, accid, nonce) + + w.Write([]byte("asfadsf")) } func (ws *WebsocketPeerHandler) upgrade(w http.ResponseWriter, r *http.Request) { @@ -147,13 +151,15 @@ func (ws *WebsocketPeerHandler) upgrade(w http.ResponseWriter, r *http.Request) return } - var alias string - if v := r.Header.Get("AS-X-ALIAS"); len(v) > 0 { - vt, _ := base64.StdEncoding.DecodeString(v) - alias = string(vt) - } else { - alias = authinfo.Account.Hex() - } + // var alias string + // if v := r.Header.Get("AS-X-ALIAS"); len(v) > 0 { + // vt, _ := base64.StdEncoding.DecodeString(v) + // alias = string(vt) + // } else { + // alias = authinfo.Account.Hex() + // } + nonce := rand.New(rand.NewSource(time.Now().UnixNano())).Uint32() + ws.upgrade_core(conn, authinfo.Account, nonce) - ws.upgrade_core(conn, authinfo.Account, alias) + w.Write([]byte("asfadsf")) }