-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
269 lines (229 loc) · 5.7 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
package main
import (
"bufio"
"fmt"
"github.com/olahol/melody"
"github.com/spf13/viper"
"log"
"net/http"
"os/exec"
"strings"
"time"
)
var cmd *exec.Cmd
var stdout *bufio.Scanner
var stderr *bufio.Scanner
var logtext = ""
var melodyWs *melody.Melody
func mainPageResponse(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/html")
cmdStr := viper.GetString("Command")
argArray := viper.GetStringSlice("Args")
logCmdStr := getLogCmdStr(cmdStr, argArray)
body := `
<html>
<head>
<style>
div#log_text {
height: 500px;
resize: vertical;
overflow-y: auto;
padding: 20px;
border-style: inset;
border-width: medium;
}
</style>
<script type="text/javascript">
window.onload = setupOnPageLoad;
function setupOnPageLoad() {
// Make the restart button functional
const restartBtn = document.getElementById('restart-btn');
restartBtn.addEventListener('click', async _ => {
doRestart();
});
// Start websocket for getting log updates
var url = "ws://" + window.location.host + "/logws";
var ws = new WebSocket(url);
var log_text = document.getElementById("log_text");
ws.onmessage = function(msg) {
doScroll = isAtBottom(log_text);
console.log(msg.data);
log_text.innerHTML += msg.data;
if(doScroll) {
scrollBottom(log_text);
}
};
async function doRestart() {
try {
var data = new FormData(document.getElementById("restartForm"));
console.log(data);
const response = await fetch('/restart', {
method: 'POST',
body: data
});
console.log('Restarted', response);
} catch(err) {
console.error('Error: ${err}');
}
}
function isAtBottom(elem) {
return elem.scrollTop >= (elem.scrollHeight - elem.offsetHeight);
}
function scrollBottom(elem) {
elem.scrollTop = elem.scrollHeight;
}
}
</script>
</head>
Command: ` + logCmdStr + `</br>
<form id="restartForm">
<input type="password" id="pw" name="pw" placeholder="password">
<input type="button" id="restart-btn" value="Restart"/></br></br>
</form>
<div id="log_text" />
</body>
</html>
`
w.Write([]byte(body))
}
func restartResponse(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
//fmt.Printf("%+v", r)
//r.ParseForm()
pw := r.FormValue("pw")
if viper.GetString("Password") != pw {
w.WriteHeader(http.StatusUnauthorized)
fmt.Printf("Unauthorized restart attempt using pw=%v\n", pw)
return
}
}
w.WriteHeader(http.StatusOK)
killRunningCommand()
runCommand()
}
func logResponse(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(logtext))
}
func getLogCmdStr(cmd string, args []string) string {
logCmdStr := cmd
if len(args) > 0 {
for _, arg := range args {
if strings.ContainsAny(arg, " ") {
logCmdStr += " '" + arg + "'"
} else {
logCmdStr += " " + arg
}
}
}
return logCmdStr
}
func killRunningCommand() {
if cmd != nil {
cmd.Process.Kill()
cmd.Wait()
// append a horizontal line to the log for connected clients, then clear it for any future connections
appendToLog("<hr>")
clearLog()
}
}
func runCommand() {
cmdStr := viper.GetString("Command")
argArray := viper.GetStringSlice("Args")
appendToLog("Starting Command `" + getLogCmdStr(cmdStr, argArray) + "` at " + time.Now().String() + "\n")
if len(argArray) > 0 {
cmd = exec.Command(cmdStr, argArray...)
} else {
cmd = exec.Command(cmdStr)
}
so, _ := cmd.StdoutPipe()
se, _ := cmd.StderrPipe()
err := cmd.Start()
if err != nil {
log.Fatal(err)
}
stdout = bufio.NewScanner(so)
stderr = bufio.NewScanner(se)
}
func clearLog() {
logtext = ""
}
func appendToLog(s string) {
s = strings.Replace(s, "\n", "<br>", -1)
logtext += s
if melodyWs != nil {
melodyWs.Broadcast([]byte(s))
}
}
func readAllStdout() {
for {
if stdout != nil {
for stdout.Scan() {
m := stdout.Text()
if viper.GetBool("StdoutToTerminal") {
fmt.Println(m)
}
appendToLog(m + "\n")
}
}
time.Sleep(100 * time.Millisecond)
}
}
func readAllStderr() {
for {
if stderr != nil {
for stderr.Scan() {
m := stderr.Text()
if viper.GetBool("StderrToTerminal") {
fmt.Println(m)
}
appendToLog(m + "\n")
}
}
time.Sleep(100 * time.Millisecond)
}
}
func main() {
viper.SetDefault("ServerAddress", ":8080")
viper.SetDefault("Command", "ls")
viper.SetDefault("Args", "-lh")
viper.SetDefault("RunOnLaunch", false)
viper.SetDefault("NoWebsocketOriginCheck", false)
viper.SetDefault("StdoutToTerminal", true)
viper.SetDefault("StderrToTerminal", true)
viper.SetDefault("Password", "")
viper.SetConfigName("config")
viper.SetConfigType("toml")
viper.AddConfigPath("$HOME/.cmdwebctrl")
viper.AddConfigPath(".")
viper.WatchConfig()
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("fatal error config file: %w", err))
}
// start the goroutine for updating log file text
go readAllStdout()
go readAllStderr()
if viper.GetBool("RunOnLaunch") {
runCommand()
}
// Start melody websocket framework
melodyWs = melody.New()
// Allow all origin hosts if needed
if viper.GetBool("NoWebsocketOriginCheck") {
melodyWs.Upgrader.CheckOrigin = func(r *http.Request) bool { return true }
}
// configure endpoints and start the web server
http.HandleFunc("/", mainPageResponse)
http.HandleFunc("/restart", restartResponse)
http.HandleFunc("/log", logResponse)
http.HandleFunc("/logws", func(w http.ResponseWriter, r *http.Request) {
melodyWs.HandleRequest(w, r)
})
melodyWs.HandleConnect(func(s *melody.Session) {
s.Write([]byte(logtext))
})
log.Fatal(http.ListenAndServe(viper.GetString("ServerAddress"), nil))
}