Example #1
0
        private static string GetSingleIssuer(string url)
        {
            string issuer = null;

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                                                   | SecurityProtocolType.Tls11
                                                   | SecurityProtocolType.Tls12
                                                   | SecurityProtocolType.Ssl3;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://" + url);

            IWebProxy proxy = request.Proxy;

            if (proxy.GetProxy(request.RequestUri).ToString().Substring(0, 8) != "https://")
            {
                string proxyuri = proxy.GetProxy(request.RequestUri).ToString();
                request.UseDefaultCredentials = true;
                request.Proxy             = new WebProxy(proxyuri, false);
                request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
            }

            request.UseDefaultCredentials = true;
            request.UserAgent             = userAgent;
            request.Timeout = 5000;
            X509Certificate cert2    = null;
            HttpWebResponse response = null;

            try
            {
                response = (HttpWebResponse)request.GetResponse();
                X509Certificate cert = request.ServicePoint.Certificate;
                cert2 = new X509Certificate2(cert);
            }
            catch (Exception e)
            {
                if (verbose)
                {
                    Console.WriteLine("[-] Failed to connect to {0}", url);
                    return(null);
                }
            }

            if (cert2 != null)
            {
                issuer = cert2.Issuer;
            }

            return(issuer);
        }
Example #2
0
        private void FillLabels()
        {
            // ISSUE: object of a compiler-generated type is created
            // ISSUE: variable of a compiler-generated type
            Settings settings = new Settings();

            this.lblExportDaysWithoutTrade.Left  += this.lblExportDaysWithoutTrade.Width;
            this.lblExportDaysWithoutTrade.Text   = settings.ExportDaysWithoutTrade ? "بلی" : "خیر";
            this.lblExportDaysWithoutTrade.Left  -= this.lblExportDaysWithoutTrade.Width;
            this.lblStorageLocation.Left         += this.lblStorageLocation.Width;
            this.lblStorageLocation.Text          = settings.StorageLocation;
            this.lblStorageLocation.Left         -= this.lblStorageLocation.Width;
            this.lblAdjustedStorageLocation.Left += this.lblAdjustedStorageLocation.Width;
            this.lblAdjustedStorageLocation.Text  = settings.AdjustedStorageLocation;
            this.lblAdjustedStorageLocation.Left -= this.lblAdjustedStorageLocation.Width;
            this.lblFileType.Left += this.lblFileType.Width;
            this.lblFileType.Text  = settings.ExcelOutput ? "Excel" : "CSV";
            this.lblFileType.Left -= this.lblFileType.Width;
            this.lblProxy.Left    += this.lblProxy.Width;
            try {
                IWebProxy defaultWebProxy = WebRequest.DefaultWebProxy;
                string    str1            = defaultWebProxy.GetProxy(new Uri("http://www.tsetmc.com")).Host.ToString();
                string    str2            = defaultWebProxy.GetProxy(new Uri("http://www.tsetmc.com")).Port.ToString();
                if (str1.Equals("www.tsetmc.com") && str2.Equals("80"))
                {
                    this.lblProxy.Text = "ندارد";
                }
                else
                {
                    this.lblProxy.Text = "دارد";
                }
            } catch {
                this.lblProxy.Text = "خطا در دریافت اطلاعات پراکسی";
            }
            this.lblProxy.Left          -= this.lblProxy.Width;
            this.txtSelectedColumns.Text = "";
            foreach (ColumnInfo columnInfo in StaticData.ColumnsInfo)
            {
                if (columnInfo.Visible)
                {
                    TextBox txtSelectedColumns = this.txtSelectedColumns;
                    txtSelectedColumns.Text = txtSelectedColumns.Text + FormFillMethods.GetColumnHeader(columnInfo.Type) + ",";
                }
            }
            this.txtSelectedColumns.Text = this.txtSelectedColumns.Text.Substring(0, this.txtSelectedColumns.Text.Length - 1);
            this.groupBoxContainer.Text  = "نمایش تنظیمات";
            this.lblWait.Visible         = false;
        }
        public void GetProxy_AllValuesSpecified_ReturnsProxyWithCredentials()
        {
            var    mockSettings  = new Mock <ISettings>();
            string proxyServer   = "http://proxyValue/";
            string proxyUser     = "******";
            string proxyPassword = "******";

            mockSettings.Setup(s => s.TryGetValue(It.Is <string>(v => v.Equals("http_proxy")), out proxyServer))
            .Returns(true);
            mockSettings.Setup(s => s.TryGetValue(It.Is <string>(v => v.Equals("http_proxy.user")), out proxyUser))
            .Returns(true);
            mockSettings.Setup(s => s.TryGetEncryptedValue(It.IsAny <string>(), out proxyPassword))
            .Returns(true);

            var       ut     = new ProxySettings(mockSettings.Object);
            IWebProxy result = ut.GetProxy(new Uri("http://test"));

            Assert.IsNotNull(result);
            Assert.AreEqual(new Uri("http://proxyValue/"), result.GetProxy(new Uri("http://test")));
            Assert.IsNotNull(result.Credentials);
            NetworkCredential cred = result.Credentials.GetCredential(new Uri("http://test"), "");

            Assert.AreEqual(proxyUser, cred.UserName);
            Assert.AreEqual(proxyPassword, cred.Password);
        }
Example #4
0
        public void WebProxy()
        {
            IWebProxy proxy = PlatformCFNetwork.GetDefaultProxy();

            Assert.True(proxy.IsBypassed(uri), "IsBypassed");
            Assert.That(proxy.GetProxy(uri), Is.SameAs(uri), "GetProxy");
        }
Example #5
0
        /// <summary>
        /// 比较代理是否相等
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        private static bool IsProxyEquals(IWebProxy x, IWebProxy y)
        {
            if (x == null && y == null)
            {
                return(true);
            }

            if (x == null || y == null)
            {
                return(false);
            }

            if (x.GetProxy(destination) != y.GetProxy(destination))
            {
                return(false);
            }

            if (x.Credentials == null && y.Credentials == null)
            {
                return(true);
            }

            if (x.Credentials == null || y.Credentials == null)
            {
                return(false);
            }
            return(true);
        }
Example #6
0
        protected virtual IActualProxy GetSystemActualProxy(SystemSettings systemSettings)
        {
            // argument checks
            Debug.Assert(systemSettings != null);

            // detect the system web proxy by try to give external urls
            // Note this implementation simply detect a possible typical proxy.
            IWebProxy systemProxy = WebRequest.GetSystemWebProxy();
            Func <string, IActualProxy> detect = (sampleExternalUrl) => {
                Uri          sampleUri = new Uri(sampleExternalUrl);
                IActualProxy value     = null;
                if (systemProxy.IsBypassed(sampleUri) == false)
                {
                    Uri uri = systemProxy.GetProxy(sampleUri);
                    if (uri != sampleUri)
                    {
                        // uri seems to be a proxy
                        value = new StaticActualProxy(new DnsEndPoint(uri.Host, uri.Port));
                    }
                }
                return(value);
            };

            // try with google's URL
            IActualProxy actualProxy = detect("http://www.google.com/");

            if (actualProxy == null)
            {
                // try with Microsoft's URL
                actualProxy = detect("http://www.microsoft.com/");
            }

            return(actualProxy);            // note that it may be null
        }
Example #7
0
        static WebProxy GetSystemProxyUri(string host)
        {
            if (_networkConfiguration.ProxyClass == ProxyType.Socks5)
            {
                if (PolipoSocks5ToHttpProxyWrapper.Instance.IsRunning)
                {
                    return(new WebProxy(PolipoSocks5ToHttpProxyWrapper.Instance.LocalUri));
                }
                return(null);
            }

            WebProxy result;

            if (!_systemCachedProxy.TryGetValue(host, out result))
            {
                lock (_systemCachedProxy)
                {
                    var uri      = new Uri("https://" + host + "/");
                    var proxyuri = _systemProxy.GetProxy(uri);
                    if (proxyuri == uri)
                    {
                        proxyuri = null;
                    }

                    result = (proxyuri == null ? null : new WebProxy(proxyuri));
                    if (!_systemCachedProxy.ContainsKey(host))
                    {
                        _systemCachedProxy.Add(host, result);
                    }
                }
            }

            return(result);
        }
Example #8
0
        private IWebProxy GetProxy(string uri)
        {
            if (_proxy != null)
            {
                return(_proxy);
            }

            if (_globalOptions.ProxyUseSystem || _globalOptions.ProxyAddress.Length == 0)
            {
                UseSystemProxy();
            }
            else
            {
                try
                {
                    _proxy             = new WebProxy(_globalOptions.ProxyAddress);
                    _proxy.Credentials = new NetworkCredential(
                        _globalOptions.ProxyUser,
                        _globalOptions.ProxySecurePassword.ConvertToUnsecureString());
                }
                catch (Exception ex)
                {
                    _eventAggregator.PublishOnUIThread(new OutputMessage(MessageType.Error, "Error connecting to HTTP Proxy specified in File > Options: " + ex.Message));
                    Log.Error("{class} {method} {message} {stacktrace}", "WebRequestFactory", "GetProxy", ex.Message, ex.StackTrace);
                    UseSystemProxy();
                }
            }

            Log.Verbose("Proxy: {proxyAddress}", _proxy.GetProxy(new Uri(uri)).AbsolutePath);
            return(_proxy);
        }
Example #9
0
        private static GeoResponse GetGeoResponse(string url)
        {
            GeoResponse res = null;

            try
            {
                HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
                request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
                request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(GeoResponse));
                IWebProxy proxy = request.Proxy;
                if (proxy != null)
                {
                    string proxyuri = proxy.GetProxy(request.RequestUri).ToString();
                    request.UseDefaultCredentials = true;
                    request.Proxy             = new WebProxy(proxyuri, false);
                    request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
                }
                Stream stream = request.GetResponse().GetResponseStream();
                res = (GeoResponse)serializer.ReadObject(stream);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to contact http://maps.googleapis.com error was : " + ex.Message, "FTAnalyzer");
            }
            return(res);
        }
        public static Uri GetCurrentConfig(string url)
        {
            IWebProxy proxy    = WebRequest.GetSystemWebProxy();
            Uri       uriProxy = proxy.GetProxy(new Uri(url));

            return(uriProxy);
        }
        public void GetProxy_AllValuesSpecified_ReturnsProxyWithCredentials()
        {
            var    mockSettings = new Mock <ISettings>();
            string proxyServer  = "http://proxyValue/";
            string proxyUser    = "******";
            // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="test value; not a real secret")]
            string proxyPassword = "******";

            mockSettings.Setup(s => s.TryGetValue(It.Is <string>(v => v.Equals("http_proxy")), out proxyServer))
            .Returns(true);
            mockSettings.Setup(s => s.TryGetValue(It.Is <string>(v => v.Equals("http_proxy.user")), out proxyUser))
            .Returns(true);
            mockSettings.Setup(s => s.TryGetEncryptedValue(It.IsAny <string>(), out proxyPassword))
            .Returns(true);

            var       ut     = new ProxySettings(mockSettings.Object);
            IWebProxy result = ut.GetProxy(new Uri("http://test"));

            Assert.IsNotNull(result);
            Assert.AreEqual(new Uri("http://proxyValue/"), result.GetProxy(new Uri("http://test")));
            Assert.IsNotNull(result.Credentials);
            NetworkCredential cred = result.Credentials.GetCredential(new Uri("http://test"), "");

            Assert.AreEqual(proxyUser, cred.UserName);
            Assert.AreEqual(proxyPassword, cred.Password);
        }
Example #12
0
        private bool CurrentProxyStatue()
        {
            HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com");

            // Obtain the 'Proxy' of the  Default browser.
            IWebProxy proxy = myWebRequest.Proxy;

            // Print the Proxy Url to the console.
            if (proxy != null)
            {
                string prx = TrimProxyString(proxy.GetProxy(myWebRequest.RequestUri).ToString());
                string res = string.Format("Proxy: {0}", prx);
                lblCPS.Text = "Current  " + res;
                if (res.Contains("micro"))
                {
                    lblCPS.Text = "No proxy";
                    Notify(lblCPS.Text);

                    return(false);
                }
                Notify(lblCPS.Text);
                return(true);
            }
            else
            {
                lblCPS.Text = "No proxy";
                Notify(lblCPS.Text);
                return(false);
            }
        }
Example #13
0
        public static IWebProxy CreateWebProxyWithCredentials(String sUrl, string ProxyUserName, string ProxyUserPassword, string sAuthType, string ProxyUserDomain)
        {
            if (String.IsNullOrEmpty(ProxyUserName) || String.IsNullOrEmpty(ProxyUserPassword))
            {
                return(null);
            }
            // get default proxy and assign it to the WebService. Alternatively, you can replace this with manual WebProxy creation.
            IWebProxy iDefaultWebProxy = WebRequest.DefaultWebProxy;
            Uri       uriProxy         = iDefaultWebProxy.GetProxy(new Uri(sUrl));
            string    sProxyUrl        = uriProxy.AbsoluteUri;

            if (sProxyUrl == sUrl)
            {//no proxy specified
                return(null);
            }
            IWebProxy proxyObject = new WebProxy(sProxyUrl, true);

            // assign the credentials to the Proxy
            //todo do we need to add credentials to  WebService too??
            if ((!String.IsNullOrEmpty(sAuthType)) && (sAuthType.ToLower() != "basic"))
            {
                //from http://www.mcse.ms/archive105-2004-10-1165271.html
                // create credentials cache - it will hold both, the WebProxy credentials (??and the WebService credentials too??)
                System.Net.CredentialCache cache = new System.Net.CredentialCache();
                // add default credentials for Proxy (notice the authType = 'Kerberos' !) Other types are 'Basic', 'Digest', 'Negotiate', 'NTLM'
                cache.Add(new Uri(sProxyUrl), sAuthType, new System.Net.NetworkCredential(ProxyUserName, ProxyUserPassword, ProxyUserDomain));
                proxyObject.Credentials = cache;
            }
            else//special case for Basic (from http://www.xmlwebservices.cc/index_FAQ.htm )
            {
                proxyObject.Credentials = new System.Net.NetworkCredential(ProxyUserName, ProxyUserPassword);
            }
            return(proxyObject);
        }
Example #14
0
        static async Task <ClientWebSocket> CreateClientWebSocketAsync(Uri websocketUri, TimeSpan timeout)
        {
            var websocket = new ClientWebSocket();

            // Set SubProtocol to AMQPWSB10
            websocket.Options.AddSubProtocol(WebSocketConstants.SubProtocols.Amqpwsb10);

            // Check if we're configured to use a proxy server
            IWebProxy webProxy     = WebRequest.DefaultWebProxy;
            Uri       proxyAddress = null;

            try
            {
                proxyAddress = webProxy?.GetProxy(websocketUri);
                if (!websocketUri.Equals(proxyAddress))
                {
                    // Configure proxy server
                    websocket.Options.Proxy = webProxy;
                }
            }
            catch (PlatformNotSupportedException)
            {
                // .NET Core 2.0 doesn't support proxy. Ignore this setting.
            }

            websocket.Options.UseDefaultCredentials = true;

            using (var cancellationTokenSource = new CancellationTokenSource(timeout))
            {
                await websocket.ConnectAsync(websocketUri, cancellationTokenSource.Token).ConfigureAwait(false);
            }

            return(websocket);
        }
