-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathserver.go
426 lines (358 loc) · 11.4 KB
/
server.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package factorio
import (
"bufio"
"encoding/json"
"errors"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"github.com/OpenFactorioServerManager/factorio-server-manager/api/websocket"
"github.com/OpenFactorioServerManager/factorio-server-manager/bootstrap"
"github.com/OpenFactorioServerManager/rcon"
)
type Server struct {
Cmd *exec.Cmd `json:"-"`
Savefile string `json:"savefile"`
Latency int `json:"latency"`
BindIP string `json:"bindip"`
Port int `json:"port"`
DefaultPort string `json:"default_port"`
PortLock bool `json:"port_lock"`
Running bool `json:"running"`
Version Version `json:"fac_version"`
BaseModVersion string `json:"base_mod_version"`
StdOut io.ReadCloser `json:"-"`
StdErr io.ReadCloser `json:"-"`
StdIn io.WriteCloser `json:"-"`
Settings map[string]interface{} `json:"-"`
Rcon *rcon.RemoteConsole `json:"-"`
LogChan chan []string `json:"-"`
}
var instantiated Server
var once sync.Once
func (server *Server) SetRunning(newState bool) {
if server.Running != newState {
log.Println("new state, will also send to correct room")
server.Running = newState
wsRoom := websocket.WebsocketHub.GetRoom("server_status")
response, _ := json.Marshal(server)
wsRoom.Send(string(response))
}
}
func (server *Server) GetRunning() bool {
return server.Running
}
func (server *Server) autostart() {
var err error
if server.BindIP == "" {
server.BindIP = "0.0.0.0"
}
if server.Port == 0 {
server.Port = 34197
}
server.Savefile = "Load Latest"
err = server.Run()
if err != nil {
log.Printf("Error starting Factorio server: %+v", err)
return
}
}
func SetFactorioServer(server Server) {
instantiated = server
}
func NewFactorioServer() (err error) {
server := Server{}
server.Settings = make(map[string]interface{})
config := bootstrap.GetConfig()
if err = os.MkdirAll(config.FactorioConfigDir, 0755); err != nil {
log.Printf("failed to create config directory: %v", err)
return
}
settingsPath := config.SettingsFile
var settings *os.File
if _, err = os.Stat(settingsPath); os.IsNotExist(err) {
// copy example settings to supplied settings file, if not exists
log.Printf("Server settings at %s not found, copying example server settings.\n", settingsPath)
examplePath := filepath.Join(config.FactorioDir, "data", "server-settings.example.json")
var example *os.File
example, err = os.Open(examplePath)
if err != nil {
log.Printf("failed to open example server settings: %v", err)
return
}
defer example.Close()
settings, err = os.Create(settingsPath)
if err != nil {
log.Printf("failed to create server settings file: %v", err)
return
}
defer settings.Close()
_, err = io.Copy(settings, example)
if err != nil {
log.Printf("failed to copy example server settings: %v", err)
return
}
err = example.Close()
if err != nil {
log.Printf("failed to close example server settings: %s", err)
return
}
} else {
// otherwise, open file normally
settings, err = os.Open(settingsPath)
if err != nil {
log.Printf("failed to open server settings file: %v", err)
return
}
defer settings.Close()
}
// before reading reset offset
if _, err = settings.Seek(0, 0); err != nil {
log.Printf("error while seeking in settings file: %v", err)
return
}
if err = json.NewDecoder(settings).Decode(&server.Settings); err != nil {
log.Printf("error reading %s: %v", settingsPath, err)
return
}
log.Printf("Loaded Factorio settings from %s\n", settingsPath)
out := []byte{}
//Load factorio version
if config.GlibcCustom == "true" {
out, err = exec.Command(config.GlibcLocation, "--library-path", config.GlibcLibLoc, config.FactorioBinary, "--version").Output()
} else {
out, err = exec.Command(config.FactorioBinary, "--version").Output()
}
if err != nil {
log.Printf("error on loading factorio version: %s", err)
return
}
reg := regexp.MustCompile("Version.*?((\\d+\\.)?(\\d+\\.)?(\\*|\\d+)+)")
found := reg.FindStringSubmatch(string(out))
err = server.Version.UnmarshalText([]byte(found[1]))
if err != nil {
log.Printf("could not parse version: %v", err)
return
}
//Load baseMod version
baseModInfoFile := filepath.Join(config.FactorioBaseModDir, "info.json")
bmifBa, err := ioutil.ReadFile(baseModInfoFile)
if err != nil {
log.Printf("couldn't open baseMods info.json: %s", err)
return
}
var modInfo ModInfo
err = json.Unmarshal(bmifBa, &modInfo)
if err != nil {
log.Printf("error unmarshalling baseMods info.json to a modInfo: %s", err)
return
}
server.BaseModVersion = modInfo.Version
// load admins from additional file
if (server.Version.Greater(Version{0, 17, 0})) {
if _, err = os.Stat(config.FactorioAdminFile); os.IsNotExist(err) {
//save empty admins-file
err = ioutil.WriteFile(config.FactorioAdminFile, []byte("[]"), 0664)
server.Settings["admins"] = make([]string, 0)
} else {
var data []byte
data, err = ioutil.ReadFile(config.FactorioAdminFile)
if err != nil {
log.Printf("Error loading FactorioAdminFile: %s", err)
return
}
var jsonData interface{}
err = json.Unmarshal(data, &jsonData)
if err != nil {
log.Printf("Error unmarshalling FactorioAdminFile: %s", err)
return
}
server.Settings["admins"] = jsonData
}
}
SetFactorioServer(server)
// autostart factorio is configured to do so
if config.Autostart == "true" {
go instantiated.autostart()
}
return
}
func GetFactorioServer() (f *Server) {
return &instantiated
}
func (server *Server) Run() error {
var err error
config := bootstrap.GetConfig()
data, err := json.MarshalIndent(server.Settings, "", " ")
if err != nil {
log.Println("Failed to marshal FactorioServerSettings: ", err)
} else {
ioutil.WriteFile(config.SettingsFile, data, 0644)
}
saves, err := ListSaves(config.FactorioSavesDir)
if err != nil {
log.Println("Failed to get saves list: ", err)
}
if len(saves) == 0 {
return errors.New("No savefile exists on the server")
}
args := []string{}
//The factorio server refenences its executable-path, since we execute the ld.so file and pass the factorio binary as a parameter
//the game would use the path to the ld.so file as it's executable path and crash, to prevent this the parameter "--executable-path" is added
if config.GlibcCustom == "true" {
log.Println("Custom glibc selected, glibc.so location:", config.GlibcLocation, " lib location:", config.GlibcLibLoc)
args = append(args, "--library-path", config.GlibcLibLoc, config.FactorioBinary, "--executable-path", config.FactorioBinary)
}
game_port := strconv.Itoa(server.Port)
if (config.FactorioPortLock) {
// Force the game to run with --game-port when "factorio_port_lock" is set to true in conf.json.
game_port = config.FactorioPort
}
args = append(args,
"--bind", server.BindIP,
"--port", game_port,
"--server-settings", config.SettingsFile,
"--rcon-port", strconv.Itoa(config.FactorioRconPort),
"--rcon-password", config.FactorioRconPass)
if (server.Version.Greater(Version{0, 17, 0})) {
args = append(args, "--server-adminlist", config.FactorioAdminFile)
}
if server.Savefile == "Load Latest" {
args = append(args, "--start-server-load-latest")
} else {
args = append(args, "--start-server", filepath.Join(config.FactorioSavesDir, server.Savefile))
}
// Write chat log to a different file if requested (if not it will be mixed-in with the default logfile)
if config.ChatLogFile != "" {
args = append(args, "--console-log", config.ChatLogFile)
}
if config.GlibcCustom == "true" {
log.Println("Starting server with command: ", config.GlibcLocation, args)
server.Cmd = exec.Command(config.GlibcLocation, args...)
} else {
log.Println("Starting server with command: ", config.FactorioBinary, args)
server.Cmd = exec.Command(config.FactorioBinary, args...)
}
server.StdOut, err = server.Cmd.StdoutPipe()
if err != nil {
log.Printf("Error opening stdout pipe: %s", err)
return err
}
server.StdIn, err = server.Cmd.StdinPipe()
if err != nil {
log.Printf("Error opening stdin pipe: %s", err)
return err
}
server.StdErr, err = server.Cmd.StderrPipe()
if err != nil {
log.Printf("Error opening stderr pipe: %s", err)
return err
}
go server.parseRunningCommand(server.StdOut)
go server.parseRunningCommand(server.StdErr)
err = server.Cmd.Start()
if err != nil {
log.Printf("Factorio process failed to start: %s", err)
return err
}
server.SetRunning(true)
err = server.Cmd.Wait()
log.Printf("Factorio process is closed")
server.SetRunning(false)
if err != nil {
log.Printf("Factorio process exited with error: %s", err)
return err
}
return nil
}
func (server *Server) parseRunningCommand(std io.ReadCloser) (err error) {
stdScanner := bufio.NewScanner(std)
for stdScanner.Scan() {
text := stdScanner.Text()
log.Printf("Factorio Server: %s", text)
if err := server.writeLog(text); err != nil {
log.Printf("Error: %s", err)
}
// send the reported line per websocket
wsRoom := websocket.WebsocketHub.GetRoom("gamelog")
go wsRoom.Send(text)
line := strings.Fields(text)
// Ensure logline slice is in bounds
if len(line) > 1 {
// Check if Factorio Server reports any errors if so handle it
if line[1] == "Error" {
err := server.checkLogError(line)
if err != nil {
log.Printf("Error checking Factorio Server Error: %s", err)
}
}
// If rcon port opens indicated in log connect to rcon
rconLog := "Starting RCON interface at IP"
// check if slice index is greater than 2 to prevent panic
if len(line) > 2 {
// log line for opened rcon connection
if strings.Contains(text, rconLog) {
log.Printf("Rcon running on Factorio Server")
err = connectRC()
if err != nil {
log.Printf("Error: %s", err)
}
}
server.checkProcessHealth(text)
}
}
}
if err := stdScanner.Err(); err != nil {
log.Printf("Error reading std buffer: %s", err)
return err
}
return nil
}
func (server *Server) writeLog(logline string) error {
config := bootstrap.GetConfig()
logfileName := config.ConsoleLogFile
file, err := os.OpenFile(logfileName, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
log.Printf("Cannot open logfile %s for appending Factorio Server output: %s", logfileName, err)
return err
}
defer file.Close()
logline = logline + "\n"
if _, err = file.WriteString(logline); err != nil {
log.Printf("Error appending to %s: %s", logfileName, err)
return err
}
return nil
}
func (server *Server) checkLogError(logline []string) error {
// TODO Handle errors generated by running Factorio Server
log.Println(logline)
return nil
}
func init() {
websocket.WebsocketHub.RegisterControlHandler <- serverWebsocketControl
}
// react to websocket control messages and run the command if it is requested
func serverWebsocketControl(controls websocket.WsControls) {
log.Println(controls)
if controls.Type == "command" {
command := controls.Value
server := GetFactorioServer()
if server.GetRunning() {
log.Printf("Received command: %v", command)
reqId, err := server.Rcon.Write(command)
if err != nil {
log.Printf("Error sending rcon command: %s", err)
return
}
log.Printf("Command send to Factorio: %s, with rcon request id: %v", command, reqId)
}
}
}