-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
168 lines (128 loc) · 3.13 KB
/
main.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"errors"
"log"
"os"
"strconv"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/ztrue/tracerr"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type Config struct {
WebServerPort int `json:"web_server_port"`
ExternalURL string `json:"external_url"`
PreSharedKey string `json:"psk"`
DatabaseFilePath string `json:"db_file_path"`
}
type Ban struct {
gorm.Model
BannedUUID string
BannerUUID string
Duration time.Duration
}
type Session struct {
gorm.Model
PlayerUUID string
LastActiveAt time.Time
Token string
}
type CreateSession struct {
PlayerUUID string `json:"playerUniqueId"`
}
type CreatedSession struct {
Token string `json:"token"`
}
func generateRandomString(length int) string {
b := make([]byte, length)
_, err := rand.Read(b)
if err != nil {
panic(err)
}
return hex.EncodeToString(b)
}
func configExists() bool {
_, err := os.Stat("../config.json")
return err == nil
}
func getConfig() (*Config, error) {
config := new(Config)
data, fileError := os.ReadFile("../config.json")
if fileError != nil {
return nil, fileError
}
jsonError := json.Unmarshal(data, config)
if jsonError != nil {
return nil, jsonError
}
return config, nil
}
func createLogFile() {
file, _ := os.Create("./log.txt")
file.Close()
}
func appendToLogFile(msg string) {
file, _ := os.OpenFile("./log.txt", os.O_APPEND|os.O_WRONLY, os.ModePerm)
file.WriteString("[" + time.Now().Format(time.RFC1123) + "] " + msg)
file.WriteString("\n")
file.Close()
}
func main() {
// dsn := "root:localroot@tcp(127.0.0.1:3999)/bigbans?charset=utf8mb4&parseTime=True&loc=Local"
// db, dberr := gorm.Open(mysql.Open(dsn), &gorm.Config{
// NamingStrategy: schema.NamingStrategy{
// SingularTable: false,
// NoLowerCase: true,
// },
// })
createLogFile()
var config *Config
if configExists() {
var newConfig, configError = getConfig()
if configError != nil {
appendToLogFile(tracerr.SprintSource(configError))
return
}
config = newConfig
} else {
appendToLogFile(tracerr.SprintSource(errors.New("Config does not exist")))
return
}
db, dberr := gorm.Open(sqlite.Open(config.DatabaseFilePath), &gorm.Config{})
if dberr != nil {
panic("Failed to connect to local database")
}
db.AutoMigrate(&Ban{})
db.AutoMigrate(&Session{})
app := fiber.New(fiber.Config{})
app.Use(logger.New())
app.Use(cors.New())
app.Static("/", "public")
app.Post("/api/sessions", func(c *fiber.Ctx) error {
var psk = c.Get("X-PSK")
if psk != config.PreSharedKey {
return c.SendStatus(fiber.StatusUnauthorized)
}
var body = new(CreateSession)
var bodyError = c.BodyParser(&body)
if bodyError != nil {
return c.SendStatus(fiber.StatusBadRequest)
}
var session = Session{
PlayerUUID: body.PlayerUUID,
Token: generateRandomString(16),
}
db.Save(&session)
var createdSession = CreatedSession{
Token: session.Token,
}
return c.JSON(createdSession)
})
log.Fatal(app.Listen(":" + strconv.Itoa(config.WebServerPort)))
}