From 68e1a43cd8d4eae9ea4b04627d7f2978ef1ad05f Mon Sep 17 00:00:00 2001 From: Hamidreza Ghavami <70919649+hamid-gh98@users.noreply.github.com> Date: Wed, 13 Mar 2024 03:12:09 +0330 Subject: [PATCH] [iplimit] fix access log path in settings service better to avoid hardcoding the access log path to enhance flexibility. not all users prefer the default './access.log' --- web/job/check_client_ip_job.go | 16 +++++++++++++--- web/service/setting.go | 18 ++++-------------- xray/process.go | 12 ++++++------ 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/web/job/check_client_ip_job.go b/web/job/check_client_ip_job.go index 4b799ab1..441f3521 100644 --- a/web/job/check_client_ip_job.go +++ b/web/job/check_client_ip_job.go @@ -58,8 +58,11 @@ func (j *CheckClientIpJob) clearAccessLog() { logAccessP, err := os.OpenFile(xray.GetAccessPersistentLogPath(), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644) j.checkError(err) + // get access log path to open it + accessLogPath, err := xray.GetAccessLogPath() + j.checkError(err) + // reopen the access log file for reading - accessLogPath := xray.GetAccessLogPath() file, err := os.Open(accessLogPath) j.checkError(err) @@ -114,7 +117,8 @@ func (j *CheckClientIpJob) checkFail2BanInstalled() bool { } func (j *CheckClientIpJob) processLogFile() bool { - accessLogPath := xray.GetAccessLogPath() + accessLogPath, err := xray.GetAccessLogPath() + j.checkError(err) file, err := os.Open(accessLogPath) j.checkError(err) @@ -171,9 +175,14 @@ func (j *CheckClientIpJob) processLogFile() bool { } func (j *CheckClientIpJob) checkAccessLogAvailable(doWarning bool) bool { - accessLogPath := xray.GetAccessLogPath() + accessLogPath, err := xray.GetAccessLogPath() + if err != nil { + return false + } + isAvailable := true warningMsg := "" + // access log is not available if it is set to 'none' or an empty string switch accessLogPath { case "none": @@ -183,6 +192,7 @@ func (j *CheckClientIpJob) checkAccessLogAvailable(doWarning bool) bool { warningMsg = "Access log doesn't exist in your Xray Configs" isAvailable = false } + if doWarning && warningMsg != "" { logger.Warning(warningMsg) } diff --git a/web/service/setting.go b/web/service/setting.go index 4a293dc0..7750b6be 100644 --- a/web/service/setting.go +++ b/web/service/setting.go @@ -17,6 +17,7 @@ import ( "x-ui/util/random" "x-ui/util/reflect_util" "x-ui/web/entity" + "x-ui/xray" ) //go:embed config.json @@ -460,22 +461,11 @@ func (s *SettingService) SetWarp(data string) error { } func (s *SettingService) GetIpLimitEnable() (bool, error) { - templateConfig, err := s.GetXrayConfigTemplate() + accessLogPath, err := xray.GetAccessLogPath() if err != nil { - return false, err + return false, nil } - - var xrayConfig map[string]interface{} - err = json.Unmarshal([]byte(templateConfig), &xrayConfig) - if err != nil { - return false, err - } - if logConfig, ok := xrayConfig["log"].(map[string]interface{}); ok { - if accessLogPath, ok := logConfig["access"].(string); ok { - return accessLogPath == "./access.log", nil - } - } - return false, nil + return (accessLogPath != "none" && accessLogPath != ""), nil } func (s *SettingService) UpdateAllSetting(allSetting *entity.AllSetting) error { diff --git a/xray/process.go b/xray/process.go index fcbe6f78..3203d7cd 100644 --- a/xray/process.go +++ b/xray/process.go @@ -57,28 +57,28 @@ func GetAccessPersistentPrevLogPath() string { return config.GetLogFolder() + "/3xipl-ap.prev.log" } -func GetAccessLogPath() string { +func GetAccessLogPath() (string, error) { config, err := os.ReadFile(GetConfigPath()) if err != nil { logger.Warningf("Something went wrong: %s", err) + return "", err } jsonConfig := map[string]interface{}{} err = json.Unmarshal([]byte(config), &jsonConfig) if err != nil { logger.Warningf("Something went wrong: %s", err) + return "", err } if jsonConfig["log"] != nil { jsonLog := jsonConfig["log"].(map[string]interface{}) if jsonLog["access"] != nil { - accessLogPath := jsonLog["access"].(string) - - return accessLogPath + return accessLogPath, nil } } - return "" + return "", err } func stopProcess(p *Process) { @@ -203,7 +203,7 @@ func (p *process) Start() (err error) { return common.NewErrorf("Failed to generate xray configuration file: %v", err) } - err = os.MkdirAll(config.GetLogFolder(), 0770) + err = os.MkdirAll(config.GetLogFolder(), 0o770) if err != nil { logger.Warningf("Something went wrong: %s", err) }