forked from xuri/aurora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
267 lines (248 loc) · 7.04 KB
/
utils.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
// Copyright 2016 - 2018 The aurora Authors. All rights reserved. Use of this
// source code is governed by a MIT license that can be found in the LICENSE
// file.
//
// The aurora is a web-based Beanstalk queue server console written in Go
// and works on macOS, Linux and Windows machines. Main idea behind using Go
// for backend development is to utilize ability of the compiler to produce
// zero-dependency binaries for multiple platforms. aurora was created as an
// attempt to build very simple and portable application to work with local or
// remote Beanstalk server.
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"html"
"io"
"io/ioutil"
"math/rand"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/BurntSushi/toml"
)
// readConf read external config file when program startup.
func readConf() error {
buf := new(bytes.Buffer)
if _, err := os.Stat(ConfigFile); os.IsNotExist(err) {
err := ioutil.WriteFile(ConfigFile, []byte(ConfigFileTemplate), 0644)
if err != nil {
return err
}
}
buf.Reset()
tomlData, err := os.Open(ConfigFile)
if err != nil {
return err
}
_, err = io.Copy(buf, tomlData)
if err != nil {
return err
}
tomlData.Close()
if _, err := toml.Decode(buf.String(), &pubConf); err != nil {
return err
}
if err := json.Unmarshal([]byte(pubConf.Sample.Storage), &sampleJobs); err != nil {
return err
}
parseConf()
return nil
}
// parseConf parse server config in external config file.
func parseConf() {
selfConf.Servers = append(selfConf.Servers, pubConf.Servers...)
}
// removeArrayDuplicates provide a function remove duplicates value elements in
// a slice.
func removeArrayDuplicates(elements []string) []string {
// Use map to record duplicates as we find them.
encountered := map[string]bool{}
result := []string{}
for v := range elements {
if encountered[elements[v]] {
// Do not add duplicate.
} else {
// Record this element as an encountered element.
encountered[elements[v]] = true
// Append to result slice.
result = append(result, elements[v])
}
}
// Return the new slice.
return result
}
// removeArrayEmpty provide a function remove empty value elements in a slice.
func removeArrayEmpty(s []string) []string {
var r []string
for _, str := range s {
if str != "" {
r = append(r, str)
}
}
return r
}
// removeServerInConfig provide a method to remove property in config by given
// field.
func removeServerInConfig(server string) {
for k, v := range selfConf.Servers {
if v == server {
selfConf.Servers = selfConf.Servers[:k+copy(selfConf.Servers[k:], selfConf.Servers[k+1:])]
}
}
}
// runCmd run command opens a new browser window pointing to url.
func runCmd(prog string, args ...string) error {
cmd := exec.Command(prog, args...)
cmd.Stdout = Stdout
cmd.Stderr = Stderr
return cmd.Run()
}
// checkInSlice return bool type value to check if exits in slice by given
// string.
func checkInSlice(list []string, value string) bool {
set := make(map[string]bool)
for _, v := range list {
set[v] = true
}
return set[value]
}
// prettyJSON provide method get JSON string with indent.
func prettyJSON(b []byte) []byte {
var out bytes.Buffer
err := json.Indent(&out, b, "", "\t")
if err != nil {
return b
}
return out.Bytes()
}
// base64Decode provide method get Base64 decode string.
func base64Decode(b string) string {
data, err := base64.StdEncoding.DecodeString(b)
if err != nil {
return string(b)
}
return string(data)
}
// preformat provide method get job body after format with config.
func preformat(jobBody []byte) string {
var job = string(jobBody)
if selfConf.IsDisabledJSONDecode != 1 {
job = string(prettyJSON(jobBody))
}
if selfConf.IsEnabledBase64Decode != 0 {
job = base64Decode(job)
}
job = html.EscapeString(job)
return job
}
// parseFlags parse flags of program.
func parseFlags() {
configPtr := flag.String("c", "", "Use config file.")
verPtr := flag.Bool("v", false, "Output version and exit.")
helpPtr := flag.Bool("h", false, "Output this help and exit.")
flag.Parse()
if *configPtr == "" {
selfDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
os.Exit(0)
}
ConfigFile = selfDir + string(os.PathSeparator) + `aurora.toml`
} else {
ConfigFile = *configPtr
}
if *verPtr {
fmt.Printf("aurora version: %.1f\r\n", Version)
os.Exit(0)
}
if *helpPtr {
fmt.Printf("aurora version: %.1f\r\nCopyright (c) 2016 - 2018 Ri Xu https://xuri.me All rights reserved.\r\n\r\nUsage: aurora [OPTIONS] [cmd [arg ...]]\n -c <filename> Use config file. (default: aurora.toml)\r\n -h \t\t Output this help and exit.\r\n -v \t\t Output version and exit.\r\n", Version)
os.Exit(0)
}
}
// basicAuth provide a simple method to HTTP authenticate.
func basicAuth(f ViewFunc) ViewFunc {
if !pubConf.Auth.Enabled {
return func(w http.ResponseWriter, r *http.Request) {
f(w, r)
}
}
return func(w http.ResponseWriter, r *http.Request) {
basicAuthPrefix := "Basic "
// Parse request header
auth := r.Header.Get("Authorization")
if strings.HasPrefix(auth, basicAuthPrefix) {
// Decoding authentication information.
payload, err := base64.StdEncoding.DecodeString(
auth[len(basicAuthPrefix):],
)
if err == nil {
pair := bytes.SplitN(payload, []byte(":"), 2)
if len(pair) == 2 && bytes.Equal(pair[0], []byte(pubConf.Auth.Username)) &&
bytes.Equal(pair[1], []byte(pubConf.Auth.Password)) {
f(w, r)
return
}
}
}
// Authorization fail, return 401 Unauthorized.
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted"`)
w.WriteHeader(http.StatusUnauthorized)
}
}
// randToken generate a random token with MD5.
func randToken() string {
rand.Seed(time.Now().UnixNano())
b := make([]byte, 16)
rand.Read(b)
return fmt.Sprintf("%x", b)
}
// setHeader provide common method set HTTP header response.
func setHeader(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Server", "WebServer")
w.Header().Set("Content-Type", "text/html")
w.Header().Set("Cache-Control", "no-cache, private, max-age=0")
w.Header().Set("Expires", time.Unix(0, 0).Format(http.TimeFormat))
w.Header().Set("Pragma", "no-cache")
w.Header().Set("X-Accel-Expires", "0")
}
// checkUpdate render update notice alert.
func checkUpdate() string {
if updateInfo != "uncheck" {
return updateInfo
}
updateInfo = ""
r, err := http.Get(UpdateURL)
if err != nil {
return updateInfo
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
r.Body.Close()
return updateInfo
}
r.Body.Close()
u := UpdateTags{}
err = json.Unmarshal(body, &u)
if err != nil {
return updateInfo
}
if len(u) < 1 {
return updateInfo
}
v, err := strconv.ParseFloat(u[0].Name, 64)
if err != nil {
return updateInfo
}
if Version < v {
updateInfo = fmt.Sprintf(`<br/><div class="alert alert-info" style="position: relative;top:50px;"><span>You are currently running version %.1f of aurora. A new version is available: <b>%.1f</b> Get it from <b><a href="https://github.com/xuri/aurora" target="_blank">GitHub</a></b></span></div>`, Version, v)
}
return updateInfo
}