1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2024-11-24 08:12:38 +02:00
imgproxy/config.go

301 lines
7.0 KiB
Go
Raw Normal View History

2017-06-20 15:58:55 +02:00
package main
import (
2018-09-07 19:41:06 +02:00
"bufio"
2017-06-26 00:20:48 +02:00
"bytes"
2017-06-20 15:58:55 +02:00
"encoding/hex"
"flag"
"fmt"
2017-06-20 15:58:55 +02:00
"io/ioutil"
"log"
"os"
2017-09-28 19:17:23 +02:00
"runtime"
2017-06-26 00:20:48 +02:00
"strconv"
2018-09-07 19:41:06 +02:00
"strings"
2017-06-20 15:58:55 +02:00
)
2017-06-26 00:20:48 +02:00
func intEnvConfig(i *int, name string) {
if env, err := strconv.Atoi(os.Getenv(name)); err == nil {
*i = env
}
2017-06-20 15:58:55 +02:00
}
func megaIntEnvConfig(f *int, name string) {
if env, err := strconv.ParseFloat(os.Getenv(name), 64); err == nil {
*f = int(env * 1000000)
}
}
2017-06-26 00:20:48 +02:00
func strEnvConfig(s *string, name string) {
if env := os.Getenv(name); len(env) > 0 {
*s = env
2017-06-20 15:58:55 +02:00
}
2017-06-26 00:20:48 +02:00
}
2017-06-20 15:58:55 +02:00
func boolEnvConfig(b *bool, name string) {
*b = false
if env, err := strconv.ParseBool(os.Getenv(name)); err == nil {
*b = env
}
}
2017-06-26 00:20:48 +02:00
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)
}
}
2017-06-20 15:58:55 +02:00
}
2017-06-26 00:20:48 +02:00
func hexFileConfig(b *[]byte, filepath string) {
if len(filepath) == 0 {
return
}
2017-06-20 15:58:55 +02:00
2017-06-27 10:29:57 +02:00
f, err := os.Open(filepath)
2017-06-20 15:58:55 +02:00
if err != nil {
2017-06-27 10:29:57 +02:00
log.Fatalf("Can't open file %s\n", filepath)
2017-06-20 15:58:55 +02:00
}
2017-06-26 00:20:48 +02:00
src, err := ioutil.ReadAll(f)
2017-06-20 15:58:55 +02:00
if err != nil {
log.Fatalln(err)
}
2017-06-26 00:20:48 +02:00
src = bytes.TrimSpace(src)
dst := make([]byte, hex.DecodedLen(len(src)))
n, err := hex.Decode(dst, src)
2017-06-20 15:58:55 +02:00
if err != nil {
2017-06-27 10:29:57 +02:00
log.Fatalf("%s expected to contain hex-encoded string\n", filepath)
2017-06-20 15:58:55 +02:00
}
2017-06-26 00:20:48 +02:00
*b = dst[:n]
}
2017-06-20 15:58:55 +02:00
2018-09-07 19:41:06 +02:00
func presetEnvConfig(p *presets, name string) {
if env := os.Getenv(name); len(env) > 0 {
presetStrings := strings.Split(env, ",")
for _, presetStr := range presetStrings {
parsePreset(p, presetStr)
}
}
}
func presetFileConfig(p *presets, filepath string) {
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() {
parsePreset(p, scanner.Text())
}
}
2017-06-26 00:20:48 +02:00
type config struct {
2017-07-05 14:28:22 +02:00
Bind string
ReadTimeout int
2018-03-15 16:53:39 +02:00
WaitTimeout int
2017-07-05 14:28:22 +02:00
WriteTimeout int
DownloadTimeout int
Concurrency int
2017-09-15 12:29:51 +02:00
MaxClients int
2017-07-05 14:28:22 +02:00
TTL int
2017-07-03 12:10:44 +02:00
MaxSrcDimension int
MaxSrcResolution int
2017-06-20 15:58:55 +02:00
AllowInsecure bool
JpegProgressive bool
PngInterlaced bool
2017-06-27 02:41:37 +02:00
Quality int
GZipCompression int
2017-06-20 15:58:55 +02:00
2018-09-11 14:26:27 +02:00
EnableWebpDetection bool
EnforceWebp bool
2017-06-26 00:20:48 +02:00
Key []byte
Salt []byte
Secret string
2018-04-26 13:22:31 +02:00
AllowOrigin string
IgnoreSslVerification bool
LocalFileSystemRoot string
2018-02-26 11:41:37 +02:00
ETagEnabled bool
2018-04-26 13:38:40 +02:00
BaseURL string
2018-09-07 19:41:06 +02:00
Presets presets
2017-06-26 00:20:48 +02:00
}
2017-06-20 15:58:55 +02:00
2017-06-26 00:20:48 +02:00
var conf = config{
Bind: ":8080",
ReadTimeout: 10,
WriteTimeout: 10,
DownloadTimeout: 5,
Concurrency: runtime.NumCPU() * 2,
TTL: 3600,
IgnoreSslVerification: false,
MaxSrcDimension: 8192,
MaxSrcResolution: 16800000,
AllowInsecure: false,
Quality: 80,
GZipCompression: 5,
ETagEnabled: false,
2017-06-26 00:20:48 +02:00
}
func init() {
2018-09-07 19:41:06 +02: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 00:20:48 +02: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 00:20:48 +02:00
strEnvConfig(&conf.Bind, "IMGPROXY_BIND")
intEnvConfig(&conf.ReadTimeout, "IMGPROXY_READ_TIMEOUT")
intEnvConfig(&conf.WriteTimeout, "IMGPROXY_WRITE_TIMEOUT")
2017-07-05 14:28:22 +02:00
intEnvConfig(&conf.DownloadTimeout, "IMGPROXY_DOWNLOAD_TIMEOUT")
2017-07-04 16:05:53 +02:00
intEnvConfig(&conf.Concurrency, "IMGPROXY_CONCURRENCY")
2017-09-15 12:29:51 +02:00
intEnvConfig(&conf.MaxClients, "IMGPROXY_MAX_CLIENTS")
2017-06-26 00:20:48 +02:00
2017-07-03 12:10:44 +02:00
intEnvConfig(&conf.TTL, "IMGPROXY_TTL")
2017-06-26 00:20:48 +02:00
intEnvConfig(&conf.MaxSrcDimension, "IMGPROXY_MAX_SRC_DIMENSION")
megaIntEnvConfig(&conf.MaxSrcResolution, "IMGPROXY_MAX_SRC_RESOLUTION")
2017-06-26 00:20:48 +02:00
boolEnvConfig(&conf.AllowInsecure, "IMGPROXY_ALLOW_INSECURE")
boolEnvConfig(&conf.JpegProgressive, "IMGPROXY_JPEG_PROGRESSIVE")
boolEnvConfig(&conf.PngInterlaced, "IMGPROXY_PNG_INTERLACED")
2017-06-26 00:20:48 +02:00
intEnvConfig(&conf.Quality, "IMGPROXY_QUALITY")
2017-06-27 02:41:37 +02:00
intEnvConfig(&conf.GZipCompression, "IMGPROXY_GZIP_COMPRESSION")
2017-06-20 15:58:55 +02:00
2018-09-11 14:26:27 +02:00
boolEnvConfig(&conf.EnableWebpDetection, "IMGPROXY_ENABLE_WEBP_DETECTION")
boolEnvConfig(&conf.EnforceWebp, "IMGPROXY_ENFORCE_WEBP")
2017-06-26 00:20:48 +02:00
hexEnvConfig(&conf.Key, "IMGPROXY_KEY")
hexEnvConfig(&conf.Salt, "IMGPROXY_SALT")
2018-09-07 19:41:06 +02:00
hexFileConfig(&conf.Key, *keyPath)
hexFileConfig(&conf.Salt, *saltPath)
2017-06-26 00:20:48 +02:00
strEnvConfig(&conf.Secret, "IMGPROXY_SECRET")
2018-04-26 13:22:31 +02:00
strEnvConfig(&conf.AllowOrigin, "IMGPROXY_ALLOW_ORIGIN")
boolEnvConfig(&conf.IgnoreSslVerification, "IMGPROXY_IGNORE_SSL_VERIFICATION")
strEnvConfig(&conf.LocalFileSystemRoot, "IMGPROXY_LOCAL_FILESYSTEM_ROOT")
2018-02-26 11:41:37 +02:00
boolEnvConfig(&conf.ETagEnabled, "IMGPROXY_USE_ETAG")
2018-04-26 13:38:40 +02:00
strEnvConfig(&conf.BaseURL, "IMGPROXY_BASE_URL")
2018-09-07 19:41:06 +02:00
conf.Presets = make(presets)
presetEnvConfig(&conf.Presets, "IMGPROXY_PRESETS")
presetFileConfig(&conf.Presets, *presetsPath)
2017-06-26 00:20:48 +02:00
if len(conf.Key) == 0 {
log.Fatalln("Key is not defined")
}
if len(conf.Salt) == 0 {
log.Fatalln("Salt is not defined")
2017-06-20 15:58:55 +02:00
}
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 14:28:22 +02:00
if conf.DownloadTimeout <= 0 {
log.Fatalf("Download timeout should be greater than 0, now - %d\n", conf.DownloadTimeout)
}
2017-07-04 16:05:53 +02:00
if conf.Concurrency <= 0 {
log.Fatalf("Concurrency should be greater than 0, now - %d\n", conf.Concurrency)
}
2017-09-15 12:29:51 +02:00
if conf.MaxClients <= 0 {
2018-03-19 11:06:08 +02:00
conf.MaxClients = conf.Concurrency * 10
2017-09-15 12:29:51 +02:00
}
2017-07-03 12:10:44 +02:00
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.AllowInsecure {
warning("Token validation is disabled. Hope you know what you're doing")
}
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 10:42:49 +02:00
if conf.IgnoreSslVerification {
log.Println("Ignoring SSL verification is very unsafe. Hope you know what you're doing")
}
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")
}
}
2017-09-27 10:42:49 +02:00
initVips()
2017-11-13 22:28:04 +02:00
initDownloading()
2017-06-20 15:58:55 +02:00
}