Write access.log to accessp.log on truncate

Optimised launching of fail2ban
This commit is contained in:
somebodywashere 2023-06-23 18:01:13 +00:00
parent 63274d1bd5
commit 962f2e8eb0
2 changed files with 23 additions and 6 deletions

View file

@ -32,13 +32,18 @@ func (j *CheckClientIpJob) Run() {
logger.Debug("Check Client IP Job...")
if hasLimitIp() {
//create log file for Fail2ban IP Limit
logIpFile, err := os.OpenFile("/var/log/3xipl.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644)
if err != nil {
logger.Errorf("Failed to create or open IP Limit Log file: %s", err)
}
checkError(err)
defer logIpFile.Close()
log.SetOutput(logIpFile)
log.SetFlags(log.LstdFlags)
//create file to collect access.log to another file accessp.log (p=persistent)
logAccessP, err := os.OpenFile("/usr/local/x-ui/accessp.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644)
checkError(err)
defer logAccessP.Close()
processLogFile()
}
@ -140,6 +145,12 @@ func processLogFile() {
time.Sleep(time.Second * 3)
//added 3 seconds delay before cleaning logs to reduce chance of logging IP that already has been banned
if shouldCleanLog {
//copy log
input, err := os.ReadFile(accessLogPath)
checkError(err)
if err := os.WriteFile("/usr/local/x-ui/accessp.log", input, 0644); err != nil {
checkError(err)
}
// clean log
if err := os.Truncate(GetAccessLogPath(), 0); err != nil {
checkError(err)

12
x-ui.sh
View file

@ -733,7 +733,6 @@ install_iplimit() {
esac
echo -e "${green}Fail2ban installed successfully!${plain}\n"
else
sudo systemctl stop fail2ban
echo -e "${yellow}Fail2ban is already installed.${plain}\n"
fi
@ -788,9 +787,16 @@ actionunban = <iptables> -D f2b-<name> -s <ip> -j <blocktype>
[Init]
EOF
#Launching fail2ban
if ! sudo systemctl is-active --quiet fail2ban; then
sudo systemctl start fail2ban
echo test-start
else
systemctl restart fail2ban
echo test-restart
fi
sudo systemctl enable fail2ban
sudo systemctl start fail2ban
echo -e "${green}IP Limit installed and configured successfully!${plain}\n"
before_show_menu