-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
gamo.go
284 lines (220 loc) · 6.96 KB
/
gamo.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
package main
import (
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"flag"
"fmt"
"io"
"math"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/Jeffail/tunny"
"github.com/discord/lilliput"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
)
const (
DEFAULT_BIND = "127.0.0.1:8081"
DEFAULT_KEY = "0x24FEEDFACEDEADBEEFCAFE"
MAX_CONTENT_LENGTH = 5242880
MAX_DIMENSIONS = 8912
OUTPUT_BUFFER_SIZE = 10 * 1024 * 1024
)
type imageOpsWorker struct {
ops *lilliput.ImageOps
}
type imageOpsPayload struct {
decoder lilliput.Decoder
options *lilliput.ImageOptions
}
type imageOpsResult struct {
result []byte
err error
}
func (w *imageOpsWorker) Process(payload interface{}) interface{} {
log.Debug("Allocating memory for transformation")
input := payload.(*imageOpsPayload)
output := make([]byte, OUTPUT_BUFFER_SIZE)
output, err := w.ops.Transform(input.decoder, input.options, output)
return &imageOpsResult{
result: output,
err: err,
}
}
func (w *imageOpsWorker) BlockUntilReady() {
//
}
func (w *imageOpsWorker) Interrupt() {
//
}
func (w *imageOpsWorker) Terminate() {
log.Debug("Shutting down worker")
w.ops.Close()
}
func newImageOpsWorker() *imageOpsWorker {
log.Debug("Initializing worker")
return &imageOpsWorker{
ops: lilliput.NewImageOps(MAX_DIMENSIONS),
}
}
var (
configListenAddr string
configSharedKey string
configDimensions string
)
var EncodeOptions = map[string]map[int]int{
".jpeg": {lilliput.JpegQuality: 85},
".png": {lilliput.PngCompression: 7},
".webp": {lilliput.WebpQuality: 85},
}
func nextRequestID() string {
return uuid.New().String()
}
var log = logrus.New()
func main() {
log.Out = os.Stdout
log.Level = logrus.DebugLevel
flag.StringVar(&configListenAddr, "bind", DEFAULT_BIND, "Bind address")
flag.StringVar(&configSharedKey, "key", DEFAULT_KEY, "Shared HMAC secret")
flag.StringVar(&configDimensions, "dimensions", "", "Which target sizes besides the original one will be accessible (comma-separated)")
flag.Parse()
listenAddr := configListenAddr
sharedKey := []byte(configSharedKey)
dimensionsMap := map[int64]bool{}
log.Info("Welcome to Gamo, the image proxy and optimization server")
log.Info(fmt.Sprintf("Starting on %s...", listenAddr))
if len(configDimensions) > 0 {
log.Info("With dimensions: ", configDimensions)
for _, x := range strings.Split(configDimensions, ",") {
parsedDimensions, err := strconv.ParseInt(x, 10, 0)
if err != nil {
log.Fatal("Unrecognized value in dimensions: ", x)
}
dimensionsMap[parsedDimensions] = true
}
} else {
log.Info("Without resizing")
}
pool := tunny.New(2, func() tunny.Worker {
return newImageOpsWorker()
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
requestID := nextRequestID()
requestLog := log.WithFields(logrus.Fields{"request-id": requestID})
w.Header().Set("X-Request-Id", requestID)
segments := strings.Split(r.URL.Path, "/")
dimensions := MAX_DIMENSIONS
if len(segments) < 3 {
http.Error(w, "Not found", http.StatusNotFound)
return
}
encodedMAC, encodedImageURL := segments[1], segments[2]
if len(segments) >= 4 {
parsedDimensions, err := strconv.ParseInt(segments[3], 10, 0)
if err != nil || !dimensionsMap[parsedDimensions] {
http.Error(w, "Not found", http.StatusNotFound)
return
}
dimensions = int(parsedDimensions)
}
imageURL, err := hex.DecodeString(encodedImageURL)
if err != nil {
http.Error(w, "Bad request", http.StatusBadRequest)
return
}
messageMAC, err := hex.DecodeString(encodedMAC)
if err != nil {
http.Error(w, "Bad request", http.StatusBadRequest)
return
}
mac := hmac.New(sha1.New, sharedKey)
mac.Write(imageURL)
expectedMAC := mac.Sum(nil)
if hmac.Equal(messageMAC, expectedMAC) {
requestLog = requestLog.WithFields(logrus.Fields{"url": string(imageURL)})
resp, err := http.Get(string(imageURL))
if err != nil {
requestLog.Error(fmt.Sprintf("Error performing request: %s", err))
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
defer resp.Body.Close()
contentLength, _ := strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 0)
if contentLength > MAX_CONTENT_LENGTH {
requestLog.Error("Image exceeds length limit")
http.Error(w, "Bad request", http.StatusBadRequest)
return
}
originalImage, err := io.ReadAll(resp.Body)
if err != nil {
requestLog.Error(fmt.Sprintf("Error reading response body: %s", err))
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
decoder, err := lilliput.NewDecoder(originalImage)
originalImage = nil
if err != nil {
requestLog.Error(fmt.Sprintf("Error decoding image: %s", err))
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
defer decoder.Close()
header, err := decoder.Header()
if err != nil {
requestLog.Error(fmt.Sprintf("Error reading image header: %s", err))
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
outputFormat := "." + strings.ToLower(decoder.Description())
outputPixels := dimensions * dimensions
var (
outputWidth int
outputHeight int
)
if (header.Width() * header.Height()) > outputPixels {
outputWidth = int(math.Round(math.Sqrt(float64(outputPixels) * (float64(header.Width()) / float64(header.Height())))))
outputHeight = int(math.Round(math.Sqrt(float64(outputPixels) * (float64(header.Height()) / float64(header.Width())))))
} else {
outputWidth = header.Width()
outputHeight = header.Height()
}
resizeOptions := &lilliput.ImageOptions{
FileType: outputFormat,
Width: outputWidth,
Height: outputHeight,
ResizeMethod: lilliput.ImageOpsResize,
NormalizeOrientation: true,
EncodeOptions: EncodeOptions[outputFormat],
}
result := pool.Process(&imageOpsPayload{
decoder: decoder,
options: resizeOptions,
}).(*imageOpsResult)
if result.err != nil {
requestLog.Error(fmt.Sprintf("Error transforming image: %s", err))
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
outputImage := result.result
w.Header().Set("Content-Length", strconv.FormatInt(int64(len(outputImage)), 10))
w.Header().Set("Content-Type", resp.Header.Get("Content-Type"))
w.Header().Set("Cache-Control", "public, max-age=31536000")
w.Header().Set("Expires", time.Now().Add(31536000*time.Second).In(time.UTC).Format("Mon, 02 Jan 2006 15:04:05 GMT"))
w.Header().Set("Vary", "Accept-Encoding")
w.Header().Set("Etag", fmt.Sprintf("%d-%x", len(outputImage), sha1.Sum(outputImage)))
if r.Method != "HEAD" {
_, err = w.Write(outputImage)
if err != nil {
requestLog.Error(fmt.Sprintf("Error writing response: %s", err))
}
}
} else {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
}
})
log.Fatal(http.ListenAndServe(listenAddr, nil))
}