This repository was archived by the owner on Oct 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
101 lines (87 loc) · 2.19 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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
)
var (
// ServerKey is the key used by the https server
ServerKey = os.Getenv("SERVER_KEY")
// ServerCert is the certificate used by the https server
ServerCert = os.Getenv("SERVER_CRT")
// Port is the port where the https server is listening to
Port = os.Getenv("PORT")
// SSL tells gora to listen for encrypted connections
SSL = os.Getenv("SSL") == "true"
)
// Gora is the main handler of the Gora app.
// It handles GET/POST:
// - GET: Returns the environment list
// - POST: Executes the data in bash
func Gora(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case "HEAD":
case "GET":
log.Println("Handling GET request. Returning environment variables")
var env []byte
w.Header().Set("Content-Type", "text/plain")
for _, e := range os.Environ() {
e = e + "\n"
for _, r := range e {
env = append(env, byte(r))
}
}
w.Write(env)
case "POST":
body := req.Body
data, err := ioutil.ReadAll(body)
if err != nil {
http.Error(w, "failed parsing body", http.StatusInternalServerError)
}
command := string(data)
log.Printf("Handling POST request. Executing '%s'\n", command)
cmd := exec.Command("/bin/bash", "-c", command)
if out, err := cmd.CombinedOutput(); err != nil {
http.Error(w, fmt.Sprintf("500 - failed executing: %s\n error: %s", command, string(out)), http.StatusInternalServerError)
} else {
w.Write([]byte("OK"))
}
default:
http.Error(w, "not supported", http.StatusInternalServerError)
}
}
func sslGora() {
if ServerKey == "" {
log.Fatal("SERVER_KEY missing")
}
if ServerCert == "" {
log.Fatal("SERVER_CRT missing")
}
if Port == "" {
log.Fatal("PORT missing")
}
log.Println("Starting gora over SSL")
err := http.ListenAndServeTLS(fmt.Sprintf(":%s", Port), ServerCert, ServerKey, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func plainGora() {
log.Println("Starting gora over plain http connection")
err := http.ListenAndServe(fmt.Sprintf(":%s", Port), nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func main() {
http.HandleFunc("/", Gora)
switch SSL {
case true:
sslGora()
default:
plainGora()
}
}