// 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
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
}\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
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
{\r
Task.Run(() => ShutdownProxy());\r
if (_config.Proxy.Auto)\r
- _systemProxy.RestoreSettings();\r
+ RestoreSystemProxy();\r
SystemEvents.PowerModeChanged -= SystemEvents_PowerModeChanged;\r
}\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