config.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. WaitTimeout int
  67. WriteTimeout int
  68. DownloadTimeout int
  69. Concurrency int
  70. MaxClients int
  71. TTL int
  72. MaxSrcDimension int
  73. MaxSrcResolution int
  74. JpegProgressive bool
  75. PngInterlaced bool
  76. Quality int
  77. GZipCompression int
  78. Key []byte
  79. Salt []byte
  80. Secret string
  81. AllowOrigin string
  82. LocalFileSystemRoot string
  83. ETagEnabled bool
  84. ETagSignature []byte
  85. BaseURL string
  86. }
  87. var conf = config{
  88. Bind: ":8080",
  89. ReadTimeout: 10,
  90. WriteTimeout: 10,
  91. DownloadTimeout: 5,
  92. Concurrency: runtime.NumCPU() * 2,
  93. TTL: 3600,
  94. MaxSrcDimension: 8192,
  95. MaxSrcResolution: 16800000,
  96. Quality: 80,
  97. GZipCompression: 5,
  98. ETagEnabled: false,
  99. }
  100. func init() {
  101. keypath := flag.String("keypath", "", "path of the file with hex-encoded key")
  102. saltpath := flag.String("saltpath", "", "path of the file with hex-encoded salt")
  103. flag.Parse()
  104. if port := os.Getenv("PORT"); len(port) > 0 {
  105. conf.Bind = fmt.Sprintf(":%s", port)
  106. }
  107. strEnvConfig(&conf.Bind, "IMGPROXY_BIND")
  108. intEnvConfig(&conf.ReadTimeout, "IMGPROXY_READ_TIMEOUT")
  109. intEnvConfig(&conf.WriteTimeout, "IMGPROXY_WRITE_TIMEOUT")
  110. intEnvConfig(&conf.DownloadTimeout, "IMGPROXY_DOWNLOAD_TIMEOUT")
  111. intEnvConfig(&conf.Concurrency, "IMGPROXY_CONCURRENCY")
  112. intEnvConfig(&conf.MaxClients, "IMGPROXY_MAX_CLIENTS")
  113. intEnvConfig(&conf.TTL, "IMGPROXY_TTL")
  114. intEnvConfig(&conf.MaxSrcDimension, "IMGPROXY_MAX_SRC_DIMENSION")
  115. megaIntEnvConfig(&conf.MaxSrcResolution, "IMGPROXY_MAX_SRC_RESOLUTION")
  116. boolEnvConfig(&conf.JpegProgressive, "IMGPROXY_JPEG_PROGRESSIVE")
  117. boolEnvConfig(&conf.PngInterlaced, "IMGPROXY_PNG_INTERLACED")
  118. intEnvConfig(&conf.Quality, "IMGPROXY_QUALITY")
  119. intEnvConfig(&conf.GZipCompression, "IMGPROXY_GZIP_COMPRESSION")
  120. hexEnvConfig(&conf.Key, "IMGPROXY_KEY")
  121. hexEnvConfig(&conf.Salt, "IMGPROXY_SALT")
  122. hexFileConfig(&conf.Key, *keypath)
  123. hexFileConfig(&conf.Salt, *saltpath)
  124. strEnvConfig(&conf.Secret, "IMGPROXY_SECRET")
  125. strEnvConfig(&conf.AllowOrigin, "IMGPROXY_ALLOW_ORIGIN")
  126. strEnvConfig(&conf.LocalFileSystemRoot, "IMGPROXY_LOCAL_FILESYSTEM_ROOT")
  127. boolEnvConfig(&conf.ETagEnabled, "IMGPROXY_USE_ETAG")
  128. strEnvConfig(&conf.BaseURL, "IMGPROXY_BASE_URL")
  129. if len(conf.Key) == 0 {
  130. log.Fatalln("Key is not defined")
  131. }
  132. if len(conf.Salt) == 0 {
  133. log.Fatalln("Salt is not defined")
  134. }
  135. if len(conf.Bind) == 0 {
  136. log.Fatalln("Bind address is not defined")
  137. }
  138. if conf.ReadTimeout <= 0 {
  139. log.Fatalf("Read timeout should be greater than 0, now - %d\n", conf.ReadTimeout)
  140. }
  141. if conf.WriteTimeout <= 0 {
  142. log.Fatalf("Write timeout should be greater than 0, now - %d\n", conf.WriteTimeout)
  143. }
  144. if conf.DownloadTimeout <= 0 {
  145. log.Fatalf("Download timeout should be greater than 0, now - %d\n", conf.DownloadTimeout)
  146. }
  147. if conf.Concurrency <= 0 {
  148. log.Fatalf("Concurrency should be greater than 0, now - %d\n", conf.Concurrency)
  149. }
  150. if conf.MaxClients <= 0 {
  151. conf.MaxClients = conf.Concurrency * 10
  152. }
  153. if conf.TTL <= 0 {
  154. log.Fatalf("TTL should be greater than 0, now - %d\n", conf.TTL)
  155. }
  156. if conf.MaxSrcDimension <= 0 {
  157. log.Fatalf("Max src dimension should be greater than 0, now - %d\n", conf.MaxSrcDimension)
  158. }
  159. if conf.MaxSrcResolution <= 0 {
  160. log.Fatalf("Max src resolution should be greater than 0, now - %d\n", conf.MaxSrcResolution)
  161. }
  162. if conf.Quality <= 0 {
  163. log.Fatalf("Quality should be greater than 0, now - %d\n", conf.Quality)
  164. } else if conf.Quality > 100 {
  165. log.Fatalf("Quality can't be greater than 100, now - %d\n", conf.Quality)
  166. }
  167. if conf.GZipCompression < 0 {
  168. log.Fatalf("GZip compression should be greater than or quual to 0, now - %d\n", conf.GZipCompression)
  169. } else if conf.GZipCompression > 9 {
  170. log.Fatalf("GZip compression can't be greater than 9, now - %d\n", conf.GZipCompression)
  171. }
  172. if conf.LocalFileSystemRoot != "" {
  173. stat, err := os.Stat(conf.LocalFileSystemRoot)
  174. if err != nil {
  175. log.Fatalf("Cannot use local directory: %s", err)
  176. } else {
  177. if !stat.IsDir() {
  178. log.Fatalf("Cannot use local directory: not a directory")
  179. }
  180. }
  181. if conf.LocalFileSystemRoot == "/" {
  182. log.Print("Exposing root via IMGPROXY_LOCAL_FILESYSTEM_ROOT is unsafe")
  183. }
  184. }
  185. if conf.ETagEnabled {
  186. conf.ETagSignature = make([]byte, 16)
  187. rand.Read(conf.ETagSignature)
  188. log.Printf("ETag support is activated. The random value was generated to be used for ETag calculation: %s\n",
  189. fmt.Sprintf("%x", conf.ETagSignature))
  190. }
  191. initVips()
  192. initDownloading()
  193. }