Example #15
0
        static void SimulateMonoGetProxy(Uri uri)
        {
            Console.WriteLine("# Mono - simulate Mono's WebRequest.DefaultWebProxy");
            IWebProxy proxy = MonoWebRequest.DefaultWebProxy;

            if (proxy != null)
            {
                Console.WriteLine("MonoWebRequest.DefaultWebProxy.GetType: {0}", proxy.GetType().FullName);
                Uri proxyAddress = new Uri(proxy.GetProxy(uri).AbsoluteUri);
                if (string.Equals(proxyAddress.AbsoluteUri, uri.AbsoluteUri))
                {
                    Console.WriteLine("ProxyAddress matches request uri. Ignoring proxy uri: '{0}'", proxyAddress);
                    return;
                }
                if (proxy.IsBypassed(uri))
                {
                    Console.WriteLine("Proxy IsByPassed for '{0}'", uri);
                    return;
                }
                Console.WriteLine("Proxy found. Uri: '{0}'", proxyAddress);
            }
            else
            {
                Console.WriteLine("MonoWebRequest.DefaultWebProxy is null. Trying WebRequest.GetSystemWebProxy");
            }
        }
Example #16
0
        private string GetCurrentProxyString()
        {
            HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com");

            // Obtain the 'Proxy' of the  Default browser.
            IWebProxy proxy = myWebRequest.Proxy;

            // Print the Proxy Url to the console.
            if (proxy != null)
            {
                string res = proxy.GetProxy(myWebRequest.RequestUri).ToString();//string.Format("Proxy: {0}", );
                if (res.Contains("micro"))
                {
                    return("No proxy");
                }
                else
                {
                    return(res);
                }
            }
            else
            {
                return("No proxy");
            }
        }
Example #17
0
        public override ch.cyberduck.core.proxy.Proxy find(string host)
        {
            Uri target;

            try
            {
                target = new Uri(host);
            }
            catch (UriFormatException)
            {
                return(ch.cyberduck.core.proxy.Proxy.DIRECT);
            }

            if (_system.IsBypassed(target))
            {
                return(ch.cyberduck.core.proxy.Proxy.DIRECT);
            }

            // Hack to make Secur32/IWA work. With a non-us locale we get an invalid codepage (1) exception when using the native library.
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

            Uri proxy = _system.GetProxy(target);

            return(new ch.cyberduck.core.proxy.Proxy(ch.cyberduck.core.proxy.Proxy.Type.valueOf(proxy.Scheme.ToUpper()),
                                                     proxy.Host, proxy.Port, proxy.UserInfo));
        }
        private static bool ProxyAddressIfNecessary(ref Uri address, IWebProxy proxy)
        {
            if (proxy != null && !address.IsLoopback)
            {
                try
                {
                    Uri proxyAddress = proxy.GetProxy(address);
                    if (proxyAddress != null)
                    {
                        if (proxyAddress.Scheme != Uri.UriSchemeHttp)
                        {
                            throw new NotSupportedException(SR.Format(SR.net_proxyschemenotsupported, address.Scheme));
                        }

                        address = proxyAddress;
                        return(true);
                    }
                }
                catch (PlatformNotSupportedException)
                {
                    // HttpWebRequest has a dummy SystemWebProxy that's used as a sentinel
                    // and whose GetProxy method throws a PlatformNotSupportedException.
                    // For the purposes of this stand-in ServicePointManager implementation,
                    // we ignore this default "system" proxy for the purposes of mapping
                    // to a particular ServicePoint instance.
                }
            }

            return(false);
        }
Example #19
0
        public void GetProxyInformation(string url)
        {
            IWebProxy wp  = WebRequest.GetSystemWebProxy();
            Uri       req = new Uri(url);

            Console.Out.WriteLine("URL Requested: " + req.AbsoluteUri);
            Uri proxy = wp.GetProxy(req);

            if (String.Compare(req.AbsoluteUri, proxy.AbsoluteUri) == 0)
            {
                Console.Out.WriteLine("Proxy: DIRECT");
            }
            else
            {
                Console.Out.WriteLine("Proxy: " + proxy.AbsoluteUri);
            }

            if (wp.Credentials != null)
            {
                NetworkCredential cred = wp.Credentials.GetCredential(req, "basic");
                Console.Out.WriteLine("Proxy Username: "******"Proxy Password: "******"Proxy Domain: " + cred.Domain);
            }
        }
        async Task <ClientWebSocket> CreateClientWebSocketAsync(Uri websocketUri, TimeSpan timeout)
        {
            var websocket = new ClientWebSocket();

            // Set SubProtocol to AMQPWSB10
            websocket.Options.AddSubProtocol(WebSocketConstants.SubProtocols.Amqpwsb10);

            // Check if we're configured to use a proxy server
            IWebProxy webProxy     = WebRequest.DefaultWebProxy;
            Uri       proxyAddress = webProxy != null?webProxy.GetProxy(websocketUri) : null;

            if (!websocketUri.Equals(proxyAddress))
            {
                // Configure proxy server
                websocket.Options.Proxy = webProxy;
            }

            if (this.AmqpTransportSettings.ClientCertificate != null)
            {
                websocket.Options.ClientCertificates.Add(this.AmqpTransportSettings.ClientCertificate);
            }
            else
            {
                websocket.Options.UseDefaultCredentials = true;
            }

            using (var cancellationTokenSource = new CancellationTokenSource(timeout))
            {
                await websocket.ConnectAsync(websocketUri, cancellationTokenSource.Token);
            }

            return(websocket);
        }
