-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
349 lines (280 loc) · 7.53 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/**
* @file main.go
* @author Mikhail Klementyev jollheef<AT>riseup.net
* @license GNU AGPLv3
* @date October, 2015
* @brief task-based ctf daemon
*
* Entry point for task-based ctf daemon
*/
package main
import (
"database/sql"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"syscall"
"time"
"github.com/jollheef/henhouse/config"
"github.com/jollheef/henhouse/db"
"github.com/jollheef/henhouse/game"
"github.com/jollheef/henhouse/scoreboard"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
var (
configPath = kingpin.Arg("config",
"Path to configuration file.").Required().String()
dbReinit = kingpin.Flag("reinit", "Reinit database.").Bool()
)
var (
// CommitID fill in ./build.sh
CommitID string
// BuildDate fill in ./build.sh
BuildDate string
// BuildTime fill in ./build.sh
BuildTime string
)
func fillTranslateFallback(task * config.Task) {
if task.NameEn == "" {
task.NameEn = task.Name
}
if task.Name == "" {
task.Name = task.NameEn
}
if task.DescriptionEn == "" {
task.DescriptionEn = task.Description
}
if task.Description == "" {
task.Description = task.DescriptionEn
}
return
}
func reinitDatabase(database *sql.DB, cfg config.Config) (err error) {
log.Println("Reinit database")
for _, team := range cfg.Teams {
log.Println("Add team", team.Name)
err = db.AddTeam(database, &db.Team{
Name: team.Name,
Desc: team.Description,
Token: team.Token,
Test: team.Test,
})
if err != nil {
return
}
}
entries, err := ioutil.ReadDir(cfg.TaskDir)
if err != nil {
return
}
var categories []db.Category
for _, entry := range entries {
if entry.IsDir() {
continue
}
var content []byte
content, err = ioutil.ReadFile(cfg.TaskDir + "/" +
entry.Name())
if err != nil {
return
}
var task config.Task
task, err = config.ParseXMLTask(content)
if err != nil {
return
}
var finded bool
var taskCategory db.Category
for _, cat := range categories {
if cat.Name == task.Category {
finded = true
taskCategory = cat
break
}
}
if !finded {
taskCategory.Name = task.Category
err = db.AddCategory(database, &taskCategory)
if err != nil {
return
}
categories = append(categories, taskCategory)
log.Println("Add category", taskCategory.Name)
}
fillTranslateFallback(&task)
err = db.AddTask(database, &db.Task{
Name: task.Name,
Desc: task.Description,
NameEn: task.NameEn,
DescEn: task.DescriptionEn,
Tags: task.Tags,
CategoryID: taskCategory.ID,
Level: task.Level,
Flag: task.Flag,
Price: 500, // TODO support non-shared task
Shared: true, // TODO support non-shared task
MaxSharePrice: 500, // TODO support value from xml
MinSharePrice: 100, // TODO support value from xml
Opened: false, // by default task is closed
Author: task.Author,
ForceClosed: task.ForceClosed,
})
log.Println("Add task", task.Name)
if err != nil {
return
}
}
return
}
func checkTaskPrices(cfg *config.Config)(err error){
if cfg.TaskPrice.P200 == 0 || cfg.TaskPrice.P300 == 0 ||
cfg.TaskPrice.P400 == 0 || cfg.TaskPrice.P500 == 0 {
err = errors.New("Error: Task price not setted")
}
return
}
func initGame(database *sql.DB, cfg config.Config) (err error) {
var teamBase float64
if cfg.TaskPrice.UseNonLinear {
teamBase, err = game.CalcTeamsBase(database)
if err != nil {
return
}
log.Println("Use teams amount based on session counter")
} else if cfg.TaskPrice.UseTeamsBase {
teamBase = float64(cfg.TaskPrice.TeamsBase)
log.Println("Set teams base to", cfg.TaskPrice.TeamsBase)
} else {
teamBase = float64(len(cfg.Teams))
log.Println("Use teams amount as teams base")
}
log.Println("Start game at", cfg.Game.Start.Time)
log.Println("End game at", cfg.Game.End.Time)
g, err := game.NewGame(database, cfg.Game.Start.Time,
cfg.Game.End.Time, teamBase)
if err != nil {
return
}
if cfg.TaskPrice.UseNonLinear {
go g.TeamsBaseUpdater(database,
cfg.Scoreboard.RecalcTimeout.Duration)
}
err = checkTaskPrices(&cfg)
if err != nil{
return
}
fmt := "Set task price %d if solved less than %d%%\n"
log.Printf(fmt, 200, cfg.TaskPrice.P200)
log.Printf(fmt, 300, cfg.TaskPrice.P300)
log.Printf(fmt, 400, cfg.TaskPrice.P400)
log.Printf(fmt, 500, cfg.TaskPrice.P500)
g.SetTaskPrice(cfg.TaskPrice.P500, cfg.TaskPrice.P400,
cfg.TaskPrice.P300, cfg.TaskPrice.P200)
log.Println("Set task open timeout to", cfg.Task.OpenTimeout.Duration)
g.OpenTimeout = cfg.Task.OpenTimeout.Duration
if cfg.Task.AutoOpen {
log.Println("Auto open tasks after",
cfg.Task.AutoOpenTimeout.Duration)
} else {
log.Println("Auto open tasks disabled")
}
g.AutoOpen = cfg.Task.AutoOpen
g.AutoOpenTimeout = cfg.Task.AutoOpenTimeout.Duration
go g.Run()
infoD := cfg.WebsocketTimeout.Info.Duration
if infoD != 0 {
scoreboard.InfoTimeout = infoD
}
log.Println("Update info timeout:", scoreboard.InfoTimeout)
scoreboardD := cfg.WebsocketTimeout.Scoreboard.Duration
if scoreboardD != 0 {
scoreboard.ScoreboardTimeout = scoreboardD
}
log.Println("Update scoreboard timeout:", scoreboard.ScoreboardTimeout)
tasksD := cfg.WebsocketTimeout.Tasks.Duration
if tasksD != 0 {
scoreboard.TasksTimeout = tasksD
}
log.Println("Update tasks timeout:", scoreboard.TasksTimeout)
flagSendD := cfg.Flag.SendTimeout.Duration
if flagSendD != 0 {
scoreboard.FlagTimeout = flagSendD
}
log.Println("Flag timeout:", scoreboard.FlagTimeout)
scoreboardRecalcD := cfg.Scoreboard.RecalcTimeout.Duration
if scoreboardRecalcD != 0 {
scoreboard.ScoreboardRecalcTimeout = scoreboardRecalcD
}
log.Println("Score recalc timeout:", scoreboard.ScoreboardRecalcTimeout)
log.Println("Use html files from", cfg.Scoreboard.WwwPath)
log.Println("Listen at", cfg.Scoreboard.Addr)
err = scoreboard.Scoreboard(database, &g,
cfg.Scoreboard.WwwPath,
cfg.Scoreboard.TemplatePath,
cfg.Scoreboard.Addr,
cfg.Scoreboard.UnderProxy)
return
}
func main() {
if len(CommitID) > 7 {
CommitID = CommitID[:7] // abbreviated commit hash
}
version := BuildDate + " " + CommitID +
" (Mikhail Klementyev <[email protected]>)"
kingpin.Version(version)
kingpin.Parse()
fmt.Println(version)
cfg, err := config.ReadConfig(*configPath)
if err != nil {
log.Fatalln("Cannot open config:", err)
}
logFile, err := os.OpenFile(cfg.LogFile,
os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalln("Cannot open file:", err)
}
defer logFile.Close()
log.SetOutput(logFile)
log.Println(version)
var rlim syscall.Rlimit
err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim)
if err != nil {
log.Fatalln("Getrlimit fail:", err)
}
log.Println("RLIMIT_NOFILE CUR:", rlim.Cur, "MAX:", rlim.Max)
var database *sql.DB
if *dbReinit {
if cfg.Database.SafeReinit {
if time.Now().After(cfg.Game.Start.Time) {
log.Fatalln("Reinit after start not allowed")
}
}
database, err = db.InitDatabase(cfg.Database.Connection)
if err != nil {
log.Fatalln("Error:", err)
}
err = db.CleanDatabase(database)
if err != nil {
log.Fatalln("Error:", err)
}
defer database.Close()
err = reinitDatabase(database, cfg)
if err != nil {
log.Fatalln("Error:", err)
}
} else {
database, err = db.OpenDatabase(cfg.Database.Connection)
if err != nil {
log.Fatalln("Error:", err)
}
defer database.Close()
}
log.Println("Set max db connections to", cfg.Database.MaxConnections)
database.SetMaxOpenConns(cfg.Database.MaxConnections)
err = initGame(database, cfg)
if err != nil {
log.Fatalln("Error:", err)
}
}