From c1ef32b42024ccd790bb98791ac447e0aa0a31a6 Mon Sep 17 00:00:00 2001
From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com>
Date: Sat, 4 Jul 2020 02:18:35 +0800
Subject: [PATCH] restore previous proxy settings after stop http proxy
---
Netch/Controllers/HTTPController.cs | 36 +++++++++++++++++++++++++++--
1 file changed, 34 insertions(+), 2 deletions(-)
diff --git a/Netch/Controllers/HTTPController.cs b/Netch/Controllers/HTTPController.cs
index 1332514b..2912be8e 100644
--- a/Netch/Controllers/HTTPController.cs
+++ b/Netch/Controllers/HTTPController.cs
@@ -1,12 +1,15 @@
using Netch.Utils;
using System;
-using System.Windows;
using System.Windows.Forms;
+using Microsoft.Win32;
namespace Netch.Controllers
{
public class HTTPController
{
+ private bool prevEnabled;
+ private string prevBypass, prevHTTP, prevPAC="";
+
///
/// 实例
///
@@ -38,6 +41,8 @@ namespace Netch.Controllers
if (mode.Type != 5)
{
+ RecordPrevious();
+
NativeMethods.SetGlobal($"127.0.0.1:{Global.Settings.HTTPLocalPort}", "");
// HTTP 系统代理模式,启动系统代理
@@ -66,6 +71,27 @@ namespace Netch.Controllers
return true;
}
+ private void RecordPrevious()
+ {
+ var registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings");
+ if (registry == null)
+ {
+ prevEnabled = false;
+ prevHTTP = prevBypass = "";
+ return;
+ }
+ if (registry.GetValue("AutoConfigURL") != null)
+ {
+ prevPAC = registry.GetValue("AutoConfigURL").ToString() ?? "";
+ }
+ if ((registry.GetValue("ProxyEnable")?.Equals(1) ?? false) || prevPAC != "")
+ {
+ prevEnabled = true;
+ }
+ prevHTTP = registry.GetValue("ProxyServer").ToString();
+ prevBypass = registry.GetValue("ProxyOverride").ToString();
+ }
+
///
/// 停止
///
@@ -82,7 +108,13 @@ namespace Netch.Controllers
Utils.Logging.Info(e.ToString());
}
- NativeMethods.SetDIRECT();
+ NativeMethods.SetGlobal(prevHTTP, prevBypass);
+ if (prevPAC != "")
+ NativeMethods.SetURL(prevPAC);
+ if (!prevEnabled)
+ NativeMethods.SetDIRECT();
+ prevEnabled = false;
+
/*
using (var registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true))