1
0

config.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. if err := parsePreset(p, presetStr); err != nil {
  74. log.Fatalln(err)
  75. }
  76. }
  77. }
  78. }
  79. func presetFileConfig(p presets, filepath string) {
  80. if len(filepath) == 0 {
  81. return
  82. }
  83. f, err := os.Open(filepath)
  84. if err != nil {
  85. log.Fatalf("Can't open file %s\n", filepath)
  86. }
  87. scanner := bufio.NewScanner(f)
  88. for scanner.Scan() {
  89. if err := parsePreset(p, scanner.Text()); err != nil {
  90. log.Fatalln(err)
  91. }
  92. }
  93. if err := scanner.Err(); err != nil {
  94. log.Fatalf("Failed to read presets file: %s", err)
  95. }
  96. }
  97. type config struct {
  98. Bind string
  99. ReadTimeout int
  100. WaitTimeout int
  101. WriteTimeout int
  102. DownloadTimeout int
  103. Concurrency int
  104. MaxClients int
  105. TTL int
  106. MaxSrcDimension int
  107. MaxSrcResolution int
  108. JpegProgressive bool
  109. PngInterlaced bool
  110. Quality int
  111. GZipCompression int
  112. EnableWebpDetection bool
  113. EnforceWebp bool
  114. EnableClientHints bool
  115. Key []byte
  116. Salt []byte
  117. AllowInsecure bool
  118. SignatureSize int
  119. Secret string
  120. AllowOrigin string
  121. UserAgent string
  122. IgnoreSslVerification bool
  123. LocalFileSystemRoot string
  124. S3Enabled bool
  125. GCSKey string
  126. ETagEnabled bool
  127. BaseURL string
  128. Presets presets
  129. WatermarkData string
  130. WatermarkPath string
  131. WatermarkURL string
  132. WatermarkOpacity float64
  133. NewRelicAppName string
  134. NewRelicKey string
  135. PrometheusBind string
  136. }
  137. var conf = config{
  138. Bind: ":8080",
  139. ReadTimeout: 10,
  140. WriteTimeout: 10,
  141. DownloadTimeout: 5,
  142. Concurrency: runtime.NumCPU() * 2,
  143. TTL: 3600,
  144. IgnoreSslVerification: false,
  145. MaxSrcDimension: 8192,
  146. MaxSrcResolution: 16800000,
  147. AllowInsecure: false,
  148. SignatureSize: 32,
  149. Quality: 80,
  150. GZipCompression: 5,
  151. UserAgent: fmt.Sprintf("imgproxy/%s", version),
  152. ETagEnabled: false,
  153. S3Enabled: false,
  154. WatermarkOpacity: 1,
  155. }
  156. func init() {
  157. keyPath := flag.String("keypath", "", "path of the file with hex-encoded key")
  158. saltPath := flag.String("saltpath", "", "path of the file with hex-encoded salt")
  159. presetsPath := flag.String("presets", "", "path of the file with presets")
  160. showVersion := flag.Bool("v", false, "show version")
  161. flag.Parse()
  162. if *showVersion {
  163. fmt.Println(version)
  164. os.Exit(0)
  165. }
  166. if port := os.Getenv("PORT"); len(port) > 0 {
  167. conf.Bind = fmt.Sprintf(":%s", port)
  168. }
  169. strEnvConfig(&conf.Bind, "IMGPROXY_BIND")
  170. intEnvConfig(&conf.ReadTimeout, "IMGPROXY_READ_TIMEOUT")
  171. intEnvConfig(&conf.WriteTimeout, "IMGPROXY_WRITE_TIMEOUT")
  172. intEnvConfig(&conf.DownloadTimeout, "IMGPROXY_DOWNLOAD_TIMEOUT")
  173. intEnvConfig(&conf.Concurrency, "IMGPROXY_CONCURRENCY")
  174. intEnvConfig(&conf.MaxClients, "IMGPROXY_MAX_CLIENTS")
  175. intEnvConfig(&conf.TTL, "IMGPROXY_TTL")
  176. intEnvConfig(&conf.MaxSrcDimension, "IMGPROXY_MAX_SRC_DIMENSION")
  177. megaIntEnvConfig(&conf.MaxSrcResolution, "IMGPROXY_MAX_SRC_RESOLUTION")
  178. boolEnvConfig(&conf.JpegProgressive, "IMGPROXY_JPEG_PROGRESSIVE")
  179. boolEnvConfig(&conf.PngInterlaced, "IMGPROXY_PNG_INTERLACED")
  180. intEnvConfig(&conf.Quality, "IMGPROXY_QUALITY")
  181. intEnvConfig(&conf.GZipCompression, "IMGPROXY_GZIP_COMPRESSION")
  182. boolEnvConfig(&conf.EnableWebpDetection, "IMGPROXY_ENABLE_WEBP_DETECTION")
  183. boolEnvConfig(&conf.EnforceWebp, "IMGPROXY_ENFORCE_WEBP")
  184. boolEnvConfig(&conf.EnableClientHints, "IMGPROXY_ENABLE_CLIENT_HINTS")
  185. hexEnvConfig(&conf.Key, "IMGPROXY_KEY")
  186. hexEnvConfig(&conf.Salt, "IMGPROXY_SALT")
  187. intEnvConfig(&conf.SignatureSize, "IMGPROXY_SIGNATURE_SIZE")
  188. hexFileConfig(&conf.Key, *keyPath)
  189. hexFileConfig(&conf.Salt, *saltPath)
  190. strEnvConfig(&conf.Secret, "IMGPROXY_SECRET")
  191. strEnvConfig(&conf.AllowOrigin, "IMGPROXY_ALLOW_ORIGIN")
  192. strEnvConfig(&conf.UserAgent, "IMGPROXY_USER_AGENT")
  193. boolEnvConfig(&conf.IgnoreSslVerification, "IMGPROXY_IGNORE_SSL_VERIFICATION")
  194. strEnvConfig(&conf.LocalFileSystemRoot, "IMGPROXY_LOCAL_FILESYSTEM_ROOT")
  195. boolEnvConfig(&conf.S3Enabled, "IMGPROXY_USE_S3")
  196. strEnvConfig(&conf.GCSKey, "IMGPROXY_GCS_KEY")
  197. boolEnvConfig(&conf.ETagEnabled, "IMGPROXY_USE_ETAG")
  198. strEnvConfig(&conf.BaseURL, "IMGPROXY_BASE_URL")
  199. conf.Presets = make(presets)
  200. presetEnvConfig(conf.Presets, "IMGPROXY_PRESETS")
  201. presetFileConfig(conf.Presets, *presetsPath)
  202. strEnvConfig(&conf.WatermarkData, "IMGPROXY_WATERMARK_DATA")
  203. strEnvConfig(&conf.WatermarkPath, "IMGPROXY_WATERMARK_PATH")
  204. strEnvConfig(&conf.WatermarkURL, "IMGPROXY_WATERMARK_URL")
  205. floatEnvConfig(&conf.WatermarkOpacity, "IMGPROXY_WATERMARK_OPACITY")
  206. strEnvConfig(&conf.NewRelicAppName, "IMGPROXY_NEW_RELIC_APP_NAME")
  207. strEnvConfig(&conf.NewRelicKey, "IMGPROXY_NEW_RELIC_KEY")
  208. strEnvConfig(&conf.PrometheusBind, "IMGPROXY_PROMETHEUS_BIND")
  209. if len(conf.Key) == 0 {
  210. warning("Key is not defined, so signature checking is disabled")
  211. conf.AllowInsecure = true
  212. }
  213. if len(conf.Salt) == 0 {
  214. warning("Salt is not defined, so signature checking is disabled")
  215. conf.AllowInsecure = true
  216. }
  217. if conf.SignatureSize < 1 || conf.SignatureSize > 32 {
  218. log.Fatalf("Signature size should be within 1 and 32, now - %d\n", conf.SignatureSize)
  219. }
  220. if len(conf.Bind) == 0 {
  221. log.Fatalln("Bind address is not defined")
  222. }
  223. if conf.ReadTimeout <= 0 {
  224. log.Fatalf("Read timeout should be greater than 0, now - %d\n", conf.ReadTimeout)
  225. }
  226. if conf.WriteTimeout <= 0 {
  227. log.Fatalf("Write timeout should be greater than 0, now - %d\n", conf.WriteTimeout)
  228. }
  229. if conf.DownloadTimeout <= 0 {
  230. log.Fatalf("Download timeout should be greater than 0, now - %d\n", conf.DownloadTimeout)
  231. }
  232. if conf.Concurrency <= 0 {
  233. log.Fatalf("Concurrency should be greater than 0, now - %d\n", conf.Concurrency)
  234. }
  235. if conf.MaxClients <= 0 {
  236. conf.MaxClients = conf.Concurrency * 10
  237. }
  238. if conf.TTL <= 0 {
  239. log.Fatalf("TTL should be greater than 0, now - %d\n", conf.TTL)
  240. }
  241. if conf.MaxSrcDimension <= 0 {
  242. log.Fatalf("Max src dimension should be greater than 0, now - %d\n", conf.MaxSrcDimension)
  243. }
  244. if conf.MaxSrcResolution <= 0 {
  245. log.Fatalf("Max src resolution should be greater than 0, now - %d\n", conf.MaxSrcResolution)
  246. }
  247. if conf.Quality <= 0 {
  248. log.Fatalf("Quality should be greater than 0, now - %d\n", conf.Quality)
  249. } else if conf.Quality > 100 {
  250. log.Fatalf("Quality can't be greater than 100, now - %d\n", conf.Quality)
  251. }
  252. if conf.GZipCompression < 0 {
  253. log.Fatalf("GZip compression should be greater than or quual to 0, now - %d\n", conf.GZipCompression)
  254. } else if conf.GZipCompression > 9 {
  255. log.Fatalf("GZip compression can't be greater than 9, now - %d\n", conf.GZipCompression)
  256. }
  257. if conf.IgnoreSslVerification {
  258. warning("Ignoring SSL verification is very unsafe")
  259. }
  260. if conf.LocalFileSystemRoot != "" {
  261. stat, err := os.Stat(conf.LocalFileSystemRoot)
  262. if err != nil {
  263. log.Fatalf("Cannot use local directory: %s", err)
  264. } else {
  265. if !stat.IsDir() {
  266. log.Fatalf("Cannot use local directory: not a directory")
  267. }
  268. }
  269. if conf.LocalFileSystemRoot == "/" {
  270. log.Print("Exposing root via IMGPROXY_LOCAL_FILESYSTEM_ROOT is unsafe")
  271. }
  272. }
  273. if err := checkPresets(conf.Presets); err != nil {
  274. log.Fatalln(err)
  275. }
  276. if conf.WatermarkOpacity <= 0 {
  277. log.Fatalln("Watermark opacity should be greater than 0")
  278. } else if conf.WatermarkOpacity > 1 {
  279. log.Fatalln("Watermark opacity should be less than or equal to 1")
  280. }
  281. if len(conf.PrometheusBind) > 0 && conf.PrometheusBind == conf.Bind {
  282. log.Fatalln("Can't use the same binding for the main server and Prometheus")
  283. }
  284. initDownloading()
  285. initNewrelic()
  286. initPrometheus()
  287. initVips()
  288. }