Commit fc8576bf authored by Honfika's avatar Honfika

Set HTTP and HTTPS proxy in one step.

parent d42864ec
...@@ -4,6 +4,7 @@ using System.Net; ...@@ -4,6 +4,7 @@ using System.Net;
using System.Net.Security; using System.Net.Security;
using System.Threading.Tasks; using System.Threading.Tasks;
using Titanium.Web.Proxy.EventArguments; using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Helpers;
using Titanium.Web.Proxy.Models; using Titanium.Web.Proxy.Models;
namespace Titanium.Web.Proxy.Examples.Basic namespace Titanium.Web.Proxy.Examples.Basic
...@@ -92,8 +93,9 @@ namespace Titanium.Web.Proxy.Examples.Basic ...@@ -92,8 +93,9 @@ namespace Titanium.Web.Proxy.Examples.Basic
endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port); endPoint.GetType().Name, endPoint.IpAddress, endPoint.Port);
//Only explicit proxies can be set as system proxy! //Only explicit proxies can be set as system proxy!
proxyServer.SetAsSystemHttpProxy(explicitEndPoint); //proxyServer.SetAsSystemHttpProxy(explicitEndPoint);
proxyServer.SetAsSystemHttpsProxy(explicitEndPoint); //proxyServer.SetAsSystemHttpsProxy(explicitEndPoint);
proxyServer.SetAsSystemProxy(explicitEndPoint, ProxyProtocolType.AllHttp);
} }
public void Stop() public void Stop()
......
...@@ -8,10 +8,28 @@ using Microsoft.Win32; ...@@ -8,10 +8,28 @@ using Microsoft.Win32;
// Helper classes for setting system proxy settings // Helper classes for setting system proxy settings
namespace Titanium.Web.Proxy.Helpers namespace Titanium.Web.Proxy.Helpers
{ {
internal enum ProxyProtocolType [Flags]
public enum ProxyProtocolType
{ {
Http, /// <summary>
Https, /// The none
/// </summary>
None = 0,
/// <summary>
/// HTTP
/// </summary>
Http = 1,
/// <summary>
/// HTTPS
/// </summary>
Https = 2,
/// <summary>
/// Both HTTP and HTTPS
/// </summary>
AllHttp = Http | Https,
} }
internal partial class NativeMethods internal partial class NativeMethods
...@@ -25,11 +43,23 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -25,11 +43,23 @@ namespace Titanium.Web.Proxy.Helpers
{ {
internal string HostName { get; set; } internal string HostName { get; set; }
internal int Port { get; set; } internal int Port { get; set; }
internal bool IsHttps { get; set; } internal ProxyProtocolType ProtocolType { get; set; }
public override string ToString() public override string ToString()
{ {
return $"{(IsHttps ? "https" : "http")}={HostName}:{Port}"; string protocol = null;
switch (ProtocolType)
{
case ProxyProtocolType.Http:
protocol = "http";
break;
case ProxyProtocolType.Https:
protocol = "https";
break;
default:
throw new Exception("Unsupported protocol type");
}
return $"{protocol}={HostName}:{Port}";
} }
} }
...@@ -72,7 +102,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -72,7 +102,7 @@ namespace Titanium.Web.Proxy.Helpers
RemoveProxy(ProxyProtocolType.Https); RemoveProxy(ProxyProtocolType.Https);
} }
private void SetProxy(string hostname, int port, ProxyProtocolType protocolType) internal void SetProxy(string hostname, int port, ProxyProtocolType protocolType)
{ {
var reg = Registry.CurrentUser.OpenSubKey( var reg = Registry.CurrentUser.OpenSubKey(
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true); "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
...@@ -83,13 +113,26 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -83,13 +113,26 @@ namespace Titanium.Web.Proxy.Helpers
var exisitingContent = reg.GetValue("ProxyServer") as string; var exisitingContent = reg.GetValue("ProxyServer") as string;
var existingSystemProxyValues = GetSystemProxyValues(exisitingContent); var existingSystemProxyValues = GetSystemProxyValues(exisitingContent);
existingSystemProxyValues.RemoveAll(x => protocolType == ProxyProtocolType.Https ? x.IsHttps : !x.IsHttps); existingSystemProxyValues.RemoveAll(x => (protocolType & x.ProtocolType) != 0);
existingSystemProxyValues.Add(new HttpSystemProxyValue if ((protocolType & ProxyProtocolType.Http) != 0)
{
existingSystemProxyValues.Add(new HttpSystemProxyValue
{
HostName = hostname,
ProtocolType = ProxyProtocolType.Http,
Port = port
});
}
if ((protocolType & ProxyProtocolType.Https) != 0)
{ {
HostName = hostname, existingSystemProxyValues.Add(new HttpSystemProxyValue
IsHttps = protocolType == ProxyProtocolType.Https, {
Port = port HostName = hostname,
}); ProtocolType = ProxyProtocolType.Https,
Port = port
});
}
reg.SetValue("ProxyEnable", 1); reg.SetValue("ProxyEnable", 1);
reg.SetValue("ProxyServer", string.Join(";", existingSystemProxyValues.Select(x => x.ToString()).ToArray())); reg.SetValue("ProxyServer", string.Join(";", existingSystemProxyValues.Select(x => x.ToString()).ToArray()));
...@@ -98,7 +141,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -98,7 +141,7 @@ namespace Titanium.Web.Proxy.Helpers
Refresh(); Refresh();
} }
private void RemoveProxy(ProxyProtocolType protocolType) internal void RemoveProxy(ProxyProtocolType protocolType)
{ {
var reg = Registry.CurrentUser.OpenSubKey( var reg = Registry.CurrentUser.OpenSubKey(
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true); "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
...@@ -107,7 +150,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -107,7 +150,7 @@ namespace Titanium.Web.Proxy.Helpers
var exisitingContent = reg.GetValue("ProxyServer") as string; var exisitingContent = reg.GetValue("ProxyServer") as string;
var existingSystemProxyValues = GetSystemProxyValues(exisitingContent); var existingSystemProxyValues = GetSystemProxyValues(exisitingContent);
existingSystemProxyValues.RemoveAll(x => protocolType == ProxyProtocolType.Https ? x.IsHttps : !x.IsHttps); existingSystemProxyValues.RemoveAll(x => (protocolType | x.ProtocolType) != 0);
if (existingSystemProxyValues.Count != 0) if (existingSystemProxyValues.Count != 0)
{ {
...@@ -176,17 +219,32 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -176,17 +219,32 @@ namespace Titanium.Web.Proxy.Helpers
/// <returns></returns> /// <returns></returns>
private HttpSystemProxyValue ParseProxyValue(string value) private HttpSystemProxyValue ParseProxyValue(string value)
{ {
var tmp = Regex.Replace(value, @"\s+", " ").Trim().ToLower(); var tmp = Regex.Replace(value, @"\s+", " ").Trim();
if (tmp.StartsWith("http=") || tmp.StartsWith("https=")) int equalsIndex = tmp.IndexOf("=", StringComparison.InvariantCulture);
if (equalsIndex >= 0)
{ {
var endPoint = tmp.Substring(5); string protocolTypeStr = tmp.Substring(0, equalsIndex);
return new HttpSystemProxyValue ProxyProtocolType? protocolType = null;
if (protocolTypeStr.Equals("http", StringComparison.InvariantCultureIgnoreCase))
{ {
HostName = endPoint.Split(':')[0], protocolType = ProxyProtocolType.Http;
Port = int.Parse(endPoint.Split(':')[1]), }
IsHttps = tmp.StartsWith("https=") else if (protocolTypeStr.Equals("https", StringComparison.InvariantCultureIgnoreCase))
}; {
protocolType = ProxyProtocolType.Https;
}
if (protocolType.HasValue)
{
var endPointParts = tmp.Substring(equalsIndex + 1).Split(':');
return new HttpSystemProxyValue
{
HostName = endPointParts[0],
Port = int.Parse(endPointParts[1]),
ProtocolType = protocolType.Value,
};
}
} }
return null; return null;
......
...@@ -348,24 +348,7 @@ namespace Titanium.Web.Proxy ...@@ -348,24 +348,7 @@ namespace Titanium.Web.Proxy
/// <param name="endPoint"></param> /// <param name="endPoint"></param>
public void SetAsSystemHttpProxy(ExplicitProxyEndPoint endPoint) public void SetAsSystemHttpProxy(ExplicitProxyEndPoint endPoint)
{ {
if (RunTime.IsRunningOnMono) SetAsSystemProxy(endPoint, ProxyProtocolType.Http);
{
throw new Exception("Mono Runtime do not support system proxy settings.");
}
ValidateEndPointAsSystemProxy(endPoint);
//clear any settings previously added
ProxyEndPoints.OfType<ExplicitProxyEndPoint>().ToList().ForEach(x => x.IsSystemHttpProxy = false);
systemProxySettingsManager.SetHttpProxy(
Equals(endPoint.IpAddress, IPAddress.Any) | Equals(endPoint.IpAddress, IPAddress.Loopback) ? "127.0.0.1" : endPoint.IpAddress.ToString(), endPoint.Port);
endPoint.IsSystemHttpProxy = true;
firefoxProxySettingsManager.UseSystemProxy();
Console.WriteLine("Set endpoint at Ip {0} and port: {1} as System HTTP Proxy", endPoint.IpAddress, endPoint.Port);
} }
...@@ -374,6 +357,16 @@ namespace Titanium.Web.Proxy ...@@ -374,6 +357,16 @@ namespace Titanium.Web.Proxy
/// </summary> /// </summary>
/// <param name="endPoint"></param> /// <param name="endPoint"></param>
public void SetAsSystemHttpsProxy(ExplicitProxyEndPoint endPoint) public void SetAsSystemHttpsProxy(ExplicitProxyEndPoint endPoint)
{
SetAsSystemProxy(endPoint, ProxyProtocolType.Https);
}
/// <summary>
/// Set the given explicit end point as the default proxy server for current machine
/// </summary>
/// <param name="endPoint"></param>
/// <param name="protocolType"></param>
public void SetAsSystemProxy(ExplicitProxyEndPoint endPoint, ProxyProtocolType protocolType)
{ {
if (RunTime.IsRunningOnMono) if (RunTime.IsRunningOnMono)
{ {
...@@ -382,33 +375,73 @@ namespace Titanium.Web.Proxy ...@@ -382,33 +375,73 @@ namespace Titanium.Web.Proxy
ValidateEndPointAsSystemProxy(endPoint); ValidateEndPointAsSystemProxy(endPoint);
if (!endPoint.EnableSsl) bool isHttp = (protocolType & ProxyProtocolType.Http) > 0;
bool isHttps = (protocolType & ProxyProtocolType.Https) > 0;
if (isHttps)
{ {
throw new Exception("Endpoint do not support Https connections"); if (!endPoint.EnableSsl)
{
throw new Exception("Endpoint do not support Https connections");
}
EnsureRootCertificate();
//If certificate was trusted by the machine
if (!CertificateManager.CertValidated)
{
protocolType = protocolType & ~ProxyProtocolType.Https;
isHttps = false;
}
} }
//clear any settings previously added //clear any settings previously added
ProxyEndPoints.OfType<ExplicitProxyEndPoint>() if (isHttp)
.ToList() {
.ForEach(x => x.IsSystemHttpsProxy = false); ProxyEndPoints.OfType<ExplicitProxyEndPoint>().ToList().ForEach(x => x.IsSystemHttpProxy = false);
}
EnsureRootCertificate();
//If certificate was trusted by the machine if (isHttps)
if (CertificateManager.CertValidated)
{ {
systemProxySettingsManager.SetHttpsProxy( ProxyEndPoints.OfType<ExplicitProxyEndPoint>().ToList().ForEach(x => x.IsSystemHttpsProxy = false);
Equals(endPoint.IpAddress, IPAddress.Any) |
Equals(endPoint.IpAddress, IPAddress.Loopback) ? "127.0.0.1" : endPoint.IpAddress.ToString(),
endPoint.Port);
} }
systemProxySettingsManager.SetProxy(
Equals(endPoint.IpAddress, IPAddress.Any) |
Equals(endPoint.IpAddress, IPAddress.Loopback) ? "127.0.0.1" : endPoint.IpAddress.ToString(),
endPoint.Port,
protocolType);
endPoint.IsSystemHttpsProxy = true; if (isHttp)
{
endPoint.IsSystemHttpsProxy = true;
}
if (isHttps)
{
endPoint.IsSystemHttpsProxy = true;
}
firefoxProxySettingsManager.UseSystemProxy(); firefoxProxySettingsManager.UseSystemProxy();
Console.WriteLine("Set endpoint at Ip {0} and port: {1} as System HTTPS Proxy", endPoint.IpAddress, endPoint.Port); string proxyType = null;
switch (protocolType)
{
case ProxyProtocolType.Http:
proxyType = "HTTP";
break;
case ProxyProtocolType.Https:
proxyType = "HTTPS";
break;
case ProxyProtocolType.AllHttp:
proxyType = "HTTP and HTTPS";
break;
}
if (protocolType != ProxyProtocolType.None)
{
Console.WriteLine("Set endpoint at Ip {0} and port: {1} as System {2} Proxy", endPoint.IpAddress, endPoint.Port, proxyType);
}
} }
/// <summary> /// <summary>
...@@ -437,6 +470,19 @@ namespace Titanium.Web.Proxy ...@@ -437,6 +470,19 @@ namespace Titanium.Web.Proxy
systemProxySettingsManager.RemoveHttpsProxy(); systemProxySettingsManager.RemoveHttpsProxy();
} }
/// <summary>
/// Remove the specified proxy settings for current machine
/// </summary>
public void DisableSystemProxy(ProxyProtocolType protocolType)
{
if (RunTime.IsRunningOnMono)
{
throw new Exception("Mono Runtime do not support system proxy settings.");
}
systemProxySettingsManager.RemoveProxy(protocolType);
}
/// <summary> /// <summary>
/// Clear all proxy settings for current machine /// Clear all proxy settings for current machine
/// </summary> /// </summary>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment