forked from imgproxy/imgproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
240 lines (189 loc) · 5.57 KB
/
config.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
package main
import (
"bytes"
"crypto/rand"
"encoding/hex"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"runtime"
"strconv"
)
func intEnvConfig(i *int, name string) {
if env, err := strconv.Atoi(os.Getenv(name)); err == nil {
*i = env
}
}
func megaIntEnvConfig(f *int, name string) {
if env, err := strconv.ParseFloat(os.Getenv(name), 64); err == nil {
*f = int(env * 1000000)
}
}
func strEnvConfig(s *string, name string) {
if env := os.Getenv(name); len(env) > 0 {
*s = env
}
}
func boolEnvConfig(b *bool, name string) {
*b = false
if env, err := strconv.ParseBool(os.Getenv(name)); err == nil {
*b = env
}
}
func hexEnvConfig(b *[]byte, name string) {
var err error
if env := os.Getenv(name); len(env) > 0 {
if *b, err = hex.DecodeString(env); err != nil {
log.Fatalf("%s expected to be hex-encoded string\n", name)
}
}
}
func hexFileConfig(b *[]byte, filepath string) {
if len(filepath) == 0 {
return
}
f, err := os.Open(filepath)
if err != nil {
log.Fatalf("Can't open file %s\n", filepath)
}
src, err := ioutil.ReadAll(f)
if err != nil {
log.Fatalln(err)
}
src = bytes.TrimSpace(src)
dst := make([]byte, hex.DecodedLen(len(src)))
n, err := hex.Decode(dst, src)
if err != nil {
log.Fatalf("%s expected to contain hex-encoded string\n", filepath)
}
*b = dst[:n]
}
type config struct {
Bind string
ReadTimeout int
WaitTimeout int
WriteTimeout int
DownloadTimeout int
Concurrency int
MaxClients int
TTL int
MaxSrcDimension int
MaxSrcResolution int
Quality int
GZipCompression int
Key []byte
Salt []byte
Secret string
AllowOrigin string
LocalFileSystemRoot string
ETagEnabled bool
ETagSignature []byte
BaseURL string
}
var conf = config{
Bind: ":8080",
ReadTimeout: 10,
WriteTimeout: 10,
DownloadTimeout: 5,
Concurrency: runtime.NumCPU() * 2,
TTL: 3600,
MaxSrcDimension: 8192,
MaxSrcResolution: 16800000,
Quality: 80,
GZipCompression: 5,
ETagEnabled: false,
}
func init() {
keypath := flag.String("keypath", "", "path of the file with hex-encoded key")
saltpath := flag.String("saltpath", "", "path of the file with hex-encoded salt")
flag.Parse()
if port := os.Getenv("PORT"); len(port) > 0 {
conf.Bind = fmt.Sprintf(":%s", port)
}
strEnvConfig(&conf.Bind, "IMGPROXY_BIND")
intEnvConfig(&conf.ReadTimeout, "IMGPROXY_READ_TIMEOUT")
intEnvConfig(&conf.WriteTimeout, "IMGPROXY_WRITE_TIMEOUT")
intEnvConfig(&conf.DownloadTimeout, "IMGPROXY_DOWNLOAD_TIMEOUT")
intEnvConfig(&conf.Concurrency, "IMGPROXY_CONCURRENCY")
intEnvConfig(&conf.MaxClients, "IMGPROXY_MAX_CLIENTS")
intEnvConfig(&conf.TTL, "IMGPROXY_TTL")
intEnvConfig(&conf.MaxSrcDimension, "IMGPROXY_MAX_SRC_DIMENSION")
megaIntEnvConfig(&conf.MaxSrcResolution, "IMGPROXY_MAX_SRC_RESOLUTION")
intEnvConfig(&conf.Quality, "IMGPROXY_QUALITY")
intEnvConfig(&conf.GZipCompression, "IMGPROXY_GZIP_COMPRESSION")
hexEnvConfig(&conf.Key, "IMGPROXY_KEY")
hexEnvConfig(&conf.Salt, "IMGPROXY_SALT")
hexFileConfig(&conf.Key, *keypath)
hexFileConfig(&conf.Salt, *saltpath)
strEnvConfig(&conf.Secret, "IMGPROXY_SECRET")
strEnvConfig(&conf.AllowOrigin, "IMGPROXY_ALLOW_ORIGIN")
strEnvConfig(&conf.LocalFileSystemRoot, "IMGPROXY_LOCAL_FILESYSTEM_ROOT")
boolEnvConfig(&conf.ETagEnabled, "IMGPROXY_USE_ETAG")
strEnvConfig(&conf.BaseURL, "IMGPROXY_BASE_URL")
if len(conf.Key) == 0 {
log.Fatalln("Key is not defined")
}
if len(conf.Salt) == 0 {
log.Fatalln("Salt is not defined")
}
if len(conf.Bind) == 0 {
log.Fatalln("Bind address is not defined")
}
if conf.ReadTimeout <= 0 {
log.Fatalf("Read timeout should be greater than 0, now - %d\n", conf.ReadTimeout)
}
if conf.WriteTimeout <= 0 {
log.Fatalf("Write timeout should be greater than 0, now - %d\n", conf.WriteTimeout)
}
if conf.DownloadTimeout <= 0 {
log.Fatalf("Download timeout should be greater than 0, now - %d\n", conf.DownloadTimeout)
}
if conf.Concurrency <= 0 {
log.Fatalf("Concurrency should be greater than 0, now - %d\n", conf.Concurrency)
}
if conf.MaxClients <= 0 {
conf.MaxClients = conf.Concurrency * 10
}
if conf.TTL <= 0 {
log.Fatalf("TTL should be greater than 0, now - %d\n", conf.TTL)
}
if conf.MaxSrcDimension <= 0 {
log.Fatalf("Max src dimension should be greater than 0, now - %d\n", conf.MaxSrcDimension)
}
if conf.MaxSrcResolution <= 0 {
log.Fatalf("Max src resolution should be greater than 0, now - %d\n", conf.MaxSrcResolution)
}
if conf.Quality <= 0 {
log.Fatalf("Quality should be greater than 0, now - %d\n", conf.Quality)
} else if conf.Quality > 100 {
log.Fatalf("Quality can't be greater than 100, now - %d\n", conf.Quality)
}
if conf.GZipCompression < 0 {
log.Fatalf("GZip compression should be greater than or quual to 0, now - %d\n", conf.GZipCompression)
} else if conf.GZipCompression > 9 {
log.Fatalf("GZip compression can't be greater than 9, now - %d\n", conf.GZipCompression)
}
if conf.LocalFileSystemRoot != "" {
stat, err := os.Stat(conf.LocalFileSystemRoot)
if err != nil {
log.Fatalf("Cannot use local directory: %s", err)
} else {
if !stat.IsDir() {
log.Fatalf("Cannot use local directory: not a directory")
}
}
if conf.LocalFileSystemRoot == "/" {
log.Print("Exposing root via IMGPROXY_LOCAL_FILESYSTEM_ROOT is unsafe")
}
}
if conf.ETagEnabled {
conf.ETagSignature = make([]byte, 16)
rand.Read(conf.ETagSignature)
log.Printf("ETag support is activated. The random value was generated to be used for ETag calculation: %s\n",
fmt.Sprintf("%x", conf.ETagSignature))
}
initVips()
initDownloading()
}