config.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 boolEnvConfig(b *bool, name string) {
  34. if env, err := strconv.ParseBool(os.Getenv(name)); err == nil {
  35. *b = env
  36. }
  37. }
  38. func hexEnvConfig(b *[]securityKey, name string) {
  39. var err error
  40. if env := os.Getenv(name); len(env) > 0 {
  41. parts := strings.Split(env, ",")
  42. keys := make([]securityKey, len(parts))
  43. for i, part := range parts {
  44. if keys[i], err = hex.DecodeString(part); err != nil {
  45. logFatal("%s expected to be hex-encoded strings. Invalid: %s\n", name, part)
  46. }
  47. }
  48. *b = keys
  49. }
  50. }
  51. func hexFileConfig(b *[]securityKey, filepath string) {
  52. if len(filepath) == 0 {
  53. return
  54. }
  55. f, err := os.Open(filepath)
  56. if err != nil {
  57. logFatal("Can't open file %s\n", filepath)
  58. }
  59. keys := []securityKey{}
  60. scanner := bufio.NewScanner(f)
  61. for scanner.Scan() {
  62. part := scanner.Text()
  63. if len(part) == 0 {
  64. continue
  65. }
  66. if key, err := hex.DecodeString(part); err == nil {
  67. keys = append(keys, key)
  68. } else {
  69. logFatal("%s expected to contain hex-encoded strings. Invalid: %s\n", filepath, part)
  70. }
  71. }
  72. if err := scanner.Err(); err != nil {
  73. logFatal("Failed to read file %s: %s", filepath, err)
  74. }
  75. *b = keys
  76. }
  77. func presetEnvConfig(p presets, name string) {
  78. if env := os.Getenv(name); len(env) > 0 {
  79. presetStrings := strings.Split(env, ",")
  80. for _, presetStr := range presetStrings {
  81. if err := parsePreset(p, presetStr); err != nil {
  82. logFatal(err.Error())
  83. }
  84. }
  85. }
  86. }
  87. func presetFileConfig(p presets, filepath string) {
  88. if len(filepath) == 0 {
  89. return
  90. }
  91. f, err := os.Open(filepath)
  92. if err != nil {
  93. logFatal("Can't open file %s\n", filepath)
  94. }
  95. scanner := bufio.NewScanner(f)
  96. for scanner.Scan() {
  97. if err := parsePreset(p, scanner.Text()); err != nil {
  98. logFatal(err.Error())
  99. }
  100. }
  101. if err := scanner.Err(); err != nil {
  102. logFatal("Failed to read presets file: %s", err)
  103. }
  104. }
  105. func sourceEnvConfig(allowedsources *[]string, name string) {
  106. sources := []string{}
  107. if env := os.Getenv(name); len(env) > 0 {
  108. for _, source := range strings.Split(env, ",") {
  109. sources = append(sources, fmt.Sprintf("%s://", strings.TrimSpace(source)))
  110. }
  111. }
  112. *allowedsources = sources
  113. }
  114. type config struct {
  115. Bind string
  116. ReadTimeout int
  117. WriteTimeout int
  118. KeepAliveTimeout int
  119. DownloadTimeout int
  120. Concurrency int
  121. MaxClients int
  122. TTL int
  123. SoReuseport bool
  124. MaxSrcDimension int
  125. MaxSrcResolution int
  126. MaxSrcFileSize int
  127. MaxAnimationFrames int
  128. JpegProgressive bool
  129. PngInterlaced bool
  130. PngQuantize bool
  131. PngQuantizationColors int
  132. Quality int
  133. GZipCompression int
  134. EnableWebpDetection bool
  135. EnforceWebp bool
  136. EnableClientHints bool
  137. UseLinearColorspace bool
  138. DisableShrinkOnLoad bool
  139. Keys []securityKey
  140. Salts []securityKey
  141. AllowInsecure bool
  142. SignatureSize int
  143. Secret string
  144. AllowOrigin string
  145. UserAgent string
  146. IgnoreSslVerification bool
  147. DevelopmentErrorsMode bool
  148. AllowedSources []string
  149. LocalFileSystemRoot string
  150. S3Enabled bool
  151. S3Region string
  152. S3Endpoint string
  153. GCSEnabled bool
  154. GCSKey string
  155. ETagEnabled bool
  156. BaseURL string
  157. Presets presets
  158. OnlyPresets bool
  159. WatermarkData string
  160. WatermarkPath string
  161. WatermarkURL string
  162. WatermarkOpacity float64
  163. NewRelicAppName string
  164. NewRelicKey string
  165. PrometheusBind string
  166. BugsnagKey string
  167. BugsnagStage string
  168. HoneybadgerKey string
  169. HoneybadgerEnv string
  170. SentryDSN string
  171. SentryEnvironment string
  172. SentryRelease string
  173. ReportDownloadingErrors bool
  174. FreeMemoryInterval int
  175. DownloadBufferSize int
  176. GZipBufferSize int
  177. BufferPoolCalibrationThreshold int
  178. }
  179. var conf = config{
  180. Bind: ":8080",
  181. ReadTimeout: 10,
  182. WriteTimeout: 10,
  183. KeepAliveTimeout: 10,
  184. DownloadTimeout: 5,
  185. Concurrency: runtime.NumCPU() * 2,
  186. TTL: 3600,
  187. MaxSrcResolution: 16800000,
  188. MaxAnimationFrames: 1,
  189. SignatureSize: 32,
  190. PngQuantizationColors: 256,
  191. Quality: 80,
  192. UserAgent: fmt.Sprintf("imgproxy/%s", version),
  193. Presets: make(presets),
  194. WatermarkOpacity: 1,
  195. BugsnagStage: "production",
  196. HoneybadgerEnv: "production",
  197. SentryEnvironment: "production",
  198. SentryRelease: fmt.Sprintf("imgproxy/%s", version),
  199. ReportDownloadingErrors: true,
  200. FreeMemoryInterval: 10,
  201. BufferPoolCalibrationThreshold: 1024,
  202. }
  203. func configure() {
  204. keyPath := flag.String("keypath", "", "path of the file with hex-encoded key")
  205. saltPath := flag.String("saltpath", "", "path of the file with hex-encoded salt")
  206. presetsPath := flag.String("presets", "", "path of the file with presets")
  207. showVersion := flag.Bool("v", false, "show version")
  208. flag.Parse()
  209. if *showVersion {
  210. fmt.Println(version)
  211. os.Exit(0)
  212. }
  213. if port := os.Getenv("PORT"); len(port) > 0 {
  214. conf.Bind = fmt.Sprintf(":%s", port)
  215. }
  216. strEnvConfig(&conf.Bind, "IMGPROXY_BIND")
  217. intEnvConfig(&conf.ReadTimeout, "IMGPROXY_READ_TIMEOUT")
  218. intEnvConfig(&conf.WriteTimeout, "IMGPROXY_WRITE_TIMEOUT")
  219. intEnvConfig(&conf.KeepAliveTimeout, "IMGPROXY_KEEP_ALIVE_TIMEOUT")
  220. intEnvConfig(&conf.DownloadTimeout, "IMGPROXY_DOWNLOAD_TIMEOUT")
  221. intEnvConfig(&conf.Concurrency, "IMGPROXY_CONCURRENCY")
  222. intEnvConfig(&conf.MaxClients, "IMGPROXY_MAX_CLIENTS")
  223. intEnvConfig(&conf.TTL, "IMGPROXY_TTL")
  224. boolEnvConfig(&conf.SoReuseport, "IMGPROXY_SO_REUSEPORT")
  225. intEnvConfig(&conf.MaxSrcDimension, "IMGPROXY_MAX_SRC_DIMENSION")
  226. megaIntEnvConfig(&conf.MaxSrcResolution, "IMGPROXY_MAX_SRC_RESOLUTION")
  227. intEnvConfig(&conf.MaxSrcFileSize, "IMGPROXY_MAX_SRC_FILE_SIZE")
  228. if _, ok := os.LookupEnv("IMGPROXY_MAX_GIF_FRAMES"); ok {
  229. logWarning("`IMGPROXY_MAX_GIF_FRAMES` is deprecated and will be removed in future versions. Use `IMGPROXY_MAX_ANIMATION_FRAMES` instead")
  230. intEnvConfig(&conf.MaxAnimationFrames, "IMGPROXY_MAX_GIF_FRAMES")
  231. }
  232. intEnvConfig(&conf.MaxAnimationFrames, "IMGPROXY_MAX_ANIMATION_FRAMES")
  233. sourceEnvConfig(&conf.AllowedSources, "IMGPROXY_ALLOWED_SOURCES")
  234. boolEnvConfig(&conf.JpegProgressive, "IMGPROXY_JPEG_PROGRESSIVE")
  235. boolEnvConfig(&conf.PngInterlaced, "IMGPROXY_PNG_INTERLACED")
  236. boolEnvConfig(&conf.PngQuantize, "IMGPROXY_PNG_QUANTIZE")
  237. intEnvConfig(&conf.PngQuantizationColors, "IMGPROXY_PNG_QUANTIZATION_COLORS")
  238. intEnvConfig(&conf.Quality, "IMGPROXY_QUALITY")
  239. intEnvConfig(&conf.GZipCompression, "IMGPROXY_GZIP_COMPRESSION")
  240. boolEnvConfig(&conf.EnableWebpDetection, "IMGPROXY_ENABLE_WEBP_DETECTION")
  241. boolEnvConfig(&conf.EnforceWebp, "IMGPROXY_ENFORCE_WEBP")
  242. boolEnvConfig(&conf.EnableClientHints, "IMGPROXY_ENABLE_CLIENT_HINTS")
  243. boolEnvConfig(&conf.UseLinearColorspace, "IMGPROXY_USE_LINEAR_COLORSPACE")
  244. boolEnvConfig(&conf.DisableShrinkOnLoad, "IMGPROXY_DISABLE_SHRINK_ON_LOAD")
  245. hexEnvConfig(&conf.Keys, "IMGPROXY_KEY")
  246. hexEnvConfig(&conf.Salts, "IMGPROXY_SALT")
  247. intEnvConfig(&conf.SignatureSize, "IMGPROXY_SIGNATURE_SIZE")
  248. hexFileConfig(&conf.Keys, *keyPath)
  249. hexFileConfig(&conf.Salts, *saltPath)
  250. strEnvConfig(&conf.Secret, "IMGPROXY_SECRET")
  251. strEnvConfig(&conf.AllowOrigin, "IMGPROXY_ALLOW_ORIGIN")
  252. strEnvConfig(&conf.UserAgent, "IMGPROXY_USER_AGENT")
  253. boolEnvConfig(&conf.IgnoreSslVerification, "IMGPROXY_IGNORE_SSL_VERIFICATION")
  254. boolEnvConfig(&conf.DevelopmentErrorsMode, "IMGPROXY_DEVELOPMENT_ERRORS_MODE")
  255. strEnvConfig(&conf.LocalFileSystemRoot, "IMGPROXY_LOCAL_FILESYSTEM_ROOT")
  256. boolEnvConfig(&conf.S3Enabled, "IMGPROXY_USE_S3")
  257. strEnvConfig(&conf.S3Region, "IMGPROXY_S3_REGION")
  258. strEnvConfig(&conf.S3Endpoint, "IMGPROXY_S3_ENDPOINT")
  259. boolEnvConfig(&conf.GCSEnabled, "IMGPROXY_USE_GCS")
  260. strEnvConfig(&conf.GCSKey, "IMGPROXY_GCS_KEY")
  261. boolEnvConfig(&conf.ETagEnabled, "IMGPROXY_USE_ETAG")
  262. strEnvConfig(&conf.BaseURL, "IMGPROXY_BASE_URL")
  263. presetEnvConfig(conf.Presets, "IMGPROXY_PRESETS")
  264. presetFileConfig(conf.Presets, *presetsPath)
  265. boolEnvConfig(&conf.OnlyPresets, "IMGPROXY_ONLY_PRESETS")
  266. strEnvConfig(&conf.WatermarkData, "IMGPROXY_WATERMARK_DATA")
  267. strEnvConfig(&conf.WatermarkPath, "IMGPROXY_WATERMARK_PATH")
  268. strEnvConfig(&conf.WatermarkURL, "IMGPROXY_WATERMARK_URL")
  269. floatEnvConfig(&conf.WatermarkOpacity, "IMGPROXY_WATERMARK_OPACITY")
  270. strEnvConfig(&conf.NewRelicAppName, "IMGPROXY_NEW_RELIC_APP_NAME")
  271. strEnvConfig(&conf.NewRelicKey, "IMGPROXY_NEW_RELIC_KEY")
  272. strEnvConfig(&conf.PrometheusBind, "IMGPROXY_PROMETHEUS_BIND")
  273. strEnvConfig(&conf.BugsnagKey, "IMGPROXY_BUGSNAG_KEY")
  274. strEnvConfig(&conf.BugsnagStage, "IMGPROXY_BUGSNAG_STAGE")
  275. strEnvConfig(&conf.HoneybadgerKey, "IMGPROXY_HONEYBADGER_KEY")
  276. strEnvConfig(&conf.HoneybadgerEnv, "IMGPROXY_HONEYBADGER_ENV")
  277. strEnvConfig(&conf.SentryDSN, "IMGPROXY_SENTRY_DSN")
  278. strEnvConfig(&conf.SentryEnvironment, "IMGPROXY_SENTRY_ENVIRONMENT")
  279. strEnvConfig(&conf.SentryRelease, "IMGPROXY_SENTRY_RELEASE")
  280. boolEnvConfig(&conf.ReportDownloadingErrors, "IMGPROXY_REPORT_DOWNLOADING_ERRORS")
  281. intEnvConfig(&conf.FreeMemoryInterval, "IMGPROXY_FREE_MEMORY_INTERVAL")
  282. intEnvConfig(&conf.DownloadBufferSize, "IMGPROXY_DOWNLOAD_BUFFER_SIZE")
  283. intEnvConfig(&conf.GZipBufferSize, "IMGPROXY_GZIP_BUFFER_SIZE")
  284. intEnvConfig(&conf.BufferPoolCalibrationThreshold, "IMGPROXY_BUFFER_POOL_CALIBRATION_THRESHOLD")
  285. if len(conf.Keys) != len(conf.Salts) {
  286. logFatal("Number of keys and number of salts should be equal. Keys: %d, salts: %d", len(conf.Keys), len(conf.Salts))
  287. }
  288. if len(conf.Keys) == 0 {
  289. logWarning("No keys defined, so signature checking is disabled")
  290. conf.AllowInsecure = true
  291. }
  292. if len(conf.Salts) == 0 {
  293. logWarning("No salts defined, so signature checking is disabled")
  294. conf.AllowInsecure = true
  295. }
  296. if conf.SignatureSize < 1 || conf.SignatureSize > 32 {
  297. logFatal("Signature size should be within 1 and 32, now - %d\n", conf.SignatureSize)
  298. }
  299. if len(conf.Bind) == 0 {
  300. logFatal("Bind address is not defined")
  301. }
  302. if conf.ReadTimeout <= 0 {
  303. logFatal("Read timeout should be greater than 0, now - %d\n", conf.ReadTimeout)
  304. }
  305. if conf.WriteTimeout <= 0 {
  306. logFatal("Write timeout should be greater than 0, now - %d\n", conf.WriteTimeout)
  307. }
  308. if conf.KeepAliveTimeout < 0 {
  309. logFatal("KeepAlive timeout should be greater than or equal to 0, now - %d\n", conf.KeepAliveTimeout)
  310. }
  311. if conf.DownloadTimeout <= 0 {
  312. logFatal("Download timeout should be greater than 0, now - %d\n", conf.DownloadTimeout)
  313. }
  314. if conf.Concurrency <= 0 {
  315. logFatal("Concurrency should be greater than 0, now - %d\n", conf.Concurrency)
  316. }
  317. if conf.MaxClients <= 0 {
  318. conf.MaxClients = conf.Concurrency * 10
  319. }
  320. if conf.TTL <= 0 {
  321. logFatal("TTL should be greater than 0, now - %d\n", conf.TTL)
  322. }
  323. if conf.MaxSrcDimension < 0 {
  324. logFatal("Max src dimension should be greater than or equal to 0, now - %d\n", conf.MaxSrcDimension)
  325. } else if conf.MaxSrcDimension > 0 {
  326. logWarning("IMGPROXY_MAX_SRC_DIMENSION is deprecated and can be removed in future versions. Use IMGPROXY_MAX_SRC_RESOLUTION")
  327. }
  328. if conf.MaxSrcResolution <= 0 {
  329. logFatal("Max src resolution should be greater than 0, now - %d\n", conf.MaxSrcResolution)
  330. }
  331. if conf.MaxSrcFileSize < 0 {
  332. logFatal("Max src file size should be greater than or equal to 0, now - %d\n", conf.MaxSrcFileSize)
  333. }
  334. if conf.MaxAnimationFrames <= 0 {
  335. logFatal("Max animation frames should be greater than 0, now - %d\n", conf.MaxAnimationFrames)
  336. }
  337. if conf.PngQuantizationColors < 2 {
  338. logFatal("Png quantization colors should be greater than 1, now - %d\n", conf.PngQuantizationColors)
  339. } else if conf.PngQuantizationColors > 256 {
  340. logFatal("Png quantization colors can't be greater than 256, now - %d\n", conf.PngQuantizationColors)
  341. }
  342. if conf.Quality <= 0 {
  343. logFatal("Quality should be greater than 0, now - %d\n", conf.Quality)
  344. } else if conf.Quality > 100 {
  345. logFatal("Quality can't be greater than 100, now - %d\n", conf.Quality)
  346. }
  347. if conf.GZipCompression < 0 {
  348. logFatal("GZip compression should be greater than or equal to 0, now - %d\n", conf.GZipCompression)
  349. } else if conf.GZipCompression > 9 {
  350. logFatal("GZip compression can't be greater than 9, now - %d\n", conf.GZipCompression)
  351. }
  352. if conf.GZipCompression > 0 {
  353. logWarning("GZip compression is deprecated and can be removed in future versions")
  354. }
  355. if conf.IgnoreSslVerification {
  356. logWarning("Ignoring SSL verification is very unsafe")
  357. }
  358. if conf.LocalFileSystemRoot != "" {
  359. stat, err := os.Stat(conf.LocalFileSystemRoot)
  360. if err != nil {
  361. logFatal("Cannot use local directory: %s", err)
  362. }
  363. if !stat.IsDir() {
  364. logFatal("Cannot use local directory: not a directory")
  365. }
  366. if conf.LocalFileSystemRoot == "/" {
  367. logWarning("Exposing root via IMGPROXY_LOCAL_FILESYSTEM_ROOT is unsafe")
  368. }
  369. }
  370. if _, ok := os.LookupEnv("IMGPROXY_USE_GCS"); !ok && len(conf.GCSKey) > 0 {
  371. logWarning("Set IMGPROXY_USE_GCS to true since it may be required by future versions to enable GCS support")
  372. conf.GCSEnabled = true
  373. }
  374. if conf.WatermarkOpacity <= 0 {
  375. logFatal("Watermark opacity should be greater than 0")
  376. } else if conf.WatermarkOpacity > 1 {
  377. logFatal("Watermark opacity should be less than or equal to 1")
  378. }
  379. if len(conf.PrometheusBind) > 0 && conf.PrometheusBind == conf.Bind {
  380. logFatal("Can't use the same binding for the main server and Prometheus")
  381. }
  382. if conf.FreeMemoryInterval <= 0 {
  383. logFatal("Free memory interval should be greater than zero")
  384. }
  385. if conf.DownloadBufferSize < 0 {
  386. logFatal("Download buffer size should be greater than or equal to 0")
  387. } else if conf.DownloadBufferSize > math.MaxInt32 {
  388. logFatal("Download buffer size can't be greater than %d", math.MaxInt32)
  389. }
  390. if conf.GZipBufferSize < 0 {
  391. logFatal("GZip buffer size should be greater than or equal to 0")
  392. } else if conf.GZipBufferSize > math.MaxInt32 {
  393. logFatal("GZip buffer size can't be greater than %d", math.MaxInt32)
  394. }
  395. if conf.BufferPoolCalibrationThreshold < 64 {
  396. logFatal("Buffer pool calibration threshold should be greater than or equal to 64")
  397. }
  398. }