config.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package main
  2. import (
  3. "bytes"
  4. "crypto/rand"
  5. "encoding/hex"
  6. "flag"
  7. "fmt"
  8. "io/ioutil"
  9. "log"
  10. "os"
  11. "runtime"
  12. "strconv"
  13. )
  14. func intEnvConfig(i *int, name string) {
  15. if env, err := strconv.Atoi(os.Getenv(name)); err == nil {
  16. *i = env
  17. }
  18. }
  19. func megaIntEnvConfig(f *int, name string) {
  20. if env, err := strconv.ParseFloat(os.Getenv(name), 64); err == nil {
  21. *f = int(env * 1000000)
  22. }
  23. }
  24. func strEnvConfig(s *string, name string) {
  25. if env := os.Getenv(name); len(env) > 0 {
  26. *s = env
  27. }
  28. }
  29. func boolEnvConfig(b *bool, name string) {
  30. *b = false
  31. if env, err := strconv.ParseBool(os.Getenv(name)); err == nil {
  32. *b = env
  33. }
  34. }
  35. func hexEnvConfig(b *[]byte, name string) {
  36. var err error
  37. if env := os.Getenv(name); len(env) > 0 {
  38. if *b, err = hex.DecodeString(env); err != nil {
  39. log.Fatalf("%s expected to be hex-encoded string\n", name)
  40. }
  41. }
  42. }
  43. func hexFileConfig(b *[]byte, filepath string) {
  44. if len(filepath) == 0 {
  45. return
  46. }
  47. f, err := os.Open(filepath)
  48. if err != nil {
  49. log.Fatalf("Can't open file %s\n", filepath)
  50. }
  51. src, err := ioutil.ReadAll(f)
  52. if err != nil {
  53. log.Fatalln(err)
  54. }
  55. src = bytes.TrimSpace(src)
  56. dst := make([]byte, hex.DecodedLen(len(src)))
  57. n, err := hex.Decode(dst, src)
  58. if err != nil {
  59. log.Fatalf("%s expected to contain hex-encoded string\n", filepath)
  60. }
  61. *b = dst[:n]
  62. }
  63. type config struct {
  64. Bind string
  65. ReadTimeout int
  66. WriteTimeout int
  67. DownloadTimeout int
  68. Concurrency int
  69. MaxClients int
  70. TTL int
  71. MaxSrcDimension int
  72. MaxSrcResolution int
  73. Quality int
  74. GZipCompression int
  75. Key []byte
  76. Salt []byte
  77. Secret string
  78. LocalFileSystemRoot string
  79. ETagEnabled bool
  80. ETagSignature []byte
  81. }
  82. var conf = config{
  83. Bind: ":8080",
  84. ReadTimeout: 10,
  85. WriteTimeout: 10,
  86. DownloadTimeout: 5,
  87. Concurrency: runtime.NumCPU() * 2,
  88. TTL: 3600,
  89. MaxSrcDimension: 8192,
  90. MaxSrcResolution: 16800000,
  91. Quality: 80,
  92. GZipCompression: 5,
  93. ETagEnabled: false,
  94. }
  95. func init() {
  96. keypath := flag.String("keypath", "", "path of the file with hex-encoded key")
  97. saltpath := flag.String("saltpath", "", "path of the file with hex-encoded salt")
  98. flag.Parse()
  99. if port := os.Getenv("PORT"); len(port) > 0 {
  100. conf.Bind = fmt.Sprintf(":%s", port)
  101. }
  102. strEnvConfig(&conf.Bind, "IMGPROXY_BIND")
  103. intEnvConfig(&conf.ReadTimeout, "IMGPROXY_READ_TIMEOUT")
  104. intEnvConfig(&conf.WriteTimeout, "IMGPROXY_WRITE_TIMEOUT")
  105. intEnvConfig(&conf.DownloadTimeout, "IMGPROXY_DOWNLOAD_TIMEOUT")
  106. intEnvConfig(&conf.Concurrency, "IMGPROXY_CONCURRENCY")
  107. intEnvConfig(&conf.MaxClients, "IMGPROXY_MAX_CLIENTS")
  108. intEnvConfig(&conf.TTL, "IMGPROXY_TTL")
  109. intEnvConfig(&conf.MaxSrcDimension, "IMGPROXY_MAX_SRC_DIMENSION")
  110. megaIntEnvConfig(&conf.MaxSrcResolution, "IMGPROXY_MAX_SRC_RESOLUTION")
  111. intEnvConfig(&conf.Quality, "IMGPROXY_QUALITY")
  112. intEnvConfig(&conf.GZipCompression, "IMGPROXY_GZIP_COMPRESSION")
  113. hexEnvConfig(&conf.Key, "IMGPROXY_KEY")
  114. hexEnvConfig(&conf.Salt, "IMGPROXY_SALT")
  115. hexFileConfig(&conf.Key, *keypath)
  116. hexFileConfig(&conf.Salt, *saltpath)
  117. strEnvConfig(&conf.Secret, "IMGPROXY_SECRET")
  118. strEnvConfig(&conf.LocalFileSystemRoot, "IMGPROXY_LOCAL_FILESYSTEM_ROOT")
  119. boolEnvConfig(&conf.ETagEnabled, "IMGPROXY_USE_ETAG")
  120. if len(conf.Key) == 0 {
  121. log.Fatalln("Key is not defined")
  122. }
  123. if len(conf.Salt) == 0 {
  124. log.Fatalln("Salt is not defined")
  125. }
  126. if len(conf.Bind) == 0 {
  127. log.Fatalln("Bind address is not defined")
  128. }
  129. if conf.ReadTimeout <= 0 {
  130. log.Fatalf("Read timeout should be greater than 0, now - %d\n", conf.ReadTimeout)
  131. }
  132. if conf.WriteTimeout <= 0 {
  133. log.Fatalf("Write timeout should be greater than 0, now - %d\n", conf.WriteTimeout)
  134. }
  135. if conf.DownloadTimeout <= 0 {
  136. log.Fatalf("Download timeout should be greater than 0, now - %d\n", conf.DownloadTimeout)
  137. }
  138. if conf.Concurrency <= 0 {
  139. log.Fatalf("Concurrency should be greater than 0, now - %d\n", conf.Concurrency)
  140. }
  141. if conf.MaxClients <= 0 {
  142. conf.MaxClients = conf.Concurrency * 5
  143. }
  144. if conf.TTL <= 0 {
  145. log.Fatalf("TTL should be greater than 0, now - %d\n", conf.TTL)
  146. }
  147. if conf.MaxSrcDimension <= 0 {
  148. log.Fatalf("Max src dimension should be greater than 0, now - %d\n", conf.MaxSrcDimension)
  149. }
  150. if conf.MaxSrcResolution <= 0 {
  151. log.Fatalf("Max src resolution should be greater than 0, now - %d\n", conf.MaxSrcResolution)
  152. }
  153. if conf.Quality <= 0 {
  154. log.Fatalf("Quality should be greater than 0, now - %d\n", conf.Quality)
  155. } else if conf.Quality > 100 {
  156. log.Fatalf("Quality can't be greater than 100, now - %d\n", conf.Quality)
  157. }
  158. if conf.GZipCompression < 0 {
  159. log.Fatalf("GZip compression should be greater than or quual to 0, now - %d\n", conf.GZipCompression)
  160. } else if conf.GZipCompression > 9 {
  161. log.Fatalf("GZip compression can't be greater than 9, now - %d\n", conf.GZipCompression)
  162. }
  163. if conf.LocalFileSystemRoot != "" {
  164. stat, err := os.Stat(conf.LocalFileSystemRoot)
  165. if err != nil {
  166. log.Fatalf("Cannot use local directory: %s", err)
  167. } else {
  168. if !stat.IsDir() {
  169. log.Fatalf("Cannot use local directory: not a directory")
  170. }
  171. }
  172. if conf.LocalFileSystemRoot == "/" {
  173. log.Print("Exposing root via IMGPROXY_LOCAL_FILESYSTEM_ROOT is unsafe")
  174. }
  175. }
  176. if conf.ETagEnabled {
  177. conf.ETagSignature = make([]byte, 16)
  178. rand.Read(conf.ETagSignature)
  179. log.Printf("ETag support is activated. The random value was generated to be used for ETag calculation: %s\n",
  180. fmt.Sprintf("%x", conf.ETagSignature))
  181. }
  182. initVips()
  183. initDownloading()
  184. }