mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-10-09 07:52:11 +07:00
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:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user