Skip to content

Commit

Permalink
Update check_client_ip_job.go
Browse files Browse the repository at this point in the history
  • Loading branch information
MHSanaei committed Apr 27, 2023
1 parent 607c5d3 commit 865e47e
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions web/job/check_client_ip_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@ import (
"encoding/json"
"os"
"regexp"
ss "strings"
"x-ui/database"
"x-ui/database/model"
"x-ui/logger"
"x-ui/web/service"
"x-ui/xray"
// "strconv"
"github.com/go-cmd/cmd"

"net"
"sort"
"strings"
"time"

"github.com/go-cmd/cmd"
)

type CheckClientIpJob struct {
xrayService service.XrayService
inboundService service.InboundService
xrayService service.XrayService
}

var job *CheckClientIpJob
Expand All @@ -36,7 +35,7 @@ func (j *CheckClientIpJob) Run() {
processLogFile()

// disAllowedIps = []string{"192.168.1.183","192.168.1.197"}
blockedIps := []byte(ss.Join(disAllowedIps, ","))
blockedIps := []byte(strings.Join(disAllowedIps, ","))
err := os.WriteFile(xray.GetBlockedIPsPath(), blockedIps, 0755)
checkError(err)

Expand All @@ -58,7 +57,7 @@ func processLogFile() {
checkError(err)
}

lines := ss.Split(string(data), "\n")
lines := strings.Split(string(data), "\n")
for _, line := range lines {
ipRegx, _ := regexp.Compile(`[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+`)
emailRegx, _ := regexp.Compile(`email:.+`)
Expand All @@ -74,7 +73,7 @@ func processLogFile() {
if matchesEmail == "" {
continue
}
matchesEmail = ss.Split(matchesEmail, "email: ")[1]
matchesEmail = strings.Split(matchesEmail, "email: ")[1]

if InboundClientIps[matchesEmail] != nil {
if contains(InboundClientIps[matchesEmail], ip) {
Expand All @@ -92,14 +91,12 @@ func processLogFile() {

for clientEmail, ips := range InboundClientIps {
inboundClientIps, err := GetInboundClientIps(clientEmail)
sort.Sort(sort.StringSlice(ips))
sort.Strings(ips)
if err != nil {
addInboundClientIps(clientEmail, ips)

} else {
updateInboundClientIps(inboundClientIps, clientEmail, ips)
}

}

// check if inbound connection is more than limited ip and drop connection
Expand Down Expand Up @@ -202,6 +199,8 @@ func updateInboundClientIps(inboundClientIps *model.InboundClientIps, clientEmai
json.Unmarshal([]byte(inbound.Settings), &settings)
clients := settings["clients"]

var disAllowedIps []string // initialize the slice

for _, client := range clients {
if client.Email == clientEmail {

Expand All @@ -214,7 +213,7 @@ func updateInboundClientIps(inboundClientIps *model.InboundClientIps, clientEmai
}
}
logger.Debug("disAllowedIps ", disAllowedIps)
sort.Sort(sort.StringSlice(disAllowedIps))
sort.Strings(disAllowedIps)

db := database.GetDB()
err = db.Save(inboundClientIps).Error
Expand All @@ -223,6 +222,7 @@ func updateInboundClientIps(inboundClientIps *model.InboundClientIps, clientEmai
}
return nil
}

func DisableInbound(id int) error {
db := database.GetDB()
result := db.Model(model.Inbound{}).
Expand Down

0 comments on commit 865e47e

Please sign in to comment.