/
config.go
72 lines (64 loc) · 1.38 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
package dao
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"github.com/liamylian/x-rsa/golang/xrsa"
home "github.com/mitchellh/go-homedir"
"github.com/naiba/cloudssh/internal/model"
"github.com/naiba/cloudssh/pkg/xcrypto"
)
// Config ..
type Config struct {
User model.User
MasterKey xcrypto.CryptoKey
Server string
}
// Conf ..
var Conf *Config
var confFile string
// InitConfig ..
func InitConfig() {
Conf = &Config{}
homeDir, err := home.Dir()
if err != nil {
panic(err)
}
confFile = homeDir + "/.cloudssh.json"
content, err := ioutil.ReadFile(confFile)
if err != nil {
log.Println("InitConfig ReadFile error:", err)
return
}
if err = json.Unmarshal(content, Conf); err != nil {
log.Println("InitConfig Unmarshal error:", err)
}
}
// Save ..
func (c *Config) Save() error {
content, err := json.Marshal(c)
if err != nil {
return err
}
return ioutil.WriteFile(confFile, content, os.FileMode(0655))
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
// GerUserXRsa ..
func (c *Config) GerUserXRsa() (*xrsa.XRsa, error) {
cs, err := xcrypto.NewCipherString(c.User.Privatekey)
if err != nil {
return nil, err
}
userPrivateKeyByte, err := cs.Decrypt(c.MasterKey)
if err != nil {
return nil, err
}
return xrsa.NewXRsa([]byte(c.User.Pubkey), userPrivateKeyByte)
}