/
main.go
192 lines (148 loc) · 4.02 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
/**
* @file main.go
* @author Mikhail Klementyev jollheef<AT>riseup.net
* @license GNU AGPLv3
* @date September, 2015
* @brief contest checking system daemon
*
* Entry point for contest checking system daemon
*/
package main
import (
"database/sql"
"fmt"
"log"
"os"
"syscall"
"time"
"github.com/jollheef/tin_foil_hat/checker"
"github.com/jollheef/tin_foil_hat/config"
"github.com/jollheef/tin_foil_hat/pulse"
"github.com/jollheef/tin_foil_hat/receiver"
"github.com/jollheef/tin_foil_hat/scoreboard"
"github.com/jollheef/tin_foil_hat/steward"
"github.com/jollheef/tin_foil_hat/vexillary"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
var (
configPath = kingpin.Arg("config",
"Path to configuration file.").String()
dbReinit = kingpin.Flag("reinit", "Reinit database.").Bool()
)
var (
commitID string
buildDate string
buildTime string
)
func buildInfo() (str string) {
if len(commitID) > 7 {
commitID = commitID[:7] // abbreviated commit hash
}
str = fmt.Sprintf("Version: tin_foil_hat %s %s %s\n",
commitID, buildDate, buildTime)
str += "Author: Mikhail Klementyev <jollheef@riseup.net>\n"
return
}
func reinitDatabase(db *sql.DB, config config.Config) {
var err error
if config.Database.SafeReinit {
if time.Now().After(config.Pulse.Start.Time) {
log.Fatalln("Reinit after start not allowed")
}
}
log.Println("Reinit database")
log.Println("Clean database")
steward.CleanDatabase(db)
for _, team := range config.Teams {
log.Println("Add team", team.Name)
_, err = steward.AddTeam(db, team)
if err != nil {
log.Fatalln("Add team failed:", err)
}
}
for _, svc := range config.Services {
var network string
if svc.UDP {
network = "udp"
} else {
network = "tcp"
}
log.Printf("Add service %s (%s)\n", svc.Name, network)
err = steward.AddService(db, svc)
if err != nil {
log.Fatalln("Add service failed:", err)
}
}
}
func main() {
fmt.Println(buildInfo())
kingpin.Parse()
if *configPath == "" {
log.Println("Use default config path")
*configPath = "/etc/tinfoilhat/tinfoilhat.toml"
}
config, err := config.ReadConfig(*configPath)
if err != nil {
log.Fatalln("Cannot open config:", err)
}
logFile, err := os.OpenFile(config.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(buildInfo())
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)
db, err := steward.OpenDatabase(config.Database.Connection)
if err != nil {
log.Fatalln("Open database fail:", err)
}
defer db.Close()
db.SetMaxOpenConns(config.Database.MaxConnections)
if *dbReinit {
reinitDatabase(db, config)
}
checker.SetTimeout(config.CheckerTimeout.Duration)
if config.AdvisoryReceiver.Disabled {
scoreboard.DisableAdvisory()
}
priv, err := vexillary.GenerateKey()
if err != nil {
log.Fatalln("Generate key fail:", err)
}
attackFlow := make(chan scoreboard.Attack, config.API.AttackBuffer)
go receiver.FlagReceiver(db, priv, config.FlagReceiver.Addr,
config.FlagReceiver.ReceiveTimeout.Duration,
config.FlagReceiver.SocketTimeout.Duration,
attackFlow)
go receiver.AdvisoryReceiver(db, config.AdvisoryReceiver.Addr,
config.AdvisoryReceiver.ReceiveTimeout.Duration,
config.AdvisoryReceiver.SocketTimeout.Duration)
go scoreboard.Scoreboard(db, attackFlow,
config.Scoreboard.WwwPath,
config.Scoreboard.Addr,
config.Scoreboard.UpdateTimeout.Duration,
config.Pulse.Start.Time,
config.Pulse.Half.Duration,
config.Pulse.Lunch.Duration,
config.Pulse.DarkestTime.Duration)
err = pulse.Pulse(db, priv,
config.Pulse.Start.Time,
config.Pulse.Half.Duration,
config.Pulse.Lunch.Duration,
config.Pulse.RoundLen.Duration,
config.Pulse.CheckTimeout.Duration)
if err != nil {
log.Fatalln("Game error:", err)
}
log.Println("It's now safe to turn off you computer")
for {
time.Sleep(time.Hour)
}
}