agent.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**
  2. * @author oldj
  3. * @blog http://oldj.net
  4. *
  5. * 和系统、平台相关的方法
  6. */
  7. 'use strict';
  8. var sys_host_path = '/etc/hosts';
  9. var work_path = MacGap.homePath + '/.SwitchHosts';
  10. var data_path = work_path + '/data.json';
  11. var preference_path = work_path + '/preferences.json';
  12. var is_work_path_made;
  13. var _preferences;
  14. function mixObj(a, b) {
  15. var k;
  16. for (k in b) {
  17. if (b.hasOwnProperty(k)) {
  18. a[k] = b[k];
  19. }
  20. }
  21. return a;
  22. }
  23. function writeFile(path, content) {
  24. MacGap.File.write(path, content, 'string');
  25. }
  26. function readFile(path) {
  27. return MacGap.File.read(path, 'string');
  28. }
  29. function existPath(path) {
  30. return MacGap.File.exists(path);
  31. }
  32. function makeBackupHosts() {
  33. return {
  34. title: 'backup',
  35. on: true,
  36. content: getSysHosts()
  37. }
  38. }
  39. function tryToCreateWorkDir() {
  40. if (existPath(work_path)) return;
  41. var cmd = 'mkdir -p \'' + work_path + '\'';
  42. var my_task = MacGap.Task.create('/bin/sh', function (result) {
  43. if (result.status == 0) {
  44. //MacGap.File.write(sys_host_path, val, 'string');
  45. } else {
  46. alert('Fail to create work directory!\n\npath: ' + work_path);
  47. }
  48. });
  49. my_task['arguments'] = ['-c', cmd];
  50. my_task.launch();
  51. }
  52. function getSysHosts() {
  53. var s;
  54. try {
  55. s = readFile(sys_host_path);
  56. } catch (e) {
  57. alert(e.message);
  58. }
  59. return s || '';
  60. }
  61. function setSysHosts(val, sudo_pswd, callback) {
  62. var tmp_f = work_path + '/tmp.txt';
  63. //var cmd_f = work_path + '/cmd.sh';
  64. sudo_pswd = sudo_pswd || '';
  65. writeFile(tmp_f, val);
  66. var cmd;
  67. //var cmd2;
  68. if (!sudo_pswd) {
  69. cmd = [
  70. 'cat "' + tmp_f + '" > ' + sys_host_path
  71. , 'rm -rf ' + tmp_f
  72. ].join(' && ');
  73. } else {
  74. sudo_pswd = sudo_pswd.replace(/'/g, '\\x27');
  75. cmd = [
  76. 'echo \'' + sudo_pswd + '\' | sudo -S chmod 777 ' + sys_host_path
  77. , 'cat "' + tmp_f + '" > ' + sys_host_path
  78. , 'echo \'' + sudo_pswd + '\' | sudo -S chmod 644 ' + sys_host_path
  79. , 'rm -rf ' + tmp_f
  80. ].join(' && ');
  81. //cmd2 = [
  82. // 'echo \'' + sudo_pswd + '\' | sudo -S launchctl unload -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist'
  83. // , 'echo \'' + sudo_pswd + '\' | sudo -S launchctl load -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist'
  84. // , 'echo \'' + sudo_pswd + '\' | sudo -S launchctl unload -w /System/Library/LaunchDaemons/com.apple.discoveryd.plist'
  85. // , 'echo \'' + sudo_pswd + '\' | sudo -S launchctl load -w /System/Library/LaunchDaemons/com.apple.discoveryd.plist'
  86. //].join(' ; ');
  87. //cmd = cmd + ';' + cmd2;
  88. //cmd = "$'" + cmd.replace(/\\/g, '\\\\').replace(/'/g, "\\'") + "'";
  89. }
  90. var task = MacGap.Task.create('/bin/sh', function (result) {
  91. if (result.status == 0) {
  92. setTimeout(function () {
  93. afterSetHosts(sudo_pswd);
  94. }, 10);
  95. callback && callback();
  96. } else {
  97. //alert('An error occurred!');
  98. callback && callback(result);
  99. }
  100. });
  101. task['arguments'] = ['-c', cmd];
  102. task.launch();
  103. }
  104. function getData(config) {
  105. if (!is_work_path_made) {
  106. tryToCreateWorkDir();
  107. is_work_path_made = true;
  108. }
  109. var default_hosts = {
  110. title: 'My Hosts',
  111. on: false,
  112. content: '# My Hosts\n'
  113. };
  114. var default_vals = {
  115. sys: getSysHosts(),
  116. list: [default_hosts, makeBackupHosts()]
  117. };
  118. if (!existPath(data_path)) {
  119. return default_vals;
  120. }
  121. var vals = {};
  122. mixObj(vals, config);
  123. var s;
  124. try {
  125. s = readFile(data_path);
  126. } catch (e) {
  127. alert(e.message);
  128. return default_hosts;
  129. }
  130. try {
  131. s = JSON.parse(s);
  132. } catch (e) {
  133. alert(e.message);
  134. return default_hosts;
  135. }
  136. mixObj(vals, s);
  137. return vals;
  138. }
  139. function setData(data) {
  140. try {
  141. writeFile(data_path, JSON.stringify(data));
  142. } catch (e) {
  143. alert(e);
  144. }
  145. }
  146. function getAllPreferences() {
  147. if (!_preferences) {
  148. var c = readFile(preference_path);
  149. try {
  150. c = JSON.parse(c);
  151. } catch (e) {
  152. c = {};
  153. }
  154. _preferences = c;
  155. }
  156. return _preferences;
  157. }
  158. function getPreference(key) {
  159. var p = getAllPreferences();
  160. return p[key];
  161. }
  162. function setPreference(key, value) {
  163. var p = getAllPreferences();
  164. p[key] = value;
  165. writeFile(preference_path, JSON.stringify(p));
  166. }
  167. function getURL(url, data, success, fail) {
  168. if (!data._r) {
  169. data._r = Math.random();
  170. }
  171. $.ajax({
  172. url: url,
  173. data: data,
  174. //async: false,
  175. success: function (s) {
  176. success && success(s);
  177. },
  178. error: function (e) {
  179. fail && fail(e);
  180. }
  181. });
  182. }
  183. function openURL(url) {
  184. MacGap.openURL(url);
  185. }
  186. function activate() {
  187. setTimeout(function () {
  188. MacGap.activate();
  189. }, 0);
  190. }
  191. function notify(type, title, content) {
  192. MacGap.notify({
  193. type: type,
  194. title: title,
  195. content: content
  196. });
  197. }
  198. function afterSetHosts(sudo_pswd, callback) {
  199. // sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist
  200. // sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist
  201. // sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.discoveryd.plist
  202. // sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.discoveryd.plist
  203. if (!sudo_pswd) {
  204. callback && callback();
  205. return;
  206. }
  207. var cmd;
  208. //sudo_pswd = sudo_pswd.replace(/'/g, '\\x27');
  209. cmd = [
  210. //'echo \'' + sudo_pswd + '\' | sudo -S launchctl unload -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist'
  211. //, 'echo \'' + sudo_pswd + '\' | sudo -S launchctl load -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist'
  212. //, 'echo \'' + sudo_pswd + '\' | sudo -S launchctl unload -w /System/Library/LaunchDaemons/com.apple.discoveryd.plist'
  213. //, 'echo \'' + sudo_pswd + '\' | sudo -S launchctl load -w /System/Library/LaunchDaemons/com.apple.discoveryd.plist'
  214. 'sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist'
  215. , 'sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist'
  216. , 'sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.discoveryd.plist'
  217. , 'sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.discoveryd.plist'
  218. ].join('\n');
  219. //cmd = "$'" + cmd.replace(/\\/g, '\\\\').replace(/'/g, "\\'") + "'";
  220. //alert(cmd);
  221. var path = work_path + '/_restart_mDNSResponder.sh';
  222. MacGap.File.write(path, cmd, 'string');
  223. var task = MacGap.Task.create('/bin/sh', function (result) {
  224. if (result.status == 0) {
  225. callback && callback();
  226. } else {
  227. callback && callback(result);
  228. }
  229. });
  230. cmd = [
  231. '/bin/sh ' + path
  232. , 'rm -rf \'' + path + '\''
  233. ].join(';');
  234. task['arguments'] = ['-c', cmd];
  235. //task['arguments'] = [path];
  236. task.launch();
  237. }
  238. module.exports = {
  239. readFile: readFile,
  240. writeFile: writeFile,
  241. existPath: existPath,
  242. getSysHosts: getSysHosts,
  243. setSysHosts: setSysHosts,
  244. getData: getData,
  245. setData: setData,
  246. getAllPreferences: getAllPreferences,
  247. getPreference: getPreference,
  248. setPreference: setPreference,
  249. getURL: getURL,
  250. openURL: openURL,
  251. activate: activate,
  252. notify: notify
  253. };