forked from DHowett/spectre
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
235 lines (196 loc) · 4.31 KB
/
util.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
package main
import (
"bytes"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/base32"
"fmt"
"io"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
"github.com/golang/glog"
"github.com/gorilla/mux"
"gopkg.in/yaml.v2"
)
const (
EnvironmentDevelopment string = "dev"
EnvironmentProduction string = "production"
SPECTRE_DEFAULT_BRAND string = "Spectre"
)
type ReadCloser struct {
io.Reader
io.Closer
}
type WriteCloser struct {
io.Writer
io.Closer
}
func constructMAC(message, key []byte) []byte {
mac := hmac.New(sha256.New, key)
mac.Write(message)
return mac.Sum(nil)
}
func checkMAC(message, messageMAC, key []byte) bool {
return hmac.Equal(messageMAC, constructMAC(message, key))
}
var base32Encoder = base32.NewEncoding("abcdefghjkmnopqrstuvwxyz23456789")
func generateRandomBytes(nbytes int) ([]byte, error) {
uuid := make([]byte, nbytes)
n, err := rand.Read(uuid)
if n != len(uuid) || err != nil {
return []byte{}, err
}
return uuid, nil
}
func generateRandomBase32String(nbytes, outlen int) (string, error) {
uuid, err := generateRandomBytes(nbytes)
if err != nil {
return "", err
}
s := base32Encoder.EncodeToString(uuid)
if outlen == -1 {
outlen = len(s)
}
return s[0:outlen], nil
}
func YAMLUnmarshalFile(filename string, i interface{}) error {
path, err := os.Getwd()
if err != nil {
panic(err)
}
yamlFile, err := os.Open(path + "/" + filename)
if err != nil {
return err
}
fi, err := yamlFile.Stat()
if err != nil {
return err
}
yml := make([]byte, fi.Size())
io.ReadFull(yamlFile, yml)
yamlFile.Close()
err = yaml.Unmarshal(yml, i)
if err != nil {
return err
}
return nil
}
func SlurpFile(path string) (out []byte, err error) {
var file *os.File
if file, err = os.Open(path); err == nil {
buf := &bytes.Buffer{}
io.Copy(buf, file)
out = buf.Bytes()
file.Close()
}
return
}
func BaseURLForRequest(r *http.Request) *url.URL {
determinedScheme := "http"
if RequestIsHTTPS(r) {
determinedScheme = "https"
}
return &url.URL{
Scheme: determinedScheme,
User: r.URL.User,
Host: r.Host,
Path: "/",
}
}
func RequestIsHTTPS(r *http.Request) bool {
proto := strings.ToLower(r.Header.Get("X-Forwarded-Proto"))
if proto == "" {
proto = strings.ToLower(r.URL.Scheme)
}
return proto == "https"
}
func SourceIPForRequest(r *http.Request) string {
ip := r.Header.Get("CF-Connecting-IP")
if ip == "" {
ip := r.Header.Get("X-Forwarded-For")
if ip == "" {
ip = r.RemoteAddr[:strings.LastIndex(r.RemoteAddr, ":")]
}
}
return ip
}
func HTTPSMuxMatcher(r *http.Request, rm *mux.RouteMatch) bool {
return Env() == EnvironmentDevelopment || RequestIsHTTPS(r)
}
func NonHTTPSMuxMatcher(r *http.Request, rm *mux.RouteMatch) bool {
return !HTTPSMuxMatcher(r, rm)
}
type ReloadFunction func()
var reloadFunctions = []ReloadFunction{}
func RegisterReloadFunction(f ReloadFunction) {
reloadFunctions = append(reloadFunctions, f)
}
func ReloadAll() {
for _, f := range reloadFunctions {
f()
}
}
type ByteSize float64
const (
_ = iota // ignore first value by assigning to blank identifier
KB ByteSize = 1 << (10 * iota)
MB
GB
TB
PB
EB
ZB
YB
)
func (b ByteSize) String() string {
switch {
case b >= YB:
return fmt.Sprintf("%.2fYB", b/YB)
case b >= ZB:
return fmt.Sprintf("%.2fZB", b/ZB)
case b >= EB:
return fmt.Sprintf("%.2fEB", b/EB)
case b >= PB:
return fmt.Sprintf("%.2fPB", b/PB)
case b >= TB:
return fmt.Sprintf("%.2fTB", b/TB)
case b >= GB:
return fmt.Sprintf("%.2fGB", b/GB)
case b >= MB:
return fmt.Sprintf("%.2fMB", b/MB)
case b >= KB:
return fmt.Sprintf("%.2fKB", b/KB)
}
return fmt.Sprintf("%.2fB", b)
}
var environment string = EnvironmentDevelopment
func Env() string {
return environment
}
func init() {
environment = os.Getenv("SPECTRE_ENV")
if environment != EnvironmentProduction {
environment = EnvironmentDevelopment
}
brand := os.Getenv("SPECTRE_BRAND")
if brand == "" {
brand = SPECTRE_DEFAULT_BRAND
}
RegisterTemplateFunction("env", func() string { return environment })
RegisterTemplateFunction("brand", func() string {
return brand
})
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGHUP)
go func() {
for _ = range sigChan {
glog.Info("Received SIGHUP")
ReloadAll()
}
}()
}