所有协议加上秘钥认证
This commit is contained in:
parent
cab6a2d07b
commit
182e3743ad
@ -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)
|
||||
|
@ -122,21 +122,18 @@ 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 {
|
||||
//只有login的时候才需要解码
|
||||
if msg.MainType == "user" && msg.SubType == "login" {
|
||||
base64Str := msg.Sec
|
||||
dec, err := base64.StdEncoding.DecodeString(base64Str[35:])
|
||||
if err != nil {
|
||||
@ -160,6 +157,8 @@ func decodeUserData(msg *pb.UserMessage) error {
|
||||
return err
|
||||
}
|
||||
msg.Data = ad
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -40,7 +40,6 @@ func main() {
|
||||
pack.NewModule(),
|
||||
mail.NewModule(),
|
||||
friend.NewModule(),
|
||||
// dbservice.NewModule(),
|
||||
)
|
||||
|
||||
}
|
||||
|
@ -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))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user