-
Notifications
You must be signed in to change notification settings - Fork 44
/
config.go
333 lines (292 loc) · 6.99 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
ss_go "github.com/dawei101/shadowsocks-go/shadowsocks"
"github.com/getlantern/pac"
)
const debug = true
const (
Logo = "logo.png"
AppName = "tongshe"
ssPort = 1271
httpProxyPort = 1272
httpManagePort = 1270
HttpProxy = "127.0.0.1:1272"
SocksProxy = "127.0.0.1:1271"
)
var storageFolder string
var cacheFolder string
func init() {
if runtime.GOOS == "darwin" {
storageFolder = os.Getenv("HOME") + "/Library/Application Support"
cacheFolder = os.Getenv("HOME") + "/Library/Caches"
} else if runtime.GOOS == "windows" {
storageFolder = os.Getenv("APPDATA")
cacheFolder = os.Getenv("LOCALAPPDATA")
} else {
if os.Getenv("XDG_CONFIG_HOME") != "" {
storageFolder = os.Getenv("XDG_CONFIG_HOME")
} else {
storageFolder = filepath.Join(os.Getenv("HOME"), ".config")
}
if os.Getenv("XDG_CACHE_HOME") != "" {
cacheFolder = os.Getenv("XDG_CACHE_HOME")
} else {
cacheFolder = filepath.Join(os.Getenv("HOME"), ".cache")
}
}
s := GetStorageDir()
if !isPathExist(s) {
os.Mkdir(s, 0755)
}
if !isPathExist(GetStorageFile(Logo)) {
err := ioutil.WriteFile(GetStorageFile(Logo), GetRes(Logo), 0644)
if err != nil {
panic(err)
}
}
}
type SSTunnel struct {
Ip string
Port string
Password string
Method string
}
func (ss *SSTunnel) ToString() string {
return fmt.Sprintf("ss://%s:%s@%s:%s", ss.Method, ss.Password, ss.Ip, ss.Port)
}
func NewSSTunnel(ss string) (*SSTunnel, error) {
re, err := regexp.Compile(`(ss)://([\d\-\w]+):([\d\w]+)\@([\d\w\.]+):(\d+)`)
if err != nil {
log.Printf("regexp for ss tunnel is not correct")
os.Exit(-1)
}
res := re.FindStringSubmatch(ss)
if res != nil {
method, password, ip, port := res[2], res[3], res[4], res[5]
if err = ss_go.CheckCipherMethod(strings.Replace(method, "-auth", "", 1)); err != nil {
return nil, errors.New("不支持的加密类型:" + method)
}
return &SSTunnel{ip, port, password, method}, nil
}
return nil, errors.New("输入不是shadowsocks格式")
}
type Timestamp time.Time
func (t *Timestamp) MarshalJSON() ([]byte, error) {
ts := time.Time(*t).Unix()
stamp := fmt.Sprint(ts)
return []byte(stamp), nil
}
func (t *Timestamp) UnmarshalJSON(b []byte) error {
ts, err := strconv.Atoi(string(b))
if err != nil {
return err
}
*t = Timestamp(time.Unix(int64(ts), 0))
return nil
}
type Traffic struct {
Month string `json:"month"`
In int64 `json:"in"`
Out int64 `json:"out"`
}
type Config struct {
SSTunnels []string `json:"ss_tunnels"`
Config map[string]string `json:"config"`
Traffic *Traffic `json:"traffic"`
}
func (c *Config) Set(name string, value string) {
if c.Config == nil {
c.Config = map[string]string{}
}
c.Config[name] = value
SaveConfig(c)
}
func (c *Config) Get(name string) string {
v, ok := c.Config[name]
if ok {
return v
}
return ""
}
func (c *Config) AddTraffic(in, out int64) {
t := time.Now().UTC()
curMonth := fmt.Sprintf("%d%d", t.Year(), t.Month())
if c.Traffic == nil || c.Traffic.Month != curMonth {
c.Traffic = &Traffic{curMonth, 0, 0}
}
atomic.AddInt64(&c.Traffic.In, in)
atomic.AddInt64(&c.Traffic.Out, out)
}
func (c *Config) GetTraffic() *Traffic {
t := time.Now().UTC()
curMonth := fmt.Sprintf("%s%s", t.Year(), t.Month())
if c.Traffic != nil && c.Traffic.Month == curMonth {
return c.Traffic
}
return &Traffic{curMonth, 0, 0}
}
func (c *Config) GetSSTunnels() []*SSTunnel {
tunnels := []*SSTunnel{}
for _, sv := range c.SSTunnels {
ss, err := NewSSTunnel(sv)
if err == nil {
tunnels = append(tunnels, ss)
}
}
return tunnels
}
func (c *Config) AddTunnel(t string) error {
_, err := NewSSTunnel(t)
if err != nil {
return err
}
for _, sv := range c.SSTunnels {
if sv == t {
return errors.New("该Shadowsocks账号已存在")
}
}
c.SSTunnels = append(c.SSTunnels, t)
return SaveConfig(c)
}
func (c *Config) UpdateTunnel(oldT, newT string) error {
_, err := NewSSTunnel(newT)
if err != nil {
return err
}
sure := true
for i, sv := range c.SSTunnels {
if sv == oldT {
c.SSTunnels[i] = newT
}
if sv == newT {
sure = false
}
}
if sure {
return SaveConfig(c)
}
return errors.New("该Shadowsocks账号已存在")
}
func (c *Config) DeleteTunnel(t string) error {
for i, sv := range c.SSTunnels {
if sv == t {
c.SSTunnels = append(c.SSTunnels[:i], c.SSTunnels[i+1:]...)
break
}
}
return SaveConfig(c)
}
var configMutex = &sync.RWMutex{}
func LoadConfig() (*Config, error) {
config := &Config{}
f := fmt.Sprintf("%s/%s", GetStorageDir(), "user.config")
//log.Printf("read lock on config file ")
configMutex.RLock()
c, err := ioutil.ReadFile(f)
configMutex.RUnlock()
//log.Printf("read lock on config file released")
if err != nil || len(c) == 0 {
config = &Config{
[]string{},
map[string]string{},
&Traffic{"201605", 0, 0},
}
SaveConfig(config)
log.Printf("read config file err:%v", err)
return config, nil
}
if err = json.Unmarshal(c, config); err != nil {
log.Printf("dejson config err:%v", err)
SaveConfig(config)
}
return config, nil
}
func SaveConfig(config *Config) error {
f := fmt.Sprintf("%s/%s", GetStorageDir(), "user.config")
b, err := json.Marshal(&config)
if err != nil {
log.Printf("enjson config err:%v", err)
return err
}
//log.Printf("write lock on config file ")
configMutex.Lock()
defer func() {
configMutex.Unlock()
//log.Printf("write lock on config file released")
}()
if err = ioutil.WriteFile(f, b, 0644); err != nil {
log.Printf("write config err:%v", err)
return err
}
//log.Printf("save config succeed")
return nil
}
func GetRes(filename string) []byte {
bt, err := Asset(filename)
if err != nil {
return []byte{}
}
return bt
}
func GetStorageDir() string {
return fmt.Sprintf("%s/%s", storageFolder, AppName)
}
func GetStorageFile(f string) string {
return fmt.Sprintf("%s/%s", GetStorageDir(), f)
}
func isPathExist(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return true
}
var pacUrl string
func SetPac() error {
err := pac.EnsureHelperToolPresent(
GetStorageFile("pac-set"),
"铜蛇请求授权,以更改系统代理设置",
GetStorageFile(Logo),
)
if err != nil {
log.Printf("Could not set pac with err: %v", err)
os.Exit(1)
}
config, _ := LoadConfig()
if len(config.GetSSTunnels()) == 0 {
return errors.New("无Shadowsocks账号, pac不打开")
}
pacUrl = fmt.Sprintf("http://%s/pac?%d", GetManagementAddr(), time.Now().Nanosecond())
return pac.On(pacUrl)
}
func UnsetPac() {
pac.Off(pacUrl)
}
func GetSocksProxy() string {
//TODO add sharing feature
return fmt.Sprintf("%s:%d;", "127.0.0.1", ssPort)
}
func GetHttpProxy() string {
//TODO add sharing feature
return fmt.Sprintf("%s:%d;", "127.0.0.1", httpProxyPort)
}
func GetManagementAddr() string {
return fmt.Sprintf("127.0.0.1:%d", httpManagePort)
}