-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
192 lines (159 loc) · 4.6 KB
/
main.go
File metadata and controls
192 lines (159 loc) · 4.6 KB
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
package main
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base32"
"encoding/binary"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
)
var otpSecret []byte
func main() {
portPtr := flag.String("port", "8080", "Server port")
secret := flag.String("otp-secret", "",
"Base32 encoded secret code for OTP authentication. Set this value only if you wish to enable OTP authentication.")
flag.Parse()
if *secret != "" {
var err error
otpSecret, err = base32.StdEncoding.DecodeString(*secret)
if err != nil {
log.Fatal("Unable to parse OTP secret:\n" + err.Error())
}
}
if otpSecret == nil {
log.Print("OTP authentication disabled")
} else {
log.Print("OTP authentication enabled")
}
log.Print("Starting server on port " + *portPtr)
http.HandleFunc("/webhook/run/", webhookRun)
http.HandleFunc("/webhook/async/", webhookAsync)
http.HandleFunc("/webhook/log/", webhookLog)
log.Fatal(http.ListenAndServe(":" + *portPtr, nil))
}
func webhookRun(res http.ResponseWriter, req *http.Request) {
log.Print("Endpoint: /webhook/run/")
if !authenticateRequest(req) {
fmt.Fprint(res, "Unauthorized.")
return
}
scriptFile, err := parseScriptFile(req.URL.Path, "/webhook/run/")
if err != nil {
fmt.Fprint(res, "An error occurred:\n\n" + err.Error())
return
}
out, err := exec.Command("/bin/bash", scriptFile).Output()
go writeLog(scriptFile, out, err)
if err != nil {
fmt.Fprint(res, "An error occurred:\n\n" + err.Error())
}
if out != nil && len(out) > 0 {
fmt.Fprint(res, "\n\nScript output:\n\n" + string(out))
}
}
func webhookAsync(res http.ResponseWriter, req *http.Request) {
log.Print("Endpoint: /webhook/async/")
if !authenticateRequest(req) {
fmt.Fprint(res, "Unauthorized.")
return
}
scriptFile, err := parseScriptFile(req.URL.Path, "/webhook/async/")
if err != nil {
fmt.Fprint(res, "An error occurred:\n\n" + err.Error())
return
}
go func() {
out, scriptErr := exec.Command("/bin/bash", scriptFile).Output()
writeLog(scriptFile, out, scriptErr)
}()
fmt.Fprint(res, "Triggered script: " + scriptFile)
}
func webhookLog(res http.ResponseWriter, req *http.Request) {
log.Print("Endpoint: /webhook/log/")
if !authenticateRequest(req) {
fmt.Fprint(res, "Unauthorized.")
return
}
scriptFile, err := parseScriptFile(req.URL.Path, "/webhook/log/")
if err != nil {
fmt.Fprint(res, "An error occurred:\n\n" + err.Error())
return
}
output, err := readLogFiles(scriptFile)
if err != nil {
fmt.Fprint(res, "An error occurred:\n\n" + err.Error())
}
fmt.Fprint(res, output)
}
func authenticateRequest(req *http.Request) bool {
if otpSecret == nil {
// otp authentication disabled
return true
}
providedOtp := req.URL.Query().Get("otp")
expectedOtp := generateOtp()
return expectedOtp == providedOtp
}
func parseScriptFile(urlPath string, endpoint string) (string, error) {
scriptFile := strings.TrimPrefix(urlPath, endpoint)
scriptFile = strings.TrimRight(scriptFile, "/") // remove trailing slashes if any
if _, err := os.Stat(scriptFile); os.IsNotExist(err) {
return "", errors.New("error: script file does not exist: " + scriptFile)
}
return scriptFile, nil
}
func writeLog(script string, output []byte, err error) {
errorMsg := ""
if err != nil {
errorMsg = "\n\nError messages:\n\n" + err.Error()
}
generated :=
"Script " + script + " last executed at:\n\n" +
time.Now().Format(time.UnixDate) +
errorMsg +
"\n\nOutput:\n\n"
fullLog := append([]byte(generated), output...)
_ = ioutil.WriteFile(getLogFileName(script), fullLog, 0666)
}
func readLogFiles(script string) (string, error) {
logFile := getLogFileName(script)
if _, err := os.Stat(logFile); os.IsNotExist(err) {
return "", errors.New("error: could not find log file (has the script been run?)")
}
output, err := ioutil.ReadFile(logFile)
if err != nil {
return "", err
}
return string(output), nil
}
func getLogFileName(scriptFile string) string {
return strings.TrimSuffix(scriptFile, filepath.Ext(scriptFile)) + ".log"
}
// never roll your own crypto, but for the sake of keeping this project simple this will do.
func generateOtp() string {
curTime := time.Now().Unix() / 30
message := make([]byte, 8)
binary.BigEndian.PutUint64(message, uint64(curTime))
hmacSha1 := hmac.New(sha1.New, otpSecret)
hmacSha1.Write(message)
hash := hmacSha1.Sum(nil)
offset := hash[len(hash) - 1] & 0b1111
truncatedHash := hash[offset : offset + 4]
code := binary.BigEndian.Uint32(truncatedHash)
code = (code & 0x7fffffff) % 1000000
otp := strconv.FormatInt(int64(code), 10)
for len(otp) < 6 {
otp = "0" + otp
}
return otp
}