From e122abaa4be391ef058583f3e669e5fc7015809b Mon Sep 17 00:00:00 2001 From: zhaocy Date: Tue, 7 Jun 2022 10:03:04 +0800 Subject: [PATCH] update --- modules/gateway/client_test.go | 59 ----------------------- modules/user/login_comp.go | 29 ++++++++++-- pb/errorcode.pb.go | 14 ++++-- pb/proto/errorcode.proto | 2 + pb/proto/user_msg.proto | 7 +-- pb/user_msg.pb.go | 85 +++++++++++++++------------------- utils/md5.go | 17 +++++++ 7 files changed, 97 insertions(+), 116 deletions(-) delete mode 100644 modules/gateway/client_test.go create mode 100644 utils/md5.go diff --git a/modules/gateway/client_test.go b/modules/gateway/client_test.go deleted file mode 100644 index 921e3590b..000000000 --- a/modules/gateway/client_test.go +++ /dev/null @@ -1,59 +0,0 @@ -package gateway_test - -import ( - "fmt" - "go_dreamfactory/pb" - "os" - "os/signal" - "syscall" - "testing" - - "github.com/gorilla/websocket" - "google.golang.org/protobuf/proto" -) - -func Test_WebSocket(t *testing.T) { - url := "ws://localhost:7891/gateway" //服务器地址 - ws, _, err := websocket.DefaultDialer.Dial(url, nil) - if err != nil { - fmt.Printf("err:%v", err) - return - } - - go func() { - var msg *pb.UserMessage = &pb.UserMessage{} - for { - _, data, err := ws.ReadMessage() - if err != nil { - fmt.Printf("err:%v\n", err) - } - if err = proto.Unmarshal(data, msg); err != nil { - fmt.Printf("err:%v\n", err) - } else { - fmt.Printf("ReadMessage msg:%v\n", msg) - } - } - }() - - loginreq := &pb.UserLoginReq{ - Name: "aaa", - } - logindata, _ := proto.Marshal(loginreq) - message := &pb.UserMessage{ - MainType: "login", - SubType: "login", - Data: logindata, - } - data, _ := proto.Marshal(message) - err = ws.WriteMessage(websocket.BinaryMessage, data) - if err != nil { - fmt.Printf("err:%v\n", err) - } - - sigterm := make(chan os.Signal, 1) - signal.Notify(sigterm, syscall.SIGINT, syscall.SIGTERM) - select { - case <-sigterm: - fmt.Printf("terminating: via signal\n") - } -} diff --git a/modules/user/login_comp.go b/modules/user/login_comp.go index 7cad5c595..247c3c62a 100644 --- a/modules/user/login_comp.go +++ b/modules/user/login_comp.go @@ -2,11 +2,14 @@ package user import ( "context" + "fmt" "go_dreamfactory/comm" "go_dreamfactory/modules" "go_dreamfactory/pb" "go_dreamfactory/sys/cache" "go_dreamfactory/sys/db" + "go_dreamfactory/utils" + "strings" "github.com/liwei1dao/lego/sys/log" "go.mongodb.org/mongo-driver/mongo" @@ -20,15 +23,33 @@ type LoginComp struct { func (this *LoginComp) Login(ctx context.Context, session comm.IUserSession, req *pb.UserLoginReq) error { log.Debugf("User - Login: session:%v rsp:%v", session.ToString(), req) - db_user, err := db.Defsys.User_FindUserByAccount(req.Name) + var code pb.ErrorCode = pb.ErrorCode_Success + rsp := &pb.UserLoginResp{} + secStr := req.Sec + + if !strings.HasPrefix(secStr, "CE:") || len(secStr) < 35 { + session.SendMsg("user", "login", pb.ErrorCode_SecKey, rsp) + return nil + } + + clientMd5Key := strings.TrimPrefix(secStr, "CE:") + rawmsg := secStr[35:] + serverMd5Key := utils.MD5Str(rawmsg) + s := fmt.Sprintf("%x", serverMd5Key) + if !strings.EqualFold(strings.ToLower(s), strings.ToLower(clientMd5Key)) { + session.SendMsg("user", "login", pb.ErrorCode_SecKeyInvalid, rsp) + return nil + } + account := "" + db_user, err := db.Defsys.User_FindUserByAccount(account) if err != nil { if err != mongo.ErrNoDocuments { return err } } - if db_user.UserId == 0 { - db_user.Account = req.Name + if db_user.UserId == "" { + db_user.Account = account err = db.Defsys.User_CreateUser(db_user) if err != nil { return err @@ -47,7 +68,7 @@ func (this *LoginComp) Login(ctx context.Context, session comm.IUserSession, req return err } - session.SendMsg("login", "login", pb.ErrorCode_Success, &pb.UserLoginResp{ + session.SendMsg("user", "login", code, &pb.UserLoginResp{ Data: &pb.Cache_UserData{ UserData: &pb.DB_UserData{ UserId: db_user.UserId, diff --git a/pb/errorcode.pb.go b/pb/errorcode.pb.go index cd54102a5..85cc5aebd 100644 --- a/pb/errorcode.pb.go +++ b/pb/errorcode.pb.go @@ -33,6 +33,8 @@ const ( ErrorCode_InsufficientPermissions ErrorCode = 16 //权限不足 ErrorCode_NoLogin ErrorCode = 17 //未登录 ErrorCode_UserSessionNobeing ErrorCode = 18 //用户不存在 + ErrorCode_SecKey ErrorCode = 19 //秘钥格式错误 + ErrorCode_SecKeyInvalid ErrorCode = 20 //秘钥无效 ) // Enum value maps for ErrorCode. @@ -48,6 +50,8 @@ var ( 16: "InsufficientPermissions", 17: "NoLogin", 18: "UserSessionNobeing", + 19: "SecKey", + 20: "SecKeyInvalid", } ErrorCode_value = map[string]int32{ "Success": 0, @@ -60,6 +64,8 @@ var ( "InsufficientPermissions": 16, "NoLogin": 17, "UserSessionNobeing": 18, + "SecKey": 19, + "SecKeyInvalid": 20, } ) @@ -94,7 +100,7 @@ var File_errorcode_proto protoreflect.FileDescriptor var file_errorcode_proto_rawDesc = []byte{ 0x0a, 0x0f, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x63, 0x6f, 0x64, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x2a, 0xd9, 0x01, 0x0a, 0x09, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x43, 0x6f, 0x64, 0x65, 0x12, + 0x6f, 0x2a, 0xf8, 0x01, 0x0a, 0x09, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x0b, 0x0a, 0x07, 0x53, 0x75, 0x63, 0x63, 0x65, 0x73, 0x73, 0x10, 0x00, 0x12, 0x11, 0x0a, 0x0d, 0x4e, 0x6f, 0x46, 0x69, 0x6e, 0x64, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x10, 0x0a, 0x12, 0x19, 0x0a, 0x15, 0x52, 0x70, 0x63, 0x46, 0x75, 0x6e, 0x63, 0x45, 0x78, 0x65, 0x63, 0x75, 0x74, @@ -107,8 +113,10 @@ var file_errorcode_proto_rawDesc = []byte{ 0x6e, 0x73, 0x75, 0x66, 0x66, 0x69, 0x63, 0x69, 0x65, 0x6e, 0x74, 0x50, 0x65, 0x72, 0x6d, 0x69, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x10, 0x10, 0x12, 0x0b, 0x0a, 0x07, 0x4e, 0x6f, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x10, 0x11, 0x12, 0x16, 0x0a, 0x12, 0x55, 0x73, 0x65, 0x72, 0x53, 0x65, 0x73, - 0x73, 0x69, 0x6f, 0x6e, 0x4e, 0x6f, 0x62, 0x65, 0x69, 0x6e, 0x67, 0x10, 0x12, 0x42, 0x06, 0x5a, - 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x73, 0x69, 0x6f, 0x6e, 0x4e, 0x6f, 0x62, 0x65, 0x69, 0x6e, 0x67, 0x10, 0x12, 0x12, 0x0a, 0x0a, + 0x06, 0x53, 0x65, 0x63, 0x4b, 0x65, 0x79, 0x10, 0x13, 0x12, 0x11, 0x0a, 0x0d, 0x53, 0x65, 0x63, + 0x4b, 0x65, 0x79, 0x49, 0x6e, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x10, 0x14, 0x42, 0x06, 0x5a, 0x04, + 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/pb/proto/errorcode.proto b/pb/proto/errorcode.proto index c2d283c43..9c3e94476 100644 --- a/pb/proto/errorcode.proto +++ b/pb/proto/errorcode.proto @@ -13,4 +13,6 @@ enum ErrorCode { InsufficientPermissions = 16; //权限不足 NoLogin = 17; //未登录 UserSessionNobeing = 18; //用户不存在 + SecKey = 19; //秘钥格式错误 + SecKeyInvalid = 20; //秘钥无效 } \ No newline at end of file diff --git a/pb/proto/user_msg.proto b/pb/proto/user_msg.proto index 5e21e3a72..061cbd035 100644 --- a/pb/proto/user_msg.proto +++ b/pb/proto/user_msg.proto @@ -3,13 +3,13 @@ option go_package = ".;pb"; import "errorcode.proto"; import "user_db.proto"; +//用户登录 message UserLoginReq { - string Name = 1; + string sec= 1;//密文 } message UserLoginResp { - ErrorCode Code = 1; - Cache_UserData data = 2; + Cache_UserData data = 1; } @@ -25,6 +25,7 @@ message UserLoadRsp { Cache_UserData data = 1; } +//创角 message UserCreateReq{ string NickName = 1;//昵称 int32 gender = 2; //性别 diff --git a/pb/user_msg.pb.go b/pb/user_msg.pb.go index d2e7e851e..47f8d000e 100644 --- a/pb/user_msg.pb.go +++ b/pb/user_msg.pb.go @@ -20,12 +20,13 @@ const ( _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) +//用户登录 type UserLoginReq struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Name string `protobuf:"bytes,1,opt,name=Name,proto3" json:"Name,omitempty"` + Sec string `protobuf:"bytes,1,opt,name=sec,proto3" json:"sec,omitempty"` //密文 } func (x *UserLoginReq) Reset() { @@ -60,9 +61,9 @@ func (*UserLoginReq) Descriptor() ([]byte, []int) { return file_user_msg_proto_rawDescGZIP(), []int{0} } -func (x *UserLoginReq) GetName() string { +func (x *UserLoginReq) GetSec() string { if x != nil { - return x.Name + return x.Sec } return "" } @@ -72,8 +73,7 @@ type UserLoginResp struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Code ErrorCode `protobuf:"varint,1,opt,name=Code,proto3,enum=ErrorCode" json:"Code,omitempty"` - Data *Cache_UserData `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"` + Data *Cache_UserData `protobuf:"bytes,1,opt,name=data,proto3" json:"data,omitempty"` } func (x *UserLoginResp) Reset() { @@ -108,13 +108,6 @@ func (*UserLoginResp) Descriptor() ([]byte, []int) { return file_user_msg_proto_rawDescGZIP(), []int{1} } -func (x *UserLoginResp) GetCode() ErrorCode { - if x != nil { - return x.Code - } - return ErrorCode_Success -} - func (x *UserLoginResp) GetData() *Cache_UserData { if x != nil { return x.Data @@ -263,6 +256,7 @@ func (x *UserLoadRsp) GetData() *Cache_UserData { return nil } +//创角 type UserCreateReq struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -362,30 +356,28 @@ var file_user_msg_proto_rawDesc = []byte{ 0x0a, 0x0e, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x6d, 0x73, 0x67, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x0f, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x63, 0x6f, 0x64, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x0d, 0x75, 0x73, 0x65, 0x72, 0x5f, 0x64, 0x62, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x22, 0x22, 0x0a, 0x0c, 0x55, 0x73, 0x65, 0x72, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, - 0x12, 0x12, 0x0a, 0x04, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, - 0x4e, 0x61, 0x6d, 0x65, 0x22, 0x54, 0x0a, 0x0d, 0x55, 0x73, 0x65, 0x72, 0x4c, 0x6f, 0x67, 0x69, - 0x6e, 0x52, 0x65, 0x73, 0x70, 0x12, 0x1e, 0x0a, 0x04, 0x43, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, - 0x01, 0x28, 0x0e, 0x32, 0x0a, 0x2e, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x43, 0x6f, 0x64, 0x65, 0x52, - 0x04, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x23, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x02, 0x20, - 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x43, 0x61, 0x63, 0x68, 0x65, 0x5f, 0x55, 0x73, 0x65, 0x72, - 0x44, 0x61, 0x74, 0x61, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x2b, 0x0a, 0x0f, 0x55, 0x73, - 0x65, 0x72, 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x71, 0x12, 0x18, 0x0a, - 0x07, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, - 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x31, 0x0a, 0x0f, 0x55, 0x73, 0x65, 0x72, 0x52, - 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x73, 0x70, 0x12, 0x1e, 0x0a, 0x04, 0x43, 0x6f, - 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x0a, 0x2e, 0x45, 0x72, 0x72, 0x6f, 0x72, - 0x43, 0x6f, 0x64, 0x65, 0x52, 0x04, 0x43, 0x6f, 0x64, 0x65, 0x22, 0x32, 0x0a, 0x0b, 0x55, 0x73, - 0x65, 0x72, 0x4c, 0x6f, 0x61, 0x64, 0x52, 0x73, 0x70, 0x12, 0x23, 0x0a, 0x04, 0x64, 0x61, 0x74, - 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x43, 0x61, 0x63, 0x68, 0x65, 0x5f, - 0x55, 0x73, 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x43, - 0x0a, 0x0d, 0x55, 0x73, 0x65, 0x72, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x12, - 0x1a, 0x0a, 0x08, 0x4e, 0x69, 0x63, 0x6b, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, - 0x09, 0x52, 0x08, 0x4e, 0x69, 0x63, 0x6b, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x67, - 0x65, 0x6e, 0x64, 0x65, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x67, 0x65, 0x6e, - 0x64, 0x65, 0x72, 0x22, 0x0f, 0x0a, 0x0d, 0x55, 0x73, 0x65, 0x72, 0x43, 0x72, 0x65, 0x61, 0x74, - 0x65, 0x52, 0x73, 0x70, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, - 0x6f, 0x74, 0x6f, 0x33, + 0x22, 0x20, 0x0a, 0x0c, 0x55, 0x73, 0x65, 0x72, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x71, + 0x12, 0x10, 0x0a, 0x03, 0x73, 0x65, 0x63, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x73, + 0x65, 0x63, 0x22, 0x34, 0x0a, 0x0d, 0x55, 0x73, 0x65, 0x72, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, + 0x65, 0x73, 0x70, 0x12, 0x23, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x0b, 0x32, 0x0f, 0x2e, 0x43, 0x61, 0x63, 0x68, 0x65, 0x5f, 0x55, 0x73, 0x65, 0x72, 0x44, 0x61, + 0x74, 0x61, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x2b, 0x0a, 0x0f, 0x55, 0x73, 0x65, 0x72, + 0x52, 0x65, 0x67, 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x65, 0x71, 0x12, 0x18, 0x0a, 0x07, 0x61, + 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x61, 0x63, + 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x31, 0x0a, 0x0f, 0x55, 0x73, 0x65, 0x72, 0x52, 0x65, 0x67, + 0x69, 0x73, 0x74, 0x65, 0x72, 0x52, 0x73, 0x70, 0x12, 0x1e, 0x0a, 0x04, 0x43, 0x6f, 0x64, 0x65, + 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x0a, 0x2e, 0x45, 0x72, 0x72, 0x6f, 0x72, 0x43, 0x6f, + 0x64, 0x65, 0x52, 0x04, 0x43, 0x6f, 0x64, 0x65, 0x22, 0x32, 0x0a, 0x0b, 0x55, 0x73, 0x65, 0x72, + 0x4c, 0x6f, 0x61, 0x64, 0x52, 0x73, 0x70, 0x12, 0x23, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x43, 0x61, 0x63, 0x68, 0x65, 0x5f, 0x55, 0x73, + 0x65, 0x72, 0x44, 0x61, 0x74, 0x61, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x22, 0x43, 0x0a, 0x0d, + 0x55, 0x73, 0x65, 0x72, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x52, 0x65, 0x71, 0x12, 0x1a, 0x0a, + 0x08, 0x4e, 0x69, 0x63, 0x6b, 0x4e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x08, 0x4e, 0x69, 0x63, 0x6b, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x67, 0x65, 0x6e, + 0x64, 0x65, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x67, 0x65, 0x6e, 0x64, 0x65, + 0x72, 0x22, 0x0f, 0x0a, 0x0d, 0x55, 0x73, 0x65, 0x72, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x52, + 0x73, 0x70, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x3b, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x33, } var ( @@ -409,19 +401,18 @@ var file_user_msg_proto_goTypes = []interface{}{ (*UserLoadRsp)(nil), // 4: UserLoadRsp (*UserCreateReq)(nil), // 5: UserCreateReq (*UserCreateRsp)(nil), // 6: UserCreateRsp - (ErrorCode)(0), // 7: ErrorCode - (*Cache_UserData)(nil), // 8: Cache_UserData + (*Cache_UserData)(nil), // 7: Cache_UserData + (ErrorCode)(0), // 8: ErrorCode } var file_user_msg_proto_depIdxs = []int32{ - 7, // 0: UserLoginResp.Code:type_name -> ErrorCode - 8, // 1: UserLoginResp.data:type_name -> Cache_UserData - 7, // 2: UserRegisterRsp.Code:type_name -> ErrorCode - 8, // 3: UserLoadRsp.data:type_name -> Cache_UserData - 4, // [4:4] is the sub-list for method output_type - 4, // [4:4] is the sub-list for method input_type - 4, // [4:4] is the sub-list for extension type_name - 4, // [4:4] is the sub-list for extension extendee - 0, // [0:4] is the sub-list for field type_name + 7, // 0: UserLoginResp.data:type_name -> Cache_UserData + 8, // 1: UserRegisterRsp.Code:type_name -> ErrorCode + 7, // 2: UserLoadRsp.data:type_name -> Cache_UserData + 3, // [3:3] is the sub-list for method output_type + 3, // [3:3] is the sub-list for method input_type + 3, // [3:3] is the sub-list for extension type_name + 3, // [3:3] is the sub-list for extension extendee + 0, // [0:3] is the sub-list for field type_name } func init() { file_user_msg_proto_init() } diff --git a/utils/md5.go b/utils/md5.go new file mode 100644 index 000000000..c5649d59a --- /dev/null +++ b/utils/md5.go @@ -0,0 +1,17 @@ +package utils + +import ( + "crypto/md5" + "encoding/hex" +) + +func MD5Str(s string) string { + return MD5Bytes([]byte(s)) +} + +func MD5Bytes(s []byte) string { + md5Ctx := md5.New() + md5Ctx.Write(s) + cipherStr := md5Ctx.Sum(nil) + return hex.EncodeToString(cipherStr) +}