-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
109 lines (91 loc) · 2.58 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"bytes"
"crypto/aes"
_ "embed"
"os"
"path/filepath"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
type Config struct {
CoreKey string `yaml:"coreKey"`
MetaKey string `yaml:"metaKey"`
InputDir string `yaml:"inputDir"`
OutputDir string `yaml:"outputDir"`
CoverOutput bool `yaml:"coverOutput"`
CoverEmbed bool `yaml:"coverEmbed"`
HighDefinitionCover bool `yaml:"highDefinitionCover"`
MultiThread bool `yaml:"multiThread"`
}
//go:embed config_default.yml
var defaultConfig []byte
func (c *Config) init() *Config {
var configPath string
_, err1 := os.Stat(filepath.Join(exeDir, "config.yml"))
_, err2 := os.Stat(filepath.Join(exeDir, "config.yaml"))
if os.IsNotExist(err1) && os.IsNotExist(err2) {
createConfigAndExit()
} else if err1 == nil {
configPath = filepath.Join(exeDir, "config.yml")
} else if err2 == nil {
configPath = filepath.Join(exeDir, "config.yaml")
} else {
log.Fatal("failed to get config path: ", err1, err2)
}
configBytes, err := os.ReadFile(configPath)
if err != nil {
log.Fatal("failed to read config: ", err)
}
err = yaml.Unmarshal(configBytes, &c)
if err != nil {
log.Fatal("failed to unmarshal config: ", err)
}
if c.CoreKey == "" || c.MetaKey == "" {
log.Fatal("coreKey and metaKey must be set")
}
if len(c.CoreKey) != 32 || len(c.MetaKey) != 32 {
log.Fatal("coreKey and metaKey must be 32 bytes long strings")
}
switch mode {
case DIRECTMODE:
c.InputDir = ""
c.OutputDir = ""
case CONFIGMODE:
if c.InputDir == "" {
c.InputDir = exeDir
}
if c.OutputDir == "" {
c.OutputDir = c.InputDir
}
}
return c
}
func createConfigAndExit() {
fs, err := os.Create(filepath.Join(exeDir, "config.yml"))
if err != nil {
log.Fatal("failed to create config: ", err)
}
_, err = fs.Write(defaultConfig)
if err != nil {
log.Fatal("failed to write config: ", err)
}
log.Info("config created, please edit it and restart the program")
os.Exit(0)
}
func checkKeys(coreKey, metaKey string) (metaOk, coreOk bool) {
bs := 16
cBlock, _ := aes.NewCipher([]byte(coreKey))
coreCtext := make([]byte, len(corePlaintext))
for i := 0; i < len(corePlaintext); i += bs {
cBlock.Encrypt(coreCtext[i:i+bs], corePlaintext[i:i+bs])
}
mBlock, _ := aes.NewCipher([]byte(metaKey))
metaCtext := make([]byte, len(metaPlaintext))
for i := 0; i < len(metaPlaintext); i += bs {
mBlock.Encrypt(metaCtext[i:i+bs], metaPlaintext[i:i+bs])
}
coreOk = bytes.Equal(coreCtext, coreCiphertext)
metaOk = bytes.Equal(metaCtext, metaCiphertext)
return
}