diff --git a/cmd/robot/robot.go b/cmd/robot/robot.go index ccf1a0dce..b185c9ee9 100644 --- a/cmd/robot/robot.go +++ b/cmd/robot/robot.go @@ -85,7 +85,7 @@ func (r *Robot) handleMsg(msg *pb.UserMessage) { //在这里添加玩家成功登录以后的测试方法 func (r *Robot) onUserLoaded() { //user - // r.CreateUser("乐谷616") + r.CreateUser("乐谷6171") //friend // r.FriendApply("1_62aa8f30d25fb8c1a7d90b50") @@ -104,6 +104,7 @@ func (r *Robot) onUserLoaded() { } func (r *Robot) SendToClient(msg *pb.UserMessage, rsp proto.Message) error { + msg.Sec = r.BuildSecStr() if comm.ProtoMarshal(rsp, msg) { data, _ := proto.Marshal(msg) return r.ws.WriteMessage(websocket.BinaryMessage, data) diff --git a/modules/gateway/agent.go b/modules/gateway/agent.go index 02a74c489..7e1620da9 100644 --- a/modules/gateway/agent.go +++ b/modules/gateway/agent.go @@ -122,44 +122,43 @@ locp: log.Debugf("agent:%s uId:%s writeLoop end!", this.sessionId, this.uId) } -//安全认证 +//安全认证 所有协议 func (this *Agent) secAuth(msg *pb.UserMessage) error { - req := &pb.UserLoginReq{} - if !comm.ProtoUnmarshal(msg, req) { - return fmt.Errorf("proto unmarshal err") - } if !utils.ValidSecretKey(msg.Sec) { //验证失败 return fmt.Errorf("key invalid") } - //解码 return decodeUserData(msg) } //解码 func decodeUserData(msg *pb.UserMessage) error { - base64Str := msg.Sec - dec, err := base64.StdEncoding.DecodeString(base64Str[35:]) - if err != nil { - log.Errorf("base64 decode err %v", err) - return nil + //只有login的时候才需要解码 + if msg.MainType == "user" && msg.SubType == "login" { + base64Str := msg.Sec + dec, err := base64.StdEncoding.DecodeString(base64Str[35:]) + if err != nil { + log.Errorf("base64 decode err %v", err) + return nil + } + now := time.Now().Unix() + jsonRet := gjson.Parse(string(dec)) + serverId := jsonRet.Get("serverId").Int() + timestamp := jsonRet.Get("timestamp").Int() + if now-time.Unix(timestamp, 0).Unix() > 100 { + return nil + } + account := jsonRet.Get("account").String() + req := &pb.UserLoginReq{ + Account: account, + Sid: int32(serverId), + } + ad, err := anypb.New(req) + if err != nil { + return err + } + msg.Data = ad } - now := time.Now().Unix() - jsonRet := gjson.Parse(string(dec)) - serverId := jsonRet.Get("serverId").Int() - timestamp := jsonRet.Get("timestamp").Int() - if now-time.Unix(timestamp, 0).Unix() > 100 { - return nil - } - account := jsonRet.Get("account").String() - req := &pb.UserLoginReq{ - Account: account, - Sid: int32(serverId), - } - ad, err := anypb.New(req) - if err != nil { - return err - } - msg.Data = ad + return nil } diff --git a/services/worker/main.go b/services/worker/main.go index 731eaa149..3458ede0f 100644 --- a/services/worker/main.go +++ b/services/worker/main.go @@ -40,7 +40,6 @@ func main() { pack.NewModule(), mail.NewModule(), friend.NewModule(), - // dbservice.NewModule(), ) } diff --git a/utils/base64.go b/utils/base64.go index 9557d5bce..f67b82908 100644 --- a/utils/base64.go +++ b/utils/base64.go @@ -29,9 +29,6 @@ func ValidSecretKey(secStr string) bool { clientMd5Key := secStr[3:35] rawmsg := secStr[35:] log.Debugf("data base: %s", rawmsg) - serverMd5Key := MD5Str(rawmsg) - if !strings.EqualFold(strings.ToLower(serverMd5Key), strings.ToLower(clientMd5Key)) { - return false - } - return true + serverMd5Key := MD5Str(rawmsg) //这里可以再加上客户端和服务端的秘钥再MD5 + return strings.EqualFold(strings.ToLower(serverMd5Key), strings.ToLower(clientMd5Key)) }