1
0

config.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. package main
  2. import (
  3. "bufio"
  4. "encoding/hex"
  5. "flag"
  6. "fmt"
  7. "math"
  8. "os"
  9. "runtime"
  10. "strconv"
  11. "strings"
  12. )
  13. func intEnvConfig(i *int, name string) {
  14. if env, err := strconv.Atoi(os.Getenv(name)); err == nil {
  15. *i = env
  16. }
  17. }
  18. func floatEnvConfig(i *float64, name string) {
  19. if env, err := strconv.ParseFloat(os.Getenv(name), 64); err == nil {
  20. *i = env
  21. }
  22. }
  23. func megaIntEnvConfig(f *int, name string) {
  24. if env, err := strconv.ParseFloat(os.Getenv(name), 64); err == nil {
  25. *f = int(env * 1000000)
  26. }
  27. }
  28. func strEnvConfig(s *string, name string) {
  29. if env := os.Getenv(name); len(env) > 0 {
  30. *s = env
  31. }
  32. }
  33. func strSliceEnvConfig(s *[]string, name string) {
  34. if env := os.Getenv(name); len(env) > 0 {
  35. parts := strings.Split(env, ",")
  36. for i, p := range parts {
  37. parts[i] = strings.TrimSpace(p)
  38. }
  39. *s = parts
  40. return
  41. }
  42. *s = []string{}
  43. }
  44. func boolEnvConfig(b *bool, name string) {
  45. if env, err := strconv.ParseBool(os.Getenv(name)); err == nil {
  46. *b = env
  47. }
  48. }
  49. func imageTypesEnvConfig(it *[]imageType, name string) {
  50. *it = []imageType{}
  51. if env := os.Getenv(name); len(env) > 0 {
  52. parts := strings.Split(env, ",")
  53. for _, p := range parts {
  54. pt := strings.TrimSpace(p)
  55. if t, ok := imageTypes[pt]; ok {
  56. *it = append(*it, t)
  57. } else {
  58. logWarning("Unknown image format to skip: %s", pt)
  59. }
  60. }
  61. }
  62. }
  63. func formatQualityEnvConfig(m map[imageType]int, name string) {
  64. if env := os.Getenv(name); len(env) > 0 {
  65. parts := strings.Split(env, ",")
  66. for _, p := range parts {
  67. i := strings.Index(p, "=")
  68. if i < 0 {
  69. logWarning("Invalid format quality string: %s", p)
  70. continue
  71. }
  72. imgtypeStr, qStr := strings.TrimSpace(p[:i]), strings.TrimSpace(p[i+1:])
  73. imgtype, ok := imageTypes[imgtypeStr]
  74. if !ok {
  75. logWarning("Invalid format: %s", p)
  76. }
  77. q, err := strconv.Atoi(qStr)
  78. if err != nil || q <= 0 || q > 100 {
  79. logWarning("Invalid quality: %s", p)
  80. }
  81. m[imgtype] = q
  82. }
  83. }
  84. }
  85. func hexEnvConfig(b *[]securityKey, name string) error {
  86. var err error
  87. if env := os.Getenv(name); len(env) > 0 {
  88. parts := strings.Split(env, ",")
  89. keys := make([]securityKey, len(parts))
  90. for i, part := range parts {
  91. if keys[i], err = hex.DecodeString(part); err != nil {
  92. return fmt.Errorf("%s expected to be hex-encoded strings. Invalid: %s\n", name, part)
  93. }
  94. }
  95. *b = keys
  96. }
  97. return nil
  98. }
  99. func hexFileConfig(b *[]securityKey, filepath string) error {
  100. if len(filepath) == 0 {
  101. return nil
  102. }
  103. f, err := os.Open(filepath)
  104. if err != nil {
  105. return fmt.Errorf("Can't open file %s\n", filepath)
  106. }
  107. keys := []securityKey{}
  108. scanner := bufio.NewScanner(f)
  109. for scanner.Scan() {
  110. part := scanner.Text()
  111. if len(part) == 0 {
  112. continue
  113. }
  114. if key, err := hex.DecodeString(part); err == nil {
  115. keys = append(keys, key)
  116. } else {
  117. return fmt.Errorf("%s expected to contain hex-encoded strings. Invalid: %s\n", filepath, part)
  118. }
  119. }
  120. if err := scanner.Err(); err != nil {
  121. return fmt.Errorf("Failed to read file %s: %s", filepath, err)
  122. }
  123. *b = keys
  124. return nil
  125. }
  126. func presetEnvConfig(p presets, name string) error {
  127. if env := os.Getenv(name); len(env) > 0 {
  128. presetStrings := strings.Split(env, ",")
  129. for _, presetStr := range presetStrings {
  130. if err := parsePreset(p, presetStr); err != nil {
  131. return fmt.Errorf(err.Error())
  132. }
  133. }
  134. }
  135. return nil
  136. }
  137. func presetFileConfig(p presets, filepath string) error {
  138. if len(filepath) == 0 {
  139. return nil
  140. }
  141. f, err := os.Open(filepath)
  142. if err != nil {
  143. return fmt.Errorf("Can't open file %s\n", filepath)
  144. }
  145. scanner := bufio.NewScanner(f)
  146. for scanner.Scan() {
  147. if err := parsePreset(p, scanner.Text()); err != nil {
  148. return fmt.Errorf(err.Error())
  149. }
  150. }
  151. if err := scanner.Err(); err != nil {
  152. return fmt.Errorf("Failed to read presets file: %s", err)
  153. }
  154. return nil
  155. }
  156. type config struct {
  157. Network string
  158. Bind string
  159. ReadTimeout int
  160. WriteTimeout int
  161. KeepAliveTimeout int
  162. DownloadTimeout int
  163. Concurrency int
  164. MaxClients int
  165. TTL int
  166. CacheControlPassthrough bool
  167. SoReuseport bool
  168. PathPrefix string
  169. MaxSrcDimension int
  170. MaxSrcResolution int
  171. MaxSrcFileSize int
  172. MaxAnimationFrames int
  173. MaxSvgCheckBytes int
  174. JpegProgressive bool
  175. PngInterlaced bool
  176. PngQuantize bool
  177. PngQuantizationColors int
  178. Quality int
  179. FormatQuality map[imageType]int
  180. GZipCompression int
  181. StripMetadata bool
  182. StripColorProfile bool
  183. AutoRotate bool
  184. EnableWebpDetection bool
  185. EnforceWebp bool
  186. EnableAvifDetection bool
  187. EnforceAvif bool
  188. EnableClientHints bool
  189. SkipProcessingFormats []imageType
  190. UseLinearColorspace bool
  191. DisableShrinkOnLoad bool
  192. Keys []securityKey
  193. Salts []securityKey
  194. AllowInsecure bool
  195. SignatureSize int
  196. Secret string
  197. AllowOrigin string
  198. UserAgent string
  199. IgnoreSslVerification bool
  200. DevelopmentErrorsMode bool
  201. AllowedSources []string
  202. LocalFileSystemRoot string
  203. S3Enabled bool
  204. S3Region string
  205. S3Endpoint string
  206. GCSEnabled bool
  207. GCSKey string
  208. ABSEnabled bool
  209. ABSName string
  210. ABSKey string
  211. ABSEndpoint string
  212. ETagEnabled bool
  213. BaseURL string
  214. Presets presets
  215. OnlyPresets bool
  216. WatermarkData string
  217. WatermarkPath string
  218. WatermarkURL string
  219. WatermarkOpacity float64
  220. FallbackImageData string
  221. FallbackImagePath string
  222. FallbackImageURL string
  223. NewRelicAppName string
  224. NewRelicKey string
  225. PrometheusBind string
  226. PrometheusNamespace string
  227. BugsnagKey string
  228. BugsnagStage string
  229. HoneybadgerKey string
  230. HoneybadgerEnv string
  231. SentryDSN string
  232. SentryEnvironment string
  233. SentryRelease string
  234. ReportDownloadingErrors bool
  235. EnableDebugHeaders bool
  236. FreeMemoryInterval int
  237. DownloadBufferSize int
  238. GZipBufferSize int
  239. BufferPoolCalibrationThreshold int
  240. }
  241. var conf = config{
  242. Network: "tcp",
  243. Bind: ":8080",
  244. ReadTimeout: 10,
  245. WriteTimeout: 10,
  246. KeepAliveTimeout: 10,
  247. DownloadTimeout: 5,
  248. Concurrency: runtime.NumCPU() * 2,
  249. TTL: 3600,
  250. MaxSrcResolution: 16800000,
  251. MaxAnimationFrames: 1,
  252. MaxSvgCheckBytes: 32 * 1024,
  253. SignatureSize: 32,
  254. PngQuantizationColors: 256,
  255. Quality: 80,
  256. FormatQuality: map[imageType]int{imageTypeAVIF: 50},
  257. StripMetadata: true,
  258. StripColorProfile: true,
  259. AutoRotate: true,
  260. UserAgent: fmt.Sprintf("imgproxy/%s", version),
  261. Presets: make(presets),
  262. WatermarkOpacity: 1,
  263. BugsnagStage: "production",
  264. HoneybadgerEnv: "production",
  265. SentryEnvironment: "production",
  266. SentryRelease: fmt.Sprintf("imgproxy/%s", version),
  267. ReportDownloadingErrors: true,
  268. FreeMemoryInterval: 10,
  269. BufferPoolCalibrationThreshold: 1024,
  270. }
  271. func configure() error {
  272. keyPath := flag.String("keypath", "", "path of the file with hex-encoded key")
  273. saltPath := flag.String("saltpath", "", "path of the file with hex-encoded salt")
  274. presetsPath := flag.String("presets", "", "path of the file with presets")
  275. flag.Parse()
  276. if port := os.Getenv("PORT"); len(port) > 0 {
  277. conf.Bind = fmt.Sprintf(":%s", port)
  278. }
  279. strEnvConfig(&conf.Network, "IMGPROXY_NETWORK")
  280. strEnvConfig(&conf.Bind, "IMGPROXY_BIND")
  281. intEnvConfig(&conf.ReadTimeout, "IMGPROXY_READ_TIMEOUT")
  282. intEnvConfig(&conf.WriteTimeout, "IMGPROXY_WRITE_TIMEOUT")
  283. intEnvConfig(&conf.KeepAliveTimeout, "IMGPROXY_KEEP_ALIVE_TIMEOUT")
  284. intEnvConfig(&conf.DownloadTimeout, "IMGPROXY_DOWNLOAD_TIMEOUT")
  285. intEnvConfig(&conf.Concurrency, "IMGPROXY_CONCURRENCY")
  286. intEnvConfig(&conf.MaxClients, "IMGPROXY_MAX_CLIENTS")
  287. intEnvConfig(&conf.TTL, "IMGPROXY_TTL")
  288. boolEnvConfig(&conf.CacheControlPassthrough, "IMGPROXY_CACHE_CONTROL_PASSTHROUGH")
  289. boolEnvConfig(&conf.SoReuseport, "IMGPROXY_SO_REUSEPORT")
  290. strEnvConfig(&conf.PathPrefix, "IMGPROXY_PATH_PREFIX")
  291. intEnvConfig(&conf.MaxSrcDimension, "IMGPROXY_MAX_SRC_DIMENSION")
  292. megaIntEnvConfig(&conf.MaxSrcResolution, "IMGPROXY_MAX_SRC_RESOLUTION")
  293. intEnvConfig(&conf.MaxSrcFileSize, "IMGPROXY_MAX_SRC_FILE_SIZE")
  294. intEnvConfig(&conf.MaxSvgCheckBytes, "IMGPROXY_MAX_SVG_CHECK_BYTES")
  295. if _, ok := os.LookupEnv("IMGPROXY_MAX_GIF_FRAMES"); ok {
  296. logWarning("`IMGPROXY_MAX_GIF_FRAMES` is deprecated and will be removed in future versions. Use `IMGPROXY_MAX_ANIMATION_FRAMES` instead")
  297. intEnvConfig(&conf.MaxAnimationFrames, "IMGPROXY_MAX_GIF_FRAMES")
  298. }
  299. intEnvConfig(&conf.MaxAnimationFrames, "IMGPROXY_MAX_ANIMATION_FRAMES")
  300. strSliceEnvConfig(&conf.AllowedSources, "IMGPROXY_ALLOWED_SOURCES")
  301. boolEnvConfig(&conf.JpegProgressive, "IMGPROXY_JPEG_PROGRESSIVE")
  302. boolEnvConfig(&conf.PngInterlaced, "IMGPROXY_PNG_INTERLACED")
  303. boolEnvConfig(&conf.PngQuantize, "IMGPROXY_PNG_QUANTIZE")
  304. intEnvConfig(&conf.PngQuantizationColors, "IMGPROXY_PNG_QUANTIZATION_COLORS")
  305. intEnvConfig(&conf.Quality, "IMGPROXY_QUALITY")
  306. formatQualityEnvConfig(conf.FormatQuality, "IMGPROXY_FORMAT_QUALITY")
  307. intEnvConfig(&conf.GZipCompression, "IMGPROXY_GZIP_COMPRESSION")
  308. boolEnvConfig(&conf.StripMetadata, "IMGPROXY_STRIP_METADATA")
  309. boolEnvConfig(&conf.StripColorProfile, "IMGPROXY_STRIP_COLOR_PROFILE")
  310. boolEnvConfig(&conf.AutoRotate, "IMGPROXY_AUTO_ROTATE")
  311. boolEnvConfig(&conf.EnableWebpDetection, "IMGPROXY_ENABLE_WEBP_DETECTION")
  312. boolEnvConfig(&conf.EnforceWebp, "IMGPROXY_ENFORCE_WEBP")
  313. boolEnvConfig(&conf.EnableAvifDetection, "IMGPROXY_ENABLE_AVIF_DETECTION")
  314. boolEnvConfig(&conf.EnforceAvif, "IMGPROXY_ENFORCE_AVIF")
  315. boolEnvConfig(&conf.EnableClientHints, "IMGPROXY_ENABLE_CLIENT_HINTS")
  316. imageTypesEnvConfig(&conf.SkipProcessingFormats, "IMGPROXY_SKIP_PROCESSING_FORMATS")
  317. boolEnvConfig(&conf.UseLinearColorspace, "IMGPROXY_USE_LINEAR_COLORSPACE")
  318. boolEnvConfig(&conf.DisableShrinkOnLoad, "IMGPROXY_DISABLE_SHRINK_ON_LOAD")
  319. if err := hexEnvConfig(&conf.Keys, "IMGPROXY_KEY"); err != nil {
  320. return err
  321. }
  322. if err := hexEnvConfig(&conf.Salts, "IMGPROXY_SALT"); err != nil {
  323. return err
  324. }
  325. intEnvConfig(&conf.SignatureSize, "IMGPROXY_SIGNATURE_SIZE")
  326. if err := hexFileConfig(&conf.Keys, *keyPath); err != nil {
  327. return err
  328. }
  329. if err := hexFileConfig(&conf.Salts, *saltPath); err != nil {
  330. return err
  331. }
  332. strEnvConfig(&conf.Secret, "IMGPROXY_SECRET")
  333. strEnvConfig(&conf.AllowOrigin, "IMGPROXY_ALLOW_ORIGIN")
  334. strEnvConfig(&conf.UserAgent, "IMGPROXY_USER_AGENT")
  335. boolEnvConfig(&conf.IgnoreSslVerification, "IMGPROXY_IGNORE_SSL_VERIFICATION")
  336. boolEnvConfig(&conf.DevelopmentErrorsMode, "IMGPROXY_DEVELOPMENT_ERRORS_MODE")
  337. strEnvConfig(&conf.LocalFileSystemRoot, "IMGPROXY_LOCAL_FILESYSTEM_ROOT")
  338. boolEnvConfig(&conf.S3Enabled, "IMGPROXY_USE_S3")
  339. strEnvConfig(&conf.S3Region, "IMGPROXY_S3_REGION")
  340. strEnvConfig(&conf.S3Endpoint, "IMGPROXY_S3_ENDPOINT")
  341. boolEnvConfig(&conf.GCSEnabled, "IMGPROXY_USE_GCS")
  342. strEnvConfig(&conf.GCSKey, "IMGPROXY_GCS_KEY")
  343. boolEnvConfig(&conf.ABSEnabled, "IMGPROXY_USE_ABS")
  344. strEnvConfig(&conf.ABSName, "IMGPROXY_ABS_NAME")
  345. strEnvConfig(&conf.ABSKey, "IMGPROXY_ABS_KEY")
  346. strEnvConfig(&conf.ABSEndpoint, "IMGPROXY_ABS_ENDPOINT")
  347. boolEnvConfig(&conf.ETagEnabled, "IMGPROXY_USE_ETAG")
  348. strEnvConfig(&conf.BaseURL, "IMGPROXY_BASE_URL")
  349. if err := presetEnvConfig(conf.Presets, "IMGPROXY_PRESETS"); err != nil {
  350. return err
  351. }
  352. if err := presetFileConfig(conf.Presets, *presetsPath); err != nil {
  353. return err
  354. }
  355. boolEnvConfig(&conf.OnlyPresets, "IMGPROXY_ONLY_PRESETS")
  356. strEnvConfig(&conf.WatermarkData, "IMGPROXY_WATERMARK_DATA")
  357. strEnvConfig(&conf.WatermarkPath, "IMGPROXY_WATERMARK_PATH")
  358. strEnvConfig(&conf.WatermarkURL, "IMGPROXY_WATERMARK_URL")
  359. floatEnvConfig(&conf.WatermarkOpacity, "IMGPROXY_WATERMARK_OPACITY")
  360. strEnvConfig(&conf.FallbackImageData, "IMGPROXY_FALLBACK_IMAGE_DATA")
  361. strEnvConfig(&conf.FallbackImagePath, "IMGPROXY_FALLBACK_IMAGE_PATH")
  362. strEnvConfig(&conf.FallbackImageURL, "IMGPROXY_FALLBACK_IMAGE_URL")
  363. strEnvConfig(&conf.NewRelicAppName, "IMGPROXY_NEW_RELIC_APP_NAME")
  364. strEnvConfig(&conf.NewRelicKey, "IMGPROXY_NEW_RELIC_KEY")
  365. strEnvConfig(&conf.PrometheusBind, "IMGPROXY_PROMETHEUS_BIND")
  366. strEnvConfig(&conf.PrometheusNamespace, "IMGPROXY_PROMETHEUS_NAMESPACE")
  367. strEnvConfig(&conf.BugsnagKey, "IMGPROXY_BUGSNAG_KEY")
  368. strEnvConfig(&conf.BugsnagStage, "IMGPROXY_BUGSNAG_STAGE")
  369. strEnvConfig(&conf.HoneybadgerKey, "IMGPROXY_HONEYBADGER_KEY")
  370. strEnvConfig(&conf.HoneybadgerEnv, "IMGPROXY_HONEYBADGER_ENV")
  371. strEnvConfig(&conf.SentryDSN, "IMGPROXY_SENTRY_DSN")
  372. strEnvConfig(&conf.SentryEnvironment, "IMGPROXY_SENTRY_ENVIRONMENT")
  373. strEnvConfig(&conf.SentryRelease, "IMGPROXY_SENTRY_RELEASE")
  374. boolEnvConfig(&conf.ReportDownloadingErrors, "IMGPROXY_REPORT_DOWNLOADING_ERRORS")
  375. boolEnvConfig(&conf.EnableDebugHeaders, "IMGPROXY_ENABLE_DEBUG_HEADERS")
  376. intEnvConfig(&conf.FreeMemoryInterval, "IMGPROXY_FREE_MEMORY_INTERVAL")
  377. intEnvConfig(&conf.DownloadBufferSize, "IMGPROXY_DOWNLOAD_BUFFER_SIZE")
  378. intEnvConfig(&conf.GZipBufferSize, "IMGPROXY_GZIP_BUFFER_SIZE")
  379. intEnvConfig(&conf.BufferPoolCalibrationThreshold, "IMGPROXY_BUFFER_POOL_CALIBRATION_THRESHOLD")
  380. if len(conf.Keys) != len(conf.Salts) {
  381. return fmt.Errorf("Number of keys and number of salts should be equal. Keys: %d, salts: %d", len(conf.Keys), len(conf.Salts))
  382. }
  383. if len(conf.Keys) == 0 {
  384. logWarning("No keys defined, so signature checking is disabled")
  385. conf.AllowInsecure = true
  386. }
  387. if len(conf.Salts) == 0 {
  388. logWarning("No salts defined, so signature checking is disabled")
  389. conf.AllowInsecure = true
  390. }
  391. if conf.SignatureSize < 1 || conf.SignatureSize > 32 {
  392. return fmt.Errorf("Signature size should be within 1 and 32, now - %d\n", conf.SignatureSize)
  393. }
  394. if len(conf.Bind) == 0 {
  395. return fmt.Errorf("Bind address is not defined")
  396. }
  397. if conf.ReadTimeout <= 0 {
  398. return fmt.Errorf("Read timeout should be greater than 0, now - %d\n", conf.ReadTimeout)
  399. }
  400. if conf.WriteTimeout <= 0 {
  401. return fmt.Errorf("Write timeout should be greater than 0, now - %d\n", conf.WriteTimeout)
  402. }
  403. if conf.KeepAliveTimeout < 0 {
  404. return fmt.Errorf("KeepAlive timeout should be greater than or equal to 0, now - %d\n", conf.KeepAliveTimeout)
  405. }
  406. if conf.DownloadTimeout <= 0 {
  407. return fmt.Errorf("Download timeout should be greater than 0, now - %d\n", conf.DownloadTimeout)
  408. }
  409. if conf.Concurrency <= 0 {
  410. return fmt.Errorf("Concurrency should be greater than 0, now - %d\n", conf.Concurrency)
  411. }
  412. if conf.MaxClients <= 0 {
  413. conf.MaxClients = conf.Concurrency * 10
  414. }
  415. if conf.TTL <= 0 {
  416. return fmt.Errorf("TTL should be greater than 0, now - %d\n", conf.TTL)
  417. }
  418. if conf.MaxSrcDimension < 0 {
  419. return fmt.Errorf("Max src dimension should be greater than or equal to 0, now - %d\n", conf.MaxSrcDimension)
  420. } else if conf.MaxSrcDimension > 0 {
  421. logWarning("IMGPROXY_MAX_SRC_DIMENSION is deprecated and can be removed in future versions. Use IMGPROXY_MAX_SRC_RESOLUTION")
  422. }
  423. if conf.MaxSrcResolution <= 0 {
  424. return fmt.Errorf("Max src resolution should be greater than 0, now - %d\n", conf.MaxSrcResolution)
  425. }
  426. if conf.MaxSrcFileSize < 0 {
  427. return fmt.Errorf("Max src file size should be greater than or equal to 0, now - %d\n", conf.MaxSrcFileSize)
  428. }
  429. if conf.MaxAnimationFrames <= 0 {
  430. return fmt.Errorf("Max animation frames should be greater than 0, now - %d\n", conf.MaxAnimationFrames)
  431. }
  432. if conf.PngQuantizationColors < 2 {
  433. return fmt.Errorf("Png quantization colors should be greater than 1, now - %d\n", conf.PngQuantizationColors)
  434. } else if conf.PngQuantizationColors > 256 {
  435. return fmt.Errorf("Png quantization colors can't be greater than 256, now - %d\n", conf.PngQuantizationColors)
  436. }
  437. if conf.Quality <= 0 {
  438. return fmt.Errorf("Quality should be greater than 0, now - %d\n", conf.Quality)
  439. } else if conf.Quality > 100 {
  440. return fmt.Errorf("Quality can't be greater than 100, now - %d\n", conf.Quality)
  441. }
  442. if conf.GZipCompression < 0 {
  443. return fmt.Errorf("GZip compression should be greater than or equal to 0, now - %d\n", conf.GZipCompression)
  444. } else if conf.GZipCompression > 9 {
  445. return fmt.Errorf("GZip compression can't be greater than 9, now - %d\n", conf.GZipCompression)
  446. }
  447. if conf.GZipCompression > 0 {
  448. logWarning("GZip compression is deprecated and can be removed in future versions")
  449. }
  450. if conf.IgnoreSslVerification {
  451. logWarning("Ignoring SSL verification is very unsafe")
  452. }
  453. if conf.LocalFileSystemRoot != "" {
  454. stat, err := os.Stat(conf.LocalFileSystemRoot)
  455. if err != nil {
  456. return fmt.Errorf("Cannot use local directory: %s", err)
  457. }
  458. if !stat.IsDir() {
  459. return fmt.Errorf("Cannot use local directory: not a directory")
  460. }
  461. if conf.LocalFileSystemRoot == "/" {
  462. logWarning("Exposing root via IMGPROXY_LOCAL_FILESYSTEM_ROOT is unsafe")
  463. }
  464. }
  465. if _, ok := os.LookupEnv("IMGPROXY_USE_GCS"); !ok && len(conf.GCSKey) > 0 {
  466. logWarning("Set IMGPROXY_USE_GCS to true since it may be required by future versions to enable GCS support")
  467. conf.GCSEnabled = true
  468. }
  469. if conf.WatermarkOpacity <= 0 {
  470. return fmt.Errorf("Watermark opacity should be greater than 0")
  471. } else if conf.WatermarkOpacity > 1 {
  472. return fmt.Errorf("Watermark opacity should be less than or equal to 1")
  473. }
  474. if len(conf.PrometheusBind) > 0 && conf.PrometheusBind == conf.Bind {
  475. return fmt.Errorf("Can't use the same binding for the main server and Prometheus")
  476. }
  477. if conf.FreeMemoryInterval <= 0 {
  478. return fmt.Errorf("Free memory interval should be greater than zero")
  479. }
  480. if conf.DownloadBufferSize < 0 {
  481. return fmt.Errorf("Download buffer size should be greater than or equal to 0")
  482. } else if conf.DownloadBufferSize > math.MaxInt32 {
  483. return fmt.Errorf("Download buffer size can't be greater than %d", math.MaxInt32)
  484. }
  485. if conf.GZipBufferSize < 0 {
  486. return fmt.Errorf("GZip buffer size should be greater than or equal to 0")
  487. } else if conf.GZipBufferSize > math.MaxInt32 {
  488. return fmt.Errorf("GZip buffer size can't be greater than %d", math.MaxInt32)
  489. }
  490. if conf.BufferPoolCalibrationThreshold < 64 {
  491. return fmt.Errorf("Buffer pool calibration threshold should be greater than or equal to 64")
  492. }
  493. return nil
  494. }