config.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. package config
  2. import (
  3. "flag"
  4. "fmt"
  5. "math"
  6. "os"
  7. "regexp"
  8. "runtime"
  9. log "github.com/sirupsen/logrus"
  10. "github.com/imgproxy/imgproxy/v3/config/configurators"
  11. "github.com/imgproxy/imgproxy/v3/imagetype"
  12. "github.com/imgproxy/imgproxy/v3/version"
  13. )
  14. var (
  15. Network string
  16. Bind string
  17. ReadTimeout int
  18. WriteTimeout int
  19. KeepAliveTimeout int
  20. DownloadTimeout int
  21. Concurrency int
  22. RequestsQueueSize int
  23. MaxClients int
  24. TTL int
  25. CacheControlPassthrough bool
  26. SetCanonicalHeader bool
  27. SoReuseport bool
  28. PathPrefix string
  29. MaxSrcResolution int
  30. MaxSrcFileSize int
  31. MaxAnimationFrames int
  32. MaxSvgCheckBytes int
  33. MaxRedirects int
  34. JpegProgressive bool
  35. PngInterlaced bool
  36. PngQuantize bool
  37. PngQuantizationColors int
  38. AvifSpeed int
  39. Quality int
  40. FormatQuality map[imagetype.Type]int
  41. StripMetadata bool
  42. KeepCopyright bool
  43. StripColorProfile bool
  44. AutoRotate bool
  45. EnforceThumbnail bool
  46. ReturnAttachment bool
  47. EnableWebpDetection bool
  48. EnforceWebp bool
  49. EnableAvifDetection bool
  50. EnforceAvif bool
  51. EnableClientHints bool
  52. PreferredFormats []imagetype.Type
  53. SkipProcessingFormats []imagetype.Type
  54. UseLinearColorspace bool
  55. DisableShrinkOnLoad bool
  56. Keys [][]byte
  57. Salts [][]byte
  58. SignatureSize int
  59. Secret string
  60. AllowOrigin string
  61. UserAgent string
  62. IgnoreSslVerification bool
  63. DevelopmentErrorsMode bool
  64. AllowedSources []*regexp.Regexp
  65. SanitizeSvg bool
  66. CookiePassthrough bool
  67. CookieBaseURL string
  68. LocalFileSystemRoot string
  69. S3Enabled bool
  70. S3Region string
  71. S3Endpoint string
  72. GCSEnabled bool
  73. GCSKey string
  74. GCSEndpoint string
  75. ABSEnabled bool
  76. ABSName string
  77. ABSKey string
  78. ABSEndpoint string
  79. SwiftEnabled bool
  80. SwiftUsername string
  81. SwiftAPIKey string
  82. SwiftAuthURL string
  83. SwiftDomain string
  84. SwiftTenant string
  85. SwiftAuthVersion int
  86. SwiftConnectTimeoutSeconds int
  87. SwiftTimeoutSeconds int
  88. ETagEnabled bool
  89. ETagBuster string
  90. BaseURL string
  91. Presets []string
  92. OnlyPresets bool
  93. WatermarkData string
  94. WatermarkPath string
  95. WatermarkURL string
  96. WatermarkOpacity float64
  97. FallbackImageData string
  98. FallbackImagePath string
  99. FallbackImageURL string
  100. FallbackImageHTTPCode int
  101. FallbackImageTTL int
  102. DataDogEnable bool
  103. NewRelicAppName string
  104. NewRelicKey string
  105. NewRelicLabels map[string]string
  106. PrometheusBind string
  107. PrometheusNamespace string
  108. BugsnagKey string
  109. BugsnagStage string
  110. HoneybadgerKey string
  111. HoneybadgerEnv string
  112. SentryDSN string
  113. SentryEnvironment string
  114. SentryRelease string
  115. AirbrakeProjecID int
  116. AirbrakeProjecKey string
  117. AirbrakeEnv string
  118. ReportDownloadingErrors bool
  119. EnableDebugHeaders bool
  120. FreeMemoryInterval int
  121. DownloadBufferSize int
  122. BufferPoolCalibrationThreshold int
  123. HealthCheckPath string
  124. )
  125. var (
  126. keyPath string
  127. saltPath string
  128. presetsPath string
  129. )
  130. func init() {
  131. Reset()
  132. flag.StringVar(&keyPath, "keypath", "", "path of the file with hex-encoded key")
  133. flag.StringVar(&saltPath, "saltpath", "", "path of the file with hex-encoded salt")
  134. flag.StringVar(&presetsPath, "presets", "", "path of the file with presets")
  135. }
  136. func Reset() {
  137. Network = "tcp"
  138. Bind = ":8080"
  139. ReadTimeout = 10
  140. WriteTimeout = 10
  141. KeepAliveTimeout = 10
  142. DownloadTimeout = 5
  143. Concurrency = runtime.NumCPU() * 2
  144. RequestsQueueSize = 0
  145. MaxClients = 2048
  146. TTL = 31536000
  147. CacheControlPassthrough = false
  148. SetCanonicalHeader = false
  149. SoReuseport = false
  150. PathPrefix = ""
  151. MaxSrcResolution = 16800000
  152. MaxSrcFileSize = 0
  153. MaxAnimationFrames = 1
  154. MaxSvgCheckBytes = 32 * 1024
  155. MaxRedirects = 10
  156. JpegProgressive = false
  157. PngInterlaced = false
  158. PngQuantize = false
  159. PngQuantizationColors = 256
  160. AvifSpeed = 5
  161. Quality = 80
  162. FormatQuality = map[imagetype.Type]int{imagetype.AVIF: 50}
  163. StripMetadata = true
  164. KeepCopyright = true
  165. StripColorProfile = true
  166. AutoRotate = true
  167. EnforceThumbnail = false
  168. ReturnAttachment = false
  169. EnableWebpDetection = false
  170. EnforceWebp = false
  171. EnableAvifDetection = false
  172. EnforceAvif = false
  173. EnableClientHints = false
  174. PreferredFormats = []imagetype.Type{
  175. imagetype.JPEG,
  176. imagetype.PNG,
  177. imagetype.GIF,
  178. imagetype.WEBP,
  179. imagetype.AVIF,
  180. imagetype.ICO,
  181. }
  182. SkipProcessingFormats = make([]imagetype.Type, 0)
  183. UseLinearColorspace = false
  184. DisableShrinkOnLoad = false
  185. Keys = make([][]byte, 0)
  186. Salts = make([][]byte, 0)
  187. SignatureSize = 32
  188. Secret = ""
  189. AllowOrigin = ""
  190. UserAgent = fmt.Sprintf("imgproxy/%s", version.Version())
  191. IgnoreSslVerification = false
  192. DevelopmentErrorsMode = false
  193. AllowedSources = make([]*regexp.Regexp, 0)
  194. SanitizeSvg = true
  195. CookiePassthrough = false
  196. CookieBaseURL = ""
  197. LocalFileSystemRoot = ""
  198. S3Enabled = false
  199. S3Region = ""
  200. S3Endpoint = ""
  201. GCSEnabled = false
  202. GCSKey = ""
  203. ABSEnabled = false
  204. ABSName = ""
  205. ABSKey = ""
  206. ABSEndpoint = ""
  207. SwiftEnabled = false
  208. SwiftUsername = ""
  209. SwiftAPIKey = ""
  210. SwiftAuthURL = ""
  211. SwiftAuthVersion = 0
  212. SwiftTenant = ""
  213. SwiftDomain = ""
  214. SwiftConnectTimeoutSeconds = 10
  215. SwiftTimeoutSeconds = 60
  216. ETagEnabled = false
  217. ETagBuster = ""
  218. BaseURL = ""
  219. Presets = make([]string, 0)
  220. OnlyPresets = false
  221. WatermarkData = ""
  222. WatermarkPath = ""
  223. WatermarkURL = ""
  224. WatermarkOpacity = 1
  225. FallbackImageData = ""
  226. FallbackImagePath = ""
  227. FallbackImageURL = ""
  228. FallbackImageHTTPCode = 200
  229. FallbackImageTTL = 0
  230. DataDogEnable = false
  231. NewRelicAppName = ""
  232. NewRelicKey = ""
  233. NewRelicLabels = make(map[string]string)
  234. PrometheusBind = ""
  235. PrometheusNamespace = ""
  236. BugsnagKey = ""
  237. BugsnagStage = "production"
  238. HoneybadgerKey = ""
  239. HoneybadgerEnv = "production"
  240. SentryDSN = ""
  241. SentryEnvironment = "production"
  242. SentryRelease = fmt.Sprintf("imgproxy@%s", version.Version())
  243. AirbrakeProjecID = 0
  244. AirbrakeProjecKey = ""
  245. AirbrakeEnv = "production"
  246. ReportDownloadingErrors = true
  247. EnableDebugHeaders = false
  248. FreeMemoryInterval = 10
  249. DownloadBufferSize = 0
  250. BufferPoolCalibrationThreshold = 1024
  251. HealthCheckPath = ""
  252. }
  253. func Configure() error {
  254. if port := os.Getenv("PORT"); len(port) > 0 {
  255. Bind = fmt.Sprintf(":%s", port)
  256. }
  257. configurators.String(&Network, "IMGPROXY_NETWORK")
  258. configurators.String(&Bind, "IMGPROXY_BIND")
  259. configurators.Int(&ReadTimeout, "IMGPROXY_READ_TIMEOUT")
  260. configurators.Int(&WriteTimeout, "IMGPROXY_WRITE_TIMEOUT")
  261. configurators.Int(&KeepAliveTimeout, "IMGPROXY_KEEP_ALIVE_TIMEOUT")
  262. configurators.Int(&DownloadTimeout, "IMGPROXY_DOWNLOAD_TIMEOUT")
  263. configurators.Int(&Concurrency, "IMGPROXY_CONCURRENCY")
  264. configurators.Int(&RequestsQueueSize, "IMGPROXY_REQUESTS_QUEUE_SIZE")
  265. configurators.Int(&MaxClients, "IMGPROXY_MAX_CLIENTS")
  266. configurators.Int(&TTL, "IMGPROXY_TTL")
  267. configurators.Bool(&CacheControlPassthrough, "IMGPROXY_CACHE_CONTROL_PASSTHROUGH")
  268. configurators.Bool(&SetCanonicalHeader, "IMGPROXY_SET_CANONICAL_HEADER")
  269. configurators.Bool(&SoReuseport, "IMGPROXY_SO_REUSEPORT")
  270. configurators.String(&PathPrefix, "IMGPROXY_PATH_PREFIX")
  271. configurators.MegaInt(&MaxSrcResolution, "IMGPROXY_MAX_SRC_RESOLUTION")
  272. configurators.Int(&MaxSrcFileSize, "IMGPROXY_MAX_SRC_FILE_SIZE")
  273. configurators.Int(&MaxSvgCheckBytes, "IMGPROXY_MAX_SVG_CHECK_BYTES")
  274. configurators.Int(&MaxAnimationFrames, "IMGPROXY_MAX_ANIMATION_FRAMES")
  275. configurators.Int(&MaxRedirects, "IMGPROXY_MAX_REDIRECTS")
  276. configurators.Patterns(&AllowedSources, "IMGPROXY_ALLOWED_SOURCES")
  277. configurators.Bool(&SanitizeSvg, "IMGPROXY_SANITIZE_SVG")
  278. configurators.Bool(&JpegProgressive, "IMGPROXY_JPEG_PROGRESSIVE")
  279. configurators.Bool(&PngInterlaced, "IMGPROXY_PNG_INTERLACED")
  280. configurators.Bool(&PngQuantize, "IMGPROXY_PNG_QUANTIZE")
  281. configurators.Int(&PngQuantizationColors, "IMGPROXY_PNG_QUANTIZATION_COLORS")
  282. configurators.Int(&AvifSpeed, "IMGPROXY_AVIF_SPEED")
  283. configurators.Int(&Quality, "IMGPROXY_QUALITY")
  284. if err := configurators.ImageTypesQuality(FormatQuality, "IMGPROXY_FORMAT_QUALITY"); err != nil {
  285. return err
  286. }
  287. configurators.Bool(&StripMetadata, "IMGPROXY_STRIP_METADATA")
  288. configurators.Bool(&KeepCopyright, "IMGPROXY_KEEP_COPYRIGHT")
  289. configurators.Bool(&StripColorProfile, "IMGPROXY_STRIP_COLOR_PROFILE")
  290. configurators.Bool(&AutoRotate, "IMGPROXY_AUTO_ROTATE")
  291. configurators.Bool(&EnforceThumbnail, "IMGPROXY_ENFORCE_THUMBNAIL")
  292. configurators.Bool(&ReturnAttachment, "IMGPROXY_RETURN_ATTACHMENT")
  293. configurators.Bool(&EnableWebpDetection, "IMGPROXY_ENABLE_WEBP_DETECTION")
  294. configurators.Bool(&EnforceWebp, "IMGPROXY_ENFORCE_WEBP")
  295. configurators.Bool(&EnableAvifDetection, "IMGPROXY_ENABLE_AVIF_DETECTION")
  296. configurators.Bool(&EnforceAvif, "IMGPROXY_ENFORCE_AVIF")
  297. configurators.Bool(&EnableClientHints, "IMGPROXY_ENABLE_CLIENT_HINTS")
  298. configurators.String(&HealthCheckPath, "IMGPROXY_HEALTH_CHECK_PATH")
  299. if err := configurators.ImageTypes(&PreferredFormats, "IMGPROXY_PREFERRED_FORMATS"); err != nil {
  300. return err
  301. }
  302. if err := configurators.ImageTypes(&SkipProcessingFormats, "IMGPROXY_SKIP_PROCESSING_FORMATS"); err != nil {
  303. return err
  304. }
  305. configurators.Bool(&UseLinearColorspace, "IMGPROXY_USE_LINEAR_COLORSPACE")
  306. configurators.Bool(&DisableShrinkOnLoad, "IMGPROXY_DISABLE_SHRINK_ON_LOAD")
  307. if err := configurators.Hex(&Keys, "IMGPROXY_KEY"); err != nil {
  308. return err
  309. }
  310. if err := configurators.Hex(&Salts, "IMGPROXY_SALT"); err != nil {
  311. return err
  312. }
  313. configurators.Int(&SignatureSize, "IMGPROXY_SIGNATURE_SIZE")
  314. if err := configurators.HexFile(&Keys, keyPath); err != nil {
  315. return err
  316. }
  317. if err := configurators.HexFile(&Salts, saltPath); err != nil {
  318. return err
  319. }
  320. configurators.String(&Secret, "IMGPROXY_SECRET")
  321. configurators.String(&AllowOrigin, "IMGPROXY_ALLOW_ORIGIN")
  322. configurators.String(&UserAgent, "IMGPROXY_USER_AGENT")
  323. configurators.Bool(&IgnoreSslVerification, "IMGPROXY_IGNORE_SSL_VERIFICATION")
  324. configurators.Bool(&DevelopmentErrorsMode, "IMGPROXY_DEVELOPMENT_ERRORS_MODE")
  325. configurators.Bool(&CookiePassthrough, "IMGPROXY_COOKIE_PASSTHROUGH")
  326. configurators.String(&CookieBaseURL, "IMGPROXY_COOKIE_BASE_URL")
  327. configurators.String(&LocalFileSystemRoot, "IMGPROXY_LOCAL_FILESYSTEM_ROOT")
  328. configurators.Bool(&S3Enabled, "IMGPROXY_USE_S3")
  329. configurators.String(&S3Region, "IMGPROXY_S3_REGION")
  330. configurators.String(&S3Endpoint, "IMGPROXY_S3_ENDPOINT")
  331. configurators.Bool(&GCSEnabled, "IMGPROXY_USE_GCS")
  332. configurators.String(&GCSKey, "IMGPROXY_GCS_KEY")
  333. configurators.String(&GCSEndpoint, "IMGPROXY_GCS_ENDPOINT")
  334. configurators.Bool(&ABSEnabled, "IMGPROXY_USE_ABS")
  335. configurators.String(&ABSName, "IMGPROXY_ABS_NAME")
  336. configurators.String(&ABSKey, "IMGPROXY_ABS_KEY")
  337. configurators.String(&ABSEndpoint, "IMGPROXY_ABS_ENDPOINT")
  338. configurators.Bool(&SwiftEnabled, "IMGPROXY_USE_SWIFT")
  339. configurators.String(&SwiftUsername, "IMGPROXY_SWIFT_USERNAME")
  340. configurators.String(&SwiftAPIKey, "IMGPROXY_SWIFT_API_KEY")
  341. configurators.String(&SwiftAuthURL, "IMGPROXY_SWIFT_AUTH_URL")
  342. configurators.String(&SwiftDomain, "IMGPROXY_SWIFT_DOMAIN")
  343. configurators.String(&SwiftTenant, "IMGPROXY_SWIFT_TENANT")
  344. configurators.Int(&SwiftConnectTimeoutSeconds, "IMGPROXY_SWIFT_CONNECT_TIMEOUT_SECONDS")
  345. configurators.Int(&SwiftTimeoutSeconds, "IMGPROXY_SWIFT_TIMEOUT_SECONDS")
  346. configurators.Bool(&ETagEnabled, "IMGPROXY_USE_ETAG")
  347. configurators.String(&ETagBuster, "IMGPROXY_ETAG_BUSTER")
  348. configurators.String(&BaseURL, "IMGPROXY_BASE_URL")
  349. configurators.StringSlice(&Presets, "IMGPROXY_PRESETS")
  350. if err := configurators.StringSliceFile(&Presets, presetsPath); err != nil {
  351. return err
  352. }
  353. configurators.Bool(&OnlyPresets, "IMGPROXY_ONLY_PRESETS")
  354. configurators.String(&WatermarkData, "IMGPROXY_WATERMARK_DATA")
  355. configurators.String(&WatermarkPath, "IMGPROXY_WATERMARK_PATH")
  356. configurators.String(&WatermarkURL, "IMGPROXY_WATERMARK_URL")
  357. configurators.Float(&WatermarkOpacity, "IMGPROXY_WATERMARK_OPACITY")
  358. configurators.String(&FallbackImageData, "IMGPROXY_FALLBACK_IMAGE_DATA")
  359. configurators.String(&FallbackImagePath, "IMGPROXY_FALLBACK_IMAGE_PATH")
  360. configurators.String(&FallbackImageURL, "IMGPROXY_FALLBACK_IMAGE_URL")
  361. configurators.Int(&FallbackImageHTTPCode, "IMGPROXY_FALLBACK_IMAGE_HTTP_CODE")
  362. configurators.Int(&FallbackImageTTL, "IMGPROXY_FALLBACK_IMAGE_TTL")
  363. configurators.Bool(&DataDogEnable, "IMGPROXY_DATADOG_ENABLE")
  364. configurators.String(&NewRelicAppName, "IMGPROXY_NEW_RELIC_APP_NAME")
  365. configurators.String(&NewRelicKey, "IMGPROXY_NEW_RELIC_KEY")
  366. configurators.StringMap(&NewRelicLabels, "IMGPROXY_NEW_RELIC_LABELS")
  367. configurators.String(&PrometheusBind, "IMGPROXY_PROMETHEUS_BIND")
  368. configurators.String(&PrometheusNamespace, "IMGPROXY_PROMETHEUS_NAMESPACE")
  369. configurators.String(&BugsnagKey, "IMGPROXY_BUGSNAG_KEY")
  370. configurators.String(&BugsnagStage, "IMGPROXY_BUGSNAG_STAGE")
  371. configurators.String(&HoneybadgerKey, "IMGPROXY_HONEYBADGER_KEY")
  372. configurators.String(&HoneybadgerEnv, "IMGPROXY_HONEYBADGER_ENV")
  373. configurators.String(&SentryDSN, "IMGPROXY_SENTRY_DSN")
  374. configurators.String(&SentryEnvironment, "IMGPROXY_SENTRY_ENVIRONMENT")
  375. configurators.String(&SentryRelease, "IMGPROXY_SENTRY_RELEASE")
  376. configurators.Int(&AirbrakeProjecID, "IMGPROXY_AIRBRAKE_PROJECT_ID")
  377. configurators.String(&AirbrakeProjecKey, "IMGPROXY_AIRBRAKE_PROJECT_KEY")
  378. configurators.String(&AirbrakeEnv, "IMGPROXY_AIRBRAKE_ENVIRONMENT")
  379. configurators.Bool(&ReportDownloadingErrors, "IMGPROXY_REPORT_DOWNLOADING_ERRORS")
  380. configurators.Bool(&EnableDebugHeaders, "IMGPROXY_ENABLE_DEBUG_HEADERS")
  381. configurators.Int(&FreeMemoryInterval, "IMGPROXY_FREE_MEMORY_INTERVAL")
  382. configurators.Int(&DownloadBufferSize, "IMGPROXY_DOWNLOAD_BUFFER_SIZE")
  383. configurators.Int(&BufferPoolCalibrationThreshold, "IMGPROXY_BUFFER_POOL_CALIBRATION_THRESHOLD")
  384. if len(Keys) != len(Salts) {
  385. return fmt.Errorf("Number of keys and number of salts should be equal. Keys: %d, salts: %d", len(Keys), len(Salts))
  386. }
  387. if len(Keys) == 0 {
  388. log.Warning("No keys defined, so signature checking is disabled")
  389. }
  390. if len(Salts) == 0 {
  391. log.Warning("No salts defined, so signature checking is disabled")
  392. }
  393. if SignatureSize < 1 || SignatureSize > 32 {
  394. return fmt.Errorf("Signature size should be within 1 and 32, now - %d\n", SignatureSize)
  395. }
  396. if len(Bind) == 0 {
  397. return fmt.Errorf("Bind address is not defined")
  398. }
  399. if ReadTimeout <= 0 {
  400. return fmt.Errorf("Read timeout should be greater than 0, now - %d\n", ReadTimeout)
  401. }
  402. if WriteTimeout <= 0 {
  403. return fmt.Errorf("Write timeout should be greater than 0, now - %d\n", WriteTimeout)
  404. }
  405. if KeepAliveTimeout < 0 {
  406. return fmt.Errorf("KeepAlive timeout should be greater than or equal to 0, now - %d\n", KeepAliveTimeout)
  407. }
  408. if DownloadTimeout <= 0 {
  409. return fmt.Errorf("Download timeout should be greater than 0, now - %d\n", DownloadTimeout)
  410. }
  411. if Concurrency <= 0 {
  412. return fmt.Errorf("Concurrency should be greater than 0, now - %d\n", Concurrency)
  413. }
  414. if RequestsQueueSize < 0 {
  415. return fmt.Errorf("Requests queue size should be greater than or equal 0, now - %d\n", RequestsQueueSize)
  416. }
  417. if MaxClients < 0 {
  418. return fmt.Errorf("Concurrency should be greater than or equal 0, now - %d\n", MaxClients)
  419. }
  420. if TTL <= 0 {
  421. return fmt.Errorf("TTL should be greater than 0, now - %d\n", TTL)
  422. }
  423. if MaxSrcResolution <= 0 {
  424. return fmt.Errorf("Max src resolution should be greater than 0, now - %d\n", MaxSrcResolution)
  425. }
  426. if MaxSrcFileSize < 0 {
  427. return fmt.Errorf("Max src file size should be greater than or equal to 0, now - %d\n", MaxSrcFileSize)
  428. }
  429. if MaxAnimationFrames <= 0 {
  430. return fmt.Errorf("Max animation frames should be greater than 0, now - %d\n", MaxAnimationFrames)
  431. }
  432. if PngQuantizationColors < 2 {
  433. return fmt.Errorf("Png quantization colors should be greater than 1, now - %d\n", PngQuantizationColors)
  434. } else if PngQuantizationColors > 256 {
  435. return fmt.Errorf("Png quantization colors can't be greater than 256, now - %d\n", PngQuantizationColors)
  436. }
  437. if AvifSpeed < 0 {
  438. return fmt.Errorf("Avif speed should be greater than 0, now - %d\n", AvifSpeed)
  439. } else if AvifSpeed > 8 {
  440. return fmt.Errorf("Avif speed can't be greater than 8, now - %d\n", AvifSpeed)
  441. }
  442. if Quality <= 0 {
  443. return fmt.Errorf("Quality should be greater than 0, now - %d\n", Quality)
  444. } else if Quality > 100 {
  445. return fmt.Errorf("Quality can't be greater than 100, now - %d\n", Quality)
  446. }
  447. if len(PreferredFormats) == 0 {
  448. return fmt.Errorf("At least one preferred format should be specified")
  449. }
  450. if IgnoreSslVerification {
  451. log.Warning("Ignoring SSL verification is very unsafe")
  452. }
  453. if LocalFileSystemRoot != "" {
  454. stat, err := os.Stat(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 LocalFileSystemRoot == "/" {
  462. log.Warning("Exposing root via IMGPROXY_LOCAL_FILESYSTEM_ROOT is unsafe")
  463. }
  464. }
  465. if _, ok := os.LookupEnv("IMGPROXY_USE_GCS"); !ok && len(GCSKey) > 0 {
  466. log.Warning("Set IMGPROXY_USE_GCS to true since it may be required by future versions to enable GCS support")
  467. GCSEnabled = true
  468. }
  469. if WatermarkOpacity <= 0 {
  470. return fmt.Errorf("Watermark opacity should be greater than 0")
  471. } else if WatermarkOpacity > 1 {
  472. return fmt.Errorf("Watermark opacity should be less than or equal to 1")
  473. }
  474. if FallbackImageHTTPCode < 100 || FallbackImageHTTPCode > 599 {
  475. return fmt.Errorf("Fallback image HTTP code should be between 100 and 599")
  476. }
  477. if len(PrometheusBind) > 0 && PrometheusBind == Bind {
  478. return fmt.Errorf("Can't use the same binding for the main server and Prometheus")
  479. }
  480. if FreeMemoryInterval <= 0 {
  481. return fmt.Errorf("Free memory interval should be greater than zero")
  482. }
  483. if DownloadBufferSize < 0 {
  484. return fmt.Errorf("Download buffer size should be greater than or equal to 0")
  485. } else if DownloadBufferSize > math.MaxInt32 {
  486. return fmt.Errorf("Download buffer size can't be greater than %d", math.MaxInt32)
  487. }
  488. if BufferPoolCalibrationThreshold < 64 {
  489. return fmt.Errorf("Buffer pool calibration threshold should be greater than or equal to 64")
  490. }
  491. return nil
  492. }