agent.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /**
  2. * @author oldj
  3. * @blog http://oldj.net
  4. *
  5. * 和系统、平台相关的方法
  6. */
  7. 'use strict';
  8. const fs = require('fs');
  9. const path = require('path');
  10. const request = require('request');
  11. const moment = require('moment');
  12. const io = require('./libs/io');
  13. const platform = process.platform;
  14. const paths = require('./libs/paths');
  15. const pref = require('./libs/pref');
  16. const sys_host_path = paths.sys_host_path;
  17. const work_path = paths.work_path;
  18. const data_path = paths.data_path;
  19. const preference_path = paths.preference_path;
  20. const exec = require('child_process').exec;
  21. const stat = require('./modules/stat');
  22. stat.init();
  23. const crypto = require('crypto');
  24. function md5(text) {
  25. return crypto.createHash('md5').update(text).digest('hex');
  26. }
  27. const m_lang = require('./lang');
  28. let sudo_pswd = '';
  29. function getUserLang() {
  30. let user_lang;
  31. let p_lang = location.search.match(/\blang=(\w+)/);
  32. user_lang = (p_lang && p_lang[1]) || pref.get('user_language') || navigator.language || navigator.userLanguage || '';
  33. user_lang = user_lang.toString().toLowerCase();
  34. if (user_lang == 'cn' || user_lang == 'zh_cn') {
  35. user_lang = 'cn';
  36. } else {
  37. user_lang = 'en';
  38. }
  39. return user_lang;
  40. }
  41. let lang_key = getUserLang();
  42. const lang = m_lang.getLang(lang_key);
  43. function getSysHosts() {
  44. let cnt = '';
  45. try {
  46. cnt = fs.readFileSync(sys_host_path, 'utf-8');
  47. } catch (e) {
  48. console.log(e.message);
  49. }
  50. return cnt;
  51. }
  52. function tryToCreateWorkDir() {
  53. if (io.isDirectory((work_path))) {
  54. console.log('work dir exists.');
  55. return;
  56. }
  57. console.log(`try to create work directory: ${work_path}`);
  58. try {
  59. fs.mkdirSync(work_path);
  60. console.log('work directory created.');
  61. } catch (e) {
  62. alert('Fail to create work directory!');
  63. }
  64. }
  65. function saveData(content) {
  66. let txt = JSON.stringify({
  67. list: content
  68. });
  69. fs.writeFile(data_path, txt, 'utf-8', (error) => {
  70. if (error) {
  71. alert(error.message);
  72. }
  73. });
  74. }
  75. function apply_UNIX(content, success) {
  76. let tmp_fn = path.join(work_path, 'tmp.txt');
  77. if (content) {
  78. fs.writeFileSync(tmp_fn, content, 'utf-8');
  79. }
  80. let cmd;
  81. if (!sudo_pswd) {
  82. cmd = [
  83. `cat "${tmp_fn}" > ${sys_host_path}`
  84. , `rm -rf ${tmp_fn}`
  85. ].join(' && ');
  86. } else {
  87. sudo_pswd = sudo_pswd.replace(/'/g, '\\x27');
  88. cmd = [
  89. `echo '${sudo_pswd}' | sudo -S chmod 777 ${sys_host_path}`
  90. , `cat "${tmp_fn}" > ${sys_host_path}`
  91. , `echo '${sudo_pswd}' | sudo -S chmod 644 ${sys_host_path}`
  92. // , 'rm -rf ' + tmp_fn
  93. ].join(' && ');
  94. }
  95. exec(cmd, function (error, stdout, stderr) {
  96. // command output is in stdout
  97. if (error) {
  98. if (!sudo_pswd) {
  99. // 尝试让用户输入管理密码
  100. SH_event.emit('show_app');
  101. SH_event.emit('sudo_prompt', (pswd) => {
  102. sudo_pswd = pswd;
  103. tryToApply(null, success);
  104. });
  105. } else {
  106. alert(stderr);
  107. }
  108. return;
  109. }
  110. if (!error) {
  111. after_apply(success);
  112. }
  113. });
  114. }
  115. function _after_apply_unix(callback) {
  116. let cmd_fn = path.join(work_path, '_restart_mDNSResponder.sh');
  117. let cmd = `
  118. p1=/System/Library/LaunchDaemons/com.apple.mDNSResponder.plist
  119. if [ -f $p1 ]; then
  120. sudo launchctl unload -w $p1
  121. sudo launchctl load -w $p1
  122. fi
  123. p2=/System/Library/LaunchDaemons/com.apple.discoveryd.plist
  124. if [ -f p2 ]; then
  125. sudo launchctl unload -w $p2
  126. sudo launchctl load -w $p2
  127. fi
  128. sudo killall -HUP mDNSResponder
  129. `;
  130. fs.writeFileSync(cmd_fn, cmd, 'utf-8');
  131. exec(`/bin/sh ${cmd_fn}`, function (error, stdout, stderr) {
  132. // command output is in stdout
  133. if (error) {
  134. console.log(error);
  135. }
  136. console.log(stdout, stderr);
  137. callback();
  138. });
  139. }
  140. function after_apply(callback) {
  141. SH_event.emit('after_apply');
  142. if (!sudo_pswd) {
  143. callback();
  144. return;
  145. }
  146. if (platform === 'darwin') {
  147. _after_apply_unix(callback);
  148. return;
  149. }
  150. callback();
  151. }
  152. function apply_Win32(content, success) {
  153. // todo 判断写入权限
  154. try {
  155. fs.writeFileSync(sys_host_path, content, 'utf-8');
  156. } catch (e) {
  157. console.log(e);
  158. let msg = e.message;
  159. if (platform === 'win32') {
  160. msg = `${msg}\n\n${lang.please_run_as_admin}`;
  161. }
  162. alert(msg);
  163. return;
  164. }
  165. success && success();
  166. // todo 更新 DNS 缓存
  167. }
  168. function tryToApply(content, success) {
  169. if (platform !== 'win32') {
  170. apply_UNIX(content, success);
  171. } else {
  172. apply_Win32(content, success);
  173. }
  174. }
  175. // init
  176. tryToCreateWorkDir();
  177. SH_event.on('test', () => {
  178. console.log('ttt');
  179. });
  180. SH_event.on('apply', (content, success) => {
  181. tryToApply(content, () => {
  182. let cmd = pref.get('after_cmd');
  183. if (cmd) {
  184. exec(cmd, function (error, stdout, stderr) {
  185. // command output is in stdout
  186. if (error) {
  187. alert(`AfterCmdError:\n\n${stderr}`);
  188. }
  189. });
  190. }
  191. success && success();
  192. });
  193. });
  194. SH_event.on('sudo_pswd', (pswd) => {
  195. sudo_pswd = pswd;
  196. });
  197. SH_event.on('show_app', (pswd) => {
  198. ipcRenderer.send('show_app');
  199. });
  200. SH_event.on('save_data', (content) => {
  201. saveData(content);
  202. ipcRenderer.send('send_host_list', content);
  203. });
  204. SH_event.on('check_host_refresh', (host, force = false) => {
  205. if (host.where !== 'remote' || !host.url || (!force && !host.refresh_interval)) {
  206. return;
  207. }
  208. let last_refresh = host.last_refresh;
  209. let refresh_interval = parseInt(host.refresh_interval) || 0;
  210. if (last_refresh && !force) {
  211. last_refresh = new Date(last_refresh);
  212. let delta = ((new Date()).getTime() - (last_refresh.getTime() || 0)) / (1000 * 3600);
  213. if (delta < refresh_interval) {
  214. return;
  215. }
  216. }
  217. // refresh
  218. // console.log(`getting '${host.url}' ..`);
  219. SH_event.emit('loading', host);
  220. host.is_loading = true;
  221. request(host.url, (err, res, body) => {
  222. console.log('got', res && res.statusCode);
  223. let out = {};
  224. // console.log(err, res && res.statusCode);
  225. host.is_loading = false;
  226. if (!err && res.statusCode === 200) {
  227. // console.log(body);
  228. host.content = body;
  229. delete host.error;
  230. host.last_refresh = moment().format('YYYY-MM-DD HH:mm:ss');
  231. SH_event.emit('change');
  232. } else {
  233. console.log(err, res && res.statusCode);
  234. // out.content = 'Error: ' + err.message;
  235. out.error = 'Error: ' + err.message;
  236. }
  237. SH_event.emit('loading_done', host, Object.assign({}, host, out));
  238. });
  239. });
  240. /**
  241. * 如果本地没有 data 文件,认为是第一次运行
  242. */
  243. function initGet() {
  244. let dd = require('./libs/default_data');
  245. let data = dd.make();
  246. data.sys.content = getSysHosts();
  247. data.list.push({
  248. title: 'backup',
  249. content: data.sys.content
  250. });
  251. return data;
  252. }
  253. module.exports = {
  254. md5: md5,
  255. getHosts: function () {
  256. let data = null;
  257. if (!io.isFile(data_path)) {
  258. return initGet();
  259. }
  260. try {
  261. let cnt = fs.readFileSync(data_path, 'utf-8');
  262. data = JSON.parse(cnt);
  263. } catch (e) {
  264. console.log(e);
  265. alert('bad data file.. :(');
  266. return initGet();
  267. }
  268. return {
  269. sys: {
  270. is_sys: true
  271. , content: getSysHosts()
  272. },
  273. list: data.list.map((i) => {
  274. return {
  275. title: i.title || ''
  276. , content: i.content || ''
  277. , on: !!i.on
  278. , where: i.where || 'local'
  279. , url: i.url || ''
  280. , last_refresh: i.last_refresh || null
  281. , refresh_interval: i.refresh_interval || 0
  282. }
  283. })
  284. };
  285. },
  286. getSysHosts: function () {
  287. return {
  288. is_sys: true
  289. , content: getSysHosts()
  290. }
  291. },
  292. readFile: function (fn, callback) {
  293. fs.readFile(fn, 'utf-8', callback);
  294. },
  295. notify: (options) => {
  296. notifier.notify(Object.assign({
  297. title: 'SwitchHosts!',
  298. message: '',
  299. icon: path.join(__dirname, 'assets', 'logo_512.png')
  300. }, options));
  301. },
  302. lang: lang,
  303. lang_key: lang_key,
  304. pref: pref,
  305. relaunch() {
  306. ipcRenderer.send('relaunch');
  307. }
  308. };