[fix] iplimit - only warning access log if f2b is installed

This commit is contained in:
Hamidreza Ghavami 2024-03-11 00:36:37 +03:30
parent 15f75f1769
commit dcb57bbb91
No known key found for this signature in database
GPG key ID: 402C6797325182D9

View file

@ -35,13 +35,13 @@ func (j *CheckClientIpJob) Run() {
j.lastClear = time.Now().Unix() j.lastClear = time.Now().Unix()
} }
shouldClearAccessLog := false
f2bInstalled := j.checkFail2BanInstalled() f2bInstalled := j.checkFail2BanInstalled()
isAccessLogAvailable := j.checkAccessLogAvailable() isAccessLogAvailable := j.checkAccessLogAvailable(f2bInstalled)
clearAccessLog := false
if j.hasLimitIp() { if j.hasLimitIp() {
if f2bInstalled && isAccessLogAvailable { if f2bInstalled && isAccessLogAvailable {
clearAccessLog = j.processLogFile() shouldClearAccessLog = j.processLogFile()
} else { } else {
if !f2bInstalled { if !f2bInstalled {
logger.Warning("fail2ban is not installed. IP limiting may not work properly.") logger.Warning("fail2ban is not installed. IP limiting may not work properly.")
@ -49,13 +49,13 @@ func (j *CheckClientIpJob) Run() {
} }
} }
if clearAccessLog || isAccessLogAvailable && time.Now().Unix()-j.lastClear > 3600 { if shouldClearAccessLog || isAccessLogAvailable && time.Now().Unix()-j.lastClear > 3600 {
j.clearAccessLog() j.clearAccessLog()
} }
} }
func (j *CheckClientIpJob) 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) j.checkError(err)
// reopen the access log file for reading // reopen the access log file for reading
@ -170,18 +170,22 @@ func (j *CheckClientIpJob) processLogFile() bool {
return shouldCleanLog return shouldCleanLog
} }
func (j *CheckClientIpJob) checkAccessLogAvailable() bool { func (j *CheckClientIpJob) checkAccessLogAvailable(doWarning bool) bool {
accessLogPath := xray.GetAccessLogPath() accessLogPath := xray.GetAccessLogPath()
isAvailable := true isAvailable := true
warningMsg := ""
// access log is not available if it is set to 'none' or an empty string // access log is not available if it is set to 'none' or an empty string
switch accessLogPath { switch accessLogPath {
case "none": case "none":
logger.Warning("Access log is set to 'none', check your Xray Configs") warningMsg = "Access log is set to 'none', check your Xray Configs"
isAvailable = false isAvailable = false
case "": case "":
logger.Warning("Access log doesn't exist in your Xray Configs") warningMsg = "Access log doesn't exist in your Xray Configs"
isAvailable = false isAvailable = false
} }
if doWarning && warningMsg != "" {
logger.Warning(warningMsg)
}
return isAvailable return isAvailable
} }
@ -260,7 +264,7 @@ func (j *CheckClientIpJob) updateInboundClientIps(inboundClientIps *model.Inboun
j.disAllowedIps = []string{} j.disAllowedIps = []string{}
// create iplimit log file channel // 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 { if err != nil {
logger.Errorf("failed to create or open ip limit log file: %s", err) logger.Errorf("failed to create or open ip limit log file: %s", err)
} }