Esempio n. 1
0
        void BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = sender as HttpApplication;

            if (application == null)
            {
                return;
            }

            HttpContext context = application.Context;

            if (string.IsNullOrEmpty(context.Request.UserAgent))
            {
                Inspect(context, BlockType.Forbidden, string.Empty);
            }

            if (!UserAgentUtility.IsSearchEngine(context.Request))
            {
                SiteGuardRules rules = GetRules(context);

                foreach (var rule in rules)
                {
                    if (context.Request.Url.AbsolutePath.StartsWith(rule.Path, StringComparison.OrdinalIgnoreCase) && ("ALL".Equals(rule.HttpMethod, StringComparison.OrdinalIgnoreCase) || context.Request.HttpMethod.Equals(rule.HttpMethod, StringComparison.OrdinalIgnoreCase)))
                    {
                        string keyBase = rule.HttpMethod.ToLower() + "_" + rule.Path.ToLower() + "_" +
                                         IpUtility.GetIp();
                        string countKey = keyBase + "_Count";
                        string blockKey = keyBase + "_Block";

                        if (context.Cache[blockKey] != null)
                        {
                            Inspect(context, rule.BlockType, rule.Message);
                            return;
                        }


                        object obj = context.Cache[countKey];
                        if (obj == null)
                        {
                            context.Cache.Add(countKey, new Counter {
                                Count = 1
                            }, null, DateTime.Now.AddSeconds(rule.TimePeriod), Cache.NoSlidingExpiration, CacheItemPriority.AboveNormal, null);
                        }
                        else
                        {
                            Counter counter = (Counter)obj;

                            counter.Count++;

                            if (counter.Count > rule.MaxRequests)
                            {
                                context.Cache.Add(blockKey, true, null, DateTime.Now.AddSeconds(rule.BlockTime), Cache.NoSlidingExpiration, CacheItemPriority.AboveNormal, null);
                                Inspect(context, rule.BlockType, rule.Message);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        /// <remarks>
        /// Unfortunately, we have to use reflection to set the user agent string as we'd like to.
        /// See https://github.com/NuGet/Home/issues/7464
        /// </remarks>
        private static void SetUserAgentString()
        {
            var userAgentString = UserAgentUtility.GetUserAgent();

            typeof(UserAgent)
            .GetProperty(nameof(UserAgent.UserAgentString))
            .SetValue(null, userAgentString);
        }
        /// <summary>
        /// Creates an instance of the default request settings.
        /// </summary>
        /// <returns>The default request settings</returns>
        private static VssClientHttpRequestSettings ConstructDefaultSettings()
        {
            // Set up reasonable defaults in case the registry keys are not present
            var settings = new VssClientHttpRequestSettings();

            settings.UserAgent = UserAgentUtility.GetDefaultRestUserAgent();

            return(settings);
        }
Esempio n. 4
0
        public Client()
        {
            try
            {
                HttpRequest request = HttpContext.Current.Request;

                this.Name      = request.UserHostName;
                this.OSVersion = UserAgentUtility.ParseOSFromUserAgent(request.UserAgent);
                this.UserAgent = request.UserAgent;
                this.IP        = request.UserHostAddress;
                this.Browser   = new Browser();
            }
            catch (Exception ex)
            {
            }
        }
Esempio n. 5
0
        protected override void Init(IDictionary <string, string> arguments, CancellationToken cancellationToken)
        {
            var source           = arguments.GetOrThrow <string>(Arguments.Source);
            var storageAccount   = arguments.GetOrThrow <string>(Arguments.StorageAccountName);
            var storageKey       = arguments.GetOrThrow <string>(Arguments.StorageKeyValue);
            var storageContainer = arguments.GetOrThrow <string>(Arguments.StorageContainer);
            var verify           = arguments.GetOrDefault(Arguments.Verify, false);
            var verbose          = arguments.GetOrDefault(Arguments.Verbose, false);

            var services = new ServiceCollection();

            services.AddSingleton(TelemetryService);
            services.AddSingleton(LoggerFactory);
            services.AddLogging();

            // Prepare the HTTP Client
            services.AddSingleton(p =>
            {
                var httpClient = new HttpClient(new WebRequestHandler
                {
                    AllowPipelining = true
                });

                httpClient.DefaultRequestHeaders.Add("User-Agent", UserAgentUtility.GetUserAgent());
                httpClient.Timeout = TimeSpan.FromMinutes(10);

                return(httpClient);
            });

            // Prepare the catalog reader.
            services.AddSingleton(p =>
            {
                var telemetryService          = p.GetRequiredService <ITelemetryService>();
                var httpMessageHandlerFactory = CommandHelpers.GetHttpMessageHandlerFactory(telemetryService, verbose);

                return(new CollectorHttpClient(httpMessageHandlerFactory()));
            });

            services.AddTransient(p =>
            {
                var collectorHttpClient = p.GetRequiredService <CollectorHttpClient>();
                var telemetryService    = p.GetRequiredService <ITelemetryService>();

                return(new CatalogIndexReader(new Uri(source), collectorHttpClient, telemetryService));
            });

            // Prepare the Azure Blob Storage container.
            services.AddSingleton(p =>
            {
                var credentials = new StorageCredentials(storageAccount, storageKey);
                var account     = new CloudStorageAccount(credentials, useHttps: true);

                return(account
                       .CreateCloudBlobClient()
                       .GetContainerReference(storageContainer));
            });

            // Prepare the handler that will run on each catalog entry.
            if (verify)
            {
                Logger.LogInformation("Validating that all packages have the proper Content MD5 hash...");

                services.AddTransient <IPackagesContainerHandler, ValidatePackageHashHandler>();
            }
            else
            {
                Logger.LogInformation("Ensuring all packages have a Content MD5 hash...");

                services.AddTransient <IPackagesContainerHandler, FixPackageHashHandler>();
            }

            services.AddTransient <PackagesContainerCatalogProcessor>();

            _serviceProvider = services.BuildServiceProvider();
        }