Repository navigation
Expand file tree
/
Copy pathconfig.go
More file actions
146 lines (105 loc) · 1.98 KB
/
Copy pathconfig.go
File metadata and controls
146 lines (105 loc) · 1.98 KB
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
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
)
const (
MinCheckSeconds = 5
)
var (
ConfigFile string
)
// Some errors
var (
ErrNoDevName = errors.New("No device name specified")
ErrCheckSeconds = fmt.Errorf("No time specified for checkseconds or less than %s", MinCheckSeconds)
)
type ClientConfig struct {
Globals Globals
Proxys []struct {
Name string
Type string
Address string
Username string
Password string
Timeout int
}
Accounts []Account
}
type Globals struct {
CacheDir string
DeviceName string
CheckSeconds int
}
type Account struct {
DeviceUUID string
Username string
Password string
Key string
Proxy string
proxyType string
proxyAddress string
proxyUsername string
proxyPassword string
proxyTimeout int
}
func (cfg *ClientConfig) Flush(f string) (err error) {
file, err := os.OpenFile(f, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
return
}
defer file.Close()
b, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return
}
buf := bufio.NewWriter(file)
_, err = buf.Write(b)
if err != nil {
return
}
defer buf.Flush()
return
}
func (cfg *ClientConfig) validate() (err error) {
if len(cfg.Globals.DeviceName) < 1 {
return ErrNoDevName
}
if cfg.Globals.CheckSeconds < MinCheckSeconds {
return ErrCheckSeconds
}
for i, v := range cfg.Accounts {
if len(v.Proxy) < 1 {
continue
}
for _, pv := range cfg.Proxys {
if cfg.Accounts[i].Proxy == pv.Name {
cfg.Accounts[i].proxyType = pv.Type
cfg.Accounts[i].proxyAddress = pv.Address
cfg.Accounts[i].proxyUsername = pv.Username
cfg.Accounts[i].proxyPassword = pv.Password
cfg.Accounts[i].proxyTimeout = pv.Timeout
}
}
}
return
}
func GetCFG(f string) (cfg *ClientConfig, err error) {
b, err := ioutil.ReadFile(f)
if err != nil {
return
}
err = json.Unmarshal(b, &cfg)
if err != nil {
return
}
err = cfg.validate()
if err != nil {
return
}
return
}