Example #21
0
        private static HttpWebRequest CreateRequest(String url)
        {
            HttpWebRequest request = null;

            request           = (HttpWebRequest)WebRequest.Create(url);
            request.KeepAlive = false;
            DateTime buildDate = System.IO.File.GetLastWriteTime(System.Reflection.Assembly.GetExecutingAssembly().Location);

            request.UserAgent = @"Ultraschall_Agent " + QueryLocalVersion() + " (" + System.Environment.OSVersion + ")";
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; // this is crucial, don't ask

            IWebProxy proxy = request.Proxy;

            if (proxy != null)
            {
                String proxyUri = proxy.GetProxy(request.RequestUri).ToString();
                if (String.IsNullOrEmpty(proxyUri) == false)
                {
                    if (proxyUri != url)
                    {
                        request.UseDefaultCredentials = true;
                        request.Proxy             = new WebProxy(proxyUri, true);
                        request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
                    }
                }
            }

            return(request);
        }
        public static ServicePoint FindServicePoint(Uri address, IWebProxy proxy)
        {
            if (address == null)
            {
                throw new ArgumentNullException("address");
            }

            var origAddress = new Uri(address.Scheme + "://" + address.Authority);

            bool usesProxy  = false;
            bool useConnect = false;

            if (proxy != null && !proxy.IsBypassed(address))
            {
                usesProxy = true;
                bool isSecure = address.Scheme == "https";
                address = proxy.GetProxy(address);
                if (address.Scheme != "http")
                {
                    throw new NotSupportedException("Proxy scheme not supported.");
                }

                if (isSecure && address.Scheme == "http")
                {
                    useConnect = true;
                }
            }

            address = new Uri(address.Scheme + "://" + address.Authority);

            var key = new SPKey(origAddress, usesProxy ? address : null, useConnect);

            lock (servicePoints) {
                if (servicePoints.TryGetValue(key, out var sp))
                {
                    return(sp);
                }

                if (maxServicePoints > 0 && servicePoints.Count >= maxServicePoints)
                {
                    throw new InvalidOperationException("maximum number of service points reached");
                }

                int limit;
#if MOBILE
                limit = defaultConnectionLimit;
#else
                string addr = address.ToString();
                limit = (int)manager.GetMaxConnections(addr);
#endif
                sp = new ServicePoint(key, address, limit, maxServicePointIdleTime);
                sp.Expect100Continue = expectContinue;
                sp.UseNagleAlgorithm = useNagle;
                sp.UsesProxy         = usesProxy;
                sp.UseConnect        = useConnect;
                sp.SetTcpKeepAlive(tcp_keepalive, tcp_keepalive_time, tcp_keepalive_interval);

                return(servicePoints.GetOrAdd(key, sp));
            }
        }
        public ICredentials GetCredentials(Uri uri, IWebProxy proxy, CredentialType credentialType, bool retrying)
        {
            // if looking for proxy credentials, we care about the proxy's URL, not the request URL
            if (credentialType == CredentialType.ProxyCredentials)
            {
                var proxyUri = proxy.GetProxy(uri);
                if (proxyUri != null)
                {
                    uri = proxyUri;
                }
            }

            lock (guiLock) {
                // If this is the first attempt, return any stored credentials. If they fail, we'll be called again.
                if (!retrying)
                {
                    var creds = GetExistingCredentials(uri, credentialType);
                    if (creds != null)
                    {
                        return(creds);
                    }
                }

                return(GetCredentialsFromUser(uri, proxy, credentialType));
            }
        }
        private Uri GetProxyUri(string strSessionUrl, bool bForceHttp)
        {
            if (string.IsNullOrEmpty(strSessionUrl))
            {
                Debug.Assert(false); return(null);
            }

            try
            {
                if (bForceHttp && !strSessionUrl.StartsWith("http:",
                                                            StrUtil.CaseIgnoreCmp))
                {
                    strSessionUrl = "http://" + UrlUtil.RemoveScheme(strSessionUrl);
                }

                Uri uriSession = new Uri(strSessionUrl);
                if ((m_prx != null) && !m_prx.IsBypassed(uriSession))
                {
                    Uri uriProxy = m_prx.GetProxy(uriSession);
                    Debug.Assert(uriProxy != uriSession);
                    return(uriProxy);
                }
            }
            catch (Exception) { Debug.Assert(false); }

            return(null);
        }
Example #25
0
        /// <summary>
        /// Return true or false if connecting through a proxy server
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        private static bool IsSystemProxySet(Uri uri)
        {
            // The reason for not calling the GetSystemProxy is because the object
            // that will be returned is no longer going to be the proxy that is set by the settings
            // on the users machine only the Address is going to be the same.
            // Not sure why the .NET team did not want to expose all of the useful settings like
            // ByPass list and other settings that we can't get because of it.
            // Anyway the reason why we need the DefaultWebProxy is to see if the uri that we are
            // getting the proxy for to should be bypassed or not. If it should be bypassed then
            // return that we don't need a proxy and we should try to connect directly.
            IWebProxy proxy = WebRequest.DefaultWebProxy;

            if (proxy != null)
            {
                Uri proxyUri = proxy.GetProxy(uri);
                if (proxyUri != null)
                {
                    Uri proxyAddress = new Uri(proxyUri.AbsoluteUri);
                    if (String.Equals(proxyAddress.AbsoluteUri, uri.AbsoluteUri))
                    {
                        return(false);
                    }
                    bool bypassUri = proxy.IsBypassed(uri);
                    if (bypassUri)
                    {
                        return(false);
                    }
                    proxy = new WebProxy(proxyAddress);
                }
            }

            return(proxy != null);
        }
Example #26
0
        public Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (_proxy == null)
            {
                return(SendAsyncCore(request, null, false, cancellationToken));
            }

            Uri proxyUri = null;

            try
            {
                if (!_proxy.IsBypassed(request.RequestUri))
                {
                    proxyUri = _proxy.GetProxy(request.RequestUri);
                }
            }
            catch (Exception ex)
            {
                // Eat any exception from the IWebProxy and just treat it as no proxy.
                // This matches the behavior of other handlers.
            }
            if (proxyUri != null && proxyUri.Scheme != "http")
            {
                throw new NotSupportedException("net_http_invalid_proxy_scheme");
            }
            return(SendAsyncCore(request, proxyUri, false, cancellationToken));
        }
Example #27
0
 /// <summary>
 /// Checks the web client and installs basic proxy settings
 /// </summary>
 /// <param name="client">web client</param>
 public static void CheckClient(WebClient client)
 {
     if (proxy == null)
     {
         string    host   = "";
         IWebProxy proxy2 = WebRequest.GetSystemWebProxy();
         //High-availability server
         host  = proxy2.GetProxy(new Uri("http://www.google.com")).Host;
         proxy = new WebProxy(host, true);
         try
         {
             //Low-data return, checks for error when downloading
             client.DownloadData("www.google.com");
         }
         catch
         {
             if (Username == "" && Password == "")
             {
                 //Always returns proxy server if there is a proxy - which does not equal the initial proxy host
                 if (proxy.Address.Host != "www.google.com")
                 {
                     PromptCredientials(proxy.Address.Host);
                     proxy.Credentials = new NetworkCredential(Username, Password);
                 }
                 else
                 {
                     proxy = null;
                 }
             }
         }
     }
     client.Proxy = proxy;
 }
Example #28
0
        public static ServicePoint FindServicePoint(Uri address, IWebProxy proxy)
        {
            if (address == null)
            {
                throw new ArgumentNullException("address");
            }

            RecycleServicePoints();

            bool usesProxy  = false;
            bool useConnect = false;

            if (proxy != null && !proxy.IsBypassed(address))
            {
                usesProxy = true;
                bool isSecure = address.Scheme == "https";
                address = proxy.GetProxy(address);
                if (address.Scheme != "http" && !isSecure)
                {
                    throw new NotSupportedException("Proxy scheme not supported.");
                }

                if (isSecure && address.Scheme == "http")
                {
                    useConnect = true;
                }
            }

            address = new Uri(address.Scheme + "://" + address.Authority);

            ServicePoint sp = null;

            lock (servicePoints) {
                int key = address.GetHashCode() + (int)((useConnect) ? 1 : 0);
                sp = servicePoints [key] as ServicePoint;
                if (sp != null)
                {
                    return(sp);
                }

                if (maxServicePoints > 0 && servicePoints.Count >= maxServicePoints)
                {
                    throw new InvalidOperationException("maximum number of service points reached");
                }

                string addr  = address.ToString();
                int    limit = (int)manager.GetMaxConnections(addr);
                sp = new ServicePoint(address, limit, maxServicePointIdleTime);
#if NET_1_1
                sp.Expect100Continue = expectContinue;
                sp.UseNagleAlgorithm = useNagle;
#endif
                sp.UsesProxy  = usesProxy;
                sp.UseConnect = useConnect;
                servicePoints.Add(key, sp);
            }

            return(sp);
        }
