config.go 4.9 KB

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