This repository has been archived by the owner on Mar 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cdn-origin.go
334 lines (292 loc) · 12.1 KB
/
cdn-origin.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
package main
import (
"bytes"
"database/sql"
"fmt"
"html/template"
"strings"
"time"
"unsafe"
"github.com/whats-this/cdn-origin/prometheus"
"github.com/whats-this/cdn-origin/weed"
log "github.com/Sirupsen/logrus"
_ "github.com/lib/pq"
prom "github.com/prometheus/client_golang/prometheus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/valyala/fasthttp"
)
// version is the current version of cdn-origin.
const version = "0.2.0"
// HTTP header strings.
const (
accept = "Accept"
host = "Host"
)
// cdnUtil is the path to CDN utilities endpoint.
const cdnUtil = "/.cdn-util"
// authKey is the query parameter for authentication on the CDN utilities endpoint.
const keyParam = "authKey"
// redirectHTML is the html/template template for generating redirect HTML.
const redirectHTML = `<html><head><meta charset="UTF-8" /><meta http-equiv=refresh content="0; url={{.}}" /><script type="text/javascript">window.location.href="{{.}}"</script><title>Redirect</title></head><body><p>If you are not redirected automatically, click <a href="{{.}}">here</a> to go to the destination.</p></body></html>`
var redirectHTMLTemplate *template.Template
// redirectPreviewHTML is the html/template template for generating redirect preview HTML.
const redirectPreviewHTML = `<html><head><meta charset="UTF-8" /><title>Redirect Preview</title></head><body><p>This link goes to <code>{{.}}</code>. If you would like to visit this link, click <a href="{{.}}">here</a> to go to the destination.</p></body></html>`
var redirectPreviewHTMLTemplate *template.Template
func init() {
// Read in configuration
flags := pflag.NewFlagSet("cdn-origin", pflag.ExitOnError)
// cdnUtil.authKey (string=""): authentication token for accessing /.cdn-util (cdnUtil)
flags.String("cdnutil-auth-key", "", "Authentication token for accessing /.cdn-util (util)")
viper.BindPFlag("cdnUtil.authKey", flags.Lookup("cdnutil-auth-key"))
viper.BindEnv("cdnUtil.authKey", "CDNUTIL_AUTH_KEY")
// cdnUtil.serveMetrics (bool=false): serve Prometheus metrics (cdnUtil)
flags.Bool("cdnutil-serve-metrics", false, "Serve Prometheus metrics (cdnUtil)")
viper.BindPFlag("cdnUtil.serveMetrics", flags.Lookup("cdnutil-serve-metrics"))
viper.BindEnv("cdnUtil.serveMetrics", "CDNUTIL_SERVE_METRICS")
// http.compressResponse (bool=false): enable transparent response compression
flags.Bool("compress-response", false, "Enable transparent response compression")
viper.BindPFlag("http.compressResponse", flags.Lookup("compress-response"))
viper.BindEnv("http.compressResponse", "HTTP_COMPRESS_RESPONSE")
// http.listenAddress (string=":8080"): TCP address to listen to for HTTP requests
flags.String("listen-address", ":8080", "TCP address to listen to for HTTP requests")
viper.BindPFlag("http.listenAddress", flags.Lookup("listen-address"))
viper.BindEnv("http.listenAddress", "HTTP_LISTEN_ADDRESS")
// log.debug (bool=false): enable debug mode (logs requests and prints other information)
flags.Bool("debug", false, "Enable debug mode (logs requests and prints other information)")
viper.BindPFlag("log.debug", flags.Lookup("debug"))
viper.BindEnv("log.debug", "DEBUG")
// database.connectionURL* (string): PostgreSQL connection URL
flags.String("database-connection-url", "", "* PostgreSQL connection URL")
viper.BindPFlag("database.connectionURL", flags.Lookup("database-connection-url"))
viper.BindEnv("database.connectionURL", "DATABASE_CONNECTION_URL")
// seaweed.masterURL* (string): SeaweedFS master URL
flags.String("seaweed-master-url", "", "* SeaweedFS master URL")
viper.BindPFlag("seaweed.masterURL", flags.Lookup("seaweed-master-url"))
viper.BindEnv("seaweed.masterURL", "SEAWEED_MASTER_URL")
// Configuration file settings
viper.SetConfigType("toml")
viper.SetConfigName("cdn-origin")
viper.AddConfigPath(".")
viper.AddConfigPath("/etc/cdn-origin/")
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
log.WithField("err", err).Fatal("failed to read in configuration")
}
}
// Enable debug mode
if viper.GetBool("log.debug") {
log.SetLevel(log.DebugLevel)
}
// Print configuration variables to debug
log.WithFields(log.Fields{
"cdnUtil.authKey": len(viper.GetString("cdnUtil.authKey")) != 0,
"cdnUtil.serveMetrics": viper.GetBool("cdnUtil.serveMetrics"),
"database.connectionURL": viper.GetString("database.connectionURL"),
"http.compressResponse": viper.GetBool("http.compressResponse"),
"http.listenAddress": viper.GetString("http.listenAddress"),
"log.debug": viper.GetBool("log.debug"),
"seaweed.masterURL": viper.GetString("seaweed.masterURL"),
}).Debug("retrieved configuration")
// Ensure required configuration variables are set
if len(viper.GetString("database.connectionURL")) == 0 {
log.Fatal("database.connectionURL is required")
}
if len(viper.GetString("seaweed.masterURL")) == 0 {
log.Fatal("seaweed.masterURL is required")
}
}
// db holds the current PostgreSQL database connection.
var db *sql.DB
// seaweed client to use for fetching files from the SeaweedFS cluster.
var seaweed *weed.Seaweed
func main() {
var err error
// Attempt to connect to SeaweedFS master
seaweed = weed.New(viper.GetString("seaweed.masterURL"), time.Second*5)
err = seaweed.Ping()
if err != nil {
log.WithField("err", err).Fatal("failed to ping SeaweedFS master")
return
}
// Connect to PostgreSQL database
db, err = sql.Open("postgres", viper.GetString("database.connectionURL"))
if err != nil {
log.WithField("err", err).Fatal("failed to open database connection")
return
}
// Parse redirect templates
redirectHTMLTemplate, err = template.New("redirectHTML").Parse(redirectHTML)
if err != nil {
log.WithField("err", err).Fatal("failed to parse redirectHTML template")
return
}
redirectPreviewHTMLTemplate, err = template.New("redirectPreviewHTML").Parse(redirectPreviewHTML)
if err != nil {
log.WithField("err", err).Fatal("failed to parse redirectPreviewHTML template")
return
}
// Launch server
h := requestHandler
if viper.GetBool("http.compressResponse") {
h = fasthttp.CompressHandler(h)
}
log.Info("Attempting to listen on " + viper.GetString("http.listenAddress"))
server := &fasthttp.Server{
Handler: h,
Name: "whats-this/cdn-origin/" + version,
ReadBufferSize: 1024 * 6, // 6 KB
ReadTimeout: time.Minute * 30,
WriteTimeout: time.Minute * 30,
GetOnly: true,
LogAllErrors: log.GetLevel() == log.DebugLevel,
DisableHeaderNamesNormalizing: true,
Logger: log.New(),
}
if err := server.ListenAndServe(viper.GetString("http.listenAddress")); err != nil {
log.WithField("err", err).Fatal("error in ListenAndServe")
}
}
func requestHandler(ctx *fasthttp.RequestCtx) {
path := string(ctx.Path())
// Log requests in debug mode, wrapped in an if statement to prevent unnecessary memory allocations
if log.GetLevel() == log.DebugLevel {
log.WithFields(log.Fields{
"connRequestNumber": ctx.ConnRequestNum(),
// "connTime": ctx.ConnTime(),
"method": string(ctx.Method()),
// "path": path,
"queryString": ctx.QueryArgs(),
"remoteIP": ctx.RemoteIP(),
"requestURI": string(ctx.RequestURI()),
// "time": ctx.Time(),
// "userAgent": string(ctx.UserAgent()),
}).Debug("request received")
}
// CDN utilities endpoint (only accessible if key is supplied and correct)
if len(viper.GetString("cdnUtil.authKey")) != 0 && strings.HasPrefix(path, cdnUtil) && string(ctx.QueryArgs().Peek(keyParam)) == viper.GetString("cdnUtil.authKey") {
switch path[len(cdnUtil):] {
case "/prometheus/metrics":
if !viper.GetBool("cdnUtil.serveMetrics") {
break
}
contentType, err := prometheus.WriteMetrics(ctx, string(ctx.Request.Header.Peek(accept)))
if err != nil {
log.WithField("err", err).Error("failed to generate util Prometheus response")
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
ctx.SetContentType("text/plain; charset=utf8")
fmt.Fprint(ctx, "500 Internal Server Error")
return
}
ctx.SetContentType(contentType)
return
}
ctx.SetStatusCode(fasthttp.StatusNotFound)
ctx.SetContentType("text/plain; charset=utf8")
fmt.Fprintf(ctx, "404 Not Found: %s", ctx.Path())
return
}
// Update metrics if metrics are being served
if viper.GetBool("cdnUtil.serveMetrics") {
host := ctx.Request.Header.Peek(host)
if len(host) != 0 {
prometheus.HTTPRequestsTotal.With(prom.Labels{"host": string(host)}).Inc()
}
}
// Fetch object from database
var backend_file_id sql.NullString
var content_type sql.NullString
var dest_url sql.NullString
var object_type int
err := db.QueryRow(
`SELECT backend_file_id, content_type, dest_url, "type" FROM objects WHERE bucket_key=$1 LIMIT 1`,
fmt.Sprintf("public%s", path)).Scan(&backend_file_id, &content_type, &dest_url, &object_type)
switch {
case err == sql.ErrNoRows:
ctx.SetStatusCode(fasthttp.StatusNotFound)
ctx.SetContentType("text/plain; charset=utf8")
fmt.Fprintf(ctx, "404 Not Found: %s", ctx.Path())
return
case err != nil:
log.WithField("err", err).Error("failed to run SELECT query on database")
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
ctx.SetContentType("text/plain; charset=utf8")
fmt.Fprint(ctx, "500 Internal Server Error")
return
}
switch object_type {
case 0: // file
// Get object from SeaweedFS and write to response
if !backend_file_id.Valid {
log.WithField("bucket_key", path).Warn("found file object with NULL backend_file_id")
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
ctx.SetContentType("text/plain; charset=utf8")
fmt.Fprint(ctx, "500 Internal Server Error")
}
if content_type.Valid {
ctx.SetContentType(content_type.String)
} else {
ctx.SetContentType("application/octet-stream")
}
// TODO: ?thumbnail query parameter for images
statusCode, err := seaweed.Get(ctx, backend_file_id.String, "")
if err != nil {
log.WithField("err", err).Warn("failed to retrieve file from SeaweedFS volume server")
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
ctx.SetContentType("text/plain; charset=utf8")
fmt.Fprint(ctx, "500 Internal Server Error")
return
}
if statusCode != fasthttp.StatusOK {
log.WithFields(log.Fields{
"expected": fasthttp.StatusOK,
"got": statusCode,
}).Warn("unexpected status code while retrieving file from SeaweedFS volume server")
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
ctx.SetContentType("text/plain; charset=utf8")
fmt.Fprint(ctx, "500 Internal Server Error")
}
case 1: // redirect
if !dest_url.Valid {
log.Warn("encountered redirect object with NULL dest_url")
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
ctx.SetContentType("text/plain; charset=utf8")
fmt.Fprint(ctx, "500 Internal Server Error")
}
if ctx.QueryArgs().Has("preview") {
buf := new(bytes.Buffer)
err := redirectPreviewHTMLTemplate.Execute(buf, dest_url.String)
if err != nil {
log.WithFields(log.Fields{
"err": err,
"dest_url": dest_url.String,
}).Warn("failed to generate redirect preview HTML to send to client")
ctx.SetContentType("text/plain; charset=utf8")
fmt.Fprintf(ctx, "Failed to generate preview page, destination URL: %s", dest_url.String)
return
}
b := buf.Bytes()
s := *(*string)(unsafe.Pointer(&b))
ctx.SetContentType("text/html; charset=utf8")
fmt.Fprint(ctx, s)
} else {
buf := new(bytes.Buffer)
err := redirectHTMLTemplate.Execute(buf, dest_url.String)
if err != nil {
log.WithFields(log.Fields{
"err": err,
"dest_url": dest_url.String,
}).Warn("failed to generate redirect HTML to send to client")
ctx.SetContentType("text/plain; charset=utf8")
fmt.Fprintf(ctx, "Failed to generate HTML fallback page, destination URL: %s", dest_url.String)
return
}
b := buf.Bytes()
s := *(*string)(unsafe.Pointer(&b))
ctx.SetStatusCode(fasthttp.StatusMovedPermanently)
ctx.SetContentType("text/html; charset=utf8")
ctx.Response.Header.Set("Location", dest_url.String)
fmt.Fprint(ctx, s)
}
}
}