-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
114 lines (94 loc) · 2.91 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
110
111
112
113
114
package main
import (
"fmt"
"strings"
"encoding/json"
"os"
"io/ioutil"
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
)
var bt = []byte{46, 57, 24, 85, 35, 24, 74, 35, 87, 88, 98, 66, 32, 14, 05, 35}
func Encode(b []byte) string {
return base64.StdEncoding.EncodeToString(b)
}
func Decode(s string) []byte {
data, err := base64.StdEncoding.DecodeString(s)
if err != nil {
panic(err)
}
return data
}
func Encrypt(text, key string) (string, error) {
block, err := aes.NewCipher([]byte(key))
if err != nil {
return "", err
}
plainText := []byte(text)
cfb := cipher.NewCFBEncrypter(block, bt)
cipherText := make([]byte, len(plainText))
cfb.XORKeyStream(cipherText, plainText)
return Encode(cipherText), nil
}
func Decrypt(text, key string) (string, error) {
block, err := aes.NewCipher([]byte(key))
if err != nil {
return "", err
}
cipherText := Decode(text)
cfb := cipher.NewCFBDecrypter(block, bt)
plainText := make([]byte, len(cipherText))
cfb.XORKeyStream(plainText, cipherText)
return string(plainText), nil
}
// config
type Configuration struct {
User []string
Password []string
Server []string
Port []string
Db []string
Init []string
}
var sqlcred = getconfig()
func getconfig() string {
file, _ := os.Open("conf.json")
defer file.Close()
decoder := json.NewDecoder(file)
configuration := Configuration{}
errcode := decoder.Decode(&configuration)
if errcode != nil {
fmt.Println("error:", errcode)
}
if strings.Join(configuration.Init," ") == "1" {
fmt.Println(md5hash(`admin`+keysalt+``))
fmt.Println("Initial Passwort wird geändert...")
changepw, err := ioutil.ReadFile("conf.json")
if err != nil {
fmt.Println("error:", errcode)
}
encText, err := Encrypt(strings.Join(configuration.Password," "), key)
newpw := bytes.Replace(changepw, []byte(`"Password": ["`+strings.Join(configuration.Password," ")+`"],`), []byte(`"Password": ["`+encText+`"],`), -1)
if err = ioutil.WriteFile("conf.json", newpw, 0660); err != nil {
fmt.Println("error:", errcode)
}
changeinit, err := ioutil.ReadFile("conf.json")
if err != nil {
fmt.Println("error:", errcode)
}
newinit := bytes.Replace(changeinit, []byte(`"Init": ["1"]`), []byte(`"Init": ["0"]`), -1)
if err = ioutil.WriteFile("conf.json", newinit, 0660); err != nil {
fmt.Println("error:", errcode)
}
return ``+strings.Join(configuration.User," ")+`:`+strings.Join(configuration.Password," ")+`@tcp(`+strings.Join(configuration.Server," ")+`:`+strings.Join(configuration.Port," ")+`)/`+strings.Join(configuration.Db," ")+``
} else
{
decText, err := Decrypt(strings.Join(configuration.Password," "), key)
if err != nil {
fmt.Println("error:", errcode)
}
return ``+strings.Join(configuration.User," ")+`:`+decText+`@tcp(`+strings.Join(configuration.Server," ")+`:`+strings.Join(configuration.Port," ")+`)/`+strings.Join(configuration.Db," ")+``
}
}