Browse Source

hosts 更新后,重启网络连接,修复 Chrome 等浏览器中 HTTP 长链接没有断开导致 hosts 没有更新的问题。

oldj 10 years ago
parent
commit
d486ebc5e8

+ 1 - 1
app/SH3/MacGap/SwitchHosts!-Info.plist

@@ -21,7 +21,7 @@
 	<key>CFBundleSignature</key>
 	<string>????</string>
 	<key>CFBundleVersion</key>
-	<string>2834</string>
+	<string>2882</string>
 	<key>LSApplicationCategoryType</key>
 	<string>public.app-category.developer-tools</string>
 	<key>LSMinimumSystemVersion</key>

+ 4 - 1
app/SH3/public/css/style.css

@@ -262,9 +262,12 @@ a:hover {
   color: #090;
 }
 .cm-s-default .cm-ip {
-  color: #00c;
+  color: #00a;
   font-weight: bold;
 }
+.cm-s-default .cm-hl {
+  background: #ff0;
+}
 .CodeMirror-gutters {
   border-right: none;
   padding-right: 6px;

+ 3 - 1
app/SH3/public/css/style.styl

@@ -287,8 +287,10 @@ unselectable()
 .cm-s-default .cm-comment
   color #090
 .cm-s-default .cm-ip
-  color #00c
+  color #00a
   font-weight bold
+.cm-s-default .cm-hl
+  background #ff0
 .CodeMirror-gutters
   border-right none
   padding-right 6px

File diff suppressed because it is too large
+ 0 - 0
app/SH3/public/js/main.js


+ 61 - 3
app/src/agent.js

@@ -78,6 +78,7 @@ function setSysHosts(val, sudo_pswd, callback) {
     writeFile(tmp_f, val);
 
     var cmd;
+    //var cmd2;
     if (!sudo_pswd) {
         cmd = [
             'cat "' + tmp_f + '" > ' + sys_host_path
@@ -91,18 +92,30 @@ function setSysHosts(val, sudo_pswd, callback) {
             , '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 myTask = MacGap.Task.create('/bin/sh', function (result) {
+    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);
         }
     });
-    myTask['arguments'] = ['-c', cmd];
-    myTask.launch();
+    task['arguments'] = ['-c', cmd];
+    task.launch();
 }
 
 function getData(config) {
@@ -216,6 +229,51 @@ function notify(type, title, 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'
+    ].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();
+}
+
 module.exports = {
     readFile: readFile,
     writeFile: writeFile,

+ 6 - 3
app/src/tt/hl.html

@@ -19,7 +19,8 @@ bbb #bbb
 127.0.0.1 localhost
 255.255.255.255 broadcasthost
 ::1 localhost
-fe80::1%lo0 localhost
+fe80::1%lo0 localhost k1
+	k2
 
 </textarea>
 
@@ -76,11 +77,13 @@ fe80::1%lo0 localhost
 	})
 	;
 
-	my_codemirror = CodeMirror.fromTextArea(document.getElementById('t'), {
+	cm = CodeMirror.fromTextArea(document.getElementById('t'), {
 		lineNumbers: true,
-		readOnly: true,
+//		readOnly: true,
 		mode: 'host'
 	});
+
+	cm.setOption("hintOptions", { "keywords" : ["k1", "k2"] });
 </script>
 </body>
 </html>

+ 0 - 1
app/src/tt/hl.js

@@ -29,7 +29,6 @@
             if (!stream.string.match(/^\s*([\d\.]+|[\da-f:\.%lo]+)\s+\w/i)) {
                 return 'error';
             }
-
             if (sol && ch.match(/[\w\.:%]/)) {
                 stream.eatWhile(/[\w\.:%]/);
                 return 'ip';

Some files were not shown because too many files changed in this diff