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
diff options
context:
space:
mode:
authorMehdi Khodayari <khodayari.khoram@gmail.com>2024-02-03 13:41:57 +0300
committerGitHub <noreply@github.com>2024-02-03 13:41:57 +0300
commit98dd6bb9493c989d2947dce3beda030a331805ec (patch)
tree524042efe30879f6f8d93e15da31d38f6bc21e23
parentf0e9aa0b8f1b81aef76246fb7ea449dc9236deba (diff)
This modification uses a Scanner to read the file line by line, which can be more memory-efficient for large files. (#1736)
-rw-r--r--web/job/check_client_ip_job.go31
1 files changed, 21 insertions, 10 deletions
diff --git a/web/job/check_client_ip_job.go b/web/job/check_client_ip_job.go
index b393e68d..ecfd5abb 100644
--- a/web/job/check_client_ip_job.go
+++ b/web/job/check_client_ip_job.go
@@ -1,7 +1,9 @@
package job
import (
+ "bufio"
"encoding/json"
+ "io"
"log"
"os"
"os/exec"
@@ -97,12 +99,16 @@ func (j *CheckClientIpJob) processLogFile() {
return
}
- data, err := os.ReadFile(accessLogPath)
- InboundClientIps := make(map[string][]string)
+ file, err := os.Open(accessLogPath)
j.checkError(err)
+ defer file.Close()
+
+ InboundClientIps := make(map[string][]string)
+
+ scanner := bufio.NewScanner(file)
+ for scanner.Scan() {
+ line := scanner.Text()
- lines := strings.Split(string(data), "\n")
- for _, line := range lines {
ipRegx, _ := regexp.Compile(`[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+`)
emailRegx, _ := regexp.Compile(`email:.+`)
@@ -131,6 +137,8 @@ func (j *CheckClientIpJob) processLogFile() {
}
}
+ j.checkError(scanner.Err())
+
shouldCleanLog := false
for clientEmail, ips := range InboundClientIps {
@@ -141,7 +149,6 @@ func (j *CheckClientIpJob) processLogFile() {
} else {
shouldCleanLog = j.updateInboundClientIps(inboundClientIps, clientEmail, ips)
}
-
}
// added delay before cleaning logs to reduce chance of logging IP that already has been banned
@@ -151,13 +158,17 @@ func (j *CheckClientIpJob) processLogFile() {
// copy access log to persistent file
logAccessP, err := os.OpenFile(xray.GetAccessPersistentLogPath(), os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644)
j.checkError(err)
- input, err := os.ReadFile(accessLogPath)
- j.checkError(err)
- if _, err := logAccessP.Write(input); err != nil {
- j.checkError(err)
- }
defer logAccessP.Close()
+ // reopen the access log file for reading
+ file, err := os.Open(accessLogPath)
+ j.checkError(err)
+ defer file.Close()
+
+ // copy access log content to persistent file
+ _, err = io.Copy(logAccessP, file)
+ j.checkError(err)
+
// clean access log
if err := os.Truncate(xray.GetAccessLogPath(), 0); err != nil {
j.checkError(err)