Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/MHSanaei/3x-ui.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/web/job
diff options
context:
space:
mode:
authorHamidreza <70919649+hamid-gh98@users.noreply.github.com>2024-03-11 00:31:24 +0300
committerGitHub <noreply@github.com>2024-03-11 00:31:24 +0300
commit64a5a9f1bc91297bf46ddda9fc3575e582acd83f (patch)
treef8a3cbd685bf4ef77bb69b6b22c130d2dad42f88 /web/job
parent32afd7200ace358fe36c8c438f5048d76fbc987b (diff)
Some fixes and improvements (#1997)
* [refactor] api controller * [fix] access log path better to not hardcode the access log path, maybe some ppl dont want to use the default ./access.log * [fix] set select options from logs paths in xray settings * [update] .gitignore * [lint] all .go files * [update] use status code for jsonMsg and 401 to unauthorize * [update] handle response status code via axios * [fix] set correct value if log paths is set to 'none' we also use the default value for the paths if its set to none * [fix] iplimit - only warning access log if f2b is installed
Diffstat (limited to 'web/job')
-rw-r--r--web/job/check_client_ip_job.go41
-rw-r--r--web/job/check_cpu_usage.go1
-rw-r--r--web/job/check_xray_running_job.go2
-rw-r--r--web/job/clear_logs_job.go5
-rw-r--r--web/job/xray_traffic_job.go1
5 files changed, 31 insertions, 19 deletions
diff --git a/web/job/check_client_ip_job.go b/web/job/check_client_ip_job.go
index 344160e1..4b799ab1 100644
--- a/web/job/check_client_ip_job.go
+++ b/web/job/check_client_ip_job.go
@@ -35,35 +35,27 @@ func (j *CheckClientIpJob) Run() {
j.lastClear = time.Now().Unix()
}
+ shouldClearAccessLog := false
f2bInstalled := j.checkFail2BanInstalled()
- accessLogPath := xray.GetAccessLogPath()
- clearAccessLog := false
+ isAccessLogAvailable := j.checkAccessLogAvailable(f2bInstalled)
if j.hasLimitIp() {
- if f2bInstalled && accessLogPath == "./access.log" {
- clearAccessLog = j.processLogFile()
+ if f2bInstalled && isAccessLogAvailable {
+ shouldClearAccessLog = j.processLogFile()
} else {
if !f2bInstalled {
logger.Warning("fail2ban is not installed. IP limiting may not work properly.")
}
- switch accessLogPath {
- 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")
- default:
- logger.Warning("Current access.log path is not compatible with IP Limit")
- }
}
}
- if clearAccessLog || accessLogPath == "./access.log" && time.Now().Unix() - j.lastClear > 3600 {
+ if shouldClearAccessLog || isAccessLogAvailable && time.Now().Unix()-j.lastClear > 3600 {
j.clearAccessLog()
}
}
func (j *CheckClientIpJob) clearAccessLog() {
- logAccessP, err := os.OpenFile(xray.GetAccessPersistentLogPath(), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
+ logAccessP, err := os.OpenFile(xray.GetAccessPersistentLogPath(), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
j.checkError(err)
// reopen the access log file for reading
@@ -178,6 +170,25 @@ func (j *CheckClientIpJob) processLogFile() bool {
return shouldCleanLog
}
+func (j *CheckClientIpJob) checkAccessLogAvailable(doWarning bool) bool {
+ accessLogPath := xray.GetAccessLogPath()
+ isAvailable := true
+ warningMsg := ""
+ // access log is not available if it is set to 'none' or an empty string
+ switch accessLogPath {
+ case "none":
+ warningMsg = "Access log is set to 'none', check your Xray Configs"
+ isAvailable = false
+ case "":
+ warningMsg = "Access log doesn't exist in your Xray Configs"
+ isAvailable = false
+ }
+ if doWarning && warningMsg != "" {
+ logger.Warning(warningMsg)
+ }
+ return isAvailable
+}
+
func (j *CheckClientIpJob) checkError(e error) {
if e != nil {
logger.Warning("client ip job err:", e)
@@ -253,7 +264,7 @@ func (j *CheckClientIpJob) updateInboundClientIps(inboundClientIps *model.Inboun
j.disAllowedIps = []string{}
// create iplimit log file channel
- logIpFile, err := os.OpenFile(xray.GetIPLimitLogPath(), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
+ logIpFile, err := os.OpenFile(xray.GetIPLimitLogPath(), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
if err != nil {
logger.Errorf("failed to create or open ip limit log file: %s", err)
}
diff --git a/web/job/check_cpu_usage.go b/web/job/check_cpu_usage.go
index 74f6a544..b84ad054 100644
--- a/web/job/check_cpu_usage.go
+++ b/web/job/check_cpu_usage.go
@@ -3,6 +3,7 @@ package job
import (
"strconv"
"time"
+
"x-ui/web/service"
"github.com/shirou/gopsutil/v3/cpu"
diff --git a/web/job/check_xray_running_job.go b/web/job/check_xray_running_job.go
index d9ffeb7a..bfef5ece 100644
--- a/web/job/check_xray_running_job.go
+++ b/web/job/check_xray_running_job.go
@@ -20,7 +20,7 @@ func (j *CheckXrayRunningJob) Run() {
j.checkTime = 0
} else {
j.checkTime++
- //only restart if it's down 2 times in a row
+ // only restart if it's down 2 times in a row
if j.checkTime > 1 {
err := j.xrayService.RestartXray(false)
j.checkTime = 0
diff --git a/web/job/clear_logs_job.go b/web/job/clear_logs_job.go
index c4cb3c87..70967657 100644
--- a/web/job/clear_logs_job.go
+++ b/web/job/clear_logs_job.go
@@ -3,6 +3,7 @@ package job
import (
"io"
"os"
+
"x-ui/logger"
"x-ui/xray"
)
@@ -17,7 +18,7 @@ func NewClearLogsJob() *ClearLogsJob {
func (j *ClearLogsJob) Run() {
logFiles := []string{xray.GetIPLimitLogPath(), xray.GetIPLimitBannedLogPath(), xray.GetAccessPersistentLogPath()}
logFilesPrev := []string{xray.GetIPLimitBannedPrevLogPath(), xray.GetAccessPersistentPrevLogPath()}
-
+
// clear old previous logs
for i := 0; i < len(logFilesPrev); i++ {
if err := os.Truncate(logFilesPrev[i], 0); err != nil {
@@ -43,7 +44,7 @@ func (j *ClearLogsJob) Run() {
if err != nil {
logger.Warning("clear logs job err:", err)
}
-
+
logFile.Close()
logFilePrev.Close()
}
diff --git a/web/job/xray_traffic_job.go b/web/job/xray_traffic_job.go
index c0de4428..dea407e0 100644
--- a/web/job/xray_traffic_job.go
+++ b/web/job/xray_traffic_job.go
@@ -36,5 +36,4 @@ func (j *XrayTrafficJob) Run() {
if needRestart0 || needRestart1 {
j.xrayService.SetToNeedRestart()
}
-
}