|
@@ -1,93 +1,125 @@
|
|
|
package main
|
|
|
|
|
|
import (
|
|
|
+ "bytes"
|
|
|
"encoding/hex"
|
|
|
"flag"
|
|
|
"io/ioutil"
|
|
|
"log"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
|
-
|
|
|
- "gopkg.in/yaml.v2"
|
|
|
+ "strconv"
|
|
|
)
|
|
|
|
|
|
-type config struct {
|
|
|
- Bind string
|
|
|
- ReadTimeout int `yaml:"read_timeout"`
|
|
|
- WriteTimeout int `yaml:"write_timeout"`
|
|
|
-
|
|
|
- Key string
|
|
|
- Salt string
|
|
|
- KeyBin []byte
|
|
|
- SaltBin []byte
|
|
|
+func absPathToFile(path string) string {
|
|
|
+ if filepath.IsAbs(path) {
|
|
|
+ return path
|
|
|
+ }
|
|
|
|
|
|
- MaxSrcDimension int `yaml:"max_src_dimension"`
|
|
|
+ appPath, err := filepath.Abs(filepath.Dir(os.Args[0]))
|
|
|
+ if err != nil {
|
|
|
+ log.Fatalln(err)
|
|
|
+ }
|
|
|
|
|
|
- Quality int
|
|
|
- Compression int
|
|
|
+ return filepath.Join(appPath, path)
|
|
|
}
|
|
|
|
|
|
-var conf = config{
|
|
|
- Bind: ":8080",
|
|
|
- MaxSrcDimension: 4096,
|
|
|
+func intEnvConfig(i *int, name string) {
|
|
|
+ if env, err := strconv.Atoi(os.Getenv(name)); err == nil {
|
|
|
+ *i = env
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-func absPathToFile(path string) string {
|
|
|
- if filepath.IsAbs(path) {
|
|
|
- return path
|
|
|
+func strEnvConfig(s *string, name string) {
|
|
|
+ if env := os.Getenv(name); len(env) > 0 {
|
|
|
+ *s = env
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
- appPath, _ := filepath.Abs(filepath.Dir(os.Args[0]))
|
|
|
- return filepath.Join(appPath, path)
|
|
|
+func hexEnvConfig(b *[]byte, name string) {
|
|
|
+ var err error
|
|
|
+
|
|
|
+ if env := os.Getenv(name); len(env) > 0 {
|
|
|
+ if *b, err = hex.DecodeString(env); err != nil {
|
|
|
+ log.Fatalf("%s expected to be hex-encoded string\n", name)
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-func init() {
|
|
|
- cpath := flag.String(
|
|
|
- "config", "./config.yml", "path to configuration file",
|
|
|
- )
|
|
|
- flag.Parse()
|
|
|
+func hexFileConfig(b *[]byte, filepath string) {
|
|
|
+ if len(filepath) == 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
|
|
|
- file, err := os.Open(absPathToFile(*cpath))
|
|
|
+ fullfp := absPathToFile(filepath)
|
|
|
+ f, err := os.Open(fullfp)
|
|
|
if err != nil {
|
|
|
- log.Fatalln(err)
|
|
|
+ log.Fatalf("Can't open file %s\n", fullfp)
|
|
|
}
|
|
|
- defer file.Close()
|
|
|
|
|
|
- cdata, err := ioutil.ReadAll(file)
|
|
|
+ src, err := ioutil.ReadAll(f)
|
|
|
if err != nil {
|
|
|
log.Fatalln(err)
|
|
|
}
|
|
|
|
|
|
- err = yaml.Unmarshal(cdata, &conf)
|
|
|
+ src = bytes.TrimSpace(src)
|
|
|
+
|
|
|
+ dst := make([]byte, hex.DecodedLen(len(src)))
|
|
|
+ n, err := hex.Decode(dst, src)
|
|
|
if err != nil {
|
|
|
- log.Fatalln(err)
|
|
|
+ log.Fatalf("%s expected to contain hex-encoded string\n", fullfp)
|
|
|
}
|
|
|
|
|
|
- if len(conf.Bind) == 0 {
|
|
|
- conf.Bind = ":8080"
|
|
|
- }
|
|
|
+ *b = dst[:n]
|
|
|
+}
|
|
|
|
|
|
- if conf.MaxSrcDimension == 0 {
|
|
|
- conf.MaxSrcDimension = 4096
|
|
|
- }
|
|
|
+type config struct {
|
|
|
+ Bind string
|
|
|
+ ReadTimeout int
|
|
|
+ WriteTimeout int
|
|
|
|
|
|
- if conf.KeyBin, err = hex.DecodeString(conf.Key); err != nil {
|
|
|
- log.Fatalln("Invalid key. Key should be encoded to hex")
|
|
|
- }
|
|
|
+ MaxSrcDimension int
|
|
|
|
|
|
- if conf.SaltBin, err = hex.DecodeString(conf.Salt); err != nil {
|
|
|
- log.Fatalln("Invalid salt. Salt should be encoded to hex")
|
|
|
- }
|
|
|
+ Quality int
|
|
|
+ Compression int
|
|
|
|
|
|
- if conf.MaxSrcDimension == 0 {
|
|
|
- conf.MaxSrcDimension = 4096
|
|
|
- }
|
|
|
+ Key []byte
|
|
|
+ Salt []byte
|
|
|
+}
|
|
|
|
|
|
- if conf.Quality == 0 {
|
|
|
- conf.Quality = 80
|
|
|
- }
|
|
|
+var conf = config{
|
|
|
+ Bind: ":8080",
|
|
|
+ ReadTimeout: 10,
|
|
|
+ WriteTimeout: 10,
|
|
|
+ MaxSrcDimension: 4096,
|
|
|
+ Quality: 80,
|
|
|
+ Compression: 6,
|
|
|
+}
|
|
|
+
|
|
|
+func init() {
|
|
|
+ keypath := flag.String("keypath", "", "path of the file with hex-encoded key")
|
|
|
+ saltpath := flag.String("saltpath", "", "path of the file with hex-encoded salt")
|
|
|
+ flag.Parse()
|
|
|
+
|
|
|
+ strEnvConfig(&conf.Bind, "IMGPROXY_BIND")
|
|
|
+ intEnvConfig(&conf.ReadTimeout, "IMGPROXY_READ_TIMEOUT")
|
|
|
+ intEnvConfig(&conf.WriteTimeout, "IMGPROXY_WRITE_TIMEOUT")
|
|
|
+
|
|
|
+ intEnvConfig(&conf.MaxSrcDimension, "IMGPROXY_MAX_SRC_DIMENSION")
|
|
|
+
|
|
|
+ intEnvConfig(&conf.Quality, "IMGPROXY_QUALITY")
|
|
|
+ intEnvConfig(&conf.Compression, "IMGPROXY_COMPRESSION")
|
|
|
|
|
|
- if conf.Compression == 0 {
|
|
|
- conf.Compression = 6
|
|
|
+ hexEnvConfig(&conf.Key, "IMGPROXY_KEY")
|
|
|
+ hexEnvConfig(&conf.Salt, "IMGPROXY_SALT")
|
|
|
+
|
|
|
+ hexFileConfig(&conf.Key, *keypath)
|
|
|
+ hexFileConfig(&conf.Salt, *saltpath)
|
|
|
+
|
|
|
+ if len(conf.Key) == 0 {
|
|
|
+ log.Fatalln("Key is not defined")
|
|
|
+ }
|
|
|
+ if len(conf.Salt) == 0 {
|
|
|
+ log.Fatalln("Salt is not defined")
|
|
|
}
|
|
|
}
|