/
index.go
66 lines (53 loc) · 1.63 KB
/
index.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
package main
import "regexp"
import (
"bytes"
"flag"
"fmt"
"net/http"
"os"
"os/exec"
)
var (
listenAddress = flag.String("listen", "0.0.0.0:80", "Where the server listens for connections. [interface]:port")
staticPath = flag.String("static", "../", "Location of static files.")
)
func init() {
flag.Parse()
}
func main() {
http.Handle("/", http.FileServer(http.Dir(*staticPath)))
http.HandleFunc("/server/", func(w http.ResponseWriter, r *http.Request) {
module := r.URL.Query().Get("module")
// Escape the contents of the variable "module" so that no shell command can leak.
r = regexp.MustCompile("\\")
module = r.ReplaceAllString(module, "\\\\")
r = regexp.MustCompile("\"")
module = r.ReplaceAllString(module, "\\\"")
r = regexp.MustCompile("`")
module = r.ReplaceAllString(module, "\`")
r = regexp.MustCompile("\$")
module = r.ReplaceAllString(module, "\\$")
if module == "" {
http.Error(w, "No module specified, or requested module doesn't exist.", 406)
return
}
// Execute the command
cmd := exec.Command("./linux_json_api.sh", module)
var output bytes.Buffer
cmd.Stdout = &output
err := cmd.Run()
if err != nil {
fmt.Printf("Error executing '%s': %s\n\tScript output: %s\n", module, err.Error(), output.String())
http.Error(w, "Unable to execute module.", http.StatusInternalServerError)
return
}
w.Write(output.Bytes())
})
fmt.Println("Starting http server at:", *listenAddress)
err := http.ListenAndServe(*listenAddress, nil)
if err != nil {
fmt.Println("Error starting http server:", err)
os.Exit(1)
}
}