mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-27 20:30:40 +08:00
Merge branch 'del' of github.com:OpenIMSDK/Open-IM-Server into del
# Conflicts: # pkg/common/db/mysql_model/im_mysql_model/ip_model.go
This commit is contained in:
commit
9b86a18752
@ -40,11 +40,18 @@ func main() {
|
|||||||
demoRouterGroup.POST("/login", register.Login)
|
demoRouterGroup.POST("/login", register.Login)
|
||||||
demoRouterGroup.POST("/reset_password", register.ResetPassword)
|
demoRouterGroup.POST("/reset_password", register.ResetPassword)
|
||||||
}
|
}
|
||||||
cmsRouterGroup := r.Group("/cms")
|
cmsRouterGroup := r.Group("/cms_admin")
|
||||||
{
|
{
|
||||||
cmsRouterGroup.POST("/generate_invitation_code", register.GenerateInvitationCode)
|
cmsRouterGroup.POST("/generate_invitation_code", register.GenerateInvitationCode)
|
||||||
cmsRouterGroup.POST("/query_invitation_code", register.QueryInvitationCode)
|
cmsRouterGroup.POST("/query_invitation_code", register.QueryInvitationCode)
|
||||||
cmsRouterGroup.POST("/get_invitation_codes", register.GetInvitationCodes)
|
cmsRouterGroup.POST("/get_invitation_codes", register.GetInvitationCodes)
|
||||||
|
cmsRouterGroup.POST("/query_user_ip_limit_login", register.QueryUserIPLimitLogin)
|
||||||
|
cmsRouterGroup.POST("/add_user_ip_limit_login", register.AddUserIPLimitLogin)
|
||||||
|
cmsRouterGroup.POST("/remove_user_ip_limit_login", register.RemoveUserIPLimitLogin)
|
||||||
|
|
||||||
|
cmsRouterGroup.POST("/query_ip_register", register.QueryIPRegister)
|
||||||
|
cmsRouterGroup.POST("/add_ip_limit", register.AddIPLimit)
|
||||||
|
cmsRouterGroup.POST("/remove_ip_Limit", register.RemoveIPLimit)
|
||||||
}
|
}
|
||||||
defaultPorts := config.Config.Demo.Port
|
defaultPorts := config.Config.Demo.Port
|
||||||
ginPort := flag.Int("port", defaultPorts[0], "get ginServerPort from cmd,default 10004 as port")
|
ginPort := flag.Int("port", defaultPorts[0], "get ginServerPort from cmd,default 10004 as port")
|
||||||
|
@ -20,8 +20,8 @@ type QueryIPRegisterReq struct {
|
|||||||
type QueryIPRegisterResp struct {
|
type QueryIPRegisterResp struct {
|
||||||
IP string `json:"ip"`
|
IP string `json:"ip"`
|
||||||
RegisterNum int `json:"num"`
|
RegisterNum int `json:"num"`
|
||||||
UserIDList []string `json:"userIDList"`
|
|
||||||
Status int `json:"status"`
|
Status int `json:"status"`
|
||||||
|
UserIDList []string `json:"userIDList"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func QueryIPRegister(c *gin.Context) {
|
func QueryIPRegister(c *gin.Context) {
|
||||||
@ -32,18 +32,24 @@ func QueryIPRegister(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req:", req)
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req:", req)
|
||||||
ips, err := imdb.QueryUserIPLimits(req.IP)
|
userIDList, err := imdb.GetRegisterUserNum(req.IP)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.NewError(req.OperationID, "GetInvitationCode failed", req.IP)
|
log.NewError(req.OperationID, "GetInvitationCode failed", req.IP)
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"errCode": constant.ErrDB, "errMsg": "QueryUserIPLimits error!"})
|
c.JSON(http.StatusInternalServerError, gin.H{"errCode": constant.ErrDB.ErrCode, "errMsg": "GetRegisterUserNum error!"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
resp.IP = req.IP
|
resp.IP = req.IP
|
||||||
resp.RegisterNum = len(ips)
|
resp.RegisterNum = len(userIDList)
|
||||||
for _, ip := range ips {
|
resp.UserIDList = userIDList
|
||||||
resp.UserIDList = append(resp.UserIDList, ip.UserID)
|
ipLimits, err := imdb.QueryIPLimits(req.IP)
|
||||||
|
if err != nil {
|
||||||
|
log.NewError(req.OperationID, "QueryIPLimits failed", req.IP)
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"errCode": constant.ErrDB.ErrCode, "errMsg": "QueryIPLimits error!"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if ipLimits.Ip != "" {
|
||||||
|
resp.Status = 1
|
||||||
}
|
}
|
||||||
|
|
||||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp:", resp)
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp:", resp)
|
||||||
c.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": "", "data": resp})
|
c.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": "", "data": resp})
|
||||||
}
|
}
|
||||||
@ -73,53 +79,113 @@ func AddIPLimit(c *gin.Context) {
|
|||||||
LimitTime: time.Time{},
|
LimitTime: time.Time{},
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), req.IP, req.LimitTime)
|
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), req.IP, req.LimitTime)
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"errCode": constant.ErrDB, "errMsg": "InsertOneIntoIpLimits error!"})
|
c.JSON(http.StatusInternalServerError, gin.H{"errCode": constant.ErrDB.ErrCode, "errMsg": "InsertOneIntoIpLimits error!"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": ""})
|
c.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": ""})
|
||||||
}
|
}
|
||||||
|
|
||||||
type RemoveIPLimitReq struct {
|
type RemoveIPLimitReq struct {
|
||||||
|
OperationID string `json:"operationID"`
|
||||||
|
IP string `json:"ip"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type RemoveIPLimitResp struct {
|
type RemoveIPLimitResp struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func RemoveIPLimit(c *gin.Context) {
|
func RemoveIPLimit(c *gin.Context) {
|
||||||
//DeleteOneFromIpLimits
|
req := AddIPLimitReq{}
|
||||||
|
//resp := AddIPLimitResp{}
|
||||||
|
if err := c.BindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req:", req)
|
||||||
|
if err := imdb.DeleteOneFromIpLimits(req.IP); err != nil {
|
||||||
|
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), req.IP, req.LimitTime)
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"errCode": constant.ErrDB.ErrCode, "errMsg": "InsertOneIntoIpLimits error!"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": ""})
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===========================================sk 写
|
// ===========================================sk ==========================
|
||||||
|
|
||||||
type QueryUserIDIPLimitReq struct {
|
type QueryUserIDIPLimitLoginReq struct {
|
||||||
UserID string `json:"userID" binding:"required"`
|
UserID string `json:"userID" binding:"required"`
|
||||||
|
OperationID string `json:"operationID" binding:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type QueryUserIDIPLimitResp struct {
|
//type QueryUserIDIPLimitLoginResp struct {
|
||||||
|
// UserIpLimit []db.UserIpLimit `json:"userIpLimit"`
|
||||||
|
//}
|
||||||
|
|
||||||
|
func QueryUserIPLimitLogin(c *gin.Context) {
|
||||||
|
req := QueryUserIDIPLimitLoginReq{}
|
||||||
|
if err := c.BindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req:", req)
|
||||||
|
resp, err := imdb.GetIpLimitsLoginByUserID(req.UserID)
|
||||||
|
if err != nil {
|
||||||
|
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), req.UserID)
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"errCode": constant.ErrDB.ErrCode, "errMsg": "GetIpLimitsByUserID error!"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp:", resp)
|
||||||
|
c.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": "", "data": resp})
|
||||||
}
|
}
|
||||||
|
|
||||||
func QueryUserIDIPLimit(c *gin.Context) {
|
type AddUserIPLimitLoginReq struct {
|
||||||
|
UserID string `json:"userID" binding:"required"`
|
||||||
|
OperationID string `json:"operationID" binding:"required"`
|
||||||
|
IP string `json:"ip"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AddUserIPLimitReq struct {
|
type AddUserIPLimitLoginResp struct {
|
||||||
}
|
|
||||||
|
|
||||||
type AddUserIPLimitResp struct {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加ip 特定用户才能登录 user_ip_limits 表
|
// 添加ip 特定用户才能登录 user_ip_limits 表
|
||||||
func AddUserIPLimit(c *gin.Context) {
|
func AddUserIPLimitLogin(c *gin.Context) {
|
||||||
|
req := AddUserIPLimitLoginReq{}
|
||||||
|
if err := c.BindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req:", req)
|
||||||
|
userIp := db.UserIpLimit{UserID: req.UserID, Ip: req.IP}
|
||||||
|
err := imdb.InsertUserIpLimitsLogin(&userIp)
|
||||||
|
if err != nil {
|
||||||
|
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), req.UserID)
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"errCode": constant.ErrDB, "errMsg": "InsertUserIpLimitsLogin error!"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": ""})
|
||||||
}
|
}
|
||||||
|
|
||||||
type RemoveUserIPLimitReq struct {
|
type RemoveUserIPLimitReq struct {
|
||||||
|
UserID string `json:"userID" binding:"required"`
|
||||||
|
OperationID string `json:"operationID" binding:"required"`
|
||||||
|
IP string `json:"ip"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type RemoveUserIPLimitResp struct {
|
type RemoveUserIPLimitResp struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除ip 特定用户才能登录 user_ip_limits 表
|
// 删除ip 特定用户才能登录 user_ip_limits 表
|
||||||
func RemoveUserIPLimit(c *gin.Context) {
|
func RemoveUserIPLimitLogin(c *gin.Context) {
|
||||||
|
req := RemoveUserIPLimitReq{}
|
||||||
|
if err := c.BindJSON(&req); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req:", req)
|
||||||
|
err := imdb.DeleteUserIpLimitsLogin(req.UserID, req.IP)
|
||||||
|
if err != nil {
|
||||||
|
log.NewError(req.OperationID, utils.GetSelfFuncName(), err.Error(), req.UserID)
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"errCode": constant.ErrDB, "errMsg": "DeleteUserIpLimitsLogin error!"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": ""})
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,6 @@ import (
|
|||||||
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
|
imdb "Open_IM/pkg/common/db/mysql_model/im_mysql_model"
|
||||||
http2 "Open_IM/pkg/common/http"
|
http2 "Open_IM/pkg/common/http"
|
||||||
"Open_IM/pkg/common/log"
|
"Open_IM/pkg/common/log"
|
||||||
pbAuth "Open_IM/pkg/proto/auth"
|
|
||||||
pbFriend "Open_IM/pkg/proto/friend"
|
pbFriend "Open_IM/pkg/proto/friend"
|
||||||
"Open_IM/pkg/utils"
|
"Open_IM/pkg/utils"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@ -47,10 +46,12 @@ func SetPassword(c *gin.Context) {
|
|||||||
Limited, LimitError := imdb.IsLimitRegisterIp(ip)
|
Limited, LimitError := imdb.IsLimitRegisterIp(ip)
|
||||||
if LimitError != nil {
|
if LimitError != nil {
|
||||||
log.Error(params.OperationID, utils.GetSelfFuncName(), LimitError, ip)
|
log.Error(params.OperationID, utils.GetSelfFuncName(), LimitError, ip)
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.ErrDB.ErrCode, "errMsg": LimitError.Error()})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if Limited {
|
if Limited {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.FormattingError, "errMsg": err.Error()})
|
c.JSON(http.StatusBadRequest, gin.H{"errCode": constant.RegisterLimit, "errMsg": "limited"})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var account string
|
var account string
|
||||||
|
@ -9,6 +9,7 @@ type Register struct {
|
|||||||
UserID string `gorm:"column:user_id;type:varchar(255)" json:"userID"`
|
UserID string `gorm:"column:user_id;type:varchar(255)" json:"userID"`
|
||||||
AreaCode string `gorm:"column:area_code;type:varchar(255)"`
|
AreaCode string `gorm:"column:area_code;type:varchar(255)"`
|
||||||
InvitationCode string `gorm:"column:invitation_code;type:varchar(255)"`
|
InvitationCode string `gorm:"column:invitation_code;type:varchar(255)"`
|
||||||
|
RegisterIP string `gorm:"column:register_ip;type:varchar(255)"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Invitation struct {
|
type Invitation struct {
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
package im_mysql_model
|
package im_mysql_model
|
||||||
|
|
||||||
import "Open_IM/pkg/common/db"
|
import (
|
||||||
|
"Open_IM/pkg/common/db"
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
func IsLimitRegisterIp(RegisterIp string) (bool, error) {
|
func IsLimitRegisterIp(RegisterIp string) (bool, error) {
|
||||||
//如果已经存在则限制
|
//如果已经存在则限制
|
||||||
@ -29,6 +33,15 @@ func IsLimitUserLoginIp(userID string, LoginIp string) (bool, error) {
|
|||||||
return count == 0, nil
|
return count == 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func QueryIPLimits(ip string) (*db.IpLimit, error) {
|
||||||
|
var ipLimit db.IpLimit
|
||||||
|
err := db.DB.MysqlDB.DefaultGormDB().Model(&db.IpLimit{}).Where("ip=?", ip).Take(&ip).Error
|
||||||
|
if gorm.IsRecordNotFoundError(err) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return &ipLimit, err
|
||||||
|
}
|
||||||
|
|
||||||
func QueryUserIPLimits(ip string) ([]db.UserIpLimit, error) {
|
func QueryUserIPLimits(ip string) ([]db.UserIpLimit, error) {
|
||||||
var ips []db.UserIpLimit
|
var ips []db.UserIpLimit
|
||||||
err := db.DB.MysqlDB.DefaultGormDB().Model(&db.UserIpLimit{}).Where("ip=?", ip).Find(&ips).Error
|
err := db.DB.MysqlDB.DefaultGormDB().Model(&db.UserIpLimit{}).Where("ip=?", ip).Find(&ips).Error
|
||||||
@ -43,3 +56,25 @@ func DeleteOneFromIpLimits(ip string) error {
|
|||||||
ipLimits := &db.IpLimit{}
|
ipLimits := &db.IpLimit{}
|
||||||
return db.DB.MysqlDB.DefaultGormDB().Model(ipLimits).Where("ip=?", ip).Delete(ipLimits).Error
|
return db.DB.MysqlDB.DefaultGormDB().Model(ipLimits).Where("ip=?", ip).Delete(ipLimits).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetIpLimitsLoginByUserID(userID string) ([]db.UserIpLimit, error) {
|
||||||
|
var ips []db.UserIpLimit
|
||||||
|
err := db.DB.MysqlDB.DefaultGormDB().Model(&db.UserIpLimit{}).Where("user_id=?", userID).Take(&ips).Error
|
||||||
|
return ips, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func InsertUserIpLimitsLogin(userIp *db.UserIpLimit) error {
|
||||||
|
userIp.CreateTime = time.Now()
|
||||||
|
return db.DB.MysqlDB.DefaultGormDB().Model(&db.UserIpLimit{}).Create(userIp).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteUserIpLimitsLogin(userID, ip string) error {
|
||||||
|
userIp := db.UserIpLimit{UserID: userID, Ip: ip}
|
||||||
|
return db.DB.MysqlDB.DefaultGormDB().Model(&db.UserIpLimit{}).Delete(&userIp).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetRegisterUserNum(ip string) ([]string, error) {
|
||||||
|
var userIDList []string
|
||||||
|
err := db.DB.MysqlDB.DefaultGormDB().Model(&db.Register{}).Where("register_ip=?", ip).Pluck("user_id", &userIDList).Error
|
||||||
|
return userIDList, err
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user