From 3c5ddad31777808f0249c689bdb89b1bc341416d Mon Sep 17 00:00:00 2001 From: Hossin Asaadi Date: Sun, 6 Nov 2022 04:39:31 -0500 Subject: [PATCH 1/2] 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 From e2150c38770ac1b67fb6623945f63bb9907c4eae Mon Sep 17 00:00:00 2001 From: Hossin Asaadi Date: Sun, 6 Nov 2022 04:43:02 -0500 Subject: [PATCH 2/2] add func --- web/job/check_clinet_ip_job.go | 85 ++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/web/job/check_clinet_ip_job.go b/web/job/check_clinet_ip_job.go index ed7563b4..31387954 100644 --- a/web/job/check_clinet_ip_job.go +++ b/web/job/check_clinet_ip_job.go @@ -140,6 +140,91 @@ func contains(s []string, str string) bool { return false } +func GetInboundClientIps(clientEmail string) (*model.InboundClientIps, error) { + db := database.GetDB() + InboundClientIps := &model.InboundClientIps{} + err := db.Model(model.InboundClientIps{}).Where("client_email = ?", clientEmail).First(InboundClientIps).Error + if err != nil { + return nil, err + } + return InboundClientIps, nil +} +func addInboundClientIps(clientEmail string,ips []string) error { + inboundClientIps := &model.InboundClientIps{} + jsonIps, err := json.Marshal(ips) + checkError(err) + + inboundClientIps.ClientEmail = clientEmail + inboundClientIps.Ips = string(jsonIps) + + + db := database.GetDB() + tx := db.Begin() + + defer func() { + if err == nil { + tx.Commit() + } else { + tx.Rollback() + } + }() + + err = tx.Save(inboundClientIps).Error + if err != nil { + return err + } + return nil +} +func updateInboundClientIps(inboundClientIps *model.InboundClientIps,clientEmail string,ips []string) error { + + jsonIps, err := json.Marshal(ips) + checkError(err) + + inboundClientIps.ClientEmail = clientEmail + inboundClientIps.Ips = string(jsonIps) + + // check inbound limitation + inbound, _ := GetInboundByEmail(clientEmail) + + limitIpRegx, _ := regexp.Compile(`"limitIp": .+`) + + limitIpMactch := limitIpRegx.FindString(inbound.Settings) + limitIpMactch = ss.Split(limitIpMactch, `"limitIp": `)[1] + limitIp, err := strconv.Atoi(limitIpMactch) + + + 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)) + + db := database.GetDB() + err = db.Save(inboundClientIps).Error + if err != nil { + return err + } + return nil +} +func DisableInbound(id int) error{ + db := database.GetDB() + result := db.Model(model.Inbound{}). + Where("id = ? and enable = ?", id, true). + Update("enable", false) + err := result.Error + logger.Warning("disable inbound with id:",id) + + if err == nil { + job.xrayService.SetToNeedRestart() + } + + return err +} func GetInboundByEmail(clientEmail string) (*model.Inbound, error) { db := database.GetDB()