This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
345 lines (300 loc) · 8.72 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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package main
import (
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"crypto/subtle"
"encoding/base64"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path"
"strconv"
"strings"
"time"
"github.com/julienschmidt/httprouter"
"github.com/rlmcpherson/s3gof3r"
"gopkg.in/h2non/bimg.v1"
)
var (
listenInterface string
maxAge int
securityKey []byte
resultBucketName string
useRRS bool
unsafeMode bool
httpClient = http.DefaultClient
resultBucket *s3gof3r.Bucket
)
type ByteSize int64
const (
_ = iota // ignore first value by assigning to blank identifier
KB ByteSize = 1 << (10 * iota)
MB
)
func main() {
log.SetFlags(0) // hide timestamps from Go logs
parseFlags()
resultBucketName = os.Getenv("RESULT_STORAGE_BUCKET")
if resultBucketName != "" {
keys, err := s3gof3r.EnvKeys()
if err != nil {
log.Fatal(err)
}
resultBucket = s3gof3r.New(s3gof3r.DefaultDomain, keys).Bucket(resultBucketName)
resultBucket.Concurrency = 4
resultBucket.PartSize = int64(2 * MB)
resultBucket.Md5Check = false
httpClient = resultBucket.Client
if rrs := os.Getenv("USE_RRS"); rrs == "true" || rrs == "1" {
useRRS = true
}
}
router := httprouter.New()
router.HEAD("/:signature/:size/*source", handleResize)
router.GET("/:signature/:size/*source", handleResize)
log.Fatal(http.ListenAndServe(listenInterface, router))
}
func parseFlags() {
securityKeyStr := ""
port := os.Getenv("PORT")
if port == "" {
port = "8888"
}
if maxAgeStr := os.Getenv("MAX_AGE"); maxAgeStr != "" {
var err error
if maxAge, err = strconv.Atoi(maxAgeStr); err != nil {
log.Fatal("invalid MAX_AGE setting")
}
}
flag.StringVar(&listenInterface, "l", ":"+port, "listen address")
flag.IntVar(&maxAge, "max-age", maxAge, "the maximum HTTP caching age to use on returned images")
flag.StringVar(&securityKeyStr, "k", os.Getenv("SECURITY_KEY"), "security key")
flag.BoolVar(&unsafeMode, "unsafe", false, "whether to allow /unsafe URLs")
flag.Parse()
if securityKeyStr == "" && !unsafeMode {
log.Fatalf("must provide a security key with -k or allow unsafe URLs")
}
securityKey = []byte(securityKeyStr)
}
func handleResize(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
reqPath := req.URL.EscapedPath()
log.Printf("%s %s", req.Method, reqPath)
sourceURL, err := url.Parse(strings.TrimPrefix(params.ByName("source"), "/"))
if err != nil || !(sourceURL.Scheme == "http" || sourceURL.Scheme == "https") {
http.Error(w, "invalid source URL", 400)
return
}
sig := params.ByName("signature")
pathToVerify := strings.TrimPrefix(reqPath, "/"+sig+"/")
if err := validateSignature(sig, pathToVerify); err != nil {
http.Error(w, "invalid signature", 401)
return
}
width, height, err := parseWidthAndHeight(params.ByName("size"))
if err != nil {
http.Error(w, "invalid height requested", 400)
return
}
resultPath := normalizePath(strings.TrimPrefix(reqPath, "/"+sig))
// TODO(bgentry): everywhere that switches on resultBucket should switch on
// something like resultStorage instead.
if resultBucket == nil {
// no result storage, just generate the thumbnail
generateThumbnail(w, req.Method, resultPath, sourceURL.String(), width, height)
return
}
// try to get stored result
r, h, err := getStoredResult(req.Method, resultPath)
if err != nil {
log.Printf("getting stored result: %s", err)
generateThumbnail(w, req.Method, resultPath, sourceURL.String(), width, height)
return
}
defer r.Close()
// return stored result
length, err := strconv.Atoi(h.Get("Content-Length"))
if err != nil {
log.Printf("invalid result content-length: %s", err)
// TODO: try to generate instead of erroring w/ 500?
http.Error(w, err.Error(), 500)
return
}
setResultHeaders(w, &result{
ContentType: h.Get("Content-Type"),
ContentLength: length,
ETag: strings.Trim(h.Get("Etag"), `"`),
Path: resultPath,
})
if _, err = io.Copy(w, r); err != nil {
log.Printf("copying from stored result: %s", err)
return
}
if err = r.Close(); err != nil {
log.Printf("closing stored result copy: %s", err)
}
}
type result struct {
Data []byte
ContentType string
ContentLength int
ETag string
Path string
}
func computeHexMD5(data []byte) string {
h := md5.New()
h.Write(data)
return fmt.Sprintf("%x", h.Sum(nil))
}
func generateThumbnail(w http.ResponseWriter, rmethod, rpath string, sourceURL string, width, height uint) {
log.Printf("generating %s", rpath)
resp, err := httpClient.Get(sourceURL)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Printf("unexpected status code from source: %d", resp.StatusCode)
http.Error(w, "", resp.StatusCode)
return
}
img, err := ioutil.ReadAll(resp.Body)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
buf, err := bimg.Resize(img, bimg.Options{
Height: int(height),
Width: int(width),
Gravity: bimg.GravityCentre,
Interpolator: bimg.Bicubic,
Quality: 50,
})
if err != nil {
responseCode := 500
if err.Error() == "Unsupported image format" || strings.Contains(err.Error(), "VIPS cannot save to") {
responseCode = 415 // Unsupported Media Type
}
http.Error(w, fmt.Sprintf("resizing image: %s", err.Error()), responseCode)
return
}
res := &result{
ContentType: "image/jpeg", // TODO: support PNGs as well
ContentLength: len(buf),
Data: buf, // TODO: check if I need to copy this
ETag: computeHexMD5(buf),
Path: rpath,
}
setResultHeaders(w, res)
if rmethod != "HEAD" {
if _, err = w.Write(buf); err != nil {
log.Printf("writing buffer to response: %s", err)
}
}
if resultBucket != nil {
go storeResult(res)
}
}
// caller is responsible for closing the returned ReadCloser
func getStoredResult(method, path string) (io.ReadCloser, http.Header, error) {
if method != "HEAD" {
return resultBucket.GetReader(path, nil)
}
s3URL := fmt.Sprintf("https://%s.s3.amazonaws.com%s", resultBucketName, path)
req, err := http.NewRequest(method, s3URL, nil)
if err != nil {
return nil, nil, err
}
resultBucket.Sign(req)
res, err := httpClient.Do(req)
if err != nil {
return nil, nil, err
}
if res.StatusCode < 200 || res.StatusCode >= 300 {
// TODO: drain res.Body to ioutil.Discard before closing?
res.Body.Close()
return nil, nil, fmt.Errorf("unexpected status code %d", res.StatusCode)
}
res.Header.Set("Content-Length", strconv.FormatInt(res.ContentLength, 10))
return res.Body, res.Header, err
}
func mustGetenv(name string) string {
value := os.Getenv(name)
if value == "" {
log.Fatalf("missing %s env", name)
}
return value
}
func normalizePath(p string) string {
// TODO(bgentry): Support for custom root path? ala RESULT_STORAGE_AWS_STORAGE_ROOT_PATH
return path.Clean(p)
}
func parseWidthAndHeight(str string) (width, height uint, err error) {
sizeParts := strings.Split(str, "x")
if len(sizeParts) != 2 {
err = fmt.Errorf("invalid size requested")
return
}
width64, err := strconv.ParseUint(sizeParts[0], 10, 64)
if err != nil {
err = fmt.Errorf("invalid width requested")
return
}
height64, err := strconv.ParseUint(sizeParts[1], 10, 64)
if err != nil {
err = fmt.Errorf("invalid height requested")
return
}
return uint(width64), uint(height64), nil
}
func setCacheHeaders(w http.ResponseWriter) {
w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d,public", maxAge))
w.Header().Set("Expires", time.Now().UTC().Add(time.Duration(maxAge)*time.Second).Format(http.TimeFormat))
}
func setResultHeaders(w http.ResponseWriter, result *result) {
w.Header().Set("Content-Type", result.ContentType)
w.Header().Set("Content-Length", strconv.Itoa(result.ContentLength))
w.Header().Set("ETag", `"`+result.ETag+`"`)
setCacheHeaders(w)
}
func storeResult(res *result) {
h := make(http.Header)
h.Set("Content-Type", res.ContentType)
if useRRS {
h.Set("x-amz-storage-class", "REDUCED_REDUNDANCY")
}
w, err := resultBucket.PutWriter(res.Path, h, nil)
if err != nil {
log.Printf("storing result for %s: %s", res.Path, err)
return
}
defer w.Close()
if _, err = w.Write(res.Data); err != nil {
log.Printf("storing result for %s: %s", res.Path, err)
return
}
if err = w.Close(); err != nil {
log.Printf("storing result for %s: %s", res.Path, err)
}
}
func validateSignature(sig, pathPart string) error {
if unsafeMode && sig == "unsafe" {
return nil
}
h := hmac.New(sha1.New, securityKey)
if _, err := h.Write([]byte(pathPart)); err != nil {
return err
}
actualSig := base64.URLEncoding.EncodeToString(h.Sum(nil))
// constant-time string comparison
if subtle.ConstantTimeCompare([]byte(sig), []byte(actualSig)) != 1 {
return fmt.Errorf("signature mismatch")
}
return nil
}