mirror of
				https://github.com/MHSanaei/3x-ui.git
				synced 2025-11-04 06:12:52 +00:00 
			
		
		
		
	[iplimit] fix access log path in settings service (#2044)
* [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' * [iplimit] fix iplimit
This commit is contained in:
		
							parent
							
								
									97489e743a
								
							
						
					
					
						commit
						569c9428fb
					
				
					 3 changed files with 31 additions and 32 deletions
				
			
		| 
						 | 
					@ -44,7 +44,7 @@ func (j *CheckClientIpJob) Run() {
 | 
				
			||||||
			shouldClearAccessLog = 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("[iplimit] fail2ban is not installed. IP limiting may not work properly.")
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -58,8 +58,11 @@ func (j *CheckClientIpJob) clearAccessLog() {
 | 
				
			||||||
	logAccessP, err := os.OpenFile(xray.GetAccessPersistentLogPath(), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
 | 
						logAccessP, err := os.OpenFile(xray.GetAccessPersistentLogPath(), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
 | 
				
			||||||
	j.checkError(err)
 | 
						j.checkError(err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// get access log path to open it
 | 
				
			||||||
 | 
						accessLogPath, err := xray.GetAccessLogPath()
 | 
				
			||||||
 | 
						j.checkError(err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// reopen the access log file for reading
 | 
						// reopen the access log file for reading
 | 
				
			||||||
	accessLogPath := xray.GetAccessLogPath()
 | 
					 | 
				
			||||||
	file, err := os.Open(accessLogPath)
 | 
						file, err := os.Open(accessLogPath)
 | 
				
			||||||
	j.checkError(err)
 | 
						j.checkError(err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -106,15 +109,9 @@ func (j *CheckClientIpJob) hasLimitIp() bool {
 | 
				
			||||||
	return false
 | 
						return false
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (j *CheckClientIpJob) checkFail2BanInstalled() bool {
 | 
					 | 
				
			||||||
	cmd := "fail2ban-client"
 | 
					 | 
				
			||||||
	args := []string{"-h"}
 | 
					 | 
				
			||||||
	err := exec.Command(cmd, args...).Run()
 | 
					 | 
				
			||||||
	return err == nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (j *CheckClientIpJob) processLogFile() bool {
 | 
					func (j *CheckClientIpJob) processLogFile() bool {
 | 
				
			||||||
	accessLogPath := xray.GetAccessLogPath()
 | 
						accessLogPath, err := xray.GetAccessLogPath()
 | 
				
			||||||
 | 
						j.checkError(err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	file, err := os.Open(accessLogPath)
 | 
						file, err := os.Open(accessLogPath)
 | 
				
			||||||
	j.checkError(err)
 | 
						j.checkError(err)
 | 
				
			||||||
| 
						 | 
					@ -170,10 +167,21 @@ func (j *CheckClientIpJob) processLogFile() bool {
 | 
				
			||||||
	return shouldCleanLog
 | 
						return shouldCleanLog
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (j *CheckClientIpJob) checkAccessLogAvailable(doWarning bool) bool {
 | 
					func (j *CheckClientIpJob) checkFail2BanInstalled() bool {
 | 
				
			||||||
	accessLogPath := xray.GetAccessLogPath()
 | 
						cmd := "fail2ban-client"
 | 
				
			||||||
 | 
						args := []string{"-h"}
 | 
				
			||||||
 | 
						err := exec.Command(cmd, args...).Run()
 | 
				
			||||||
 | 
						return err == nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (j *CheckClientIpJob) checkAccessLogAvailable(handleWarning bool) bool {
 | 
				
			||||||
	isAvailable := true
 | 
						isAvailable := true
 | 
				
			||||||
	warningMsg := ""
 | 
						warningMsg := ""
 | 
				
			||||||
 | 
						accessLogPath, err := xray.GetAccessLogPath()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return false
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 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":
 | 
				
			||||||
| 
						 | 
					@ -183,7 +191,8 @@ func (j *CheckClientIpJob) checkAccessLogAvailable(doWarning bool) bool {
 | 
				
			||||||
		warningMsg = "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 != "" {
 | 
					
 | 
				
			||||||
 | 
						if handleWarning && warningMsg != "" {
 | 
				
			||||||
		logger.Warning(warningMsg)
 | 
							logger.Warning(warningMsg)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return isAvailable
 | 
						return isAvailable
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -17,6 +17,7 @@ import (
 | 
				
			||||||
	"x-ui/util/random"
 | 
						"x-ui/util/random"
 | 
				
			||||||
	"x-ui/util/reflect_util"
 | 
						"x-ui/util/reflect_util"
 | 
				
			||||||
	"x-ui/web/entity"
 | 
						"x-ui/web/entity"
 | 
				
			||||||
 | 
						"x-ui/xray"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//go:embed config.json
 | 
					//go:embed config.json
 | 
				
			||||||
| 
						 | 
					@ -460,22 +461,11 @@ func (s *SettingService) SetWarp(data string) error {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (s *SettingService) GetIpLimitEnable() (bool, error) {
 | 
					func (s *SettingService) GetIpLimitEnable() (bool, error) {
 | 
				
			||||||
	templateConfig, err := s.GetXrayConfigTemplate()
 | 
						accessLogPath, err := xray.GetAccessLogPath()
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return false, err
 | 
							return false, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						return (accessLogPath != "none" && accessLogPath != ""), 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
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (s *SettingService) UpdateAllSetting(allSetting *entity.AllSetting) error {
 | 
					func (s *SettingService) UpdateAllSetting(allSetting *entity.AllSetting) error {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -57,28 +57,28 @@ func GetAccessPersistentPrevLogPath() string {
 | 
				
			||||||
	return config.GetLogFolder() + "/3xipl-ap.prev.log"
 | 
						return config.GetLogFolder() + "/3xipl-ap.prev.log"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func GetAccessLogPath() string {
 | 
					func GetAccessLogPath() (string, error) {
 | 
				
			||||||
	config, err := os.ReadFile(GetConfigPath())
 | 
						config, err := os.ReadFile(GetConfigPath())
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		logger.Warningf("Something went wrong: %s", err)
 | 
							logger.Warningf("Something went wrong: %s", err)
 | 
				
			||||||
 | 
							return "", err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	jsonConfig := map[string]interface{}{}
 | 
						jsonConfig := map[string]interface{}{}
 | 
				
			||||||
	err = json.Unmarshal([]byte(config), &jsonConfig)
 | 
						err = json.Unmarshal([]byte(config), &jsonConfig)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		logger.Warningf("Something went wrong: %s", err)
 | 
							logger.Warningf("Something went wrong: %s", err)
 | 
				
			||||||
 | 
							return "", err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if jsonConfig["log"] != nil {
 | 
						if jsonConfig["log"] != nil {
 | 
				
			||||||
		jsonLog := jsonConfig["log"].(map[string]interface{})
 | 
							jsonLog := jsonConfig["log"].(map[string]interface{})
 | 
				
			||||||
		if jsonLog["access"] != nil {
 | 
							if jsonLog["access"] != nil {
 | 
				
			||||||
 | 
					 | 
				
			||||||
			accessLogPath := jsonLog["access"].(string)
 | 
								accessLogPath := jsonLog["access"].(string)
 | 
				
			||||||
 | 
								return accessLogPath, nil
 | 
				
			||||||
			return accessLogPath
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return ""
 | 
						return "", err
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func stopProcess(p *Process) {
 | 
					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)
 | 
							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 {
 | 
						if err != nil {
 | 
				
			||||||
		logger.Warningf("Something went wrong: %s", err)
 | 
							logger.Warningf("Something went wrong: %s", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue