-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
agent.js
301 lines (261 loc) · 7.73 KB
/
agent.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
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
/**
* @author oldj
* @blog http://oldj.net
*
* 和系统、平台相关的方法
*/
'use strict';
var sys_host_path = '/etc/hosts';
var work_path = MacGap.homePath + '/.SwitchHosts';
var data_path = work_path + '/data.json';
var preference_path = work_path + '/preferences.json';
var is_work_path_made;
var _preferences;
function mixObj(a, b) {
var k;
for (k in b) {
if (b.hasOwnProperty(k)) {
a[k] = b[k];
}
}
return a;
}
function writeFile(path, content) {
MacGap.File.write(path, content, 'string');
}
function readFile(path) {
return MacGap.File.read(path, 'string');
}
function existPath(path) {
return MacGap.File.exists(path);
}
function makeBackupHosts() {
return {
title: 'backup',
on: true,
content: getSysHosts()
};
}
function tryToCreateWorkDir() {
if (existPath(work_path)) return;
var cmd = 'mkdir -p \'' + work_path + '\'';
var my_task = MacGap.Task.create('/bin/sh', function (result) {
if (result.status == 0) {
//MacGap.File.write(sys_host_path, val, 'string');
} else {
alert('Fail to create work directory!\n\npath: ' + work_path);
}
});
my_task['arguments'] = ['-c', cmd];
my_task.launch();
}
function getSysHosts() {
var s;
try {
s = readFile(sys_host_path);
} catch (e) {
alert(e.message);
}
return s || '';
}
function setSysHosts(val, sudo_pswd, callback) {
var tmp_f = work_path + '/tmp.txt';
//var cmd_f = work_path + '/cmd.sh';
sudo_pswd = sudo_pswd || '';
writeFile(tmp_f, val);
var cmd;
//var cmd2;
if (!sudo_pswd) {
cmd = [
'cat "' + tmp_f + '" > ' + sys_host_path
, 'rm -rf ' + tmp_f
].join(' && ');
} else {
sudo_pswd = sudo_pswd.replace(/'/g, '\\x27');
cmd = [
'echo \'' + sudo_pswd + '\' | sudo -S chmod 777 ' + sys_host_path
, 'cat "' + tmp_f + '" > ' + sys_host_path
, 'echo \'' + sudo_pswd + '\' | sudo -S chmod 644 ' + sys_host_path
, 'rm -rf ' + tmp_f
].join(' && ');
//cmd2 = [
// 'echo \'' + sudo_pswd + '\' | sudo -S launchctl unload -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist'
// , 'echo \'' + sudo_pswd + '\' | sudo -S launchctl load -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist'
// , 'echo \'' + sudo_pswd + '\' | sudo -S launchctl unload -w /System/Library/LaunchDaemons/com.apple.discoveryd.plist'
// , 'echo \'' + sudo_pswd + '\' | sudo -S launchctl load -w /System/Library/LaunchDaemons/com.apple.discoveryd.plist'
//].join(' ; ');
//cmd = cmd + ';' + cmd2;
//cmd = "$'" + cmd.replace(/\\/g, '\\\\').replace(/'/g, "\\'") + "'";
}
var task = MacGap.Task.create('/bin/sh', function (result) {
if (result.status == 0) {
setTimeout(function () {
afterSetHosts(sudo_pswd);
}, 10);
callback && callback();
} else {
//alert('An error occurred!');
callback && callback(result);
}
});
task['arguments'] = ['-c', cmd];
task.launch();
}
function getData(config) {
if (!is_work_path_made) {
tryToCreateWorkDir();
is_work_path_made = true;
}
var default_hosts = {
title: 'My Hosts',
on: false,
content: '# My Hosts\n'
};
var default_vals = {
sys: getSysHosts(),
list: [default_hosts, makeBackupHosts()]
};
if (!existPath(data_path)) {
return default_vals;
}
var vals = {};
mixObj(vals, config);
var s;
try {
s = readFile(data_path);
} catch (e) {
alert(e.message);
return default_hosts;
}
try {
s = JSON.parse(s);
} catch (e) {
alert(e.message);
return default_hosts;
}
mixObj(vals, s);
return vals;
}
function setData(data) {
try {
writeFile(data_path, JSON.stringify(data));
} catch (e) {
alert(e);
}
}
function getAllPreferences() {
if (!_preferences) {
var c = readFile(preference_path);
try {
c = JSON.parse(c);
} catch (e) {
c = {};
}
_preferences = c;
}
return _preferences;
}
function getPreference(key) {
var p = getAllPreferences();
return p[key];
}
function setPreference(key, value) {
var p = getAllPreferences();
p[key] = value;
writeFile(preference_path, JSON.stringify(p));
}
function getURL(url, data, success, fail) {
data = data || {};
if (!data._r) {
data._r = Math.random();
}
$.ajax({
url: url,
data: data,
//async: false,
success: function (s) {
success && success(s);
},
error: function (e) {
fail && fail(e);
}
});
}
function openURL(url) {
MacGap.openURL(url);
}
function activate() {
setTimeout(function () {
MacGap.activate();
}, 0);
}
function notify(type, title, content) {
MacGap.notify({
type: type,
title: title,
content: content
});
}
function afterSetHosts(sudo_pswd, callback) {
// sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist
// sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist
// sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.discoveryd.plist
// sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.discoveryd.plist
if (!sudo_pswd) {
callback && callback();
return;
}
var cmd;
//sudo_pswd = sudo_pswd.replace(/'/g, '\\x27');
cmd = [
//'echo \'' + sudo_pswd + '\' | sudo -S launchctl unload -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist'
//, 'echo \'' + sudo_pswd + '\' | sudo -S launchctl load -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist'
//, 'echo \'' + sudo_pswd + '\' | sudo -S launchctl unload -w /System/Library/LaunchDaemons/com.apple.discoveryd.plist'
//, 'echo \'' + sudo_pswd + '\' | sudo -S launchctl load -w /System/Library/LaunchDaemons/com.apple.discoveryd.plist'
'sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist'
, 'sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist'
, 'sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.discoveryd.plist'
, 'sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.discoveryd.plist'
, 'sudo killall -HUP mDNSResponder'
].join('\n');
//cmd = "$'" + cmd.replace(/\\/g, '\\\\').replace(/'/g, "\\'") + "'";
//alert(cmd);
var path = work_path + '/_restart_mDNSResponder.sh';
MacGap.File.write(path, cmd, 'string');
var task = MacGap.Task.create('/bin/sh', function (result) {
if (result.status == 0) {
callback && callback();
} else {
callback && callback(result);
}
});
cmd = [
'/bin/sh ' + path
, 'rm -rf \'' + path + '\''
].join(';');
task['arguments'] = ['-c', cmd];
//task['arguments'] = [path];
task.launch();
}
function log(msg) {
/*eslint no-console: "error"*/
// console.log(msg);
MacGap.log(msg.toString());
}
module.exports = {
log: log,
readFile: readFile,
writeFile: writeFile,
existPath: existPath,
getSysHosts: getSysHosts,
setSysHosts: setSysHosts,
getData: getData,
setData: setData,
getAllPreferences: getAllPreferences,
getPreference: getPreference,
setPreference: setPreference,
getURL: getURL,
openURL: openURL,
activate: activate,
notify: notify
};