From fdc670696b77a7841ab4fa2dff31dd293b4e2a39 Mon Sep 17 00:00:00 2001 From: W192547975 Date: Sat, 18 May 2024 15:49:25 +0800 Subject: [PATCH 1/3] PushRsp Debug Update pushhandler.go --- pushhandler.go | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/pushhandler.go b/pushhandler.go index 4e4a4ad..9e0ed56 100644 --- a/pushhandler.go +++ b/pushhandler.go @@ -37,8 +37,10 @@ func (h *pushHandler) handleMessage(ctx *msgContext) error { } gLog.Printf(LvDEBUG, "%d push to %d", pushHead.From, pushHead.To) if !isSupportMsg(head.SubType) { - rsp := PushRsp{Error: 1, Detail: "push denied: unSupported msg "} - fromSess.write(head.MainType, MsgPushRsp, rsp) + rsp, _ := json.Marshal(PushRsp{Error: 1, Detail: "push denied: unSupported msg "}) + rsp = append(ctx.msg[:openP2PHeaderSize+PushHeaderSize], rsp...) + _ = copy(rsp, encodeHeader(head.MainType, MsgPushRsp, uint32(len(rsp)-openP2PHeaderSize))) + fromSess.writeBuff(rsp) return errors.New("push denied") } gWSSessionMgr.allSessionsMtx.Lock() @@ -46,8 +48,10 @@ func (h *pushHandler) handleMessage(ctx *msgContext) error { gWSSessionMgr.allSessionsMtx.Unlock() if !ok { gLog.Printf(LvERROR, "%d push to %d error: peer offline", pushHead.From, pushHead.To) - rsp := PushRsp{Error: 1, Detail: "peer offline"} - fromSess.write(head.MainType, MsgPushRsp, rsp) + rsp, _ := json.Marshal(PushRsp{Error: 1, Detail: "peer offline"}) + rsp = append(ctx.msg[:openP2PHeaderSize+PushHeaderSize], rsp...) + _ = copy(rsp, encodeHeader(head.MainType, MsgPushRsp, uint32(len(rsp)-openP2PHeaderSize))) + fromSess.writeBuff(rsp) return errors.New("peer offline") } if head.SubType == MsgPushConnectReq { @@ -62,8 +66,10 @@ func (h *pushHandler) handleMessage(ctx *msgContext) error { t := totp.TOTP{Step: totp.RelayTOTPStep} if !(t.Verify(req.Token, toSess.token, time.Now().Unix()) || (toSess.token == req.FromToken)) { // (toSess.token == req.FromToken) is deprecated gLog.Printf(LvERROR, "%s --- %s MsgPushConnectReq push denied", req.From, toSess.node) - rsp := PushRsp{Error: 1, Detail: "MsgPushConnectReq push denied"} - fromSess.write(head.MainType, MsgPushRsp, rsp) + rsp, _ := json.Marshal(PushRsp{Error: 1, Detail: "MsgPushConnectReq push denied"}) + rsp = append(ctx.msg[:openP2PHeaderSize+PushHeaderSize], rsp...) + _ = copy(rsp, encodeHeader(head.MainType, MsgPushRsp, uint32(len(rsp)-openP2PHeaderSize))) + fromSess.writeBuff(rsp) return errors.New("push denied") } // cache push permission 60s @@ -87,14 +93,18 @@ func (h *pushHandler) handleMessage(ctx *msgContext) error { } if !granted { gLog.Printf(LvERROR, "%d --- %d push denied", pushHead.From, pushHead.To) - rsp := PushRsp{Error: 1, Detail: "push denied"} - fromSess.write(head.MainType, MsgPushRsp, rsp) + rsp, _ := json.Marshal(PushRsp{Error: 1, Detail: "push denied"}) + rsp = append(ctx.msg[:openP2PHeaderSize+PushHeaderSize], rsp...) + _ = copy(rsp, encodeHeader(head.MainType, MsgPushRsp, uint32(len(rsp)-openP2PHeaderSize))) + fromSess.writeBuff(rsp) return errors.New("push denied") } } toSess.writeBuff(ctx.msg) - rsp := PushRsp{Error: 0, Detail: fmt.Sprintf("push msgType %d,%d to %d ok", head.MainType, head.SubType, pushHead.From)} - fromSess.write(head.MainType, MsgPushRsp, rsp) + rsp, _ := json.Marshal(PushRsp{Error: 0, Detail: fmt.Sprintf("push msgType %d,%d to %d ok", head.MainType, head.SubType, pushHead.From)}) + rsp = append(ctx.msg[:openP2PHeaderSize+PushHeaderSize], rsp...) + _ = copy(rsp, encodeHeader(head.MainType, MsgPushRsp, uint32(len(rsp)-openP2PHeaderSize))) + fromSess.writeBuff(rsp) return nil } From 7c3fcca68ab0781562b2ab86c4b6fc0a4a9404a4 Mon Sep 17 00:00:00 2001 From: W192547975 Date: Fri, 24 May 2024 18:57:58 +0800 Subject: [PATCH 2/3] Revert "PushRsp Debug" This reverts commit fdc670696b77a7841ab4fa2dff31dd293b4e2a39. --- pushhandler.go | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/pushhandler.go b/pushhandler.go index 9e0ed56..4e4a4ad 100644 --- a/pushhandler.go +++ b/pushhandler.go @@ -37,10 +37,8 @@ func (h *pushHandler) handleMessage(ctx *msgContext) error { } gLog.Printf(LvDEBUG, "%d push to %d", pushHead.From, pushHead.To) if !isSupportMsg(head.SubType) { - rsp, _ := json.Marshal(PushRsp{Error: 1, Detail: "push denied: unSupported msg "}) - rsp = append(ctx.msg[:openP2PHeaderSize+PushHeaderSize], rsp...) - _ = copy(rsp, encodeHeader(head.MainType, MsgPushRsp, uint32(len(rsp)-openP2PHeaderSize))) - fromSess.writeBuff(rsp) + rsp := PushRsp{Error: 1, Detail: "push denied: unSupported msg "} + fromSess.write(head.MainType, MsgPushRsp, rsp) return errors.New("push denied") } gWSSessionMgr.allSessionsMtx.Lock() @@ -48,10 +46,8 @@ func (h *pushHandler) handleMessage(ctx *msgContext) error { gWSSessionMgr.allSessionsMtx.Unlock() if !ok { gLog.Printf(LvERROR, "%d push to %d error: peer offline", pushHead.From, pushHead.To) - rsp, _ := json.Marshal(PushRsp{Error: 1, Detail: "peer offline"}) - rsp = append(ctx.msg[:openP2PHeaderSize+PushHeaderSize], rsp...) - _ = copy(rsp, encodeHeader(head.MainType, MsgPushRsp, uint32(len(rsp)-openP2PHeaderSize))) - fromSess.writeBuff(rsp) + rsp := PushRsp{Error: 1, Detail: "peer offline"} + fromSess.write(head.MainType, MsgPushRsp, rsp) return errors.New("peer offline") } if head.SubType == MsgPushConnectReq { @@ -66,10 +62,8 @@ func (h *pushHandler) handleMessage(ctx *msgContext) error { t := totp.TOTP{Step: totp.RelayTOTPStep} if !(t.Verify(req.Token, toSess.token, time.Now().Unix()) || (toSess.token == req.FromToken)) { // (toSess.token == req.FromToken) is deprecated gLog.Printf(LvERROR, "%s --- %s MsgPushConnectReq push denied", req.From, toSess.node) - rsp, _ := json.Marshal(PushRsp{Error: 1, Detail: "MsgPushConnectReq push denied"}) - rsp = append(ctx.msg[:openP2PHeaderSize+PushHeaderSize], rsp...) - _ = copy(rsp, encodeHeader(head.MainType, MsgPushRsp, uint32(len(rsp)-openP2PHeaderSize))) - fromSess.writeBuff(rsp) + rsp := PushRsp{Error: 1, Detail: "MsgPushConnectReq push denied"} + fromSess.write(head.MainType, MsgPushRsp, rsp) return errors.New("push denied") } // cache push permission 60s @@ -93,18 +87,14 @@ func (h *pushHandler) handleMessage(ctx *msgContext) error { } if !granted { gLog.Printf(LvERROR, "%d --- %d push denied", pushHead.From, pushHead.To) - rsp, _ := json.Marshal(PushRsp{Error: 1, Detail: "push denied"}) - rsp = append(ctx.msg[:openP2PHeaderSize+PushHeaderSize], rsp...) - _ = copy(rsp, encodeHeader(head.MainType, MsgPushRsp, uint32(len(rsp)-openP2PHeaderSize))) - fromSess.writeBuff(rsp) + rsp := PushRsp{Error: 1, Detail: "push denied"} + fromSess.write(head.MainType, MsgPushRsp, rsp) return errors.New("push denied") } } toSess.writeBuff(ctx.msg) - rsp, _ := json.Marshal(PushRsp{Error: 0, Detail: fmt.Sprintf("push msgType %d,%d to %d ok", head.MainType, head.SubType, pushHead.From)}) - rsp = append(ctx.msg[:openP2PHeaderSize+PushHeaderSize], rsp...) - _ = copy(rsp, encodeHeader(head.MainType, MsgPushRsp, uint32(len(rsp)-openP2PHeaderSize))) - fromSess.writeBuff(rsp) + rsp := PushRsp{Error: 0, Detail: fmt.Sprintf("push msgType %d,%d to %d ok", head.MainType, head.SubType, pushHead.From)} + fromSess.write(head.MainType, MsgPushRsp, rsp) return nil } From c0ce3301bc764c64afaf964fefe75054df75aa20 Mon Sep 17 00:00:00 2001 From: W192547975 Date: Sat, 25 May 2024 15:02:59 +0800 Subject: [PATCH 3/3] Push Connect Update Update pushhandler.go --- pushhandler.go | 61 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/pushhandler.go b/pushhandler.go index 4e4a4ad..4b3310c 100644 --- a/pushhandler.go +++ b/pushhandler.go @@ -7,8 +7,6 @@ import ( "errors" "fmt" "time" - - "github.com/openp2p-cn/totp" ) type pushHandler struct { @@ -50,28 +48,41 @@ func (h *pushHandler) handleMessage(ctx *msgContext) error { fromSess.write(head.MainType, MsgPushRsp, rsp) return errors.New("peer offline") } - if head.SubType == MsgPushConnectReq { - // verify user/password - req := PushConnectReq{} - err := json.Unmarshal(ctx.msg[openP2PHeaderSize+PushHeaderSize:], &req) - if err != nil { - gLog.Printf(LvERROR, "wrong MsgPushConnectReq:%s", err) - return err - } - gLog.Printf(LvINFO, "%s is connecting to %s...", req.From, toSess.node) - t := totp.TOTP{Step: totp.RelayTOTPStep} - if !(t.Verify(req.Token, toSess.token, time.Now().Unix()) || (toSess.token == req.FromToken)) { // (toSess.token == req.FromToken) is deprecated - gLog.Printf(LvERROR, "%s --- %s MsgPushConnectReq push denied", req.From, toSess.node) - rsp := PushRsp{Error: 1, Detail: "MsgPushConnectReq push denied"} - fromSess.write(head.MainType, MsgPushRsp, rsp) - return errors.New("push denied") + if isByPassMsg(head.SubType) { + switch head.SubType { + case MsgPushConnectReq: + gLog.Printf(LvINFO, "%s is connecting to %s...", fromSess.node, toSess.node) + /* + // verify user/password + t := totp.TOTP{Step: totp.RelayTOTPStep} + if !(t.Verify(req.Token, toSess.token, time.Now().Unix()) || (toSess.token == req.FromToken)) { // (toSess.token == req.FromToken) is deprecated + gLog.Printf(LvERROR, "%s --- %s MsgPushConnectReq push denied", req.From, toSess.node) + rsp := PushRsp{Error: 1, Detail: "MsgPushConnectReq push denied"} + fromSess.write(head.MainType, MsgPushRsp, rsp) + return errors.New("push denied") + } + */ + case MsgPushConnectRsp: + // check rsp.Error for permission + var rsp PushConnectRsp + err := json.Unmarshal(ctx.msg[openP2PHeaderSize+PushHeaderSize:], &rsp) + if err != nil { + gLog.Printf(LvERROR, "wrong MsgPushConnectRsp:%s", err) + return err + } + if rsp.Error&0xFF == 0 { // allow more success code + // cache push permission 60s + gWSSessionMgr.pushPermission.Store(pushHead.To, &pushNodeInfo{pushHead.From, time.Now().Add(time.Minute)}) + gWSSessionMgr.pushPermission.Store(pushHead.From, &pushNodeInfo{pushHead.To, time.Now().Add(time.Minute)}) + // TODO: clear cache + } else { + gLog.Printf(LvWARN, "%s --- %s connect error %d: %s", rsp.To, rsp.From, rsp.Error, rsp.Detail) + } + case MsgPushAPPKey: + gLog.Println(LvDEBUG, "sync app key") + default: + gLog.Println(LvWARN, "unknown by pass msg ", head.SubType) } - // cache push permission 60s - gWSSessionMgr.pushPermission.Store(pushHead.From, &pushNodeInfo{pushHead.To, time.Now().Add(time.Minute)}) - gWSSessionMgr.pushPermission.Store(pushHead.To, &pushNodeInfo{pushHead.From, time.Now().Add(time.Minute)}) - // TODO: clear cache - } else if isByPassMsg(head.SubType) { - gLog.Println(LvDEBUG, "sync app key") } else { // verify push permission // verify from as key @@ -113,5 +124,7 @@ func isSupportMsg(msgType uint16) bool { } func isByPassMsg(msgType uint16) bool { - return msgType == MsgPushAPPKey + return msgType == MsgPushConnectReq || + msgType == MsgPushConnectRsp || + msgType == MsgPushAPPKey }