config.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. package main
  2. import (
  3. "bufio"
  4. "bytes"
  5. "encoding/hex"
  6. "flag"
  7. "fmt"
  8. "io/ioutil"
  9. "log"
  10. "os"
  11. "runtime"
  12. "strconv"
  13. "strings"
  14. )
  15. func intEnvConfig(i *int, name string) {
  16. if env, err := strconv.Atoi(os.Getenv(name)); err == nil {
  17. *i = env
  18. }
  19. }
  20. func floatEnvConfig(i *float64, name string) {
  21. if env, err := strconv.ParseFloat(os.Getenv(name), 64); err == nil {
  22. *i = env
  23. }
  24. }
  25. func megaIntEnvConfig(f *int, name string) {
  26. if env, err := strconv.ParseFloat(os.Getenv(name), 64); err == nil {
  27. *f = int(env * 1000000)
  28. }
  29. }
  30. func strEnvConfig(s *string, name string) {
  31. if env := os.Getenv(name); len(env) > 0 {
  32. *s = env
  33. }
  34. }
  35. func boolEnvConfig(b *bool, name string) {
  36. *b = false
  37. if env, err := strconv.ParseBool(os.Getenv(name)); err == nil {
  38. *b = env
  39. }
  40. }
  41. func hexEnvConfig(b *[]byte, name string) {
  42. var err error
  43. if env := os.Getenv(name); len(env) > 0 {
  44. if *b, err = hex.DecodeString(env); err != nil {
  45. log.Fatalf("%s expected to be hex-encoded string\n", name)
  46. }
  47. }
  48. }
  49. func hexFileConfig(b *[]byte, filepath string) {
  50. if len(filepath) == 0 {
  51. return
  52. }
  53. f, err := os.Open(filepath)
  54. if err != nil {
  55. log.Fatalf("Can't open file %s\n", filepath)
  56. }
  57. src, err := ioutil.ReadAll(f)
  58. if err != nil {
  59. log.Fatalln(err)
  60. }
  61. src = bytes.TrimSpace(src)
  62. dst := make([]byte, hex.DecodedLen(len(src)))
  63. n, err := hex.Decode(dst, src)
  64. if err != nil {
  65. log.Fatalf("%s expected to contain hex-encoded string\n", filepath)
  66. }
  67. *b = dst[:n]
  68. }
  69. func presetEnvConfig(p presets, name string) {
  70. if env := os.Getenv(name); len(env) > 0 {
  71. presetStrings := strings.Split(env, ",")
  72. for _, presetStr := range presetStrings {
  73. parsePreset(p, presetStr)
  74. }
  75. }
  76. }
  77. func presetFileConfig(p presets, filepath string) {
  78. if len(filepath) == 0 {
  79. return
  80. }
  81. f, err := os.Open(filepath)
  82. if err != nil {
  83. log.Fatalf("Can't open file %s\n", filepath)
  84. }
  85. scanner := bufio.NewScanner(f)
  86. for scanner.Scan() {
  87. parsePreset(p, scanner.Text())
  88. }
  89. if err := scanner.Err(); err != nil {
  90. log.Fatalf("Failed to read presets file: %s", err)
  91. }
  92. }
  93. type config struct {
  94. Bind string
  95. ReadTimeout int
  96. WaitTimeout int
  97. WriteTimeout int
  98. DownloadTimeout int
  99. Concurrency int
  100. MaxClients int
  101. TTL int
  102. MaxSrcDimension int
  103. MaxSrcResolution int
  104. JpegProgressive bool
  105. PngInterlaced bool
  106. Quality int
  107. GZipCompression int
  108. EnableWebpDetection bool
  109. EnforceWebp bool
  110. Key []byte
  111. Salt []byte
  112. AllowInsecure bool
  113. Secret string
  114. AllowOrigin string
  115. UserAgent string
  116. IgnoreSslVerification bool
  117. LocalFileSystemRoot string
  118. S3Enabled bool
  119. GCSKey string
  120. ETagEnabled bool
  121. BaseURL string
  122. Presets presets
  123. WatermarkData string
  124. WatermarkPath string
  125. WatermarkURL string
  126. WatermarkOpacity float64
  127. NewRelicAppName string
  128. NewRelicKey string
  129. PrometheusBind string
  130. }
  131. var conf = config{
  132. Bind: ":8080",
  133. ReadTimeout: 10,
  134. WriteTimeout: 10,
  135. DownloadTimeout: 5,
  136. Concurrency: runtime.NumCPU() * 2,
  137. TTL: 3600,
  138. IgnoreSslVerification: false,
  139. MaxSrcDimension: 8192,
  140. MaxSrcResolution: 16800000,
  141. AllowInsecure: false,
  142. Quality: 80,
  143. GZipCompression: 5,
  144. UserAgent: fmt.Sprintf("imgproxy/%s", version),
  145. ETagEnabled: false,
  146. S3Enabled: false,
  147. WatermarkOpacity: 1,
  148. }
  149. func init() {
  150. keyPath := flag.String("keypath", "", "path of the file with hex-encoded key")
  151. saltPath := flag.String("saltpath", "", "path of the file with hex-encoded salt")
  152. presetsPath := flag.String("presets", "", "path of the file with presets")
  153. showVersion := flag.Bool("v", false, "show version")
  154. flag.Parse()
  155. if *showVersion {
  156. fmt.Println(version)
  157. os.Exit(0)
  158. }
  159. if port := os.Getenv("PORT"); len(port) > 0 {
  160. conf.Bind = fmt.Sprintf(":%s", port)
  161. }
  162. strEnvConfig(&conf.Bind, "IMGPROXY_BIND")
  163. intEnvConfig(&conf.ReadTimeout, "IMGPROXY_READ_TIMEOUT")
  164. intEnvConfig(&conf.WriteTimeout, "IMGPROXY_WRITE_TIMEOUT")
  165. intEnvConfig(&conf.DownloadTimeout, "IMGPROXY_DOWNLOAD_TIMEOUT")
  166. intEnvConfig(&conf.Concurrency, "IMGPROXY_CONCURRENCY")
  167. intEnvConfig(&conf.MaxClients, "IMGPROXY_MAX_CLIENTS")
  168. intEnvConfig(&conf.TTL, "IMGPROXY_TTL")
  169. intEnvConfig(&conf.MaxSrcDimension, "IMGPROXY_MAX_SRC_DIMENSION")
  170. megaIntEnvConfig(&conf.MaxSrcResolution, "IMGPROXY_MAX_SRC_RESOLUTION")
  171. boolEnvConfig(&conf.JpegProgressive, "IMGPROXY_JPEG_PROGRESSIVE")
  172. boolEnvConfig(&conf.PngInterlaced, "IMGPROXY_PNG_INTERLACED")
  173. intEnvConfig(&conf.Quality, "IMGPROXY_QUALITY")
  174. intEnvConfig(&conf.GZipCompression, "IMGPROXY_GZIP_COMPRESSION")
  175. boolEnvConfig(&conf.EnableWebpDetection, "IMGPROXY_ENABLE_WEBP_DETECTION")
  176. boolEnvConfig(&conf.EnforceWebp, "IMGPROXY_ENFORCE_WEBP")
  177. hexEnvConfig(&conf.Key, "IMGPROXY_KEY")
  178. hexEnvConfig(&conf.Salt, "IMGPROXY_SALT")
  179. hexFileConfig(&conf.Key, *keyPath)
  180. hexFileConfig(&conf.Salt, *saltPath)
  181. strEnvConfig(&conf.Secret, "IMGPROXY_SECRET")
  182. strEnvConfig(&conf.AllowOrigin, "IMGPROXY_ALLOW_ORIGIN")
  183. strEnvConfig(&conf.UserAgent, "IMGPROXY_USER_AGENT")
  184. boolEnvConfig(&conf.IgnoreSslVerification, "IMGPROXY_IGNORE_SSL_VERIFICATION")
  185. strEnvConfig(&conf.LocalFileSystemRoot, "IMGPROXY_LOCAL_FILESYSTEM_ROOT")
  186. boolEnvConfig(&conf.S3Enabled, "IMGPROXY_USE_S3")
  187. strEnvConfig(&conf.GCSKey, "IMGPROXY_GCS_KEY")
  188. boolEnvConfig(&conf.ETagEnabled, "IMGPROXY_USE_ETAG")
  189. strEnvConfig(&conf.BaseURL, "IMGPROXY_BASE_URL")
  190. conf.Presets = make(presets)
  191. presetEnvConfig(conf.Presets, "IMGPROXY_PRESETS")
  192. presetFileConfig(conf.Presets, *presetsPath)
  193. strEnvConfig(&conf.WatermarkData, "IMGPROXY_WATERMARK_DATA")
  194. strEnvConfig(&conf.WatermarkPath, "IMGPROXY_WATERMARK_PATH")
  195. strEnvConfig(&conf.WatermarkURL, "IMGPROXY_WATERMARK_URL")
  196. floatEnvConfig(&conf.WatermarkOpacity, "IMGPROXY_WATERMARK_OPACITY")
  197. strEnvConfig(&conf.NewRelicAppName, "IMGPROXY_NEW_RELIC_APP_NAME")
  198. strEnvConfig(&conf.NewRelicKey, "IMGPROXY_NEW_RELIC_KEY")
  199. strEnvConfig(&conf.PrometheusBind, "IMGPROXY_PROMETHEUS_BIND")
  200. if len(conf.Key) == 0 {
  201. warning("Key is not defined, so signature checking is disabled")
  202. conf.AllowInsecure = true
  203. }
  204. if len(conf.Salt) == 0 {
  205. warning("Salt is not defined, so signature checking is disabled")
  206. conf.AllowInsecure = true
  207. }
  208. if len(conf.Bind) == 0 {
  209. log.Fatalln("Bind address is not defined")
  210. }
  211. if conf.ReadTimeout <= 0 {
  212. log.Fatalf("Read timeout should be greater than 0, now - %d\n", conf.ReadTimeout)
  213. }
  214. if conf.WriteTimeout <= 0 {
  215. log.Fatalf("Write timeout should be greater than 0, now - %d\n", conf.WriteTimeout)
  216. }
  217. if conf.DownloadTimeout <= 0 {
  218. log.Fatalf("Download timeout should be greater than 0, now - %d\n", conf.DownloadTimeout)
  219. }
  220. if conf.Concurrency <= 0 {
  221. log.Fatalf("Concurrency should be greater than 0, now - %d\n", conf.Concurrency)
  222. }
  223. if conf.MaxClients <= 0 {
  224. conf.MaxClients = conf.Concurrency * 10
  225. }
  226. if conf.TTL <= 0 {
  227. log.Fatalf("TTL should be greater than 0, now - %d\n", conf.TTL)
  228. }
  229. if conf.MaxSrcDimension <= 0 {
  230. log.Fatalf("Max src dimension should be greater than 0, now - %d\n", conf.MaxSrcDimension)
  231. }
  232. if conf.MaxSrcResolution <= 0 {
  233. log.Fatalf("Max src resolution should be greater than 0, now - %d\n", conf.MaxSrcResolution)
  234. }
  235. if conf.Quality <= 0 {
  236. log.Fatalf("Quality should be greater than 0, now - %d\n", conf.Quality)
  237. } else if conf.Quality > 100 {
  238. log.Fatalf("Quality can't be greater than 100, now - %d\n", conf.Quality)
  239. }
  240. if conf.GZipCompression < 0 {
  241. log.Fatalf("GZip compression should be greater than or quual to 0, now - %d\n", conf.GZipCompression)
  242. } else if conf.GZipCompression > 9 {
  243. log.Fatalf("GZip compression can't be greater than 9, now - %d\n", conf.GZipCompression)
  244. }
  245. if conf.IgnoreSslVerification {
  246. warning("Ignoring SSL verification is very unsafe")
  247. }
  248. if conf.LocalFileSystemRoot != "" {
  249. stat, err := os.Stat(conf.LocalFileSystemRoot)
  250. if err != nil {
  251. log.Fatalf("Cannot use local directory: %s", err)
  252. } else {
  253. if !stat.IsDir() {
  254. log.Fatalf("Cannot use local directory: not a directory")
  255. }
  256. }
  257. if conf.LocalFileSystemRoot == "/" {
  258. log.Print("Exposing root via IMGPROXY_LOCAL_FILESYSTEM_ROOT is unsafe")
  259. }
  260. }
  261. checkPresets(conf.Presets)
  262. if conf.WatermarkOpacity <= 0 {
  263. log.Fatalln("Watermark opacity should be greater than 0")
  264. } else if conf.WatermarkOpacity > 1 {
  265. log.Fatalln("Watermark opacity should be less than or equal to 1")
  266. }
  267. if len(conf.PrometheusBind) > 0 && conf.PrometheusBind == conf.Bind {
  268. log.Fatalln("Can't use the same binding for the main server and Prometheus")
  269. }
  270. initDownloading()
  271. initNewrelic()
  272. initPrometheus()
  273. initVips()
  274. }