mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-04-19 21:42:24 +00:00
Enhance CheckClientIpJob #1964
This commit is contained in:
parent
3a46c3302b
commit
6563d23f38
2 changed files with 44 additions and 32 deletions
|
@ -10,11 +10,12 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"x-ui/config"
|
||||||
"x-ui/database"
|
"x-ui/database"
|
||||||
"x-ui/database/model"
|
"x-ui/database/model"
|
||||||
"x-ui/config"
|
|
||||||
"x-ui/logger"
|
"x-ui/logger"
|
||||||
"x-ui/xray"
|
"x-ui/xray"
|
||||||
)
|
)
|
||||||
|
@ -38,30 +39,43 @@ func NewCheckClientIpJob() *CheckClientIpJob {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *CheckClientIpJob) Run() {
|
func (j *CheckClientIpJob) Run() {
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
// create files and dirs required for iplimit if not exists
|
if j.checkFail2BanInstalled() {
|
||||||
for i := 0; i < len(ipFiles); i++ {
|
j.openLogFiles(ipFiles)
|
||||||
err := os.MkdirAll(config.GetLogFolder(), 0770)
|
|
||||||
j.checkError(err)
|
|
||||||
file, err := os.OpenFile(ipFiles[i], os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644)
|
|
||||||
j.checkError(err)
|
|
||||||
defer file.Close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// check for limit ip
|
|
||||||
if j.hasLimitIp() {
|
if j.hasLimitIp() {
|
||||||
j.checkFail2BanInstalled()
|
if j.checkFail2BanInstalled() && xray.GetAccessLogPath() == "./access.log" {
|
||||||
j.processLogFile()
|
j.processLogFile()
|
||||||
|
} else {
|
||||||
|
if !j.checkFail2BanInstalled() {
|
||||||
|
logger.Warning("fail2ban is not installed. IP limiting may not work properly.")
|
||||||
|
}
|
||||||
|
switch xray.GetAccessLogPath() {
|
||||||
|
case "none":
|
||||||
|
logger.Warning("Access log is set to 'none', check your Xray Configs")
|
||||||
|
case "":
|
||||||
|
logger.Warning("Access log doesn't exist in your Xray Configs")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !j.hasLimitIp() && xray.GetAccessLogPath() == "./access.log" {
|
if !j.checkFail2BanInstalled() && xray.GetAccessLogPath() == "./access.log" {
|
||||||
go j.clearLogTime()
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
j.clearLogTime()
|
||||||
|
}()
|
||||||
|
wg.Wait()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *CheckClientIpJob) clearLogTime() {
|
func (j *CheckClientIpJob) clearLogTime() {
|
||||||
for {
|
ticker := time.NewTicker(time.Hour)
|
||||||
time.Sleep(time.Hour)
|
defer ticker.Stop()
|
||||||
|
|
||||||
|
for range ticker.C {
|
||||||
j.clearAccessLog()
|
j.clearAccessLog()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,15 +89,18 @@ func (j *CheckClientIpJob) clearAccessLog() {
|
||||||
// reopen the access log file for reading
|
// reopen the access log file for reading
|
||||||
file, err := os.Open(accessLogPath)
|
file, err := os.Open(accessLogPath)
|
||||||
j.checkError(err)
|
j.checkError(err)
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
// copy access log content to persistent file
|
// copy access log content to persistent file
|
||||||
_, err = io.Copy(logAccessP, file)
|
_, err = io.Copy(logAccessP, file)
|
||||||
j.checkError(err)
|
j.checkError(err)
|
||||||
|
|
||||||
|
// close the file after copying content
|
||||||
|
file.Close()
|
||||||
|
|
||||||
// clean access log
|
// clean access log
|
||||||
err = os.Truncate(accessLogPath, 0)
|
err = os.Truncate(accessLogPath, 0)
|
||||||
j.checkError(err)
|
j.checkError(err)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *CheckClientIpJob) hasLimitIp() bool {
|
func (j *CheckClientIpJob) hasLimitIp() bool {
|
||||||
|
@ -115,29 +132,26 @@ func (j *CheckClientIpJob) hasLimitIp() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *CheckClientIpJob) checkFail2BanInstalled() {
|
func (j *CheckClientIpJob) checkFail2BanInstalled() bool {
|
||||||
cmd := "fail2ban-client"
|
cmd := "fail2ban-client"
|
||||||
args := []string{"-h"}
|
args := []string{"-h"}
|
||||||
|
|
||||||
err := exec.Command(cmd, args...).Run()
|
err := exec.Command(cmd, args...).Run()
|
||||||
if err != nil {
|
return err == nil
|
||||||
logger.Warning("fail2ban is not installed. IP limiting may not work properly.")
|
}
|
||||||
|
|
||||||
|
func (j *CheckClientIpJob) openLogFiles(ipFiles []string) {
|
||||||
|
for i := 0; i < len(ipFiles); i++ {
|
||||||
|
err := os.MkdirAll(config.GetLogFolder(), 0770)
|
||||||
|
j.checkError(err)
|
||||||
|
file, err := os.OpenFile(ipFiles[i], os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644)
|
||||||
|
j.checkError(err)
|
||||||
|
defer file.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *CheckClientIpJob) processLogFile() {
|
func (j *CheckClientIpJob) processLogFile() {
|
||||||
accessLogPath := xray.GetAccessLogPath()
|
accessLogPath := xray.GetAccessLogPath()
|
||||||
|
|
||||||
if accessLogPath == "none" {
|
|
||||||
logger.Warning("Access log is set to 'none' check your Xray Configs")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if accessLogPath == "" {
|
|
||||||
logger.Warning("Access log doesn't exist in your Xray Configs")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
file, err := os.Open(accessLogPath)
|
file, err := os.Open(accessLogPath)
|
||||||
j.checkError(err)
|
j.checkError(err)
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
|
@ -1560,8 +1560,6 @@ func (t *Tgbot) sendBackup(chatId int64) {
|
||||||
} else {
|
} else {
|
||||||
logger.Error("Error in opening config.json file for backup: ", err)
|
logger.Error("Error in opening config.json file for backup: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
t.sendBanLogs(chatId, false)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tgbot) sendBanLogs(chatId int64, dt bool) {
|
func (t *Tgbot) sendBanLogs(chatId int64, dt bool) {
|
||||||
|
|
Loading…
Reference in a new issue