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