/// <summary> /// Start this proxy server instance. /// </summary> public void Start() { if (ProxyRunning) { throw new Exception("Proxy is already running."); } if (ProxyEndPoints.OfType <ExplicitProxyEndPoint>().Any(x => x.GenericCertificate == null)) { CertificateManager.EnsureRootCertificate(); } // clear any system proxy settings which is pointing to our own endpoint (causing a cycle) // due to ungracious proxy shutdown before or something else if (systemProxySettingsManager != null && RunTime.IsWindows) { var proxyInfo = systemProxySettingsManager.GetProxyInfoFromRegistry(); if (proxyInfo.Proxies != null) { var protocolToRemove = ProxyProtocolType.None; foreach (var proxy in proxyInfo.Proxies.Values) { if (NetworkHelper.IsLocalIpAddress(proxy.HostName) && ProxyEndPoints.Any(x => x.Port == proxy.Port)) { protocolToRemove |= proxy.ProtocolType; } } if (protocolToRemove != ProxyProtocolType.None) { systemProxySettingsManager.RemoveProxy(protocolToRemove, false); } } } if (ForwardToUpstreamGateway && GetCustomUpStreamProxyFunc == null && systemProxySettingsManager != null) { // Use WinHttp to handle PAC/WAPD scripts. systemProxyResolver = new WinHttpWebProxyFinder(); systemProxyResolver.LoadFromIE(); GetCustomUpStreamProxyFunc = getSystemUpStreamProxy; } ProxyRunning = true; CertificateManager.ClearIdleCertificates(); foreach (var endPoint in ProxyEndPoints) { listen(endPoint); } }
/// <summary> /// Set the given explicit end point as the default proxy server for current machine. /// </summary> /// <param name="endPoint">The explicit endpoint.</param> /// <param name="protocolType">The proxy protocol type.</param> public void SetAsSystemProxy(ExplicitProxyEndPoint endPoint, ProxyProtocolType protocolType) { if (RunTime.IsRunningOnMono) { throw new Exception("Mono Runtime do not support system proxy settings."); } validateEndPointAsSystemProxy(endPoint); bool isHttp = (protocolType & ProxyProtocolType.Http) > 0; bool isHttps = (protocolType & ProxyProtocolType.Https) > 0; if (isHttps) { CertificateManager.EnsureRootCertificate(); // If certificate was trusted by the machine if (!CertificateManager.CertValidated) { protocolType = protocolType & ~ProxyProtocolType.Https; isHttps = false; } } // clear any settings previously added if (isHttp) { ProxyEndPoints.OfType <ExplicitProxyEndPoint>().ToList().ForEach(x => x.IsSystemHttpProxy = false); } if (isHttps) { ProxyEndPoints.OfType <ExplicitProxyEndPoint>().ToList().ForEach(x => x.IsSystemHttpsProxy = false); } systemProxySettingsManager.SetProxy( Equals(endPoint.IpAddress, IPAddress.Any) | Equals(endPoint.IpAddress, IPAddress.Loopback) ? "localhost" : endPoint.IpAddress.ToString(), endPoint.Port, protocolType); if (isHttp) { endPoint.IsSystemHttpProxy = true; } if (isHttps) { endPoint.IsSystemHttpsProxy = true; } 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); } }