From 1a90dd1915662b6686f4d673f076e3c3018e8c63 Mon Sep 17 00:00:00 2001 From: Kazuhiro Fujieda Date: Wed, 20 Sep 2017 19:58:31 +0900 Subject: [PATCH] =?utf8?q?=E3=83=97=E3=83=AD=E3=82=AD=E3=82=B7=E8=A8=AD?= =?utf8?q?=E5=AE=9A=E3=81=8C=E6=B6=88=E3=81=88=E3=81=9F=E3=81=A8=E3=81=8D?= =?utf8?q?=E3=81=AB=E8=87=AA=E5=8B=95=E7=9A=84=E3=81=AB=E5=86=8D=E8=A8=AD?= =?utf8?q?=E5=AE=9A=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- KancolleSniffer/ProxyManager.cs | 46 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/KancolleSniffer/ProxyManager.cs b/KancolleSniffer/ProxyManager.cs index 2ba37fd..a89a832 100644 --- a/KancolleSniffer/ProxyManager.cs +++ b/KancolleSniffer/ProxyManager.cs @@ -12,6 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. +using System; +using System.IO; +using System.Net; using System.Net.Sockets; using System.Threading; using System.Threading.Tasks; @@ -37,7 +40,7 @@ namespace KancolleSniffer public bool ApplyConfig() { if (!_config.Proxy.Auto) - _systemProxy.RestoreSettings(); + RestoreSystemProxy(); if (_config.Proxy.UseUpstream) { HttpProxy.UpstreamProxyHost = "127.0.0.1"; @@ -52,7 +55,7 @@ namespace KancolleSniffer } if (_config.Proxy.Auto && result) { - _systemProxy.SetAutoProxyUrl($"http://localhost:{_config.Proxy.Listen}/proxy.pac"); + SetAutoProxyUrl(); } _prevProxyPort = _config.Proxy.Listen; return result; @@ -74,7 +77,7 @@ namespace KancolleSniffer if (WarnConflictPortNumber("プロキシサーバー", _config.Proxy.Listen, _config.Proxy.Auto) == DialogResult.No || !_config.Proxy.Auto) { - _systemProxy.RestoreSettings(); + RestoreSystemProxy(); return false; } HttpProxy.Startup(0, false, false); @@ -109,7 +112,7 @@ namespace KancolleSniffer { Task.Run(() => ShutdownProxy()); if (_config.Proxy.Auto) - _systemProxy.RestoreSettings(); + RestoreSystemProxy(); SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged; } @@ -117,5 +120,40 @@ namespace KancolleSniffer { HttpProxy.Shutdown(); } + + private readonly AutoResetEvent _stopEvent = new AutoResetEvent(false); + private Task _checkerTask; + + private void SetAutoProxyUrl() + { + var url = $"http://localhost:{_config.Proxy.Listen}/proxy.pac"; + _systemProxy.SetAutoProxyUrl(url); + if (_checkerTask != null && !_checkerTask.IsCompleted) + return; + _checkerTask = Task.Run(() => + { + // Windows 10でプロキシ設定がいつの間にか消えるのに対応するために、 + // 設定が消えていないか毎秒確認して、消えていたら再設定する。 + do + { + var proxy = WebRequest.GetSystemWebProxy().GetProxy(new Uri("http://125.6.184.16/")); + if (!proxy.IsLoopback) + { + File.AppendAllText("proxy.log", $"[{DateTime.Now:g}] proxy setting vanished.\r\n"); + _systemProxy.SetAutoProxyUrl(url); + } + } while (!_stopEvent.WaitOne(1000)); + }); + } + + private void RestoreSystemProxy() + { + if (_checkerTask != null && !_checkerTask.IsCompleted) + { + _stopEvent.Set(); + _checkerTask.Wait(); + } + _systemProxy.RestoreSettings(); + } } } \ No newline at end of file -- 2.11.0