config.go 16 KB

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