From 17593f74c6948bff758a067bf36ed6c1e96c8053 Mon Sep 17 00:00:00 2001 From: mountain Date: Tue, 19 Dec 2023 15:53:04 +0900 Subject: [PATCH] =?UTF-8?q?alias=EB=8C=80=EC=8B=A0=20nonce=EB=A5=BC=20?= =?UTF-8?q?=EB=B0=9B=EB=8A=94=20peer=20handler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wshandler/api_handler_test.go | 11 +++++++++ wshandler/wshandler_peer.go | 42 ++++++++++++++++++++--------------- 2 files changed, 35 insertions(+), 18 deletions(-) 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")) }