config.go 17 KB

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