OSDN Git Service

プロキシ設定が消えたときに自動的に再設定する
authorKazuhiro Fujieda <fujieda@users.osdn.me>
Wed, 20 Sep 2017 10:58:31 +0000 (19:58 +0900)
committerKazuhiro Fujieda <fujieda@users.osdn.me>
Wed, 20 Sep 2017 11:01:59 +0000 (20:01 +0900)
KancolleSniffer/ProxyManager.cs

index 2ba37fd..a89a832 100644 (file)
@@ -12,6 +12,9 @@
 // See the License for the specific language governing permissions and\r
 // limitations under the License.\r
 \r
 // See the License for the specific language governing permissions and\r
 // limitations under the License.\r
 \r
+using System;\r
+using System.IO;\r
+using System.Net;\r
 using System.Net.Sockets;\r
 using System.Threading;\r
 using System.Threading.Tasks;\r
 using System.Net.Sockets;\r
 using System.Threading;\r
 using System.Threading.Tasks;\r
@@ -37,7 +40,7 @@ namespace KancolleSniffer
         public bool ApplyConfig()\r
         {\r
             if (!_config.Proxy.Auto)\r
         public bool ApplyConfig()\r
         {\r
             if (!_config.Proxy.Auto)\r
-                _systemProxy.RestoreSettings();\r
+                RestoreSystemProxy();\r
             if (_config.Proxy.UseUpstream)\r
             {\r
                 HttpProxy.UpstreamProxyHost = "127.0.0.1";\r
             if (_config.Proxy.UseUpstream)\r
             {\r
                 HttpProxy.UpstreamProxyHost = "127.0.0.1";\r
@@ -52,7 +55,7 @@ namespace KancolleSniffer
             }\r
             if (_config.Proxy.Auto && result)\r
             {\r
             }\r
             if (_config.Proxy.Auto && result)\r
             {\r
-                _systemProxy.SetAutoProxyUrl($"http://localhost:{_config.Proxy.Listen}/proxy.pac");\r
+                SetAutoProxyUrl();\r
             }\r
             _prevProxyPort = _config.Proxy.Listen;\r
             return result;\r
             }\r
             _prevProxyPort = _config.Proxy.Listen;\r
             return result;\r
@@ -74,7 +77,7 @@ namespace KancolleSniffer
                 if (WarnConflictPortNumber("プロキシサーバー", _config.Proxy.Listen, _config.Proxy.Auto) == DialogResult.No ||\r
                     !_config.Proxy.Auto)\r
                 {\r
                 if (WarnConflictPortNumber("プロキシサーバー", _config.Proxy.Listen, _config.Proxy.Auto) == DialogResult.No ||\r
                     !_config.Proxy.Auto)\r
                 {\r
-                    _systemProxy.RestoreSettings();\r
+                    RestoreSystemProxy();\r
                     return false;\r
                 }\r
                 HttpProxy.Startup(0, false, false);\r
                     return false;\r
                 }\r
                 HttpProxy.Startup(0, false, false);\r
@@ -109,7 +112,7 @@ namespace KancolleSniffer
         {\r
             Task.Run(() => ShutdownProxy());\r
             if (_config.Proxy.Auto)\r
         {\r
             Task.Run(() => ShutdownProxy());\r
             if (_config.Proxy.Auto)\r
-                _systemProxy.RestoreSettings();\r
+                RestoreSystemProxy();\r
             SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;\r
         }\r
 \r
             SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;\r
         }\r
 \r
@@ -117,5 +120,40 @@ namespace KancolleSniffer
         {\r
             HttpProxy.Shutdown();\r
         }\r
         {\r
             HttpProxy.Shutdown();\r
         }\r
+\r
+        private readonly AutoResetEvent _stopEvent = new AutoResetEvent(false);\r
+        private Task _checkerTask;\r
+\r
+        private void SetAutoProxyUrl()\r
+        {\r
+            var url = $"http://localhost:{_config.Proxy.Listen}/proxy.pac";\r
+            _systemProxy.SetAutoProxyUrl(url);\r
+            if (_checkerTask != null && !_checkerTask.IsCompleted)\r
+                return;\r
+            _checkerTask = Task.Run(() =>\r
+            {\r
+                // Windows 10でプロキシ設定がいつの間にか消えるのに対応するために、\r
+                // 設定が消えていないか毎秒確認して、消えていたら再設定する。\r
+                do\r
+                {\r
+                    var proxy = WebRequest.GetSystemWebProxy().GetProxy(new Uri("http://125.6.184.16/"));\r
+                    if (!proxy.IsLoopback)\r
+                    {\r
+                        File.AppendAllText("proxy.log", $"[{DateTime.Now:g}] proxy setting vanished.\r\n");\r
+                        _systemProxy.SetAutoProxyUrl(url);\r
+                    }\r
+                } while (!_stopEvent.WaitOne(1000));\r
+            });\r
+        }\r
+\r
+        private void RestoreSystemProxy()\r
+        {\r
+            if (_checkerTask != null && !_checkerTask.IsCompleted)\r
+            {\r
+                _stopEvent.Set();\r
+                _checkerTask.Wait();\r
+            }\r
+            _systemProxy.RestoreSettings();\r
+        }\r
     }\r
 }
\ No newline at end of file
     }\r
 }
\ No newline at end of file