Example #29
0
        void ProcessMethod()
        {
            ServicePoint sp = GetServicePoint();

            if (sp.UsesProxy)
            {
                if (method != WebRequestMethods.Ftp.DownloadFile)
                {
                    throw new NotSupportedException("FTP+proxy only supports RETR");
                }

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(proxy.GetProxy(requestUri));
                req.Address  = requestUri;
                requestState = RequestState.Finished;
                WebResponse response = req.GetResponse();
                ftpResponse.Stream     = new FtpDataStream(this, response.GetResponseStream(), true);
                ftpResponse.StatusCode = FtpStatusCode.CommandOK;
                return;
            }
            State = RequestState.Connecting;

            ResolveHost();

            OpenControlConnection();
            CWDAndSetFileName(requestUri);
            SetType();

            switch (method)
            {
            // Open data connection and receive data
            case WebRequestMethods.Ftp.DownloadFile:
            case WebRequestMethods.Ftp.ListDirectory:
            case WebRequestMethods.Ftp.ListDirectoryDetails:
                DownloadData();
                break;

            // Open data connection and send data
            case WebRequestMethods.Ftp.AppendFile:
            case WebRequestMethods.Ftp.UploadFile:
            case WebRequestMethods.Ftp.UploadFileWithUniqueName:
                UploadData();
                break;

            // Get info from control connection
            case WebRequestMethods.Ftp.GetFileSize:
            case WebRequestMethods.Ftp.GetDateTimestamp:
            case WebRequestMethods.Ftp.PrintWorkingDirectory:
            case WebRequestMethods.Ftp.MakeDirectory:
            case WebRequestMethods.Ftp.Rename:
            case WebRequestMethods.Ftp.DeleteFile:
                ProcessSimpleMethod();
                break;

            default:             // What to do here?
                throw new Exception(String.Format("Support for command {0} not implemented yet", method));
            }

            CheckIfAborted();
        }
Example #30
0
        private static WebProxy GetSystemProxy(Uri uri)
        {
            // WebRequest.DefaultWebProxy seems to be more capable in terms of getting the default
            // proxy settings instead of the WebRequest.GetSystemProxy()
            var proxyUri = _originalSystemProxy.GetProxy(uri);

            return(new WebProxy(proxyUri));
        }
		public ICredentials GetCredentials (Uri uri, IWebProxy proxy, CredentialType credentialType, bool retrying)
		{
			// if looking for proxy credentials, we care about the proxy's URL, not the request URL
			if (credentialType == CredentialType.ProxyCredentials) {
				var proxyUri = proxy.GetProxy (uri);
				if (proxyUri != null)
					uri = proxyUri;
			}

			lock (guiLock) {
				// If this is the first attempt, return any stored credentials. If they fail, we'll be called again.
				if (!retrying) {
					var creds = GetExistingCredentials (uri, credentialType);
					if (creds != null)
						return creds;
				}

				return GetCredentialsFromUser (uri, proxy, credentialType);
			}
		}
        /// <summary>
        /// Returns an ICredentials instance that the consumer would need in order
        /// to properly authenticate to the given Uri.
        /// </summary>
        public ICredentials GetCredentials(Uri uri, IWebProxy proxy, CredentialType credentialType, bool retrying)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }
            // Capture the original proxy before we do anything 
            // so that we can re-set it once we get the credentials for the given Uri.
            IWebProxy originalProxy = null;
            if (proxy != null)
            {
                // If the current Uri should be bypassed then don't try to get the specific
                // proxy but simply capture the one that is given to us
                if (proxy.IsBypassed(uri))
                {
                    originalProxy = proxy;
                }
                // If the current Uri is not bypassed then get a valid proxy for the Uri
                // and make sure that we have the credentials also.
                else
                {
                    originalProxy = new WebProxy(proxy.GetProxy(uri));
                    originalProxy.Credentials = proxy.Credentials == null
                                                    ? null : proxy.Credentials.GetCredential(uri, null);
                }
            }

            try
            {
                // The cached credentials that we found are not valid so let's ask the user
                // until they abort or give us valid credentials.
                InitializeCredentialProxy(uri, originalProxy);

                return PromptForCredentials(uri);
            }
            finally
            {
                // Reset the original WebRequest.DefaultWebProxy to what it was when we started credential discovery.
                WebRequest.DefaultWebProxy = originalProxy;
            }
        }
Example #33
0
        public static void SetWarp(this WebRequest webRequest, WarpEngine warpEngine, IWebProxy proxy)
        {
            warpEngine.SetWarp(webRequest);

            if (proxy != null)
            {
                // warproxy header
                string base64String;

                if (proxy is WebProxy)
                {
                    base64String = Helper.FromProxy(proxy as WebProxy);
                }
                else
                {
                    WebProxy webProxy = new WebProxy(proxy.GetProxy(webRequest.RequestUri));
                    webProxy.Credentials = proxy.Credentials.GetCredential(webRequest.RequestUri, "BASIC");
                    base64String = Helper.FromProxy(webProxy);
                }

                webRequest.Headers.Set("warproxy", base64String);
            }
        }
Example #34
0
			public ICredentials GetCredentials (Uri uri, IWebProxy proxy, CredentialType credentialType, bool retrying)
			{
				if (credentialType != CredentialType.ProxyCredentials)
					return wrapped.GetCredentials (uri, proxy, credentialType, retrying);

				var proxyUri = proxy.GetProxy (uri);
				if (proxyUri == null)
					return null;

				if (!retrying) {
					var cached = CredentialStore.Instance.GetCredentials (proxyUri);
					if (cached != null)
						return cached;
				}

				lock (locker) {
					if (!retrying) {
						var cached = CredentialStore.Instance.GetCredentials (proxyUri);
						if (cached != null)
							return cached;
					}

					var creds = wrapped.GetCredentials (uri, proxy, credentialType, retrying);
					
					if (creds != null)
						CredentialStore.Instance.Add (proxyUri, creds);
					
					return creds;
				}
			}
        public static string GetWebData(string url, CookieContainer cc = null, string referer = null, IWebProxy proxy = null, bool forceUTF8 = false, bool allowUnsafeHeader = false, string userAgent = null, Encoding encoding = null)
        {
            HttpWebResponse response = null;
            try
            {
                string requestCRC = Helpers.EncryptionUtils.CalculateCRC32(string.Format("{0}{1}{2}{3}{4}", url, referer, userAgent, proxy != null ? proxy.GetProxy(new Uri(url)).AbsoluteUri : "", cc != null ? cc.GetCookieHeader(new Uri(url)) : ""));

                // try cache first
                string cachedData = WebCache.Instance[requestCRC];
                Log.Debug("GetWebData{1}: '{0}'", url, cachedData != null ? " (cached)" : "");
                if (cachedData != null) return cachedData;

                // request the data
                if (allowUnsafeHeader) Helpers.DotNetFrameworkHelper.SetAllowUnsafeHeaderParsing(true);
                HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
                if (request == null) return "";
                if (!String.IsNullOrEmpty(userAgent))
                    request.UserAgent = userAgent; // set specific UserAgent if given
                else
                    request.UserAgent = OnlineVideoSettings.Instance.UserAgent; // set OnlineVideos default UserAgent
                request.Accept = "*/*"; // we accept any content type
                request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate"); // we accept compressed content

                request.Headers.Add("X-Requested-With: XMLHttpRequest");
                request.Headers.Add("x-addr: 127.0.0.1");

                if (!String.IsNullOrEmpty(referer)) request.Referer = referer; // set referer if given
                if (cc != null) request.CookieContainer = cc; // set cookies if given
                if (proxy != null) request.Proxy = proxy; // send the request over a proxy if given
                try
                {
                    response = (HttpWebResponse)request.GetResponse();
                }
                catch (WebException webEx)
                {
                    Log.Debug(webEx.Message);
                    response = (HttpWebResponse)webEx.Response; // if the server returns a 404 or similar .net will throw a WebException that has the response
                }
                Stream responseStream;
                if (response == null) return "";
                if (response.ContentEncoding.ToLower().Contains("gzip"))
                    responseStream = new System.IO.Compression.GZipStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                else if (response.ContentEncoding.ToLower().Contains("deflate"))
                    responseStream = new System.IO.Compression.DeflateStream(response.GetResponseStream(), System.IO.Compression.CompressionMode.Decompress);
                else
                    responseStream = response.GetResponseStream();

                // UTF8 is the default encoding as fallback
                Encoding responseEncoding = Encoding.UTF8;
                // try to get the response encoding if one was specified and neither forceUTF8 nor encoding were set as parameters
                if (!forceUTF8 && encoding == null && response.CharacterSet != null && !String.IsNullOrEmpty(response.CharacterSet.Trim())) responseEncoding = Encoding.GetEncoding(response.CharacterSet.Trim(new char[] { ' ', '"' }));
                // the caller did specify a forced encoding
                if (encoding != null) responseEncoding = encoding;
                // the caller wants to force UTF8
                if (forceUTF8) responseEncoding = Encoding.UTF8;

                using (StreamReader reader = new StreamReader(responseStream, responseEncoding, true))
                {
                    string str = reader.ReadToEnd().Trim();
                    // add to cache if HTTP Status was 200 and we got more than 500 bytes (might just be an errorpage otherwise)
                    if (response.StatusCode == HttpStatusCode.OK && str.Length > 500) WebCache.Instance[requestCRC] = str;
                    return str;
                }
            }
            finally
            {
                if (response != null) ((IDisposable)response).Dispose();
                // disable unsafe header parsing if it was enabled
                if (allowUnsafeHeader) Helpers.DotNetFrameworkHelper.SetAllowUnsafeHeaderParsing(false);
            }
        }
Example #36
0
        //
        // FindServicePoint - Query using an Uri for a given server point
        //

        /// <include file='doc\ServicePointManager.uex' path='docs/doc[@for="ServicePointManager.FindServicePoint2"]/*' />
        /// <devdoc>
        /// <para>Findes an existing <see cref='System.Net.ServicePoint'/> or creates a new <see cref='System.Net.ServicePoint'/> to manage communications to the specified <see cref='System.Uri'/>
        /// instance.</para>
        /// </devdoc>
        public static ServicePoint FindServicePoint(Uri address, IWebProxy proxy) {
            if (address==null) {
                throw new ArgumentNullException("address");
            }
            GlobalLog.Enter("ServicePointManager::FindServicePoint() address:" + address.ToString());

            string tempEntry;
            bool isProxyServicePoint = false;

            ScavengeIdleServicePoints();

            //
            // find proxy info, and then switch on proxy
            //
            if (proxy!=null && !proxy.IsBypassed(address)) {
                // use proxy support
                // rework address
                Uri proxyAddress = proxy.GetProxy(address);
                if (proxyAddress.Scheme != Uri.UriSchemeHttps && proxyAddress.Scheme != Uri.UriSchemeHttp) {
                    Exception exception = new NotSupportedException(SR.GetString(SR.net_proxyschemenotsupported, proxyAddress.Scheme));
                    GlobalLog.LeaveException("ServicePointManager::FindServicePoint() proxy has unsupported scheme:" + proxyAddress.Scheme.ToString(), exception);
                    throw exception;
                }
                address = proxyAddress;

                isProxyServicePoint = true;

                //
                // Search for the correct proxy host,
                //  then match its acutal host by using ConnectionGroups
                //  which are located on the actual ServicePoint.
                //

                tempEntry = MakeQueryString(proxyAddress);
            }
            else {
                //
                // Typical Host lookup
                //
                tempEntry = MakeQueryString(address);
            }

            //
            // lookup service point in the table
            //
            ServicePoint servicePoint = null;

            lock (s_ServicePointTable) {
                //
                // once we grab the lock, check if it wasn't already added
                //
                WeakReference servicePointReference =  (WeakReference) s_ServicePointTable[tempEntry];

                if ( servicePointReference != null ) {
                    servicePoint = (ServicePoint)servicePointReference.Target;
                }

                if (servicePoint == null) {
                    //
                    // lookup failure or timeout, we need to create a new ServicePoint
                    //
                    if (s_MaxServicePoints<=0 || s_ServicePointTable.Count<s_MaxServicePoints) {
                        //
                        // Determine Connection Limit
                        //
                        int connectionLimit = InternalConnectionLimit;
                        string schemeHostPort = MakeQueryString(address);

                        if (ConfigTable.ContainsKey(schemeHostPort) ) {
                            connectionLimit = (int) ConfigTable[schemeHostPort];
                        }

                        // Note: we don't check permissions to access proxy.
                        //      Rather, we should protect proxy property from being changed

                        servicePoint = new ServicePoint(address, s_MaxServicePointIdleTime, connectionLimit);
                        servicePointReference = new WeakReference(servicePoint);

                        // only set this when created, donates a proxy, statt Server
                        servicePoint.InternalProxyServicePoint = isProxyServicePoint;

                        s_ServicePointTable[tempEntry] = servicePointReference;
                    }
                    else {
                        Exception exception = new InvalidOperationException(SR.GetString(SR.net_maxsrvpoints));
                        GlobalLog.LeaveException("ServicePointManager::FindServicePoint() reached the limit count:" + s_ServicePointTable.Count.ToString() + " limit:" + s_MaxServicePoints.ToString(), exception);
                        throw exception;
                    }
                }

            } // lock

            GlobalLog.Leave("ServicePointManager::FindServicePoint() servicePoint#" + ValidationHelper.HashString(servicePoint));

            return servicePoint;
        }
Example #37
0
		public static ServicePoint FindServicePoint (Uri address, IWebProxy proxy)
		{
			if (address == null)
				throw new ArgumentNullException ("address");

			RecycleServicePoints ();
			
			bool usesProxy = false;
			bool useConnect = false;
			if (proxy != null && !proxy.IsBypassed(address)) {
				usesProxy = true;
				bool isSecure = address.Scheme == "https";
				address = proxy.GetProxy (address);
				if (address.Scheme != "http" && !isSecure)
					throw new NotSupportedException ("Proxy scheme not supported.");

				if (isSecure && address.Scheme == "http")
					useConnect = true;
			} 

			address = new Uri (address.Scheme + "://" + address.Authority);
			
			ServicePoint sp = null;
			lock (servicePoints) {
				SPKey key = new SPKey (address, useConnect);
				sp = servicePoints [key] as ServicePoint;
				if (sp != null)
					return sp;

				if (maxServicePoints > 0 && servicePoints.Count >= maxServicePoints)
					throw new InvalidOperationException ("maximum number of service points reached");

				string addr = address.ToString ();
#if NET_2_1
				int limit = defaultConnectionLimit;
#else
				int limit = (int) manager.GetMaxConnections (addr);
#endif
				sp = new ServicePoint (address, limit, maxServicePointIdleTime);
				sp.Expect100Continue = expectContinue;
				sp.UseNagleAlgorithm = useNagle;
				sp.UsesProxy = usesProxy;
				sp.UseConnect = useConnect;
				sp.SetTcpKeepAlive (tcp_keepalive, tcp_keepalive_time, tcp_keepalive_interval);
				servicePoints.Add (key, sp);
			}
			
			return sp;
		}
