Browse Source

after_cmd 等完成。

oldj 8 years ago
parent
commit
cebcf4f923
5 changed files with 131 additions and 19 deletions
  1. 1 1
      app/bundle.js
  2. 0 1
      app/server/actions/setPref.js
  3. 78 16
      app/server/apply.js
  4. 51 0
      app/server/applyAfter_Unix.js
  5. 1 1
      app/version.js

+ 1 - 1
app/bundle.js

@@ -16629,7 +16629,7 @@ module.exports = "data:application/vnd.ms-fontobject;base64,pj8AAIw+AAABAAIAAAAA
 "use strict";
 
 
-exports.version = [3, 3, 0, 4556];
+exports.version = [3, 3, 0, 4570];
 
 /***/ }),
 /* 71 */

+ 0 - 1
app/server/actions/setPref.js

@@ -23,7 +23,6 @@ module.exports = (svr, k_or_data, v = null) => {
       })
   } else {
     // object mode
-    console.log(2666, k_or_data)
     p = p.then(() => Object.assign({}, k_or_data))
   }
 

+ 78 - 16
app/server/apply.js

@@ -7,17 +7,23 @@
 
 'use strict'
 
+const fs = require('fs')
 const path = require('path')
 const exec = require('child_process').exec
+const getPref = require('./actions/getPref')
+const getLang = require('./actions/getLang')
 const io = require('./io')
 const {sys_host_path, work_path} = require('./paths')
 const crypto = require('crypto')
 const md5File = require('md5-file')
+const applyAfter_Unix = require('./applyAfter_Unix')
 const platform = process.platform
+const svr = require('./svr')
 
 let sudo_pswd = ''
+let lang = null
 
-function needPswd(str) {
+function needPswd (str) {
   str = str.toLowerCase()
 
   console.log('---')
@@ -73,14 +79,36 @@ function apply_Unix (content, callback) {
 }
 
 function apply_Win32 (content, callback) {
-  // todo
+  // todo 判断写入权限
+
+  try {
+    fs.writeFileSync(sys_host_path, content, 'utf-8')
+  } catch (e) {
+    console.log(e)
+    let msg = e.message
+    msg = `${msg}\n\n${lang.please_run_as_admin}`
+    alert(msg)
+    return
+  }
+
+  // todo 刷新 DNS 缓存
+
+  callback()
 }
 
-function tryToApply (...args) {
+function tryToApply (content, callback) {
   if (platform !== 'win32') {
-    apply_Unix(...args)
+    // unix
+    apply_Unix(content, (e) => {
+      if (e) {
+        callback(e)
+      } else {
+        applyAfter_Unix(sudo_pswd, callback)
+      }
+    })
   } else {
-    apply_Win32(...args)
+    // win32
+    apply_Win32(content, callback)
   }
 }
 
@@ -88,18 +116,52 @@ module.exports = (cnt, pswd) => {
   if (pswd) {
     sudo_pswd = pswd
   }
+  let pref
 
-  return new Promise((resolve, reject) => {
-    let file_md5 = md5File.sync(sys_host_path)
-    let cnt_md5 = crypto.createHash('md5').update(cnt).digest('hex')
+  return Promise.resolve()
+    .then(() => {
+      return getPref()
+        .then(p => {
+          pref = p
+          return p.user_language || 'en'
+        })
+        .then(l => {
+          return getLang(svr, l)
+        })
+        .then(v => lang = v || {})
+    })
+    .then(() => {
+      return new Promise((resolve, reject) => {
+        let file_md5 = md5File.sync(sys_host_path)
+        let cnt_md5 = crypto.createHash('md5').update(cnt).digest('hex')
 
-    if (file_md5 === cnt_md5) {
-      // 文件相同
-      resolve()
-      return
-    }
+        if (file_md5 === cnt_md5) {
+          // 文件相同
+          resolve()
+          return
+        }
 
-    tryToApply(cnt, e => e ? reject(e) : resolve())
-    //reject('need_sudo')
-  })
+        tryToApply(cnt, e => e ? reject(e) : resolve())
+        //reject('need_sudo')
+      })
+    })
+    .then(() => {
+      return new Promise((resolve, reject) => {
+        let after_cmd = pref.after_cmd
+
+        if (after_cmd) {
+          exec(after_cmd, (error, stdout, stderr) => {
+            // command output is in stdout
+            if (error) {
+              reject(`AfterCmdError:\n\n${stderr}`)
+            } else {
+              resolve()
+            }
+          })
+
+        } else {
+          resolve()
+        }
+      })
+    })
 }

+ 51 - 0
app/server/applyAfter_Unix.js

@@ -0,0 +1,51 @@
+/**
+ * @author oldj
+ * @blog http://oldj.net
+ */
+
+'use strict';
+
+const fs = require('fs')
+const path = require('path')
+const {work_path} = require('./paths')
+const exec = require('child_process').exec
+const platform = process.platform
+
+function forMac(sudo_pswd, callback) {
+  let cmd_fn = path.join(work_path, '_restart_mDNSResponder.sh');
+  let cmd = `
+p1=/System/Library/LaunchDaemons/com.apple.mDNSResponder.plist
+if [ -f $p1 ]; then
+    echo '${sudo_pswd}' | sudo -S launchctl unload -w $p1
+    echo '${sudo_pswd}' | sudo -S launchctl load -w $p1
+fi
+
+p2=/System/Library/LaunchDaemons/com.apple.discoveryd.plist
+if [ -f p2 ]; then
+    echo '${sudo_pswd}' | sudo -S launchctl unload -w $p2
+    echo '${sudo_pswd}' | sudo -S launchctl load -w $p2
+fi
+
+echo '${sudo_pswd}' | sudo -S killall -HUP mDNSResponder
+`;
+
+  fs.writeFileSync(cmd_fn, cmd, 'utf-8');
+
+  exec(`/bin/sh ${cmd_fn}`, function (error, stdout, stderr) {
+    // command output is in stdout
+    if (error) {
+      console.log(error);
+    }
+    console.log(stdout, stderr);
+
+    callback();
+  });
+}
+
+module.exports = (sudo_pswd, callback) => {
+  if (sudo_pswd && platform === 'darwin') {
+    forMac(sudo_pswd, callback);
+  } else {
+    callback()
+  }
+}

+ 1 - 1
app/version.js

@@ -1 +1 @@
-exports.version = [3,3,0,4556];
+exports.version = [3,3,0,4570];