forked from shadowsocks/shadowsocks-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargs.coffee
More file actions
115 lines (99 loc) · 2.65 KB
/
args.coffee
File metadata and controls
115 lines (99 loc) · 2.65 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
localStorage = window.localStorage
util = require 'util'
fs = require 'fs'
guiconfigFilename = fs.realpathSync(process.execPath + '/..') + '/gui-config.json'
loadFromJSON = ->
# Windows users are happy to see a config file within their shadowsocks-gui folder
if process.platform == 'win32'
try
data = fs.readFileSync guiconfigFilename
temp = JSON.parse data.toString('utf-8')
# make config file easier to read
if temp.configs
temp.configs = JSON.stringify(temp.configs)
localStorage = temp
util.log 'reading config file'
catch e
console.log e
loadFromJSON()
saveToJSON = ->
if process.platform == 'win32'
util.log 'saving config file'
# make config file easier to read
temp = JSON.parse(JSON.stringify(localStorage))
if temp.configs
temp.configs = JSON.parse(temp.configs)
data = JSON.stringify(temp, null, 2)
try
fs.writeFileSync guiconfigFilename, data, 'encoding': 'utf-8'
catch e
util.log e
# This is a public server
publicConfig =
server: '209.141.36.62'
server_port: 8348
local_port: 1080
password: '$#HAL9000!'
method: 'aes-256-cfb'
timeout: 600
defaultConfig =
server_port: 8388
local_port: 1080
method: 'aes-256-cfb'
timeout: 600
loadConfigs = ->
try
JSON.parse(localStorage['configs'] or '[]')
catch e
util.log e
[]
allConfigs = ->
if localStorage['configs']
result = []
try
configs = loadConfigs()
for i of configs
c = configs[i]
result.push "#{c.server}:#{c.server_port}"
return result
catch e
[]
saveIndex = (index) ->
localStorage['index'] = index
saveToJSON()
loadIndex = ->
+localStorage['index']
saveConfigs = (configs) ->
localStorage['configs'] = JSON.stringify(configs)
saveToJSON()
saveConfig = (index, config) ->
if index == -1
# if modified based on public server, add a profile, not to modify public server
index = NaN
configs = loadConfigs()
if isNaN(index)
configs.push config
index = configs.length - 1
else
configs[index] = config
saveConfigs configs
index
loadConfig = (index) ->
if isNaN(index)
return defaultConfig
if index == -1
return publicConfig
configs = loadConfigs()
return configs[index] or defaultConfig
deleteConfig = (index) ->
if (not isNaN(index)) and not (index == -1)
configs = loadConfigs()
configs.splice index, 1
saveConfigs configs
exports.allConfigs = allConfigs
exports.saveConfig = saveConfig
exports.loadConfig = loadConfig
exports.deleteConfig = deleteConfig
exports.loadIndex = loadIndex
exports.saveIndex = saveIndex
exports.publicConfig = publicConfig