From 3c5ddad31777808f0249c689bdb89b1bc341416d Mon Sep 17 00:00:00 2001 From: Hossin Asaadi Date: Sun, 6 Nov 2022 04:39:31 -0500 Subject: [PATCH] fix clean all Ip database --- web/job/check_clinet_ip_job.go | 77 ++++------------------------------ 1 file changed, 7 insertions(+), 70 deletions(-) diff --git a/web/job/check_clinet_ip_job.go b/web/job/check_clinet_ip_job.go index 362587d6..ed7563b4 100644 --- a/web/job/check_clinet_ip_job.go +++ b/web/job/check_clinet_ip_job.go @@ -83,24 +83,20 @@ func processLogFile() { } } - err = ClearInboudClientIps() - if err != nil { - return - } - - var inboundsClientIps []*model.InboundClientIps disAllowedIps = []string{} for clientEmail, ips := range InboundClientIps { + inboundClientIps,err := GetInboundClientIps(clientEmail) sort.Sort(sort.StringSlice(ips)) - inboundClientIps := GetInboundClientIps(clientEmail, ips) - if inboundClientIps != nil { - inboundsClientIps = append(inboundsClientIps, inboundClientIps) + if(err != nil){ + addInboundClientIps(clientEmail,ips) + + }else{ + updateInboundClientIps(inboundClientIps,clientEmail,ips) } + } - err = AddInboundsClientIps(inboundsClientIps) - checkError(err) // check if inbound connection is more than limited ip and drop connection LimitDevice := func() { LimitDevice() } @@ -145,65 +141,6 @@ func contains(s []string, str string) bool { return false } -func ClearInboudClientIps() error { - db := database.GetDB() - err := db.Session(&gorm.Session{AllowGlobalUpdate: true}).Delete(&model.InboundClientIps{}).Error - checkError(err) - return err -} - -func GetInboundClientIps(clientEmail string, ips []string) *model.InboundClientIps { - jsonIps, err := json.Marshal(ips) - if err != nil { - return nil - } - - inboundClientIps := &model.InboundClientIps{} - inboundClientIps.ClientEmail = clientEmail - inboundClientIps.Ips = string(jsonIps) - - inbound, err := GetInboundByEmail(clientEmail) - if err != nil { - return nil - } - limitIpRegx, _ := regexp.Compile(`"limitIp": .+`) - limitIpMactch := limitIpRegx.FindString(inbound.Settings) - limitIpMactch = ss.Split(limitIpMactch, `"limitIp": `)[1] - limitIp, err := strconv.Atoi(limitIpMactch) - if err != nil { - return nil - } - - if(limitIp < len(ips) && limitIp != 0 && inbound.Enable) { - - if(limitIp == 1){ - limitIp = 2 - } - disAllowedIps = append(disAllowedIps,ips[limitIp - 1:]...) - - } - logger.Debug("disAllowedIps ",disAllowedIps) - sort.Sort(sort.StringSlice(disAllowedIps)) - - return inboundClientIps -} - -func AddInboundsClientIps(inboundsClientIps []*model.InboundClientIps) error { - if inboundsClientIps == nil || len(inboundsClientIps) == 0 { - return nil - } - db := database.GetDB() - tx := db.Begin() - - err := tx.Save(inboundsClientIps).Error - if err != nil { - tx.Rollback() - return err - } - tx.Commit() - return nil -} - func GetInboundByEmail(clientEmail string) (*model.Inbound, error) { db := database.GetDB() var inbounds *model.Inbound