Iplimit - improved

Ensure accurate extraction of email.
Access logs are needed when the IP limit feature is active.
This commit is contained in:
mhsanaei 2024-09-12 09:44:17 +02:00
parent 663cf5649f
commit 374d49eb92

View File

@ -36,10 +36,11 @@ func (j *CheckClientIpJob) Run() {
} }
shouldClearAccessLog := false shouldClearAccessLog := false
iplimitActive := j.hasLimitIp()
f2bInstalled := j.checkFail2BanInstalled() f2bInstalled := j.checkFail2BanInstalled()
isAccessLogAvailable := j.checkAccessLogAvailable() isAccessLogAvailable := j.checkAccessLogAvailable(iplimitActive)
if j.hasLimitIp() { if iplimitActive {
if f2bInstalled && isAccessLogAvailable { if f2bInstalled && isAccessLogAvailable {
shouldClearAccessLog = j.processLogFile() shouldClearAccessLog = j.processLogFile()
} else { } else {
@ -123,7 +124,7 @@ func (j *CheckClientIpJob) processLogFile() bool {
line := scanner.Text() line := scanner.Text()
ipRegx, _ := regexp.Compile(`from \[?([0-9a-fA-F:.]+)\]?:\d+ accepted`) ipRegx, _ := regexp.Compile(`from \[?([0-9a-fA-F:.]+)\]?:\d+ accepted`)
emailRegx, _ := regexp.Compile(`email:.+`) emailRegx, _ := regexp.Compile(`email: (\S+)$`)
matches := ipRegx.FindStringSubmatch(line) matches := ipRegx.FindStringSubmatch(line)
if len(matches) > 1 { if len(matches) > 1 {
@ -136,7 +137,7 @@ func (j *CheckClientIpJob) processLogFile() bool {
if matchesEmail == "" { if matchesEmail == "" {
continue continue
} }
matchesEmail = strings.TrimSpace(strings.Split(matchesEmail, "email: ")[1]) matchesEmail = strings.Split(matchesEmail, "email: ")[1]
if InboundClientIps[matchesEmail] != nil { if InboundClientIps[matchesEmail] != nil {
if j.contains(InboundClientIps[matchesEmail], ip) { if j.contains(InboundClientIps[matchesEmail], ip) {
@ -174,19 +175,20 @@ func (j *CheckClientIpJob) checkFail2BanInstalled() bool {
return err == nil return err == nil
} }
func (j *CheckClientIpJob) checkAccessLogAvailable() bool { func (j *CheckClientIpJob) checkAccessLogAvailable(iplimitActive bool) bool {
isAvailable := true
accessLogPath, err := xray.GetAccessLogPath() accessLogPath, err := xray.GetAccessLogPath()
if err != nil { if err != nil {
return false return false
} }
switch accessLogPath { if accessLogPath == "none" || accessLogPath == "" {
case "none", "": if iplimitActive {
isAvailable = false logger.Warning("Access log path is not set, and IP limit is active. Please configure the access log path.")
}
return false
} }
return isAvailable return true
} }
func (j *CheckClientIpJob) checkError(e error) { func (j *CheckClientIpJob) checkError(e error) {