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
This commit is contained in:
Hamidreza
2024-03-11 01:01:24 +03:30
committed by GitHub
parent 32afd7200a
commit 64a5a9f1bc
45 changed files with 211 additions and 200 deletions

View File

@@ -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)
}

View File

@@ -3,6 +3,7 @@ package job
import (
"strconv"
"time"
"x-ui/web/service"
"github.com/shirou/gopsutil/v3/cpu"

View File

@@ -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

View File

@@ -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()
}

View File

@@ -36,5 +36,4 @@ func (j *XrayTrafficJob) Run() {
if needRestart0 || needRestart1 {
j.xrayService.SetToNeedRestart()
}
}