Example #38
0
        /// <summary>
        /// This method attaches credentials to the web proxy object.
        /// </summary>
        /// <param name="proxy">The proxy to attach credentials to.</param>
        /// <param name="webCallUrl">The url for the web call.</param>
        /// <param name="oldProxyState">The current state fo the web call.</param>
        /// <param name="newProxyState">The new state for the web call.</param>
        /// <param name="okToPrompt">Prompt user for credentials if they are not available.</param>
        public WebProxyState PrepareWebProxy(IWebProxy proxy, string webCallUrl, WebProxyState oldProxyState, bool okToPrompt)
        {
            WebProxyState newProxyState = WebProxyState.Abort;

            if (string.IsNullOrEmpty(webCallUrl)) {
                Debug.Fail("PrepareWebProxy called with an empty WebCallUrl.");
                webCallUrl = "http://go.microsoft.com/fwlink/?LinkId=81947";
            }

            // Get the web proxy url for the the current web call.
            Uri webCallProxy = null;
            if (proxy != null) {
                webCallProxy = proxy.GetProxy(new Uri(webCallUrl));
            }

            if ((proxy != null) && (webCallProxy != null)) {
                // get proxy url.
                string proxyUrl = webCallProxy.Host;
                if (string.IsNullOrEmpty(currentProxyUrl)) {
                    currentProxyUrl = proxyUrl;
                }

                switch (oldProxyState) {
                    case WebProxyState.NoCredentials:
                        // Add the default credentials only if there aren't any credentials attached to
                        // the DefaultWebProxy. If the first calls attaches the correct credentials, the
                        // second call will just use them, instead of overwriting it with the default credentials.
                        // This avoids multiple web calls. Note that state is transitioned to DefaultCredentials
                        // instead of CachedCredentials. This ensures that web calls be tried with the
                        // cached credentials if the currently attached credentials don't result in successful web call.
                        if ((proxy.Credentials == null)) {
                            proxy.Credentials = CredentialCache.DefaultCredentials;
                        }
                        newProxyState = WebProxyState.DefaultCredentials;
                        break;

                    case WebProxyState.DefaultCredentials:
                        // Fetch cached credentials if they are null or if the proxy url has changed.
                        if ((cachedCredentials == null) ||
                            !string.Equals(currentProxyUrl, proxyUrl, StringComparison.OrdinalIgnoreCase)) {
                            cachedCredentials = GetCachedCredentials(proxyUrl);
                        }

                        if (cachedCredentials != null) {
                            proxy.Credentials = cachedCredentials;
                            newProxyState = WebProxyState.CachedCredentials;
                            break;
                        }

                        // Proceed to next step if cached credentials are not available.
                        goto case WebProxyState.CachedCredentials;

                    case WebProxyState.CachedCredentials:
                    case WebProxyState.PromptForCredentials:
                        if (okToPrompt) {
                            if (DialogResult.OK == PromptForCredentials(proxyUrl)) {
                                proxy.Credentials = cachedCredentials;
                                newProxyState = WebProxyState.PromptForCredentials;
                            } else {
                                newProxyState = WebProxyState.Abort;
                            }
                        } else {
                            newProxyState = WebProxyState.Abort;
                        }
                        break;

                    case WebProxyState.Abort:
                        throw new InvalidOperationException();

                    default:
                        throw new ArgumentException(string.Empty, "oldProxyState");
                }
            } else {
                // No proxy for the webCallUrl scenario.
                if (oldProxyState == WebProxyState.NoCredentials) {
                    // if it is the first call, change the state and let the web call proceed.
                    newProxyState = WebProxyState.DefaultCredentials;
                } else {
                    Debug.Fail("This method is called a second time when 407 occurs. A 407 shouldn't have occurred as there is no default proxy.");
                    // We dont have a good idea of the circumstances under which
                    // WebProxy might be null for a url. To be safe, for VS 2005 SP1,
                    // we will just return the abort state, instead of throwing
                    // an exception. Abort state will ensure that no further procesing
                    // occurs and we will not bring down the app.
                    // throw new InvalidOperationException();
                    newProxyState = WebProxyState.Abort;
                }
            }
            return newProxyState;
        }
 internal static ServicePoint FindServicePoint(Uri address, IWebProxy proxy, out ProxyChain chain, ref HttpAbortDelegate abortDelegate, ref int abortState)
 {
     if (address == null)
     {
         throw new ArgumentNullException("address");
     }
     bool isProxyServicePoint = false;
     chain = null;
     Uri current = null;
     if ((proxy != null) && !address.IsLoopback)
     {
         IAutoWebProxy proxy2 = proxy as IAutoWebProxy;
         if (proxy2 != null)
         {
             chain = proxy2.GetProxies(address);
             abortDelegate = chain.HttpAbortDelegate;
             try
             {
                 Thread.MemoryBarrier();
                 if (abortState != 0)
                 {
                     Exception exception = new WebException(NetRes.GetWebStatusString(WebExceptionStatus.RequestCanceled), WebExceptionStatus.RequestCanceled);
                     throw exception;
                 }
                 chain.Enumerator.MoveNext();
                 current = chain.Enumerator.Current;
             }
             finally
             {
                 abortDelegate = null;
             }
         }
         else if (!proxy.IsBypassed(address))
         {
             current = proxy.GetProxy(address);
         }
         if (current != null)
         {
             address = current;
             isProxyServicePoint = true;
         }
     }
     return FindServicePointHelper(address, isProxyServicePoint);
 }
        // If abortState becomes non-zero, the attempt to find a service point has been aborted.
        internal static ServicePoint FindServicePoint(Uri address, IWebProxy proxy, out ProxyChain chain, ref HttpAbortDelegate abortDelegate, ref int abortState)
        {
            if (address==null) {
                throw new ArgumentNullException("address");
            }
            GlobalLog.Enter("ServicePointManager::FindServicePoint() address:" + address.ToString());

            bool isProxyServicePoint = false;
            chain = null;

            //
            // find proxy info, and then switch on proxy
            //
            Uri proxyAddress = null;
            if (proxy!=null  && !address.IsLoopback) {
                IAutoWebProxy autoProxy = proxy as IAutoWebProxy;
                if (autoProxy != null)
                {
                    chain = autoProxy.GetProxies(address);

                    // Set up our ability to abort this MoveNext call.  Note that the current implementations of ProxyChain will only
                    // take time on the first call, so this is the only place we do this.  If a new ProxyChain takes time in later
                    // calls, this logic should be copied to other places MoveNext is called.
                    GlobalLog.Assert(abortDelegate == null, "ServicePointManager::FindServicePoint()|AbortDelegate already set.");
                    abortDelegate = chain.HttpAbortDelegate;
                    try
                    {
                        Thread.MemoryBarrier();
                        if (abortState != 0)
                        {
                            Exception exception = new WebException(NetRes.GetWebStatusString(WebExceptionStatus.RequestCanceled), WebExceptionStatus.RequestCanceled);
                            GlobalLog.LeaveException("ServicePointManager::FindServicePoint() Request aborted before proxy lookup.", exception);
                            throw exception;
                        }

                        if (!chain.Enumerator.MoveNext())
                        {
                            GlobalLog.Assert("ServicePointManager::FindServicePoint()|GetProxies() returned zero proxies.");
/*
                            Exception exception = new WebException(NetRes.GetWebStatusString(WebExceptionStatus.RequestProhibitedByProxy), WebExceptionStatus.RequestProhibitedByProxy);
                            GlobalLog.LeaveException("ServicePointManager::FindServicePoint() Proxy prevented request.", exception);
                            throw exception;
*/
                        }
                        proxyAddress = chain.Enumerator.Current;
                    }
                    finally
                    {
                        abortDelegate = null;
                    }
                }
                else if (!proxy.IsBypassed(address))
                {
                    // use proxy support
                    // rework address
                    proxyAddress = proxy.GetProxy(address);
                }

                // null means DIRECT
                if (proxyAddress!=null) {
                    address = proxyAddress;
                    isProxyServicePoint = true;
                }
            }

            ServicePoint servicePoint = FindServicePointHelper(address, isProxyServicePoint);
            GlobalLog.Leave("ServicePointManager::FindServicePoint() servicePoint#" + ValidationHelper.HashString(servicePoint));
            return servicePoint;
        }
