-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog4j-collector.go
97 lines (81 loc) · 2.23 KB
/
log4j-collector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"os"
"path/filepath"
"time"
"github.com/bluestoneag/log4j-collector/database"
"github.com/bluestoneag/log4j-collector/models"
"github.com/bluestoneag/log4j-collector/routes"
"github.com/bluestoneag/log4j-collector/util"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
var (
dbName string
dbPath string
dbLocation string
err error
)
func init() {
util.InitLoggers()
dbName = os.Getenv("DB_NAME")
if dbName == "" {
util.WarningLogger.Println("DB_NAME is not set, setting it to default value: log4j-collector.db")
dbName = "log4j-collector.db"
}
dbPath = os.Getenv("DB_PATH")
if dbPath == "" {
util.WarningLogger.Println("DB_PATH is not set, setting it to default value: ./db")
dbPath = "./db"
}
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
util.WarningLogger.Println("DB_PATH does not exist, creating it")
err = os.MkdirAll(dbPath, 0755)
if err != nil {
util.ErrorLogger.Println("Failed to create DB_PATH")
os.Exit(1)
}
}
dbLocation = filepath.Join(dbPath, filepath.Base(dbName))
util.InfoLogger.Printf("DB_LOCATION: %s", dbLocation)
_, err = os.Stat(dbLocation)
if os.IsNotExist(err) {
util.WarningLogger.Println("Database file does not exist, creating it")
file, err := os.Create(dbLocation)
if err != nil {
util.ErrorLogger.Println(err)
os.Exit(1)
}
defer file.Close()
} else if err != nil {
currentTime := time.Now().Local()
err = os.Chtimes(dbLocation, currentTime, currentTime)
if err != nil {
util.ErrorLogger.Println(err)
os.Exit(1)
}
}
database.DBConn, err = gorm.Open(sqlite.Open(dbLocation), &gorm.Config{})
if err != nil {
util.ErrorLogger.Println(err)
os.Exit(1)
}
database.DBConn.AutoMigrate(&models.Report{})
database.DBConn.AutoMigrate(&models.VulnerableFile{})
}
func main() {
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("github.com/bluestoneag/log4j-collector")
})
app.Use(cors.New(cors.Config{
AllowOrigins: "*",
AllowHeaders: "Origin, Content-Type, Accept",
}))
routes.Setup(app)
util.InfoLogger.Printf("Started web server on port 8080")
app.Listen(":8080")
// defer database.DBConn.Close()
}