-
Notifications
You must be signed in to change notification settings - Fork 507
Expand file tree
/
Copy pathmodule.go
More file actions
166 lines (145 loc) · 4.15 KB
/
Copy pathmodule.go
File metadata and controls
166 lines (145 loc) · 4.15 KB
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
// Package caddy provides ImageProxy as a Caddy module.
package caddy
import (
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
caddy "github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/gregjones/httpcache/diskcache"
"github.com/peterbourgon/diskv"
"go.uber.org/zap"
"willnorris.com/go/imageproxy"
)
func init() {
caddy.RegisterModule(ImageProxy{})
httpcaddyfile.RegisterHandlerDirective("imageproxy", parseCaddyfile)
httpcaddyfile.RegisterDirectiveOrder("imageproxy", "after", "reverse_proxy")
}
type ImageProxy struct {
Cache string `json:"cache,omitempty"`
DefaultBaseURL string `json:"default_base_url,omitempty"`
AllowHosts []string `json:"allow_hosts,omitempty"`
DenyHosts []string `json:"deny_hosts,omitempty"`
Referrers []string `json:"referrers,omitempty"`
ContentTypes []string `json:"content_types,omitempty"`
SignatureKeys []string `json:"signature_keys,omitempty"`
Verbose bool `json:"verbose,omitempty"`
logger *zap.Logger
proxy *imageproxy.Proxy
}
// interface guard
var (
_ caddyhttp.MiddlewareHandler = (*ImageProxy)(nil)
)
// CaddyModule returns the Caddy module information.
func (ImageProxy) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.imageproxy",
New: func() caddy.Module { return new(ImageProxy) },
}
}
func (p *ImageProxy) Provision(ctx caddy.Context) error {
p.logger = ctx.Logger()
cache, _ := parseCache(p.Cache)
p.proxy = imageproxy.NewProxy(nil, cache)
p.proxy.DefaultBaseURL, _ = url.Parse(p.DefaultBaseURL)
p.proxy.AllowHosts = p.AllowHosts
p.proxy.DenyHosts = p.DenyHosts
p.proxy.Referrers = p.Referrers
p.proxy.ContentTypes = p.ContentTypes
if len(p.proxy.ContentTypes) == 0 {
p.proxy.ContentTypes = []string{"image/*"}
}
for _, key := range p.SignatureKeys {
p.proxy.SignatureKeys = append(p.proxy.SignatureKeys, []byte(key))
}
p.proxy.Logger = zap.NewStdLog(p.logger)
p.proxy.Verbose = p.Verbose
p.proxy.FollowRedirects = true
return nil
}
func (p *ImageProxy) ServeHTTP(w http.ResponseWriter, r *http.Request, _ caddyhttp.Handler) error {
p.proxy.ServeHTTP(w, r)
return nil
}
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
p := new(ImageProxy)
h.Next() // consume the directive name
for nesting := h.Nesting(); h.NextBlock(nesting); {
switch h.Val() {
case "cache":
if !h.NextArg() {
return nil, h.ArgErr()
}
p.Cache = h.Val()
case "default_base_url":
if !h.NextArg() {
return nil, h.ArgErr()
}
p.DefaultBaseURL = h.Val()
case "allow_hosts":
if !h.NextArg() {
return nil, h.ArgErr()
}
p.AllowHosts = append(p.AllowHosts, strings.Split(h.Val(), ",")...)
case "deny_hosts":
if !h.NextArg() {
return nil, h.ArgErr()
}
p.DenyHosts = append(p.DenyHosts, strings.Split(h.Val(), ",")...)
case "referrers":
if !h.NextArg() {
return nil, h.ArgErr()
}
p.Referrers = append(p.Referrers, strings.Split(h.Val(), ",")...)
case "content_types":
if !h.NextArg() {
return nil, h.ArgErr()
}
p.ContentTypes = append(p.ContentTypes, strings.Split(h.Val(), ",")...)
case "signature_key":
if !h.NextArg() {
return nil, h.ArgErr()
}
p.SignatureKeys = append(p.SignatureKeys, h.Val())
case "verbose":
if !h.NextArg() {
return nil, h.ArgErr()
}
p.Verbose, _ = strconv.ParseBool(h.Val())
}
}
return p, nil
}
// parseCache parses c returns the specified Cache implementation.
func parseCache(c string) (imageproxy.Cache, error) {
const defaultMemorySize = 100
if c == "" {
return nil, nil
}
if c == "memory" {
c = fmt.Sprintf("memory:%d", defaultMemorySize)
}
u, err := url.Parse(c)
if err != nil {
return nil, fmt.Errorf("error parsing cache flag: %w", err)
}
switch u.Scheme {
case "file":
return diskCache(u.Path), nil
default:
return diskCache(c), nil
}
}
func diskCache(path string) *diskcache.Cache {
d := diskv.New(diskv.Options{
BasePath: path,
// For file "c0ffee", store file as "c0/ff/c0ffee"
Transform: func(s string) []string { return []string{s[0:2], s[2:4]} },
})
return diskcache.NewWithDiskv(d)
}