-
Notifications
You must be signed in to change notification settings - Fork 1
/
ws.go
executable file
·330 lines (254 loc) · 7.31 KB
/
ws.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
package main
import (
"bufio"
"fmt"
"log"
"net/http"
"os/exec"
"strings"
"github.com/gorilla/websocket"
"github.com/pelletier/go-toml"
)
var clients = make(map[*websocket.Conn]bool)
var broadcast = make(chan Request)
// Token = password -> sha512sum (all of this done client-side)
//var token = "3930cf659784a81d82d5e4856e5c7d356f3b40b672ceed9e3ea5c3fb622fa250dee4846daa9f33e2e3b5000cc307f40ffc3c967b2f8f662b711804e446cea21f"
var token = "a"
// Defines upgrader
var upgrader = websocket.Upgrader{}
// Request Struct for requests to/from the server/front-end
type Request struct {
Token string `json:"token"`
Action string `json:"action"`
Payload RequestPayload `json:"payload"`
}
// RequestPayload The payload the WS sends over
type RequestPayload struct {
Alias string `json:"alias,omitempty"`
Password string `json:"password,omitempty"`
Input string `json:"input,omitempty"`
}
var config, configError = toml.LoadFile("config.toml")
func main() {
if configError != nil {
return
}
// Initiates a file server (?) at /
fs := http.FileServer(http.Dir("../public"))
http.Handle("/", fs)
// Listen to WebSocket at /ws
http.HandleFunc("/ws", handleConnections)
// Async Message handlers
go handleMessages()
PORT := ":12000"
publPath := config.Get("ssl.Publ")
privPath := config.Get("ssl.Priv")
if publPath == nil || privPath == nil {
log.Printf("Cannot find Public or Private key file path!")
log.Printf("%s", "HTTP server up and listening at "+PORT)
err := http.ListenAndServe(PORT, nil)
if err != nil {
log.Fatal("ERROR! HTTP failed!", err)
}
} else {
publ := fmt.Sprintf("%s", publPath)
priv := fmt.Sprintf("%s", privPath)
log.Printf("%s", "HTTPS server up and listening at "+PORT)
err := http.ListenAndServeTLS(PORT, publ, priv, nil)
if err != nil {
log.Fatal("ERROR! HTTPS failed!", err)
}
}
}
func handleConnections(writer http.ResponseWriter, request *http.Request) {
// Upgrading HTTP(S) to WS
ws, err := upgrader.Upgrade(writer, request, nil)
if err != nil {
log.Fatal("ERROR! Upgrader failed!", err)
}
// Closes when function returns
defer ws.Close()
// Register the clients
clients[ws] = true
// Loop stuff to continuously listen
for {
var rq Request
// Parses JSON and passes it to "rq" (*Request)
err := ws.ReadJSON(&rq)
if err != nil {
log.Printf("Error at ws.ReadJSON! %v", err)
delete(clients, ws)
break // Exit loop if fail
}
// Sends the "rq" (*Request) to "broadcast"
broadcast <- rq
}
}
// Response The struct to respond after a request
type Response struct {
Signal int `json:"result"`
Output string `json:"output"`
}
// PLANNED SIGNALS:
// -1: Not finished
// 0: Success
// 1: Received error
// 2: Invalid syntax/Unknown Command
// 3: Invalid token
func handleMessages() {
// Same loop
for {
// Listen to broadcast for rq (*Request)
rq := <-broadcast
// Broadcast to all clients
for client := range clients {
var res Response
// Run the request
signal, output := processJSON(rq, client)
res.Signal = signal
res.Output = output
err := client.WriteJSON(&res)
if err != nil {
log.Printf("Error at client.WriteJSON! %v", err)
client.Close()
delete(clients, client)
}
}
}
}
// a channel to tell it to stop
var stopchan chan struct{}
// a channel to signal that it's stopped
var stoppedchan chan struct{}
func processJSON(request Request, client *websocket.Conn) (int, string) {
if request.Token != token {
log.Printf("%v", "Invalid token!")
return 3, "Invalid token!"
}
//if request.Payload == nil {
// return 2, "Empty command!"
//}
if request.Action == "CMDRUN" {
err := execAsync(request.Payload.Input, client)
if err != nil {
return 1, fmt.Sprintf("%s", err)
}
return 0, "Executed successfully."
}
payload := request.Payload
user := config.Get(payload.Alias + ".user").(string)
gameserver := config.Get(payload.Alias + ".script").(string)
password := payload.Password
input := payload.Input
if request.Action == "stopprint" {
close(stopchan) // tell it to stop
<-stoppedchan // wait for it to have stopped
return 0, "Stopped UpdateConsole."
}
if request.Action == "consoleprint" {
// a channel to tell it to stop
var stopchan chan struct{}
// a channel to signal that it's stopped
var stoppedchan chan struct{}
gamepath := strings.Split(gameserver, "/")
pane := gamepath[len(gamepath)-1]
print := `echo -e "` + password + `\n" | sudo -S -u ` + user + " tmux capture-pane -J -p -S -32767 -t " + pane
// LastOutput A hack to keep track of the last console output
LastOutput := make(chan string, 1)
go func() {
defer close(stoppedchan)
for {
select {
case <-stopchan:
return
default:
bufout, err := execAsyncUpdate(print, client, LastOutput)
if err != nil {
break
}
LastOutput <- bufout
}
}
}()
return -1, ""
}
// {"token":"TOKEN", "action":"request.Action", "payload": {"alias":"SERVER_ALIAS", "password":"SUDO PASSWORD", "input":""}}
// SUDO PASSWORD is not needed if Go backend is executed in the same user
if request.Action == "start" || request.Action == "stop" || request.Action == "monitor" || request.Action == "details" {
command := `echo -e "` + password + `\n" | sudo -S -u ` + user + " " + gameserver + " " + request.Action
err := execAsync(command, client)
if err != nil {
return 1, fmt.Sprintf("%s", err)
}
return 0, "Executed successfully."
}
if request.Action == "run" {
gamepath := strings.Split(gameserver, "/")
pane := gamepath[len(gamepath)-1]
var replacer = strings.NewReplacer(`"`, `\"`)
sanitisedInput := replacer.Replace(input)
command := `echo -e "` + password + `\n" | sudo -S -u ` + user + " tmux send-keys -t " + pane + ` "` + sanitisedInput + `" ENTER`
err := execAsync(command, client)
if err != nil {
return 1, fmt.Sprintf("Error! %s", err)
}
return 0, "Executed successfully."
}
return 2, "Unknown Command."
}
// execAsync Runs shell commands
// opt [DIFF|]
func execAsync(cmd string, client *websocket.Conn) error {
var res Response
cmd = cmd + "&> /dev/stdout"
shell := exec.Command("/bin/bash", "-c", cmd)
stdout, err := shell.StdoutPipe()
shell.Start()
bufout := bufio.NewScanner(stdout)
bufout.Split(bufio.ScanLines)
if err != nil {
return err
}
for bufout.Scan() {
res.Signal = -1
res.Output = bufout.Text()
errWrite := client.WriteJSON(&res)
if errWrite != nil {
log.Printf("Error at client.WriteJSON in execAsync! %v", err)
client.Close()
delete(clients, client)
}
}
shell.Wait()
return nil
}
func execAsyncUpdate(cmd string, client *websocket.Conn, LastOutput chan string) (string, error) {
var res Response
cmd = cmd + "&> /dev/stdout"
shell := exec.Command("/bin/bash", "-c", cmd)
stdout, err := shell.StdoutPipe()
shell.Start()
bufout := bufio.NewScanner(stdout)
bufout.Split(bufio.ScanLines)
if err != nil {
return "", err
}
for bufout.Scan() {
LO := <-LastOutput
fmt.Printf("%s", LO)
print := strings.Replace(bufout.Text(), LO, "", 1)
if print != "" {
res.Signal = -1
res.Output = print
errWrite := client.WriteJSON(&res)
if errWrite != nil {
log.Printf("Error at client.WriteJSON in execAsync! %v", err)
client.Close()
delete(clients, client)
}
}
return bufout.Text(), nil
}
shell.Wait()
return "", nil
}