You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/internal/rpc/admin_cms/admin_cms.go

157 lines
5.8 KiB

3 years ago
package admin_cms
import (
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
2 years ago
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
3 years ago
openIMHttp "Open_IM/pkg/common/http"
"Open_IM/pkg/common/log"
"Open_IM/pkg/common/token_verify"
"Open_IM/pkg/grpc-etcdv3/getcdv3"
pbAdminCMS "Open_IM/pkg/proto/admin_cms"
2 years ago
server_api_params "Open_IM/pkg/proto/sdk_ws"
3 years ago
"Open_IM/pkg/utils"
"context"
"net"
"strconv"
"strings"
3 years ago
"google.golang.org/grpc"
3 years ago
)
type adminCMSServer struct {
rpcPort int
rpcRegisterName string
etcdSchema string
etcdAddr []string
}
func NewAdminCMSServer(port int) *adminCMSServer {
3 years ago
log.NewPrivateLog(constant.LogFileName)
3 years ago
return &adminCMSServer{
rpcPort: port,
rpcRegisterName: config.Config.RpcRegisterName.OpenImAdminCMSName,
etcdSchema: config.Config.Etcd.EtcdSchema,
etcdAddr: config.Config.Etcd.EtcdAddr,
}
}
func (s *adminCMSServer) Run() {
log.NewInfo("0", "AdminCMS rpc start ")
3 years ago
listenIP := ""
if config.Config.ListenIP == "" {
listenIP = "0.0.0.0"
} else {
listenIP = config.Config.ListenIP
}
address := listenIP + ":" + strconv.Itoa(s.rpcPort)
3 years ago
//listener network
3 years ago
listener, err := net.Listen("tcp", address)
3 years ago
if err != nil {
3 years ago
panic("listening err:" + err.Error() + s.rpcRegisterName)
3 years ago
}
3 years ago
log.NewInfo("0", "listen network success, ", address, listener)
3 years ago
defer listener.Close()
//grpc server
srv := grpc.NewServer()
defer srv.GracefulStop()
//Service registers with etcd
pbAdminCMS.RegisterAdminCMSServer(srv, s)
2 years ago
rpcRegisterIP := config.Config.RpcRegisterIP
3 years ago
if config.Config.RpcRegisterIP == "" {
rpcRegisterIP, err = utils.GetLocalIP()
if err != nil {
log.Error("", "GetLocalIP failed ", err.Error())
}
}
3 years ago
log.NewInfo("", "rpcRegisterIP", rpcRegisterIP)
3 years ago
err = getcdv3.RegisterEtcd(s.etcdSchema, strings.Join(s.etcdAddr, ","), rpcRegisterIP, s.rpcPort, s.rpcRegisterName, 10)
3 years ago
if err != nil {
log.NewError("0", "RegisterEtcd failed ", err.Error())
return
}
err = srv.Serve(listener)
if err != nil {
log.NewError("0", "Serve failed ", err.Error())
return
}
log.NewInfo("0", "message cms rpc success")
}
func (s *adminCMSServer) AdminLogin(_ context.Context, req *pbAdminCMS.AdminLoginReq) (*pbAdminCMS.AdminLoginResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp := &pbAdminCMS.AdminLoginResp{}
3 years ago
for i, adminID := range config.Config.Manager.AppManagerUid {
3 years ago
if adminID == req.AdminID && config.Config.Manager.Secrets[i] == req.Secret {
token, expTime, err := token_verify.CreateToken(adminID, constant.SingleChatType)
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "generate token success", "token: ", token, "expTime:", expTime)
if err != nil {
3 years ago
log.NewError(req.OperationID, utils.GetSelfFuncName(), "generate token failed", "adminID: ", adminID, err.Error())
3 years ago
return resp, openIMHttp.WrapError(constant.ErrTokenUnknown)
3 years ago
}
3 years ago
resp.Token = token
break
3 years ago
}
}
if resp.Token == "" {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "failed")
return resp, openIMHttp.WrapError(constant.ErrTokenMalformed)
}
3 years ago
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
3 years ago
return resp, nil
3 years ago
}
2 years ago
func (s *adminCMSServer) AddUserRegisterAddFriendIDList(_ context.Context, req *pbAdminCMS.AddUserRegisterAddFriendIDListReq) (*pbAdminCMS.AddUserRegisterAddFriendIDListResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp := &pbAdminCMS.AddUserRegisterAddFriendIDListResp{}
if err := imdb.AddUserRegisterAddFriendIDList(req.UserIDList...); err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), req.UserIDList)
return resp, openIMHttp.WrapError(constant.ErrDB)
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", req.String())
return resp, nil
}
func (s *adminCMSServer) ReduceUserRegisterAddFriendIDList(_ context.Context, req *pbAdminCMS.ReduceUserRegisterAddFriendIDListReq) (*pbAdminCMS.ReduceUserRegisterAddFriendIDListResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp := &pbAdminCMS.ReduceUserRegisterAddFriendIDListResp{}
if req.Operation == 0 {
if err := imdb.ReduceUserRegisterAddFriendIDList(req.UserIDList...); err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), req.UserIDList)
return resp, openIMHttp.WrapError(constant.ErrDB)
}
} else {
if err := imdb.DeleteAllRegisterAddFriendIDList(); err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), req.UserIDList)
return resp, openIMHttp.WrapError(constant.ErrDB)
}
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", req.String())
return resp, nil
}
func (s *adminCMSServer) GetUserRegisterAddFriendIDList(_ context.Context, req *pbAdminCMS.GetUserRegisterAddFriendIDListReq) (*pbAdminCMS.GetUserRegisterAddFriendIDListResp, error) {
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req.String())
resp := &pbAdminCMS.GetUserRegisterAddFriendIDListResp{UserInfoList: []*server_api_params.UserInfo{}}
2 years ago
userIDList, err := imdb.GetRegisterAddFriendList(req.Pagination.ShowNumber, req.Pagination.PageNumber)
2 years ago
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error())
return resp, openIMHttp.WrapError(constant.ErrDB)
}
userList, err := imdb.GetUsersByUserIDList(userIDList)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), userIDList)
return resp, openIMHttp.WrapError(constant.ErrDB)
}
2 years ago
log.NewDebug(req.OperationID, utils.GetSelfFuncName(), userList, userIDList)
resp.Pagination = &server_api_params.ResponsePagination{
CurrentPage: req.Pagination.PageNumber,
ShowNumber: req.Pagination.ShowNumber,
}
2 years ago
utils.CopyStructFields(&resp.UserInfoList, userList)
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", req.String())
return resp, nil
}