/
gorcon-arma.go
267 lines (239 loc) · 6.46 KB
/
gorcon-arma.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
package main
import (
"bufio"
"flag"
"fmt"
"io"
"net"
"os"
"path"
"runtime"
"time"
rcon "github.com/playnet-public/gorcon-arma/bercon"
"github.com/playnet-public/gorcon-arma/procwatch"
"github.com/golang/glog"
"github.com/spf13/viper"
)
const (
parameterMaxprocs = "maxprocs"
)
var (
maxprocsPtr = flag.Int(parameterMaxprocs, runtime.NumCPU(), "max go procs")
)
var cfg *viper.Viper
func main() {
defer glog.Flush()
glog.CopyStandardLogTo("info")
flag.Parse()
fmt.Println("-- PlayNet GoRcon-ArmA - OpenSource Server Manager --")
fmt.Println("Version:", version)
fmt.Println("SourceCode: http://bit.ly/gorcon-code")
fmt.Println("Tasks: http://bit.ly/gorcon-issues")
fmt.Println("")
fmt.Println("This project is work in progress - Use at your own risk")
fmt.Println("--")
fmt.Println("")
fmt.Printf("Using %d go procs\n", *maxprocsPtr)
runtime.GOMAXPROCS(*maxprocsPtr)
if err := do(); err != nil {
glog.Fatal(err)
}
}
func do() error {
cfg = getConfig()
useSched := cfg.GetBool("scheduler.enabled")
useWatch := cfg.GetBool("watcher.enabled")
useRcon := cfg.GetBool("arma.enabled")
logToFile := cfg.GetBool("watcher.logToFile")
logFolder := cfg.GetString("watcher.logFolder")
logToConsole := cfg.GetBool("watcher.logToConsole")
showChat := cfg.GetBool("arma.showChat")
showEvents := cfg.GetBool("arma.showEvents")
quit := make(chan int)
var err error
var watcher *procwatch.Watcher
var client *rcon.Client
var cmdChan chan string
var stdout io.ReadCloser
var stderr io.ReadCloser
consoleOut, consoleIn := io.Pipe()
go streamConsole(consoleOut)
// TODO: Refactor so scheduler and watcher are enabled separately
if useSched || useWatch {
glog.V(4).Infoln("Starting Procwatch")
watcher, err = runWatcher(useSched, useWatch)
if err != nil {
return err
}
glog.V(4).Infoln("Retrieving Procwatch Command Channel")
cmdChan = watcher.GetCmdChannel()
glog.V(4).Infoln("Retrieving Procwatch Output Channels")
stderr, stdout = watcher.GetOutput()
if logToFile && useWatch {
go runFileLogger(stdout, stderr, logFolder)
}
if logToConsole && useWatch {
go runConsoleLogger(stdout, stderr, consoleIn)
}
} else {
fmt.Println("Scheduler is disabled")
}
if useRcon {
fmt.Println("RCon is enabled")
client, err = runRcon()
if err != nil {
return err
}
if useSched {
go pipeCommands(cmdChan, client, nil)
}
if showChat {
client.SetChatWriter(consoleIn)
}
if showEvents {
client.SetEventWriter(consoleIn)
}
client.RunCommand("say -1 PlayNet GoRcon-ArmA Connected", nil)
} else {
fmt.Println("RCon is disabled")
}
q := <-quit
if q == 1 {
return nil
}
return nil
}
func runWatcher(useSched, useWatch bool) (watcher *procwatch.Watcher, err error) {
var armaPath string
var armaParam []string
var schedulerEntity *procwatch.Schedule
if useSched {
schedulerPath := procwatch.SchedulePath(cfg.GetString("scheduler.path"))
schedulerEntity, err = schedulerPath.Parse()
if err != nil {
return
}
fmt.Println("\nScheduler is enabled")
fmt.Printf("\nScheduler Config: \n"+
"Path to scheduler.json: %v \n",
schedulerPath)
} else {
schedulerEntity = &procwatch.Schedule{}
}
if useWatch {
armaPath = cfg.GetString("watcher.path")
armaParam = cfg.GetStringSlice("watcher.params")
fmt.Println("\nWatcher is enabled")
fmt.Printf("\nWatcher Config: \n"+
"Path to ArmA Executable: %v \n"+
"ArmA Parameters: %v \n\n",
armaPath, armaParam)
}
pwcfg := procwatch.Cfg{
A3exe: armaPath,
A3par: armaParam,
Schedule: *schedulerEntity,
UseScheduler: useSched,
UseWatcher: useWatch,
}
watcher = procwatch.New(pwcfg)
watcher.Start()
return
}
func runRcon() (*rcon.Client, error) {
armaIP := cfg.GetString("arma.ip")
armaPort := cfg.GetString("arma.port")
armaPassword := cfg.GetString("arma.password")
armaKeepAliveTimer := cfg.GetInt("arma.keepAliveTimer")
armaKeepAliveTolerance := cfg.GetInt64("arma.keepAliveTolerance")
udpadr, err := net.ResolveUDPAddr("udp", armaIP+":"+armaPort)
if err != nil {
glog.Errorln("Could not convert ArmA IP and Port")
return nil, err
}
fmt.Printf("\nRCon Config: \n"+
"ArmA Server Address: %v \n"+
"ArmA Server Port: %v \n"+
"KeepAliveTimer: %v \n"+
"KeepAliveTolerance: %v \n\n",
armaIP, armaPort, armaKeepAliveTimer, armaKeepAliveTolerance)
becfg := rcon.Config{
Addr: udpadr,
Password: armaPassword,
KeepAliveTimer: armaKeepAliveTimer,
KeepAliveTolerance: armaKeepAliveTolerance,
}
client := rcon.New(becfg)
fmt.Println("Establishing Connection to Server")
client.WatcherLoop()
return client, nil
}
func runFileLogger(stdout, stderr io.ReadCloser, logFolder string) {
t := time.Now()
logFileName := fmt.Sprintf("server_log_%v%d%v_%v-%v-%v.log", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())
fmt.Println("Creating Server Logfile: ", logFileName)
_ = os.Mkdir(logFolder, 0775)
logFile, err := os.OpenFile(path.Join(logFolder, logFileName), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
//logFile, err := os.Create(path.Join(logFolder, logFileName))
if err != nil {
panic(err)
}
defer logFile.Close()
writer := io.MultiWriter(logFile)
close := make(chan int)
go func() {
_, err := io.Copy(writer, stdout)
if err != nil {
glog.Errorln(err)
}
close <- 1
}()
go func() {
_, err := io.Copy(writer, stderr)
if err != nil {
glog.Errorln(err)
}
close <- 1
}()
<-close
glog.Warningln("File Logger Closed which is unexpected")
}
func runConsoleLogger(stdout, stderr io.ReadCloser, console io.Writer) {
std := io.MultiReader(stderr, stdout)
go io.Copy(console, std)
}
func streamConsole(consoleOut io.Reader) error {
consoleScanner := bufio.NewScanner(consoleOut)
for consoleScanner.Scan() {
t := time.Now()
timestamp := t.Format("2006-01-02 15:04:05")
fmt.Println(timestamp, consoleScanner.Text())
}
if err := consoleScanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "There was an error with the consoleScanner", err)
return err
}
return nil
}
func pipeCommands(cmdChan chan string, c *rcon.Client, w io.WriteCloser) {
for {
glog.V(10).Infoln("Looping pipeCommands")
cmd := <-cmdChan
if len(cmd) != 0 {
c.RunCommand(cmd, w)
}
}
}
func getConfig() *viper.Viper {
cfg := viper.New()
cfg.SetConfigName("config")
cfg.AddConfigPath(".")
glog.V(1).Infof("Reading Config")
err := cfg.ReadInConfig()
if err != nil {
message := fmt.Sprintf("Loading Config failed with Error: %v", err.Error())
glog.Errorln(message)
panic(message)
}
return cfg
}