From da6c3763554e19d8350a78f41d0681f3418d3234 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Wed, 24 Apr 2024 16:38:32 +0800 Subject: [PATCH 1/5] feat: chat admin config --- config/share.yml | 4 +--- internal/api/admin/start.go | 2 +- internal/api/chat/start.go | 2 +- internal/rpc/admin/start.go | 14 +++++++------- pkg/common/config/config.go | 10 ++-------- 5 files changed, 12 insertions(+), 20 deletions(-) diff --git a/config/share.yml b/config/share.yml index 76e1b442..2eed7bd7 100644 --- a/config/share.yml +++ b/config/share.yml @@ -9,8 +9,6 @@ openIM: adminUserID: imAdmin chatAdmin: - - adminID: chatAdmin - imUserID: imAdmin - nickname: chatAdmin + - "chatAdmin" #proxyHeader: "X-Forwarded-For" \ No newline at end of file diff --git a/internal/api/admin/start.go b/internal/api/admin/start.go index ce30a64b..aef4602c 100644 --- a/internal/api/admin/start.go +++ b/internal/api/admin/start.go @@ -55,7 +55,7 @@ func Start(ctx context.Context, index int, config *Config) error { base := util.Api{ ImUserID: config.Share.OpenIM.AdminUserID, ProxyHeader: config.Share.ProxyHeader, - ChatAdminUserID: config.Share.ChatAdmin[0].AdminID, + ChatAdminUserID: config.Share.ChatAdmin[0], } adminApi := New(chatClient, adminClient, im, &base) mwApi := chatmw.New(adminClient) diff --git a/internal/api/chat/start.go b/internal/api/chat/start.go index d5291c6a..30ea80ff 100644 --- a/internal/api/chat/start.go +++ b/internal/api/chat/start.go @@ -54,7 +54,7 @@ func Start(ctx context.Context, index int, config *Config) error { base := util.Api{ ImUserID: config.Share.OpenIM.AdminUserID, ProxyHeader: config.Share.ProxyHeader, - ChatAdminUserID: config.Share.ChatAdmin[0].AdminID, + ChatAdminUserID: config.Share.ChatAdmin[0], } adminApi := New(chatClient, adminClient, im, &base) mwApi := chatmw.New(adminClient) diff --git a/internal/rpc/admin/start.go b/internal/rpc/admin/start.go index 36ac9801..8d46bf6f 100644 --- a/internal/rpc/admin/start.go +++ b/internal/rpc/admin/start.go @@ -59,7 +59,7 @@ func Start(ctx context.Context, config *Config, client discovery.SvcDiscoveryReg Expires: time.Duration(config.RpcConfig.TokenPolicy.Expire) * time.Hour * 24, Secret: config.RpcConfig.Secret, } - if err := srv.initAdmin(ctx, config.Share.ChatAdmin); err != nil { + if err := srv.initAdmin(ctx, config.Share.ChatAdmin, config.Share.OpenIM.AdminUserID); err != nil { return err } pbadmin.RegisterAdminServer(server, &srv) @@ -72,17 +72,17 @@ type adminServer struct { Token *tokenverify.Token } -func (o *adminServer) initAdmin(ctx context.Context, users []config.AdminUser) error { - for _, user := range users { - if _, err := o.Database.GetAdmin(ctx, user.AdminID); err == nil { +func (o *adminServer) initAdmin(ctx context.Context, admins []string, imUserID string) error { + for _, account := range admins { + if _, err := o.Database.GetAdmin(ctx, account); err == nil { continue } else if !dbutil.IsDBNotFound(err) { return err } - sum := md5.Sum([]byte(user.AdminID)) + sum := md5.Sum([]byte(account)) a := admin.Admin{ - Account: user.AdminID, - UserID: user.IMUserID, + Account: account, + UserID: imUserID, Password: hex.EncodeToString(sum[:]), Level: constant.DefaultAdminLevel, CreateTime: time.Now(), diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index f08734c3..1412f0e4 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -21,14 +21,8 @@ type Share struct { Secret string `mapstructure:"secret"` AdminUserID string `mapstructure:"adminUserID"` } `mapstructure:"openIM"` - ChatAdmin []AdminUser `mapstructure:"chatAdmin"` - ProxyHeader string `mapstructure:"proxyHeader"` -} - -type AdminUser struct { - AdminID string `mapstructure:"adminID"` - IMUserID string `mapstructure:"imUserID"` - Nickname string `mapstructure:"nickname"` + ChatAdmin []string `mapstructure:"chatAdmin"` + ProxyHeader string `mapstructure:"proxyHeader"` } type RpcRegisterName struct { From 02533a37d16f2ac36b2fbc238baf3c541f176811 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Wed, 24 Apr 2024 17:45:43 +0800 Subject: [PATCH 2/5] feat: chat admin config --- pkg/protocol/admin/admin.pb.go | 4 ---- pkg/protocol/admin/admin.proto | 20 -------------------- 2 files changed, 24 deletions(-) diff --git a/pkg/protocol/admin/admin.pb.go b/pkg/protocol/admin/admin.pb.go index b2b7b48a..749a6524 100644 --- a/pkg/protocol/admin/admin.pb.go +++ b/pkg/protocol/admin/admin.pb.go @@ -41,7 +41,6 @@ const ( _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) ) -// 登录 type LoginReq struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -3288,7 +3287,6 @@ func (*DelIPForbiddenResp) Descriptor() ([]byte, []int) { return file_admin_admin_proto_rawDescGZIP(), []int{62} } -// ################### 用户限制 ################### type CheckRegisterForbiddenReq struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -3467,7 +3465,6 @@ func (*CheckLoginForbiddenResp) Descriptor() ([]byte, []int) { return file_admin_admin_proto_rawDescGZIP(), []int{66} } -// ################### 注销 ################### type CancellationUserReq struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -3561,7 +3558,6 @@ func (*CancellationUserResp) Descriptor() ([]byte, []int) { return file_admin_admin_proto_rawDescGZIP(), []int{68} } -// ################### 封号、解封 ################### type BlockUserReq struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache diff --git a/pkg/protocol/admin/admin.proto b/pkg/protocol/admin/admin.proto index 87c4cbfd..4185cf4e 100644 --- a/pkg/protocol/admin/admin.proto +++ b/pkg/protocol/admin/admin.proto @@ -19,7 +19,6 @@ import "sdkws/sdkws.proto"; import "common/common.proto"; option go_package = "github.com/openimsdk/chat/pkg/protocol/admin"; -//登录 message LoginReq { string account = 1; string password = 2; @@ -103,8 +102,6 @@ message GetAdminInfoResp { int64 createTime = 8; } -// ################### 默认好友 ################### - message AddDefaultFriendReq { repeated string userIDs = 1; } @@ -139,9 +136,6 @@ message SearchDefaultFriendResp { repeated DefaultFriendAttribute users = 2; } -// ################### 默认群组 ################### - - message AddDefaultGroupReq { repeated string groupIDs = 1; } @@ -176,8 +170,6 @@ message SearchDefaultGroupResp { repeated string groupIDs = 2; } -// ################### 邀请码 ################### - message AddInvitationCodeReq { repeated string codes = 1; } @@ -237,8 +229,6 @@ message SearchInvitationCodeResp { repeated InvitationRegister list = 2; } -// ################### 用户登录ip限制 ################### - message SearchUserIPLimitLoginReq { string keyword = 1; openim.sdkws.RequestPagination pagination = 2; @@ -275,9 +265,6 @@ message DelUserIPLimitLoginReq { message DelUserIPLimitLoginResp { } - -// ################### 用户IP限制 ################### - message IPForbidden { string ip = 1; bool limitRegister = 2; @@ -312,7 +299,6 @@ message DelIPForbiddenReq { } message DelIPForbiddenResp {} -// ################### 用户限制 ################### message CheckRegisterForbiddenReq { string ip = 1; } @@ -328,7 +314,6 @@ message CheckLoginForbiddenReq { message CheckLoginForbiddenResp { } -// ################### 注销 ################### message CancellationUserReq{ string userID = 1; string reason = 2; @@ -336,7 +321,6 @@ message CancellationUserReq{ message CancellationUserResp {} -// ################### 封号、解封 ################### message BlockUserReq{ string userID = 1; string reason = 2; @@ -392,8 +376,6 @@ message FindUserBlockInfoResp{ repeated BlockInfo blocks = 2; } -// ################### TOKEN ################### - message CreateTokenReq{ string userID = 2; int32 userType = 3; @@ -413,8 +395,6 @@ message ParseTokenResp{ int64 expireTimeSeconds = 4; } -// ################### 小程序 ################### - message AddAppletReq { string id = 1; string name = 2; From 693fad44972f8ec6f8248d5cc0b089d8eb5311ca Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Sun, 28 Apr 2024 16:49:00 +0800 Subject: [PATCH 3/5] fix: chat args check --- internal/rpc/chat/login.go | 2 +- pkg/protocol/chat/chat.go | 163 ++++++++++++++++++++----------------- 2 files changed, 89 insertions(+), 76 deletions(-) diff --git a/internal/rpc/chat/login.go b/internal/rpc/chat/login.go index 86f6bcc3..cc89c233 100644 --- a/internal/rpc/chat/login.go +++ b/internal/rpc/chat/login.go @@ -313,7 +313,7 @@ func (o *chatSvr) RegisterUser(ctx context.Context, req *chat.RegisterUserReq) ( } var registerType int32 if req.User.PhoneNumber != "" { - if req.User.AreaCode[0] != '+' { + if !strings.HasPrefix(req.User.AreaCode, "+") { req.User.AreaCode = "+" + req.User.AreaCode } if _, err := strconv.ParseUint(req.User.AreaCode[1:], 10, 64); err != nil { diff --git a/pkg/protocol/chat/chat.go b/pkg/protocol/chat/chat.go index 12b76c87..dde34766 100644 --- a/pkg/protocol/chat/chat.go +++ b/pkg/protocol/chat/chat.go @@ -17,6 +17,7 @@ package chat import ( "regexp" "strconv" + "strings" "github.com/openimsdk/chat/pkg/common/constant" pconstant "github.com/openimsdk/protocol/constant" @@ -36,7 +37,7 @@ func (x *UpdateUserInfoReq) Check() error { } func (x *FindUserPublicInfoReq) Check() error { - if x.UserIDs == nil { + if len(x.UserIDs) == 0 { return errs.ErrArgs.WrapMsg("userIDs is empty") } return nil @@ -56,7 +57,7 @@ func (x *SearchUserPublicInfoReq) Check() error { } func (x *FindUserFullInfoReq) Check() error { - if x.UserIDs == nil { + if len(x.UserIDs) == 0 { return errs.ErrArgs.WrapMsg("userIDs is empty") } return nil @@ -66,53 +67,52 @@ func (x *SendVerifyCodeReq) Check() error { if x.UsedFor < constant.VerificationCodeForRegister || x.UsedFor > constant.VerificationCodeForLogin { return errs.ErrArgs.WrapMsg("usedFor flied is empty") } - if x.Email == "" { - if x.AreaCode == "" { - return errs.ErrArgs.WrapMsg("AreaCode is empty") - } else if err := AreaCodeCheck(x.AreaCode); err != nil { + if x.PhoneNumber == "" && x.Email == "" { + return errs.ErrArgs.WrapMsg("PhoneNumber and Email is empty") + } + if x.PhoneNumber != "" && x.Email != "" { + return errs.ErrArgs.WrapMsg("PhoneNumber and Email is not empty") + } + if x.Email != "" { + if err := EmailCheck(x.Email); err != nil { return err } - if x.PhoneNumber == "" { - return errs.ErrArgs.WrapMsg("PhoneNumber is empty") - } else if err := PhoneNumberCheck(x.PhoneNumber); err != nil { + } + if x.PhoneNumber != "" { + if err := AreaCodeCheck(x.AreaCode); err != nil { return err } - } else { - if err := EmailCheck(x.Email); err != nil { + if err := PhoneNumberCheck(x.PhoneNumber); err != nil { return err } } - return nil } func (x *VerifyCodeReq) Check() error { - if x.Email == "" { - if x.AreaCode == "" { - return errs.ErrArgs.WrapMsg("AreaCode is empty") - } else if err := AreaCodeCheck(x.AreaCode); err != nil { + if x.PhoneNumber == "" && x.Email == "" { + return errs.ErrArgs.WrapMsg("PhoneNumber and Email is empty") + } + if x.PhoneNumber != "" && x.Email != "" { + return errs.ErrArgs.WrapMsg("PhoneNumber and Email is not empty") + } + if x.Email != "" { + if err := EmailCheck(x.Email); err != nil { return err } - if x.PhoneNumber == "" { - return errs.ErrArgs.WrapMsg("PhoneNumber is empty") - } else if err := PhoneNumberCheck(x.PhoneNumber); err != nil { + } + if x.PhoneNumber != "" { + if err := AreaCodeCheck(x.AreaCode); err != nil { return err } - } else { - if err := EmailCheck(x.Email); err != nil { + if err := PhoneNumberCheck(x.PhoneNumber); err != nil { return err } } - if x.VerifyCode == "" { - return errs.ErrArgs.WrapMsg("VerifyCode is empty") - } return nil } func (x *RegisterUserReq) Check() error { - //if x.VerifyCode == "" { - // return errs.ErrArgs.WrapMsg("VerifyCode is empty") - //} if x.User.Nickname == "" { return errs.ErrArgs.WrapMsg("Nickname is nil") } @@ -122,19 +122,22 @@ func (x *RegisterUserReq) Check() error { if x.User == nil { return errs.ErrArgs.WrapMsg("user is empty") } - if x.User.Email == "" { - if x.User.AreaCode == "" { - return errs.ErrArgs.WrapMsg("AreaCode is empty") - } else if err := AreaCodeCheck(x.User.AreaCode); err != nil { + if x.User.PhoneNumber == "" && x.User.Email == "" { + return errs.ErrArgs.WrapMsg("PhoneNumber and Email is empty") + } + if x.User.PhoneNumber != "" && x.User.Email != "" { + return errs.ErrArgs.WrapMsg("PhoneNumber and Email is not empty") + } + if x.User.Email != "" { + if err := EmailCheck(x.User.Email); err != nil { return err } - if x.User.PhoneNumber == "" { - return errs.ErrArgs.WrapMsg("PhoneNumber is empty") - } else if err := PhoneNumberCheck(x.User.PhoneNumber); err != nil { + } + if x.User.PhoneNumber != "" { + if err := AreaCodeCheck(x.User.AreaCode); err != nil { return err } - } else { - if err := EmailCheck(x.User.Email); err != nil { + if err := PhoneNumberCheck(x.User.PhoneNumber); err != nil { return err } } @@ -145,19 +148,16 @@ func (x *LoginReq) Check() error { if x.Platform < pconstant.IOSPlatformID || x.Platform > pconstant.AdminPlatformID { return errs.ErrArgs.WrapMsg("platform is invalid") } - if x.Email == "" { - if x.AreaCode == "" { - return errs.ErrArgs.WrapMsg("AreaCode is empty") - } else if err := AreaCodeCheck(x.AreaCode); err != nil { + if x.Email != "" { + if err := EmailCheck(x.Email); err != nil { return err } - if x.PhoneNumber == "" { - return errs.ErrArgs.WrapMsg("PhoneNumber is empty") - } else if err := PhoneNumberCheck(x.PhoneNumber); err != nil { + } + if x.PhoneNumber != "" { + if err := AreaCodeCheck(x.AreaCode); err != nil { return err } - } else { - if err := EmailCheck(x.Email); err != nil { + if err := PhoneNumberCheck(x.PhoneNumber); err != nil { return err } } @@ -168,25 +168,25 @@ func (x *ResetPasswordReq) Check() error { if x.Password == "" { return errs.ErrArgs.WrapMsg("password is empty") } - if x.Email == "" { - if x.AreaCode == "" { - return errs.ErrArgs.WrapMsg("AreaCode is empty") - } else if err := AreaCodeCheck(x.AreaCode); err != nil { + if x.PhoneNumber == "" && x.Email == "" { + return errs.ErrArgs.WrapMsg("PhoneNumber and Email is empty") + } + if x.PhoneNumber != "" && x.Email != "" { + return errs.ErrArgs.WrapMsg("PhoneNumber and Email is not empty") + } + if x.Email != "" { + if err := EmailCheck(x.Email); err != nil { return err } - if x.PhoneNumber == "" { - return errs.ErrArgs.WrapMsg("PhoneNumber is empty") - } else if err := PhoneNumberCheck(x.PhoneNumber); err != nil { + } + if x.PhoneNumber != "" { + if err := AreaCodeCheck(x.AreaCode); err != nil { return err } - } else { - if err := EmailCheck(x.Email); err != nil { + if err := PhoneNumberCheck(x.PhoneNumber); err != nil { return err } } - if x.VerifyCode == "" { - return errs.ErrArgs.WrapMsg("VerifyCode is empty") - } return nil } @@ -251,10 +251,22 @@ func EmailCheck(email string) error { } func AreaCodeCheck(areaCode string) error { - //pattern := `\+[1-9][0-9]{1,2}` - //if err := regexMatch(pattern, areaCode); err != nil { - // return errs.WrapMsg(err, "AreaCode is invalid") - //} + if areaCode == "" { + return errs.ErrArgs.WrapMsg("AreaCode is empty") + } + if strings.HasPrefix(areaCode, "+") { + areaCode = areaCode[1:] + } + if areaCode == "" { + return errs.ErrArgs.WrapMsg("invalid AreaCode") + } + val, err := strconv.ParseUint(areaCode, 10, 32) + if err != nil { + return errs.ErrArgs.WrapMsg("invalid AreaCode") + } + if val > 1000 { + return errs.ErrArgs.WrapMsg("invalid AreaCode") + } return nil } @@ -296,22 +308,23 @@ func (x *AddUserAccountReq) Check() error { return errs.ErrArgs.WrapMsg("user is empty") } - if x.User.Email == "" { - if x.User.AreaCode == "" || x.User.PhoneNumber == "" { - return errs.ErrArgs.WrapMsg("area code or phone number is empty") - } - if x.User.AreaCode[0] != '+' { - x.User.AreaCode = "+" + x.User.AreaCode - } - if _, err := strconv.ParseUint(x.User.AreaCode[1:], 10, 64); err != nil { - return errs.ErrArgs.WrapMsg("area code must be number") + if x.User.PhoneNumber == "" && x.User.Email == "" { + return errs.ErrArgs.WrapMsg("PhoneNumber and Email is empty") + } + if x.User.PhoneNumber != "" && x.User.Email != "" { + return errs.ErrArgs.WrapMsg("PhoneNumber and Email is not empty") + } + if x.User.Email != "" { + if err := EmailCheck(x.User.Email); err != nil { + return err } - if _, err := strconv.ParseUint(x.User.PhoneNumber, 10, 64); err != nil { - return errs.ErrArgs.WrapMsg("phone number must be number") + } + if x.User.PhoneNumber != "" { + if err := AreaCodeCheck(x.User.AreaCode); err != nil { + return err } - } else { - if err := EmailCheck(x.User.Email); err != nil { - return errs.ErrArgs.WrapMsg("email must be right") + if err := PhoneNumberCheck(x.User.PhoneNumber); err != nil { + return err } } From 35a4ceaaf46110c2de51821db275a9462a9211a4 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Mon, 29 Apr 2024 10:06:37 +0800 Subject: [PATCH 4/5] fix: chat args check --- internal/rpc/chat/login.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/rpc/chat/login.go b/internal/rpc/chat/login.go index cc89c233..87d161a3 100644 --- a/internal/rpc/chat/login.go +++ b/internal/rpc/chat/login.go @@ -50,7 +50,7 @@ func (o *chatSvr) SendVerifyCode(ctx context.Context, req *chat.SendVerifyCodeRe if req.AreaCode == "" || req.PhoneNumber == "" { return nil, errs.ErrArgs.WrapMsg("area code or phone number is empty") } - if req.AreaCode[0] != '+' { + if !strings.HasPrefix(req.AreaCode, "+") { req.AreaCode = "+" + req.AreaCode } if _, err := strconv.ParseUint(req.AreaCode[1:], 10, 64); err != nil { @@ -415,7 +415,7 @@ func (o *chatSvr) Login(ctx context.Context, req *chat.LoginReq) (*chat.LoginRes if req.AreaCode == "" { return nil, errs.ErrArgs.WrapMsg("area code must") } - if req.AreaCode[0] != '+' { + if !strings.HasPrefix(req.AreaCode, "+") { req.AreaCode = "+" + req.AreaCode } if _, err := strconv.ParseUint(req.AreaCode[1:], 10, 64); err != nil { From 2afeaa9c2d83f2a8fbb68fef65272a24c6cda29c Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Mon, 29 Apr 2024 10:44:29 +0800 Subject: [PATCH 5/5] update gomake --- go.mod | 4 +++- go.sum | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index f8caab7d..7947b357 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,8 @@ module github.com/openimsdk/chat go 1.21.2 +toolchain go1.21.9 + require ( github.com/gin-gonic/gin v1.9.1 github.com/golang-jwt/jwt/v4 v4.5.0 @@ -25,7 +27,7 @@ require ( require ( github.com/livekit/protocol v1.10.1 github.com/mitchellh/mapstructure v1.5.0 - github.com/openimsdk/gomake v0.0.9 + github.com/openimsdk/gomake v0.0.11 github.com/openimsdk/protocol v0.0.63 github.com/openimsdk/tools v0.0.49-alpha.3 github.com/redis/go-redis/v9 v9.5.1 diff --git a/go.sum b/go.sum index 9052f235..0f8859dd 100644 --- a/go.sum +++ b/go.sum @@ -178,8 +178,8 @@ github.com/nats-io/nkeys v0.4.6/go.mod h1:4DxZNzenSVd1cYQoAa8948QY3QDjrHfcfVADym github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw= github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= -github.com/openimsdk/gomake v0.0.9 h1:ouf25ygN2PMQ68Gfgns/EQRPiLPnp+77SIr68GfE+n4= -github.com/openimsdk/gomake v0.0.9/go.mod h1:PndCozNc2IsQIciyn9mvEblYWZwJmAI+06z94EY+csI= +github.com/openimsdk/gomake v0.0.11 h1:jJ9286zKFfBeARkmfqMEcUYg9lJ+Cj9lylxP8W9uCFM= +github.com/openimsdk/gomake v0.0.11/go.mod h1:PndCozNc2IsQIciyn9mvEblYWZwJmAI+06z94EY+csI= github.com/openimsdk/protocol v0.0.63 h1:9DnweZe9nEYDFa4fGTbC9Cqi0gLUdtBhRo1NRP2X3WQ= github.com/openimsdk/protocol v0.0.63/go.mod h1:OZQA9FR55lseYoN2Ql1XAHYKHJGu7OMNkUbuekrKCM8= github.com/openimsdk/tools v0.0.49-alpha.3 h1:mXKU09asKTLjj0yCsfqqMizs3ibl1vLimW7ZYzAMw44=