-
Notifications
You must be signed in to change notification settings - Fork 42
/
config.go
184 lines (161 loc) · 3.39 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package upx
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"github.com/upyun/upx/xerrors"
)
const (
LOGIN = true
NO_LOGIN = false
MinJitter = 5
MaxJitter = 60
MaxRetry = 10
)
type Config struct {
SessionId int `json:"user_idx"`
Sessions []*Session `json:"users"`
}
func (c *Config) PopCurrent() {
if c.SessionId == -1 {
c.SessionId = 0
}
c.Sessions = append(c.Sessions[0:c.SessionId], c.Sessions[c.SessionId+1:]...)
c.SessionId = 0
}
func (c *Config) Insert(sess *Session) {
for idx, s := range c.Sessions {
if s.Bucket == sess.Bucket && s.Operator == sess.Operator {
c.Sessions[idx] = sess
c.SessionId = idx
return
}
}
c.Sessions = append(c.Sessions, sess)
c.SessionId = len(c.Sessions) - 1
}
var (
confname string
config *Config
)
func makeAuthStr(bucket, operator, password string) (string, error) {
sess := &Session{
Bucket: bucket,
Operator: operator,
Password: password,
CWD: "/",
}
if err := sess.Init(); err != nil {
return "", err
}
s := []string{bucket, operator, password}
b, err := json.Marshal(s)
if err != nil {
return "", err
}
return hashEncode(base64.StdEncoding.EncodeToString(b)), nil
}
func authStrToConfig(auth string) error {
data, err := base64.StdEncoding.DecodeString(hashEncode(auth))
if err != nil {
return err
}
ss := []string{}
if err := json.Unmarshal(data, &ss); err != nil {
return err
}
if len(ss) == 3 {
session = &Session{
Bucket: ss[0],
Operator: ss[1],
Password: ss[2],
CWD: "/",
}
if err := session.Init(); err != nil {
return err
}
} else {
return fmt.Errorf("invalid auth string")
}
return nil
}
func readConfigFromFile(login bool) error {
if confname == "" {
confname = getConfigName()
}
b, err := ioutil.ReadFile(confname)
if err != nil {
os.RemoveAll(confname)
if os.IsNotExist(err) && login == NO_LOGIN {
return nil
}
return err
}
data, err := base64.StdEncoding.DecodeString(hashEncode(string(b)))
if err != nil {
os.RemoveAll(confname)
return err
}
config = &Config{SessionId: -1}
if err := json.Unmarshal(data, config); err != nil {
os.RemoveAll(confname)
return err
}
if config.SessionId != -1 && config.SessionId < len(config.Sessions) {
session = config.Sessions[config.SessionId]
if login == LOGIN {
if err := session.Init(); err != nil {
config.PopCurrent()
return err
}
}
} else {
if login == LOGIN {
return xerrors.ErrRequireLogin
}
}
return nil
}
func saveConfigToFile() {
if confname == "" {
confname = getConfigName()
}
b, err := json.Marshal(config)
if err != nil {
PrintErrorAndExit("save config: %v", err)
}
s := hashEncode(base64.StdEncoding.EncodeToString(b))
fd, err := os.Create(confname)
if err != nil {
PrintErrorAndExit("save config: %v", err)
}
defer fd.Close()
_, err = fd.WriteString(s)
if err != nil {
PrintErrorAndExit("save config: %v", err)
}
}
func getConfigName() string {
if runtime.GOOS == "windows" {
return filepath.Join(os.Getenv("USERPROFILE"), ".upx.cfg")
}
return filepath.Join(os.Getenv("HOME"), ".upx.cfg")
}
func hashEncode(s string) string {
r := []rune(s)
for i := 0; i < len(r); i++ {
switch {
case r[i] >= 'a' && r[i] <= 'z':
r[i] += 'A' - 'a'
case r[i] >= 'A' && r[i] <= 'Z':
r[i] += 'a' - 'A'
case r[i] >= '0' && r[i] <= '9':
r[i] = '9' - r[i] + '0'
}
}
return string(r)
}