update and rename client ip job file

This commit is contained in:
Hamidreza Ghavami 2023-04-14 00:07:13 +04:30
parent 4548755375
commit a3b170d6c4

View file

@ -1,26 +1,28 @@
package job package job
import ( import (
"x-ui/logger" "encoding/json"
"x-ui/web/service" "os"
"regexp"
ss "strings"
"x-ui/database" "x-ui/database"
"x-ui/database/model" "x-ui/database/model"
"os" "x-ui/logger"
ss "strings" "x-ui/web/service"
"regexp" "x-ui/xray"
"encoding/json"
// "strconv" // "strconv"
"github.com/go-cmd/cmd"
"net"
"sort"
"strings" "strings"
"time" "time"
"net"
"github.com/go-cmd/cmd"
"sort"
) )
type CheckClientIpJob struct { type CheckClientIpJob struct {
xrayService service.XrayService xrayService service.XrayService
inboundService service.InboundService inboundService service.InboundService
} }
var job *CheckClientIpJob var job *CheckClientIpJob
var disAllowedIps []string var disAllowedIps []string
@ -35,14 +37,14 @@ func (j *CheckClientIpJob) Run() {
// disAllowedIps = []string{"192.168.1.183","192.168.1.197"} // disAllowedIps = []string{"192.168.1.183","192.168.1.197"}
blockedIps := []byte(ss.Join(disAllowedIps, ",")) blockedIps := []byte(ss.Join(disAllowedIps, ","))
err := os.WriteFile("./bin/blockedIPs", blockedIps, 0755) err := os.WriteFile(xray.GetBlockedIPsPath(), blockedIps, 0755)
checkError(err) checkError(err)
} }
func processLogFile() { func processLogFile() {
accessLogPath := GetAccessLogPath() accessLogPath := GetAccessLogPath()
if(accessLogPath == "") { if accessLogPath == "" {
logger.Warning("xray log not init in config.json") logger.Warning("xray log not init in config.json")
return return
} }
@ -62,26 +64,24 @@ func processLogFile() {
emailRegx, _ := regexp.Compile(`email:.+`) emailRegx, _ := regexp.Compile(`email:.+`)
matchesIp := ipRegx.FindString(line) matchesIp := ipRegx.FindString(line)
if(len(matchesIp) > 0) { if len(matchesIp) > 0 {
ip := string(matchesIp) ip := string(matchesIp)
if( ip == "127.0.0.1" || ip == "1.1.1.1") { if ip == "127.0.0.1" || ip == "1.1.1.1" {
continue continue
} }
matchesEmail := emailRegx.FindString(line) matchesEmail := emailRegx.FindString(line)
if(matchesEmail == "") { if matchesEmail == "" {
continue continue
} }
matchesEmail = ss.Split(matchesEmail, "email: ")[1] matchesEmail = ss.Split(matchesEmail, "email: ")[1]
if(InboundClientIps[matchesEmail] != nil) { if InboundClientIps[matchesEmail] != nil {
if(contains(InboundClientIps[matchesEmail],ip)){ if contains(InboundClientIps[matchesEmail], ip) {
continue continue
} }
InboundClientIps[matchesEmail] = append(InboundClientIps[matchesEmail], ip) InboundClientIps[matchesEmail] = append(InboundClientIps[matchesEmail], ip)
} else { } else {
InboundClientIps[matchesEmail] = append(InboundClientIps[matchesEmail], ip) InboundClientIps[matchesEmail] = append(InboundClientIps[matchesEmail], ip)
} }
@ -93,7 +93,7 @@ func processLogFile() {
for clientEmail, ips := range InboundClientIps { for clientEmail, ips := range InboundClientIps {
inboundClientIps, err := GetInboundClientIps(clientEmail) inboundClientIps, err := GetInboundClientIps(clientEmail)
sort.Sort(sort.StringSlice(ips)) sort.Sort(sort.StringSlice(ips))
if(err != nil){ if err != nil {
addInboundClientIps(clientEmail, ips) addInboundClientIps(clientEmail, ips)
} else { } else {
@ -102,7 +102,6 @@ func processLogFile() {
} }
// 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() }
@ -113,15 +112,15 @@ func processLogFile() {
} }
func GetAccessLogPath() string { func GetAccessLogPath() string {
config, err := os.ReadFile("bin/config.json") config, err := os.ReadFile(xray.GetConfigPath())
checkError(err) checkError(err)
jsonConfig := map[string]interface{}{} jsonConfig := map[string]interface{}{}
err = json.Unmarshal([]byte(config), &jsonConfig) err = json.Unmarshal([]byte(config), &jsonConfig)
checkError(err) checkError(err)
if(jsonConfig["log"] != nil) { if jsonConfig["log"] != nil {
jsonLog := jsonConfig["log"].(map[string]interface{}) jsonLog := jsonConfig["log"].(map[string]interface{})
if(jsonLog["access"] != nil) { if jsonLog["access"] != nil {
accessLogPath := jsonLog["access"].(string) accessLogPath := jsonLog["access"].(string)
@ -208,7 +207,7 @@ func updateInboundClientIps(inboundClientIps *model.InboundClientIps,clientEmail
limitIp := client.LimitIP limitIp := client.LimitIP
if(limitIp < len(ips) && limitIp != 0 && inbound.Enable) { if limitIp < len(ips) && limitIp != 0 && inbound.Enable {
disAllowedIps = append(disAllowedIps, ips[limitIp:]...) disAllowedIps = append(disAllowedIps, ips[limitIp:]...)
} }
@ -288,7 +287,6 @@ func LimitDevice() {
} }
} }
func LocalIP() ([]string, error) { func LocalIP() ([]string, error) {
// get machine ips // get machine ips
@ -321,8 +319,7 @@ func LocalIP() ([]string, error) {
return ips, nil return ips, nil
} }
func IPsToRegex(ips []string) string {
func IPsToRegex(ips []string) (string){
regx := "" regx := ""
for _, ip := range ips { for _, ip := range ips {