Merge pull request #22 from hossinasaadi/limit-connection

fix DB clean IPs problem
This commit is contained in:
Hossin Asaadi
2022-11-06 13:18:57 +03:30
committed by GitHub

View File

@@ -83,24 +83,20 @@ func processLogFile() {
} }
} }
err = ClearInboudClientIps()
if err != nil {
return
}
var inboundsClientIps []*model.InboundClientIps
disAllowedIps = []string{} disAllowedIps = []string{}
for clientEmail, ips := range InboundClientIps { for clientEmail, ips := range InboundClientIps {
inboundClientIps,err := GetInboundClientIps(clientEmail)
sort.Sort(sort.StringSlice(ips)) sort.Sort(sort.StringSlice(ips))
inboundClientIps := GetInboundClientIps(clientEmail, ips) if(err != nil){
if inboundClientIps != nil { addInboundClientIps(clientEmail,ips)
inboundsClientIps = append(inboundsClientIps, inboundClientIps)
}else{
updateInboundClientIps(inboundClientIps,clientEmail,ips)
} }
} }
err = AddInboundsClientIps(inboundsClientIps)
checkError(err)
// check if inbound connection is more than limited ip and drop connection // check if inbound connection is more than limited ip and drop connection
LimitDevice := func() { LimitDevice() } LimitDevice := func() { LimitDevice() }
@@ -144,35 +140,58 @@ func contains(s []string, str string) bool {
return false return false
} }
func GetInboundClientIps(clientEmail string) (*model.InboundClientIps, error) {
func ClearInboudClientIps() error {
db := database.GetDB() db := database.GetDB()
err := db.Session(&gorm.Session{AllowGlobalUpdate: true}).Delete(&model.InboundClientIps{}).Error InboundClientIps := &model.InboundClientIps{}
checkError(err) err := db.Model(model.InboundClientIps{}).Where("client_email = ?", clientEmail).First(InboundClientIps).Error
return err
}
func GetInboundClientIps(clientEmail string, ips []string) *model.InboundClientIps {
jsonIps, err := json.Marshal(ips)
if err != nil { if err != nil {
return nil return nil, err
} }
return InboundClientIps, nil
}
func addInboundClientIps(clientEmail string,ips []string) error {
inboundClientIps := &model.InboundClientIps{} inboundClientIps := &model.InboundClientIps{}
jsonIps, err := json.Marshal(ips)
checkError(err)
inboundClientIps.ClientEmail = clientEmail inboundClientIps.ClientEmail = clientEmail
inboundClientIps.Ips = string(jsonIps) inboundClientIps.Ips = string(jsonIps)
inbound, err := GetInboundByEmail(clientEmail)
db := database.GetDB()
tx := db.Begin()
defer func() {
if err == nil {
tx.Commit()
} else {
tx.Rollback()
}
}()
err = tx.Save(inboundClientIps).Error
if err != nil { if err != nil {
return 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": .+`) limitIpRegx, _ := regexp.Compile(`"limitIp": .+`)
limitIpMactch := limitIpRegx.FindString(inbound.Settings) limitIpMactch := limitIpRegx.FindString(inbound.Settings)
limitIpMactch = ss.Split(limitIpMactch, `"limitIp": `)[1] limitIpMactch = ss.Split(limitIpMactch, `"limitIp": `)[1]
limitIp, err := strconv.Atoi(limitIpMactch) limitIp, err := strconv.Atoi(limitIpMactch)
if err != nil {
return nil
}
if(limitIp < len(ips) && limitIp != 0 && inbound.Enable) { if(limitIp < len(ips) && limitIp != 0 && inbound.Enable) {
@@ -185,24 +204,27 @@ func GetInboundClientIps(clientEmail string, ips []string) *model.InboundClientI
logger.Debug("disAllowedIps ",disAllowedIps) logger.Debug("disAllowedIps ",disAllowedIps)
sort.Sort(sort.StringSlice(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() db := database.GetDB()
tx := db.Begin() err = db.Save(inboundClientIps).Error
err := tx.Save(inboundsClientIps).Error
if err != nil { if err != nil {
tx.Rollback()
return err return err
} }
tx.Commit()
return nil 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) { func GetInboundByEmail(clientEmail string) (*model.Inbound, error) {
db := database.GetDB() db := database.GetDB()