所有协议加上秘钥认证

This commit is contained in:
zhaocy 2022-06-17 18:28:35 +08:00
parent cab6a2d07b
commit 182e3743ad
4 changed files with 31 additions and 35 deletions

View File

@ -85,7 +85,7 @@ func (r *Robot) handleMsg(msg *pb.UserMessage) {
//在这里添加玩家成功登录以后的测试方法 //在这里添加玩家成功登录以后的测试方法
func (r *Robot) onUserLoaded() { func (r *Robot) onUserLoaded() {
//user //user
// r.CreateUser("乐谷616") r.CreateUser("乐谷6171")
//friend //friend
// r.FriendApply("1_62aa8f30d25fb8c1a7d90b50") // r.FriendApply("1_62aa8f30d25fb8c1a7d90b50")
@ -104,6 +104,7 @@ func (r *Robot) onUserLoaded() {
} }
func (r *Robot) SendToClient(msg *pb.UserMessage, rsp proto.Message) error { func (r *Robot) SendToClient(msg *pb.UserMessage, rsp proto.Message) error {
msg.Sec = r.BuildSecStr()
if comm.ProtoMarshal(rsp, msg) { if comm.ProtoMarshal(rsp, msg) {
data, _ := proto.Marshal(msg) data, _ := proto.Marshal(msg)
return r.ws.WriteMessage(websocket.BinaryMessage, data) return r.ws.WriteMessage(websocket.BinaryMessage, data)

View File

@ -122,44 +122,43 @@ locp:
log.Debugf("agent:%s uId:%s writeLoop end!", this.sessionId, this.uId) log.Debugf("agent:%s uId:%s writeLoop end!", this.sessionId, this.uId)
} }
//安全认证 //安全认证 所有协议
func (this *Agent) secAuth(msg *pb.UserMessage) error { 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) { //验证失败 if !utils.ValidSecretKey(msg.Sec) { //验证失败
return fmt.Errorf("key invalid") return fmt.Errorf("key invalid")
} }
//解码
return decodeUserData(msg) return decodeUserData(msg)
} }
//解码 //解码
func decodeUserData(msg *pb.UserMessage) error { func decodeUserData(msg *pb.UserMessage) error {
base64Str := msg.Sec //只有login的时候才需要解码
dec, err := base64.StdEncoding.DecodeString(base64Str[35:]) if msg.MainType == "user" && msg.SubType == "login" {
if err != nil { base64Str := msg.Sec
log.Errorf("base64 decode err %v", err) dec, err := base64.StdEncoding.DecodeString(base64Str[35:])
return nil 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 return nil
} }

View File

@ -40,7 +40,6 @@ func main() {
pack.NewModule(), pack.NewModule(),
mail.NewModule(), mail.NewModule(),
friend.NewModule(), friend.NewModule(),
// dbservice.NewModule(),
) )
} }

View File

@ -29,9 +29,6 @@ func ValidSecretKey(secStr string) bool {
clientMd5Key := secStr[3:35] clientMd5Key := secStr[3:35]
rawmsg := secStr[35:] rawmsg := secStr[35:]
log.Debugf("data base: %s", rawmsg) log.Debugf("data base: %s", rawmsg)
serverMd5Key := MD5Str(rawmsg) serverMd5Key := MD5Str(rawmsg) //这里可以再加上客户端和服务端的秘钥再MD5
if !strings.EqualFold(strings.ToLower(serverMd5Key), strings.ToLower(clientMd5Key)) { return strings.EqualFold(strings.ToLower(serverMd5Key), strings.ToLower(clientMd5Key))
return false
}
return true
} }