cf.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * @author oldj
  3. * @blog http://oldj.net
  4. */
  5. "use strict";
  6. const fs = require('fs');
  7. const path = require('path');
  8. const exec = require('child_process').exec;
  9. const mkdirp = require('mkdirp');
  10. const is_win = process.platform == 'win32';
  11. let sys_host_path;
  12. if (is_win) {
  13. // todo windows 有可能不在 C 盘,需要先取得当前系统安装盘
  14. sys_host_path = 'C:\\Windows\\System32\\drivers\\etc\\hosts';
  15. } else {
  16. sys_host_path = '/etc/hosts';
  17. }
  18. const work_path = getDataHome();
  19. const data_path = path.join(work_path, 'data.json');
  20. console.log('work_path:', work_path);
  21. mkdirp(work_path, function (err) {
  22. err && console.log(err);
  23. });
  24. function copyObj(o) {
  25. let k;
  26. let o2 = {};
  27. for (k in o) {
  28. if (o.hasOwnProperty(k)) {
  29. o2[k] = o[k];
  30. }
  31. }
  32. return o2;
  33. }
  34. function mixObj(a, b) {
  35. let k;
  36. for (k in b) {
  37. if (b.hasOwnProperty(k)) {
  38. a[k] = b[k];
  39. }
  40. }
  41. return a;
  42. }
  43. function getSysHosts() {
  44. let s;
  45. try {
  46. s = fs.readFileSync(sys_host_path, 'utf-8');
  47. } catch (e) {
  48. console.log(sys_host_path);
  49. alert(e.message);
  50. }
  51. return s;
  52. }
  53. function getDataHome() {
  54. let data_path;
  55. let folder = 'SwitchHosts';
  56. if (is_win) {
  57. data_path = process.env.LOCALAPPDATA || process.env.APPDATA || process.env.USERPROFILE;
  58. } else {
  59. data_path = process.env.HOME || process.env.HOMEPATH;
  60. folder = '.' + folder;
  61. }
  62. data_path = data_path || process.cwd() || '';
  63. return path.join(data_path, folder);
  64. }
  65. function makeBackupHosts() {
  66. return {
  67. title: 'backup',
  68. on: true,
  69. content: getSysHosts()
  70. }
  71. }
  72. function getData(config) {
  73. let s;
  74. config = copyObj(config || {});
  75. let default_hosts = {
  76. title: 'My Hosts',
  77. on: false,
  78. content: '# My Hosts\n'
  79. };
  80. if (!fs.existsSync(data_path)) {
  81. return mixObj(config, {
  82. sys: getSysHosts(),
  83. list: [default_hosts, makeBackupHosts()]
  84. });
  85. } else {
  86. s = fs.readFileSync(data_path, 'utf-8');
  87. }
  88. try {
  89. s = JSON.parse(s);
  90. s = mixObj(config, s);
  91. if (typeof s == 'object') {
  92. s.sys = getSysHosts();
  93. s.list = s.list || [default_hosts, makeBackupHosts()];
  94. s.list.map(function (item) {
  95. // set default value
  96. item.title = item.title || '';
  97. item.on = !!item.on;
  98. item.content = item.content || '';
  99. });
  100. return s
  101. }
  102. alert('bad format!');
  103. } catch (e) {
  104. alert(e.message);
  105. }
  106. }
  107. function saveData(data) {
  108. fs.writeFile(data_path, JSON.stringify(data), 'utf-8', (e) => {
  109. if (e) {
  110. alert(e.message);
  111. return;
  112. }
  113. //console.log('data saved.');
  114. });
  115. }
  116. function saveHost(content, sudo_pswd, callback) {
  117. let cmd;
  118. //console.log(fs.statSync(sys_host_path));
  119. if (sudo_pswd) {
  120. // try to change the host file's permission
  121. cmd = `echo '${sudo_pswd}' | sudo -S chmod 766 ${sys_host_path}`;
  122. exec(cmd, function (err, stdout, stderr) {
  123. if (!err) {
  124. fs.writeFile(sys_host_path, content, 'utf-8', function (err) {
  125. // change the host file's permission back
  126. cmd = `echo '${sudo_pswd}' | sudo -S chmod 644 ${sys_host_path}`;
  127. exec(cmd, function (err) {
  128. //callback(err);
  129. err && console.log(err);
  130. });
  131. callback(err);
  132. });
  133. } else {
  134. callback(err);
  135. }
  136. });
  137. } else {
  138. // try to write host directly
  139. fs.writeFile(sys_host_path, content, 'utf-8', function (err) {
  140. callback(err);
  141. });
  142. }
  143. }
  144. exports.sys_host_path = sys_host_path;
  145. exports.work_path = work_path;
  146. exports.getSysHosts = getSysHosts;
  147. exports.getData = getData;
  148. exports.saveData = saveData;
  149. exports.saveHost = saveHost;