config.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. SetCanonicalHeader bool
  168. SoReuseport bool
  169. PathPrefix string
  170. MaxSrcDimension int
  171. MaxSrcResolution int
  172. MaxSrcFileSize int
  173. MaxAnimationFrames int
  174. MaxSvgCheckBytes int
  175. JpegProgressive bool
  176. PngInterlaced bool
  177. PngQuantize bool
  178. PngQuantizationColors int
  179. Quality int
  180. FormatQuality map[imageType]int
  181. GZipCompression int
  182. StripMetadata bool
  183. StripColorProfile bool
  184. AutoRotate bool
  185. EnableWebpDetection bool
  186. EnforceWebp bool
  187. EnableAvifDetection bool
  188. EnforceAvif bool
  189. EnableClientHints bool
  190. SkipProcessingFormats []imageType
  191. UseLinearColorspace bool
  192. DisableShrinkOnLoad bool
  193. Keys []securityKey
  194. Salts []securityKey
  195. AllowInsecure bool
  196. SignatureSize int
  197. Secret string
  198. AllowOrigin string
  199. UserAgent string
  200. IgnoreSslVerification bool
  201. DevelopmentErrorsMode bool
  202. AllowedSources []string
  203. LocalFileSystemRoot string
  204. S3Enabled bool
  205. S3Region string
  206. S3Endpoint string
  207. GCSEnabled bool
  208. GCSKey string
  209. ABSEnabled bool
  210. ABSName string
  211. ABSKey string
  212. ABSEndpoint string
  213. ETagEnabled bool
  214. BaseURL string
  215. Presets presets
  216. OnlyPresets bool
  217. WatermarkData string
  218. WatermarkPath string
  219. WatermarkURL string
  220. WatermarkOpacity float64
  221. FallbackImageData string
  222. FallbackImagePath string
  223. FallbackImageURL string
  224. NewRelicAppName string
  225. NewRelicKey string
  226. PrometheusBind string
  227. PrometheusNamespace string
  228. BugsnagKey string
  229. BugsnagStage string
  230. HoneybadgerKey string
  231. HoneybadgerEnv string
  232. SentryDSN string
  233. SentryEnvironment string
  234. SentryRelease string
  235. ReportDownloadingErrors bool
  236. EnableDebugHeaders bool
  237. FreeMemoryInterval int
  238. DownloadBufferSize int
  239. GZipBufferSize int
  240. BufferPoolCalibrationThreshold int
  241. }
  242. var conf = config{
  243. Network: "tcp",
  244. Bind: ":8080",
  245. ReadTimeout: 10,
  246. WriteTimeout: 10,
  247. KeepAliveTimeout: 10,
  248. DownloadTimeout: 5,
  249. Concurrency: runtime.NumCPU() * 2,
  250. TTL: 3600,
  251. MaxSrcResolution: 16800000,
  252. MaxAnimationFrames: 1,
  253. MaxSvgCheckBytes: 32 * 1024,
  254. SignatureSize: 32,
  255. PngQuantizationColors: 256,
  256. Quality: 80,
  257. FormatQuality: map[imageType]int{imageTypeAVIF: 50},
  258. StripMetadata: true,
  259. StripColorProfile: true,
  260. AutoRotate: true,
  261. UserAgent: fmt.Sprintf("imgproxy/%s", version),
  262. Presets: make(presets),
  263. WatermarkOpacity: 1,
  264. BugsnagStage: "production",
  265. HoneybadgerEnv: "production",
  266. SentryEnvironment: "production",
  267. SentryRelease: fmt.Sprintf("imgproxy/%s", version),
  268. ReportDownloadingErrors: true,
  269. FreeMemoryInterval: 10,
  270. BufferPoolCalibrationThreshold: 1024,
  271. }
  272. func configure() error {
  273. keyPath := flag.String("keypath", "", "path of the file with hex-encoded key")
  274. saltPath := flag.String("saltpath", "", "path of the file with hex-encoded salt")
  275. presetsPath := flag.String("presets", "", "path of the file with presets")
  276. flag.Parse()
  277. if port := os.Getenv("PORT"); len(port) > 0 {
  278. conf.Bind = fmt.Sprintf(":%s", port)
  279. }
  280. strEnvConfig(&conf.Network, "IMGPROXY_NETWORK")
  281. strEnvConfig(&conf.Bind, "IMGPROXY_BIND")
  282. intEnvConfig(&conf.ReadTimeout, "IMGPROXY_READ_TIMEOUT")
  283. intEnvConfig(&conf.WriteTimeout, "IMGPROXY_WRITE_TIMEOUT")
  284. intEnvConfig(&conf.KeepAliveTimeout, "IMGPROXY_KEEP_ALIVE_TIMEOUT")
  285. intEnvConfig(&conf.DownloadTimeout, "IMGPROXY_DOWNLOAD_TIMEOUT")
  286. intEnvConfig(&conf.Concurrency, "IMGPROXY_CONCURRENCY")
  287. intEnvConfig(&conf.MaxClients, "IMGPROXY_MAX_CLIENTS")
  288. intEnvConfig(&conf.TTL, "IMGPROXY_TTL")
  289. boolEnvConfig(&conf.CacheControlPassthrough, "IMGPROXY_CACHE_CONTROL_PASSTHROUGH")
  290. boolEnvConfig(&conf.SetCanonicalHeader, "IMGPROXY_SET_CANONICAL_HEADER")
  291. boolEnvConfig(&conf.SoReuseport, "IMGPROXY_SO_REUSEPORT")
  292. strEnvConfig(&conf.PathPrefix, "IMGPROXY_PATH_PREFIX")
  293. intEnvConfig(&conf.MaxSrcDimension, "IMGPROXY_MAX_SRC_DIMENSION")
  294. megaIntEnvConfig(&conf.MaxSrcResolution, "IMGPROXY_MAX_SRC_RESOLUTION")
  295. intEnvConfig(&conf.MaxSrcFileSize, "IMGPROXY_MAX_SRC_FILE_SIZE")
  296. intEnvConfig(&conf.MaxSvgCheckBytes, "IMGPROXY_MAX_SVG_CHECK_BYTES")
  297. if _, ok := os.LookupEnv("IMGPROXY_MAX_GIF_FRAMES"); ok {
  298. logWarning("`IMGPROXY_MAX_GIF_FRAMES` is deprecated and will be removed in future versions. Use `IMGPROXY_MAX_ANIMATION_FRAMES` instead")
  299. intEnvConfig(&conf.MaxAnimationFrames, "IMGPROXY_MAX_GIF_FRAMES")
  300. }
  301. intEnvConfig(&conf.MaxAnimationFrames, "IMGPROXY_MAX_ANIMATION_FRAMES")
  302. strSliceEnvConfig(&conf.AllowedSources, "IMGPROXY_ALLOWED_SOURCES")
  303. boolEnvConfig(&conf.JpegProgressive, "IMGPROXY_JPEG_PROGRESSIVE")
  304. boolEnvConfig(&conf.PngInterlaced, "IMGPROXY_PNG_INTERLACED")
  305. boolEnvConfig(&conf.PngQuantize, "IMGPROXY_PNG_QUANTIZE")
  306. intEnvConfig(&conf.PngQuantizationColors, "IMGPROXY_PNG_QUANTIZATION_COLORS")
  307. intEnvConfig(&conf.Quality, "IMGPROXY_QUALITY")
  308. formatQualityEnvConfig(conf.FormatQuality, "IMGPROXY_FORMAT_QUALITY")
  309. intEnvConfig(&conf.GZipCompression, "IMGPROXY_GZIP_COMPRESSION")
  310. boolEnvConfig(&conf.StripMetadata, "IMGPROXY_STRIP_METADATA")
  311. boolEnvConfig(&conf.StripColorProfile, "IMGPROXY_STRIP_COLOR_PROFILE")
  312. boolEnvConfig(&conf.AutoRotate, "IMGPROXY_AUTO_ROTATE")
  313. boolEnvConfig(&conf.EnableWebpDetection, "IMGPROXY_ENABLE_WEBP_DETECTION")
  314. boolEnvConfig(&conf.EnforceWebp, "IMGPROXY_ENFORCE_WEBP")
  315. boolEnvConfig(&conf.EnableAvifDetection, "IMGPROXY_ENABLE_AVIF_DETECTION")
  316. boolEnvConfig(&conf.EnforceAvif, "IMGPROXY_ENFORCE_AVIF")
  317. boolEnvConfig(&conf.EnableClientHints, "IMGPROXY_ENABLE_CLIENT_HINTS")
  318. imageTypesEnvConfig(&conf.SkipProcessingFormats, "IMGPROXY_SKIP_PROCESSING_FORMATS")
  319. boolEnvConfig(&conf.UseLinearColorspace, "IMGPROXY_USE_LINEAR_COLORSPACE")
  320. boolEnvConfig(&conf.DisableShrinkOnLoad, "IMGPROXY_DISABLE_SHRINK_ON_LOAD")
  321. if err := hexEnvConfig(&conf.Keys, "IMGPROXY_KEY"); err != nil {
  322. return err
  323. }
  324. if err := hexEnvConfig(&conf.Salts, "IMGPROXY_SALT"); err != nil {
  325. return err
  326. }
  327. intEnvConfig(&conf.SignatureSize, "IMGPROXY_SIGNATURE_SIZE")
  328. if err := hexFileConfig(&conf.Keys, *keyPath); err != nil {
  329. return err
  330. }
  331. if err := hexFileConfig(&conf.Salts, *saltPath); err != nil {
  332. return err
  333. }
  334. strEnvConfig(&conf.Secret, "IMGPROXY_SECRET")
  335. strEnvConfig(&conf.AllowOrigin, "IMGPROXY_ALLOW_ORIGIN")
  336. strEnvConfig(&conf.UserAgent, "IMGPROXY_USER_AGENT")
  337. boolEnvConfig(&conf.IgnoreSslVerification, "IMGPROXY_IGNORE_SSL_VERIFICATION")
  338. boolEnvConfig(&conf.DevelopmentErrorsMode, "IMGPROXY_DEVELOPMENT_ERRORS_MODE")
  339. strEnvConfig(&conf.LocalFileSystemRoot, "IMGPROXY_LOCAL_FILESYSTEM_ROOT")
  340. boolEnvConfig(&conf.S3Enabled, "IMGPROXY_USE_S3")
  341. strEnvConfig(&conf.S3Region, "IMGPROXY_S3_REGION")
  342. strEnvConfig(&conf.S3Endpoint, "IMGPROXY_S3_ENDPOINT")
  343. boolEnvConfig(&conf.GCSEnabled, "IMGPROXY_USE_GCS")
  344. strEnvConfig(&conf.GCSKey, "IMGPROXY_GCS_KEY")
  345. boolEnvConfig(&conf.ABSEnabled, "IMGPROXY_USE_ABS")
  346. strEnvConfig(&conf.ABSName, "IMGPROXY_ABS_NAME")
  347. strEnvConfig(&conf.ABSKey, "IMGPROXY_ABS_KEY")
  348. strEnvConfig(&conf.ABSEndpoint, "IMGPROXY_ABS_ENDPOINT")
  349. boolEnvConfig(&conf.ETagEnabled, "IMGPROXY_USE_ETAG")
  350. strEnvConfig(&conf.BaseURL, "IMGPROXY_BASE_URL")
  351. if err := presetEnvConfig(conf.Presets, "IMGPROXY_PRESETS"); err != nil {
  352. return err
  353. }
  354. if err := presetFileConfig(conf.Presets, *presetsPath); err != nil {
  355. return err
  356. }
  357. boolEnvConfig(&conf.OnlyPresets, "IMGPROXY_ONLY_PRESETS")
  358. strEnvConfig(&conf.WatermarkData, "IMGPROXY_WATERMARK_DATA")
  359. strEnvConfig(&conf.WatermarkPath, "IMGPROXY_WATERMARK_PATH")
  360. strEnvConfig(&conf.WatermarkURL, "IMGPROXY_WATERMARK_URL")
  361. floatEnvConfig(&conf.WatermarkOpacity, "IMGPROXY_WATERMARK_OPACITY")
  362. strEnvConfig(&conf.FallbackImageData, "IMGPROXY_FALLBACK_IMAGE_DATA")
  363. strEnvConfig(&conf.FallbackImagePath, "IMGPROXY_FALLBACK_IMAGE_PATH")
  364. strEnvConfig(&conf.FallbackImageURL, "IMGPROXY_FALLBACK_IMAGE_URL")
  365. strEnvConfig(&conf.NewRelicAppName, "IMGPROXY_NEW_RELIC_APP_NAME")
  366. strEnvConfig(&conf.NewRelicKey, "IMGPROXY_NEW_RELIC_KEY")
  367. strEnvConfig(&conf.PrometheusBind, "IMGPROXY_PROMETHEUS_BIND")
  368. strEnvConfig(&conf.PrometheusNamespace, "IMGPROXY_PROMETHEUS_NAMESPACE")
  369. strEnvConfig(&conf.BugsnagKey, "IMGPROXY_BUGSNAG_KEY")
  370. strEnvConfig(&conf.BugsnagStage, "IMGPROXY_BUGSNAG_STAGE")
  371. strEnvConfig(&conf.HoneybadgerKey, "IMGPROXY_HONEYBADGER_KEY")
  372. strEnvConfig(&conf.HoneybadgerEnv, "IMGPROXY_HONEYBADGER_ENV")
  373. strEnvConfig(&conf.SentryDSN, "IMGPROXY_SENTRY_DSN")
  374. strEnvConfig(&conf.SentryEnvironment, "IMGPROXY_SENTRY_ENVIRONMENT")
  375. strEnvConfig(&conf.SentryRelease, "IMGPROXY_SENTRY_RELEASE")
  376. boolEnvConfig(&conf.ReportDownloadingErrors, "IMGPROXY_REPORT_DOWNLOADING_ERRORS")
  377. boolEnvConfig(&conf.EnableDebugHeaders, "IMGPROXY_ENABLE_DEBUG_HEADERS")
  378. intEnvConfig(&conf.FreeMemoryInterval, "IMGPROXY_FREE_MEMORY_INTERVAL")
  379. intEnvConfig(&conf.DownloadBufferSize, "IMGPROXY_DOWNLOAD_BUFFER_SIZE")
  380. intEnvConfig(&conf.GZipBufferSize, "IMGPROXY_GZIP_BUFFER_SIZE")
  381. intEnvConfig(&conf.BufferPoolCalibrationThreshold, "IMGPROXY_BUFFER_POOL_CALIBRATION_THRESHOLD")
  382. if len(conf.Keys) != len(conf.Salts) {
  383. return fmt.Errorf("Number of keys and number of salts should be equal. Keys: %d, salts: %d", len(conf.Keys), len(conf.Salts))
  384. }
  385. if len(conf.Keys) == 0 {
  386. logWarning("No keys defined, so signature checking is disabled")
  387. conf.AllowInsecure = true
  388. }
  389. if len(conf.Salts) == 0 {
  390. logWarning("No salts defined, so signature checking is disabled")
  391. conf.AllowInsecure = true
  392. }
  393. if conf.SignatureSize < 1 || conf.SignatureSize > 32 {
  394. return fmt.Errorf("Signature size should be within 1 and 32, now - %d\n", conf.SignatureSize)
  395. }
  396. if len(conf.Bind) == 0 {
  397. return fmt.Errorf("Bind address is not defined")
  398. }
  399. if conf.ReadTimeout <= 0 {
  400. return fmt.Errorf("Read timeout should be greater than 0, now - %d\n", conf.ReadTimeout)
  401. }
  402. if conf.WriteTimeout <= 0 {
  403. return fmt.Errorf("Write timeout should be greater than 0, now - %d\n", conf.WriteTimeout)
  404. }
  405. if conf.KeepAliveTimeout < 0 {
  406. return fmt.Errorf("KeepAlive timeout should be greater than or equal to 0, now - %d\n", conf.KeepAliveTimeout)
  407. }
  408. if conf.DownloadTimeout <= 0 {
  409. return fmt.Errorf("Download timeout should be greater than 0, now - %d\n", conf.DownloadTimeout)
  410. }
  411. if conf.Concurrency <= 0 {
  412. return fmt.Errorf("Concurrency should be greater than 0, now - %d\n", conf.Concurrency)
  413. }
  414. if conf.MaxClients <= 0 {
  415. conf.MaxClients = conf.Concurrency * 10
  416. }
  417. if conf.TTL <= 0 {
  418. return fmt.Errorf("TTL should be greater than 0, now - %d\n", conf.TTL)
  419. }
  420. if conf.MaxSrcDimension < 0 {
  421. return fmt.Errorf("Max src dimension should be greater than or equal to 0, now - %d\n", conf.MaxSrcDimension)
  422. } else if conf.MaxSrcDimension > 0 {
  423. logWarning("IMGPROXY_MAX_SRC_DIMENSION is deprecated and can be removed in future versions. Use IMGPROXY_MAX_SRC_RESOLUTION")
  424. }
  425. if conf.MaxSrcResolution <= 0 {
  426. return fmt.Errorf("Max src resolution should be greater than 0, now - %d\n", conf.MaxSrcResolution)
  427. }
  428. if conf.MaxSrcFileSize < 0 {
  429. return fmt.Errorf("Max src file size should be greater than or equal to 0, now - %d\n", conf.MaxSrcFileSize)
  430. }
  431. if conf.MaxAnimationFrames <= 0 {
  432. return fmt.Errorf("Max animation frames should be greater than 0, now - %d\n", conf.MaxAnimationFrames)
  433. }
  434. if conf.PngQuantizationColors < 2 {
  435. return fmt.Errorf("Png quantization colors should be greater than 1, now - %d\n", conf.PngQuantizationColors)
  436. } else if conf.PngQuantizationColors > 256 {
  437. return fmt.Errorf("Png quantization colors can't be greater than 256, now - %d\n", conf.PngQuantizationColors)
  438. }
  439. if conf.Quality <= 0 {
  440. return fmt.Errorf("Quality should be greater than 0, now - %d\n", conf.Quality)
  441. } else if conf.Quality > 100 {
  442. return fmt.Errorf("Quality can't be greater than 100, now - %d\n", conf.Quality)
  443. }
  444. if conf.GZipCompression < 0 {
  445. return fmt.Errorf("GZip compression should be greater than or equal to 0, now - %d\n", conf.GZipCompression)
  446. } else if conf.GZipCompression > 9 {
  447. return fmt.Errorf("GZip compression can't be greater than 9, now - %d\n", conf.GZipCompression)
  448. }
  449. if conf.GZipCompression > 0 {
  450. logWarning("GZip compression is deprecated and can be removed in future versions")
  451. }
  452. if conf.IgnoreSslVerification {
  453. logWarning("Ignoring SSL verification is very unsafe")
  454. }
  455. if conf.LocalFileSystemRoot != "" {
  456. stat, err := os.Stat(conf.LocalFileSystemRoot)
  457. if err != nil {
  458. return fmt.Errorf("Cannot use local directory: %s", err)
  459. }
  460. if !stat.IsDir() {
  461. return fmt.Errorf("Cannot use local directory: not a directory")
  462. }
  463. if conf.LocalFileSystemRoot == "/" {
  464. logWarning("Exposing root via IMGPROXY_LOCAL_FILESYSTEM_ROOT is unsafe")
  465. }
  466. }
  467. if _, ok := os.LookupEnv("IMGPROXY_USE_GCS"); !ok && len(conf.GCSKey) > 0 {
  468. logWarning("Set IMGPROXY_USE_GCS to true since it may be required by future versions to enable GCS support")
  469. conf.GCSEnabled = true
  470. }
  471. if conf.WatermarkOpacity <= 0 {
  472. return fmt.Errorf("Watermark opacity should be greater than 0")
  473. } else if conf.WatermarkOpacity > 1 {
  474. return fmt.Errorf("Watermark opacity should be less than or equal to 1")
  475. }
  476. if len(conf.PrometheusBind) > 0 && conf.PrometheusBind == conf.Bind {
  477. return fmt.Errorf("Can't use the same binding for the main server and Prometheus")
  478. }
  479. if conf.FreeMemoryInterval <= 0 {
  480. return fmt.Errorf("Free memory interval should be greater than zero")
  481. }
  482. if conf.DownloadBufferSize < 0 {
  483. return fmt.Errorf("Download buffer size should be greater than or equal to 0")
  484. } else if conf.DownloadBufferSize > math.MaxInt32 {
  485. return fmt.Errorf("Download buffer size can't be greater than %d", math.MaxInt32)
  486. }
  487. if conf.GZipBufferSize < 0 {
  488. return fmt.Errorf("GZip buffer size should be greater than or equal to 0")
  489. } else if conf.GZipBufferSize > math.MaxInt32 {
  490. return fmt.Errorf("GZip buffer size can't be greater than %d", math.MaxInt32)
  491. }
  492. if conf.BufferPoolCalibrationThreshold < 64 {
  493. return fmt.Errorf("Buffer pool calibration threshold should be greater than or equal to 64")
  494. }
  495. return nil
  496. }