1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-02-02 11:34:20 +02:00
imgproxy/config.go

410 lines
10 KiB
Go
Raw Normal View History

2017-06-20 16:58:55 +03:00
package main
import (
2018-09-07 23:41:06 +06:00
"bufio"
2017-06-20 16:58:55 +03:00
"encoding/hex"
"flag"
"fmt"
2017-06-20 16:58:55 +03:00
"log"
"os"
2017-09-28 23:17:23 +06:00
"runtime"
2017-06-26 01:20:48 +03:00
"strconv"
2018-09-07 23:41:06 +06:00
"strings"
2017-06-20 16:58:55 +03:00
)
2017-06-26 01:20:48 +03:00
func intEnvConfig(i *int, name string) {
if env, err := strconv.Atoi(os.Getenv(name)); err == nil {
*i = env
}
2017-06-20 16:58:55 +03:00
}
2018-10-19 15:47:11 +06:00
func floatEnvConfig(i *float64, name string) {
if env, err := strconv.ParseFloat(os.Getenv(name), 64); 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)
}
}
2017-06-26 01:20:48 +03:00
func strEnvConfig(s *string, name string) {
if env := os.Getenv(name); len(env) > 0 {
*s = env
2017-06-20 16:58:55 +03:00
}
2017-06-26 01:20:48 +03:00
}
2017-06-20 16:58:55 +03:00
func boolEnvConfig(b *bool, name string) {
*b = false
if env, err := strconv.ParseBool(os.Getenv(name)); err == nil {
*b = env
}
}
2018-11-15 18:35:06 +06:00
func hexEnvConfig(b *[]securityKey, name string) {
2017-06-26 01:20:48 +03:00
var err error
if env := os.Getenv(name); len(env) > 0 {
2018-11-15 18:35:06 +06:00
parts := strings.Split(env, ",")
keys := make([]securityKey, len(parts))
for i, part := range parts {
if keys[i], err = hex.DecodeString(part); err != nil {
log.Fatalf("%s expected to be hex-encoded strings. Invalid: %s\n", name, part)
}
2017-06-26 01:20:48 +03:00
}
2018-11-15 18:35:06 +06:00
*b = keys
2017-06-26 01:20:48 +03:00
}
2017-06-20 16:58:55 +03:00
}
2018-11-15 18:35:06 +06:00
func hexFileConfig(b *[]securityKey, filepath string) {
2017-06-26 01:20:48 +03:00
if len(filepath) == 0 {
return
}
2017-06-20 16:58:55 +03:00
2017-06-27 11:29:57 +03:00
f, err := os.Open(filepath)
2017-06-20 16:58:55 +03:00
if err != nil {
2017-06-27 11:29:57 +03:00
log.Fatalf("Can't open file %s\n", filepath)
2017-06-20 16:58:55 +03:00
}
2018-11-15 18:35:06 +06:00
keys := []securityKey{}
2017-06-20 16:58:55 +03:00
2018-11-15 18:35:06 +06:00
scanner := bufio.NewScanner(f)
for scanner.Scan() {
part := scanner.Text()
2017-06-26 01:20:48 +03:00
2018-11-15 18:35:06 +06:00
if len(part) == 0 {
continue
}
if key, err := hex.DecodeString(part); err == nil {
keys = append(keys, key)
} else {
log.Fatalf("%s expected to contain hex-encoded strings. Invalid: %s\n", filepath, part)
}
}
if err := scanner.Err(); err != nil {
log.Fatalf("Failed to read file %s: %s", filepath, err)
2017-06-20 16:58:55 +03:00
}
2018-11-15 18:35:06 +06:00
*b = keys
2017-06-26 01:20:48 +03:00
}
2017-06-20 16:58:55 +03:00
func presetEnvConfig(p presets, name string) {
2018-09-07 23:41:06 +06:00
if env := os.Getenv(name); len(env) > 0 {
presetStrings := strings.Split(env, ",")
for _, presetStr := range presetStrings {
2018-11-06 17:19:34 +06:00
if err := parsePreset(p, presetStr); err != nil {
log.Fatalln(err)
}
2018-09-07 23:41:06 +06:00
}
}
}
func presetFileConfig(p presets, filepath string) {
2018-09-07 23:41:06 +06:00
if len(filepath) == 0 {
return
}
f, err := os.Open(filepath)
if err != nil {
log.Fatalf("Can't open file %s\n", filepath)
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
2018-11-06 17:19:34 +06:00
if err := parsePreset(p, scanner.Text()); err != nil {
log.Fatalln(err)
}
2018-09-07 23:41:06 +06:00
}
if err := scanner.Err(); err != nil {
log.Fatalf("Failed to read presets file: %s", err)
}
2018-09-07 23:41:06 +06:00
}
2017-06-26 01:20:48 +03:00
type config struct {
2017-07-05 18:28:22 +06:00
Bind string
ReadTimeout int
2018-03-15 20:53:39 +06:00
WaitTimeout int
2017-07-05 18:28:22 +06:00
WriteTimeout int
DownloadTimeout int
Concurrency int
2017-09-15 13:29:51 +03:00
MaxClients int
2017-07-05 18:28:22 +06:00
TTL int
2017-07-03 16:10:44 +06:00
MaxSrcDimension int
MaxSrcResolution int
2018-11-15 20:04:12 +06:00
MaxGifFrames int
2017-06-20 16:58:55 +03:00
JpegProgressive bool
PngInterlaced bool
2017-06-27 03:41:37 +03:00
Quality int
GZipCompression int
2017-06-20 16:58:55 +03:00
2018-09-11 18:26:27 +06:00
EnableWebpDetection bool
EnforceWebp bool
EnableClientHints bool
2018-09-11 18:26:27 +06:00
2018-11-15 18:35:06 +06:00
Keys []securityKey
Salts []securityKey
AllowInsecure bool
SignatureSize int
Secret string
2018-04-26 17:22:31 +06:00
AllowOrigin string
UserAgent string
IgnoreSslVerification bool
LocalFileSystemRoot string
S3Enabled bool
2018-11-13 19:23:59 +06:00
S3Region string
S3Endpoint string
2018-10-30 18:12:56 +06:00
GCSKey string
2018-02-26 15:41:37 +06:00
ETagEnabled bool
2018-04-26 17:38:40 +06:00
BaseURL string
2018-09-07 23:41:06 +06:00
Presets presets
2018-10-19 15:47:11 +06:00
WatermarkData string
WatermarkPath string
WatermarkURL string
WatermarkOpacity float64
2018-10-25 19:24:34 +06:00
NewRelicAppName string
NewRelicKey string
2018-10-29 18:04:47 +06:00
PrometheusBind string
2018-11-14 19:41:16 +06:00
BugsnagKey string
BugsnagStage string
HoneybadgerKey string
HoneybadgerEnv string
2017-06-26 01:20:48 +03:00
}
2017-06-20 16:58:55 +03:00
2017-06-26 01:20:48 +03:00
var conf = config{
Bind: ":8080",
ReadTimeout: 10,
WriteTimeout: 10,
DownloadTimeout: 5,
Concurrency: runtime.NumCPU() * 2,
TTL: 3600,
IgnoreSslVerification: false,
MaxSrcResolution: 16800000,
2018-11-15 20:04:12 +06:00
MaxGifFrames: 1,
AllowInsecure: false,
SignatureSize: 32,
Quality: 80,
GZipCompression: 5,
UserAgent: fmt.Sprintf("imgproxy/%s", version),
ETagEnabled: false,
S3Enabled: false,
2018-10-19 15:47:11 +06:00
WatermarkOpacity: 1,
2018-11-14 19:41:16 +06:00
BugsnagStage: "production",
HoneybadgerEnv: "production",
2017-06-26 01:20:48 +03:00
}
func init() {
2018-09-07 23:41:06 +06:00
keyPath := flag.String("keypath", "", "path of the file with hex-encoded key")
saltPath := flag.String("saltpath", "", "path of the file with hex-encoded salt")
presetsPath := flag.String("presets", "", "path of the file with presets")
showVersion := flag.Bool("v", false, "show version")
2017-06-26 01:20:48 +03:00
flag.Parse()
if *showVersion {
fmt.Println(version)
os.Exit(0)
}
if port := os.Getenv("PORT"); len(port) > 0 {
conf.Bind = fmt.Sprintf(":%s", port)
}
2017-06-26 01:20:48 +03:00
strEnvConfig(&conf.Bind, "IMGPROXY_BIND")
intEnvConfig(&conf.ReadTimeout, "IMGPROXY_READ_TIMEOUT")
intEnvConfig(&conf.WriteTimeout, "IMGPROXY_WRITE_TIMEOUT")
2017-07-05 18:28:22 +06:00
intEnvConfig(&conf.DownloadTimeout, "IMGPROXY_DOWNLOAD_TIMEOUT")
2017-07-04 20:05:53 +06:00
intEnvConfig(&conf.Concurrency, "IMGPROXY_CONCURRENCY")
2017-09-15 13:29:51 +03:00
intEnvConfig(&conf.MaxClients, "IMGPROXY_MAX_CLIENTS")
2017-06-26 01:20:48 +03:00
2017-07-03 16:10:44 +06:00
intEnvConfig(&conf.TTL, "IMGPROXY_TTL")
2017-06-26 01:20:48 +03:00
intEnvConfig(&conf.MaxSrcDimension, "IMGPROXY_MAX_SRC_DIMENSION")
megaIntEnvConfig(&conf.MaxSrcResolution, "IMGPROXY_MAX_SRC_RESOLUTION")
2018-11-15 20:04:12 +06:00
intEnvConfig(&conf.MaxGifFrames, "IMGPROXY_MAX_GIF_FRAMES")
2017-06-26 01:20:48 +03:00
boolEnvConfig(&conf.JpegProgressive, "IMGPROXY_JPEG_PROGRESSIVE")
boolEnvConfig(&conf.PngInterlaced, "IMGPROXY_PNG_INTERLACED")
2017-06-26 01:20:48 +03:00
intEnvConfig(&conf.Quality, "IMGPROXY_QUALITY")
2017-06-27 03:41:37 +03:00
intEnvConfig(&conf.GZipCompression, "IMGPROXY_GZIP_COMPRESSION")
2017-06-20 16:58:55 +03:00
2018-09-11 18:26:27 +06:00
boolEnvConfig(&conf.EnableWebpDetection, "IMGPROXY_ENABLE_WEBP_DETECTION")
boolEnvConfig(&conf.EnforceWebp, "IMGPROXY_ENFORCE_WEBP")
boolEnvConfig(&conf.EnableClientHints, "IMGPROXY_ENABLE_CLIENT_HINTS")
2018-09-11 18:26:27 +06:00
2018-11-15 18:35:06 +06:00
hexEnvConfig(&conf.Keys, "IMGPROXY_KEY")
hexEnvConfig(&conf.Salts, "IMGPROXY_SALT")
intEnvConfig(&conf.SignatureSize, "IMGPROXY_SIGNATURE_SIZE")
2017-06-26 01:20:48 +03:00
2018-11-15 18:35:06 +06:00
hexFileConfig(&conf.Keys, *keyPath)
hexFileConfig(&conf.Salts, *saltPath)
2017-06-26 01:20:48 +03:00
strEnvConfig(&conf.Secret, "IMGPROXY_SECRET")
2018-04-26 17:22:31 +06:00
strEnvConfig(&conf.AllowOrigin, "IMGPROXY_ALLOW_ORIGIN")
strEnvConfig(&conf.UserAgent, "IMGPROXY_USER_AGENT")
boolEnvConfig(&conf.IgnoreSslVerification, "IMGPROXY_IGNORE_SSL_VERIFICATION")
strEnvConfig(&conf.LocalFileSystemRoot, "IMGPROXY_LOCAL_FILESYSTEM_ROOT")
2018-02-26 15:41:37 +06:00
2018-05-26 16:22:41 +02:00
boolEnvConfig(&conf.S3Enabled, "IMGPROXY_USE_S3")
2018-11-13 19:23:59 +06:00
strEnvConfig(&conf.S3Region, "IMGPROXY_S3_REGION")
strEnvConfig(&conf.S3Endpoint, "IMGPROXY_S3_ENDPOINT")
2018-10-30 18:12:56 +06:00
strEnvConfig(&conf.GCSKey, "IMGPROXY_GCS_KEY")
2018-05-26 16:22:41 +02:00
boolEnvConfig(&conf.ETagEnabled, "IMGPROXY_USE_ETAG")
2018-04-26 17:38:40 +06:00
strEnvConfig(&conf.BaseURL, "IMGPROXY_BASE_URL")
2018-09-07 23:41:06 +06:00
conf.Presets = make(presets)
presetEnvConfig(conf.Presets, "IMGPROXY_PRESETS")
presetFileConfig(conf.Presets, *presetsPath)
2018-09-07 23:41:06 +06:00
2018-10-19 15:47:11 +06:00
strEnvConfig(&conf.WatermarkData, "IMGPROXY_WATERMARK_DATA")
strEnvConfig(&conf.WatermarkPath, "IMGPROXY_WATERMARK_PATH")
strEnvConfig(&conf.WatermarkURL, "IMGPROXY_WATERMARK_URL")
floatEnvConfig(&conf.WatermarkOpacity, "IMGPROXY_WATERMARK_OPACITY")
2018-10-25 19:24:34 +06:00
strEnvConfig(&conf.NewRelicAppName, "IMGPROXY_NEW_RELIC_APP_NAME")
strEnvConfig(&conf.NewRelicKey, "IMGPROXY_NEW_RELIC_KEY")
2018-10-29 18:04:47 +06:00
strEnvConfig(&conf.PrometheusBind, "IMGPROXY_PROMETHEUS_BIND")
2018-11-14 19:41:16 +06:00
strEnvConfig(&conf.BugsnagKey, "IMGPROXY_BUGSNAG_KEY")
strEnvConfig(&conf.BugsnagStage, "IMGPROXY_BUGSNAG_STAGE")
strEnvConfig(&conf.HoneybadgerKey, "IMGPROXY_HONEYBADGER_KEY")
strEnvConfig(&conf.HoneybadgerEnv, "IMGPROXY_HONEYBADGER_ENV")
2018-11-15 18:35:06 +06:00
if len(conf.Keys) != len(conf.Salts) {
log.Fatalf("Number of keys and number of salts should be equal. Keys: %d, salts: %d", len(conf.Keys), len(conf.Salts))
}
if len(conf.Keys) == 0 {
warning("No keys defined, so signature checking is disabled")
conf.AllowInsecure = true
2017-06-26 01:20:48 +03:00
}
2018-11-15 18:35:06 +06:00
if len(conf.Salts) == 0 {
warning("No salts defined, so signature checking is disabled")
conf.AllowInsecure = true
2017-06-20 16:58:55 +03:00
}
2018-11-15 18:35:06 +06:00
if conf.SignatureSize < 1 || conf.SignatureSize > 32 {
log.Fatalf("Signature size should be within 1 and 32, now - %d\n", conf.SignatureSize)
}
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)
}
2017-07-05 18:28:22 +06:00
if conf.DownloadTimeout <= 0 {
log.Fatalf("Download timeout should be greater than 0, now - %d\n", conf.DownloadTimeout)
}
2017-07-04 20:05:53 +06:00
if conf.Concurrency <= 0 {
log.Fatalf("Concurrency should be greater than 0, now - %d\n", conf.Concurrency)
}
2017-09-15 13:29:51 +03:00
if conf.MaxClients <= 0 {
2018-03-19 15:06:08 +06:00
conf.MaxClients = conf.Concurrency * 10
2017-09-15 13:29:51 +03:00
}
2017-07-03 16:10:44 +06:00
if conf.TTL <= 0 {
log.Fatalf("TTL should be greater than 0, now - %d\n", conf.TTL)
}
2018-11-15 19:25:53 +06:00
if conf.MaxSrcDimension < 0 {
log.Fatalf("Max src dimension should be greater than or equal to 0, now - %d\n", conf.MaxSrcDimension)
} else if conf.MaxSrcDimension > 0 {
warning("IMGPROXY_MAX_SRC_DIMENSION is deprecated and can be removed in future versions. Use IMGPROXY_MAX_SRC_RESOLUTION")
}
if conf.MaxSrcResolution <= 0 {
log.Fatalf("Max src resolution should be greater than 0, now - %d\n", conf.MaxSrcResolution)
}
2018-11-15 20:04:12 +06:00
if conf.MaxGifFrames <= 0 {
log.Fatalf("Max GIF frames should be greater than 0, now - %d\n", conf.MaxGifFrames)
}
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)
}
2017-09-27 14:42:49 +06:00
if conf.IgnoreSslVerification {
warning("Ignoring SSL verification is very unsafe")
}
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")
}
}
2018-11-06 17:19:34 +06:00
if err := checkPresets(conf.Presets); err != nil {
log.Fatalln(err)
}
2018-10-19 15:47:11 +06:00
if conf.WatermarkOpacity <= 0 {
log.Fatalln("Watermark opacity should be greater than 0")
} else if conf.WatermarkOpacity > 1 {
log.Fatalln("Watermark opacity should be less than or equal to 1")
}
2018-10-29 18:04:47 +06:00
if len(conf.PrometheusBind) > 0 && conf.PrometheusBind == conf.Bind {
log.Fatalln("Can't use the same binding for the main server and Prometheus")
}
2017-11-14 02:28:04 +06:00
initDownloading()
2018-10-25 19:24:34 +06:00
initNewrelic()
2018-10-29 18:04:47 +06:00
initPrometheus()
2018-11-14 19:41:16 +06:00
initErrorsReporting()
2018-10-19 15:47:11 +06:00
initVips()
2017-06-20 16:58:55 +03:00
}