80 lines
1.7 KiB
Go
80 lines
1.7 KiB
Go
package internal
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
logFile *os.File
|
|
mu sync.Mutex
|
|
)
|
|
|
|
func InitLog() {
|
|
var err error
|
|
logFileName := os.Getenv("CCTV_LOG_FILE")
|
|
if logFileName == "" {
|
|
logFileName = "app.log" // Default log file name if the environment variable is not set
|
|
}
|
|
|
|
var logFilePath string
|
|
if runtime.GOOS == "windows" {
|
|
logFilePath = filepath.Join("C:\\ProgramData\\CCTV", logFileName)
|
|
} else {
|
|
logFilePath = filepath.Join("/var/log", logFileName)
|
|
}
|
|
|
|
// Ensure directory exists
|
|
err = os.MkdirAll(filepath.Dir(logFilePath), 0755)
|
|
if err != nil {
|
|
fmt.Println("Error creating log directory:", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
logFile, err = os.OpenFile(logFilePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
|
if err != nil {
|
|
fmt.Println("Error opening log file:", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func LogFailedAttempt(ip string) {
|
|
logEntry := fmt.Sprintf("[%s] Failed auth attempt from IP: %s\n", time.Now().Format(time.RFC3339), ip)
|
|
logFile.WriteString(logEntry)
|
|
logFile.Sync()
|
|
|
|
fmt.Print(logEntry)
|
|
}
|
|
|
|
func LogBan(ip string) {
|
|
logEntry := fmt.Sprintf("[%s] Banned IP: %s\n", time.Now().Format(time.RFC3339), ip)
|
|
logFile.WriteString(logEntry)
|
|
logFile.Sync()
|
|
|
|
fmt.Print(logEntry)
|
|
}
|
|
|
|
func LogSuccessUpload(ip string, fileName string) {
|
|
logEntry := fmt.Sprintf("[%s] Successfully uploaded file %s from IP: %s\n", time.Now().Format(time.RFC3339), fileName, ip)
|
|
logFile.WriteString(logEntry)
|
|
logFile.Sync()
|
|
|
|
fmt.Print(logEntry)
|
|
}
|
|
|
|
func LogFileServerRequest(ip string, fileName string) {
|
|
logEntry := fmt.Sprintf("[%s] File server request for %s from IP: %s\n", time.Now().Format(time.RFC3339), fileName, ip)
|
|
logFile.WriteString(logEntry)
|
|
logFile.Sync()
|
|
|
|
fmt.Print(logEntry)
|
|
}
|
|
|
|
func CloseLog() {
|
|
logFile.Close()
|
|
}
|