forked from shadowsocks/shadowsocks-gui
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
242 lines (240 loc) · 7.76 KB
/
main.js
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
// Generated by CoffeeScript 1.8.0
(function() {
var __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
$(function() {
var addConfig, addServer, args, chooseServer, deleteConfig, divWarning, divWarningShown, gui, hide, isRestarting, load, local, menu, os, publicConfig, qrcode, quit, reloadServerList, restartServer, save, serverHistory, show, tray, update, util, win;
os = require('os');
gui = require('nw.gui');
divWarning = $('#divWarning');
divWarningShown = false;
serverHistory = function() {
return (localStorage['server_history'] || '').split('|');
};
util = require('util');
util.log = function(s) {
console.log(new Date().toLocaleString() + (" - " + s));
if (!divWarningShown) {
divWarning.show();
divWarningShown = true;
}
return divWarning.text(s);
};
args = require('./args');
local = require('shadowsocks');
update = require('./update');
update.checkUpdate(function(url, version) {
var divNewVersion, span;
divNewVersion = $('#divNewVersion');
span = $("<span style='cursor:pointer'>New version " + version + " found, click here to download</span>");
span.click(function() {
return gui.Shell.openExternal(url);
});
divNewVersion.find('.msg').append(span);
return divNewVersion.fadeIn();
});
addServer = function(serverIP) {
var newServers, server, servers, _i, _len;
servers = (localStorage['server_history'] || '').split('|');
servers.push(serverIP);
newServers = [];
for (_i = 0, _len = servers.length; _i < _len; _i++) {
server = servers[_i];
if (server && __indexOf.call(newServers, server) < 0) {
newServers.push(server);
}
}
return localStorage['server_history'] = newServers.join('|');
};
$('#inputServerIP').typeahead({
source: serverHistory
});
chooseServer = function() {
var index;
index = +$(this).attr('data-key');
args.saveIndex(index);
load(false);
return reloadServerList();
};
reloadServerList = function() {
var configName, configs, currentIndex, divider, i, menuItem, serverMenu, _results;
currentIndex = args.loadIndex();
configs = args.allConfigs();
divider = $('#serverIPMenu .insert-point');
serverMenu = $('#serverIPMenu .divider');
$('#serverIPMenu li.server').remove();
i = 0;
_results = [];
for (configName in configs) {
if (i === currentIndex) {
menuItem = $("<li class='server'><a tabindex='-1' data-key='" + i + "' href='#'><i class='icon-ok'></i> " + configs[configName] + "</a> </li>");
} else {
menuItem = $("<li class='server'><a tabindex='-1' data-key='" + i + "' href='#'><i class='icon-not-ok'></i> " + configs[configName] + "</a> </li>");
}
menuItem.find('a').click(chooseServer);
menuItem.insertBefore(divider, serverMenu);
_results.push(i++);
}
return _results;
};
addConfig = function() {
args.saveIndex(NaN);
reloadServerList();
return load(false);
};
deleteConfig = function() {
args.deleteConfig(args.loadIndex());
args.saveIndex(NaN);
reloadServerList();
return load(false);
};
publicConfig = function() {
args.saveIndex(-1);
reloadServerList();
return load(false);
};
save = function() {
var config, index;
config = {};
$('input,select').each(function() {
var key, val;
key = $(this).attr('data-key');
if (this.type === 'checkbox') {
val = this.checked;
} else {
val = $(this).val();
}
return config[key] = val;
});
index = args.saveConfig(args.loadIndex(), config);
args.saveIndex(index);
reloadServerList();
util.log('config saved');
restartServer(config);
return false;
};
load = function(restart) {
var config;
config = args.loadConfig(args.loadIndex());
$('input,select').each(function() {
var key, val;
key = $(this).attr('data-key');
val = config[key] || '';
if (this.type === 'checkbox') {
this.checked = val;
return config[key] = this.checked;
} else {
$(this).val(val);
return config[key] = this.value;
}
});
if (restart) {
return restartServer(config);
}
};
isRestarting = false;
restartServer = function(config) {
var e, start;
if (config.server && +config.server_port && config.password && +config.local_port && config.method) {
if (isRestarting) {
util.log("Already restarting");
return;
}
isRestarting = true;
start = function() {
var e;
try {
isRestarting = false;
util.log('Starting shadowsocks...');
console.log(config);
window.local = local.createServer(config.server, config.server_port, config.local_port, config.password, config.method, 1000 * 600, config.share ? '0.0.0.0' : '127.0.0.1');
addServer(config.server);
$('#divError').fadeOut();
return gui.Window.get().hide();
} catch (_error) {
e = _error;
return util.log(e);
}
};
if (window.local != null) {
try {
util.log('Restarting shadowsocks');
if (window.local.address()) {
window.local.close();
}
return setTimeout(start, 1000);
} catch (_error) {
e = _error;
isRestarting = false;
return util.log(e);
}
} else {
return start();
}
} else {
return $('#divError').fadeIn();
}
};
qrcode = function(method, password, hostname, port) {
return "ss://" + (btoa("" + method + ":" + password + "@" + hostname + ":" + port));
};
$('#buttonSave').on('click', save);
$('#buttonNewProfile').on('click', addConfig);
$('#buttonDeleteProfile').on('click', deleteConfig);
$('#buttonPublicServer').on('click', publicConfig);
$('#buttonConsole').on('click', function() {
return gui.Window.get().showDevTools();
});
$('#buttonAbout').on('click', function() {
return gui.Shell.openExternal('https://github.com/shadowsocks/shadowsocks-gui');
});
$("#buttonQr").on('click', function() {
return window.qrCode.makeCode(qrcode($('#selectMethod').val(), $('#inputPassword').val(), $('#inputServerIP').val(), $('#inputServerPort').val()));
});
tray = new gui.Tray({
icon: '[email protected]'
});
menu = new gui.Menu();
tray.on('click', function() {
return gui.Window.get().show();
});
show = new gui.MenuItem({
type: 'normal',
label: 'Show',
click: function() {
return gui.Window.get().show();
}
});
quit = new gui.MenuItem({
type: 'normal',
label: 'Quit',
click: function() {
return gui.Window.get().close(true);
}
});
hide = new gui.MenuItem({
type: 'normal',
label: 'Hide',
click: function() {
return gui.Window.get().hide();
}
});
menu.append(show);
menu.append(hide);
menu.append(quit);
tray.menu = menu;
window.tray = tray;
win = gui.Window.get();
win.on('minimize', function() {
return this.hide();
});
win.on('close', function(quit) {
if (os.platform() === 'darwin' && !quit) {
return this.hide();
} else {
return this.close(true);
}
});
reloadServerList();
return load(true);
});
}).call(this);