Example #41
0
        private string MyGetWebData(Uri uri, string postData = null, CookieContainer cookies = null, string referer = null, IWebProxy proxy = null, bool forceUTF8 = false, bool allowUnsafeHeader = false, string userAgent = null, Encoding encoding = null, NameValueCollection headers = null, bool cache = true)
        {
            // do not use the cache when doing a POST
            if (postData != null) cache = false;
            // set a few headers if none were given
            if (headers == null)
            {
                headers = new NameValueCollection();
                headers.Add("Accept", "*/*"); // accept any content type
                headers.Add("User-Agent", userAgent ?? OnlineVideoSettings.Instance.UserAgent); // set the default OnlineVideos UserAgent when none specified
            }
            if (referer != null) headers.Set("Referer", referer);
            HttpWebResponse response = null;
            try
            {
                // build a CRC of the url and all headers + proxy + cookies for caching
                string requestCRC = Helpers.EncryptionUtils.CalculateCRC32(
                    string.Format("{0}{1}{2}{3}",
                    uri.ToString(),
                    headers != null ? string.Join("&", (from item in headers.AllKeys select string.Format("{0}={1}", item, headers[item])).ToArray()) : "",
                    proxy != null ? proxy.GetProxy(uri).AbsoluteUri : "",
                    cookies != null ? cookies.GetCookieHeader(uri) : ""));

                // try cache first
                string cachedData = cache ? WebCache.Instance[requestCRC] : null;
                Log.Debug("GetWebData-{2}{1}: '{0}'", uri.ToString(), cachedData != null ? " (cached)" : "", postData != null ? "POST" : "GET");
                if (cachedData != null) return cachedData;

                // build the request
                if (allowUnsafeHeader) Helpers.DotNetFrameworkHelper.SetAllowUnsafeHeaderParsing(true);
                HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
                if (request == null) return "";
                request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; // turn on automatic decompression of both formats (adds header "AcceptEncoding: gzip,deflate" to the request)
                if (cookies != null) request.CookieContainer = cookies; // set cookies if given
                if (proxy != null) request.Proxy = proxy; // send the request over a proxy if given
                if (headers != null) // set user defined headers
                {
                    foreach (var headerName in headers.AllKeys)
                    {
                        switch (headerName.ToLowerInvariant())
                        {
                            case "accept":
                                request.Accept = headers[headerName];
                                break;
                            case "user-agent":
                                request.UserAgent = headers[headerName];
                                break;
                            case "referer":
                                request.Referer = headers[headerName];
                                break;
                            default:
                                request.Headers.Set(headerName, headers[headerName]);
                                break;
                        }
                    }
                }
                if (postData != null)
                {
                    byte[] data = encoding != null ? encoding.GetBytes(postData) : Encoding.UTF8.GetBytes(postData);
                    request.Method = "POST";
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.ContentLength = data.Length;
                    request.ProtocolVersion = HttpVersion.Version10;
                    Stream requestStream = request.GetRequestStream();
                    requestStream.Write(data, 0, data.Length);
                    requestStream.Close();
                }

                // request the data
                try
                {
                    response = (HttpWebResponse)request.GetResponse();
                }
                catch (WebException webEx)
                {
                    Log.Debug(webEx.Message);
                    response = (HttpWebResponse)webEx.Response; // if the server returns a 404 or similar .net will throw a WebException that has the response
                }
                Stream responseStream = response.GetResponseStream();

                // UTF8 is the default encoding as fallback
                Encoding responseEncoding = Encoding.UTF8;
                // try to get the response encoding if one was specified and neither forceUTF8 nor encoding were set as parameters
                if (!forceUTF8 && encoding == null && response.CharacterSet != null && !String.IsNullOrEmpty(response.CharacterSet.Trim())) responseEncoding = Encoding.GetEncoding(response.CharacterSet.Trim(new char[] { ' ', '"' }));
                // the caller did specify a forced encoding
                if (encoding != null) responseEncoding = encoding;
                // the caller wants to force UTF8
                if (forceUTF8) responseEncoding = Encoding.UTF8;

                using (StreamReader reader = new StreamReader(responseStream, responseEncoding, true))
                {
                    string str = reader.ReadToEnd().Trim();
                    // add to cache if HTTP Status was 200 and we got more than 500 bytes (might just be an errorpage otherwise)
                    if (cache && response.StatusCode == HttpStatusCode.OK && str.Length > 500) WebCache.Instance[requestCRC] = str;
                    return str;
                }
            }
            finally
            {
                if (response != null) ((IDisposable)response).Dispose();
                // disable unsafe header parsing if it was enabled
                if (allowUnsafeHeader) Helpers.DotNetFrameworkHelper.SetAllowUnsafeHeaderParsing(false);
            }
        }
Example #42
0
        /// <summary>
        /// This function will connect a stream to a uri (host and port), 
        /// negotiating proxies and SSL
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="timeout_ms">Timeout, in ms. 0 for no timeout.</param>
        /// <returns></returns>
        public static Stream ConnectStream(Uri uri, IWebProxy proxy, bool nodelay, int timeout_ms)
        {
            IMockWebProxy mockProxy = proxy != null ? proxy as IMockWebProxy : null;
            if (mockProxy != null)
                return mockProxy.GetStream(uri);

            Stream stream;
            bool useProxy = proxy != null && !proxy.IsBypassed(uri);

            if (useProxy)
            {
                Uri proxyURI = proxy.GetProxy(uri);
                stream = ConnectSocket(proxyURI, nodelay, timeout_ms);
            }
            else
            {
                stream = ConnectSocket(uri, nodelay, timeout_ms);
            }

            try
            {
                if (useProxy)
                {
                    string line = String.Format("CONNECT {0}:{1} HTTP/1.0", uri.Host, uri.Port);

                    WriteLine(line, stream);
                    WriteLine(stream);

                    ReadHttpHeaders(ref stream, proxy, nodelay, timeout_ms);
                }

                if (UseSSL(uri))
                {
                    SslStream sslStream = new SslStream(stream, false,
                        new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
                    sslStream.AuthenticateAsClient("");

                    stream = sslStream;
                }

                return stream;
            }
            catch
            {
                stream.Close();
                throw;
            }
        }
Example #43
0
        private static bool ProxyAddressIfNecessary(ref Uri address, IWebProxy proxy)
        {
            if (proxy != null && !address.IsLoopback)
            {
                Uri proxyAddress = proxy.GetProxy(address);
                if (proxyAddress != null)
                {
                    if (proxyAddress.Scheme != Uri.UriSchemeHttp)
                    {
                        throw new NotSupportedException(SR.Format(SR.net_proxyschemenotsupported, address.Scheme));
                    }

                    address = proxyAddress;
                    return true;
                }
            }

            return false;
        }
Example #44
0
 public static ServicePoint FindServicePoint(Uri address, IWebProxy proxy)
 {
     if (address == null)
     {
         throw new ArgumentNullException("address");
     }
     var usesProxy = false;
     if (proxy != null && !proxy.IsBypassed(address))
     {
         usesProxy = true;
         var isSecure = address.Scheme == Uri.UriSchemeHttps;
         address = proxy.GetProxy(address);
         if (address.Scheme != Uri.UriSchemeHttp && !isSecure)
         {
             throw new NotSupportedException("Proxy scheme not supported.");
         }
     }
     var key = MakeQueryString(address, usesProxy);
     return _servicePoints.GetOrAdd(key, new Lazy<ServicePoint>(() =>
     {
         if (_maxServicePoints > 0 && _servicePoints.Count >= _maxServicePoints)
         {
             throw new InvalidOperationException("maximum number of service points reached");
         }
         return new ServicePoint(address, _defaultConnectionLimit, key, usesProxy);
     }, false)).Value;
 }