Exemple #1
0
        public async Task <IActionResult> Get(string hostname)
        {
            if (string.IsNullOrWhiteSpace(hostname) || !hostname.Contains("."))
            {
                return(new BadRequestResult());
            }

            var url = $"http://{hostname}";

            if (!Uri.TryCreate(url, UriKind.Absolute, out var uri))
            {
                return(new BadRequestResult());
            }

            var domain = uri.Host;
            // Convert sub.domain.com => domain.com
            //if(DomainName.TryParseBaseDomain(domain, out var baseDomain))
            //{
            //    domain = baseDomain;
            //}

            var mappedDomain = _domainMappingService.MapDomain(domain);

            if (!_iconsSettings.CacheEnabled || !_memoryCache.TryGetValue(mappedDomain, out Icon icon))
            {
                var result = await _iconFetchingService.GetIconAsync(domain);

                if (result == null)
                {
                    _logger.LogWarning("Null result returned for {0}.", domain);
                    icon = null;
                }
                else
                {
                    icon = result.Icon;
                }

                // Only cache not found and smaller images (<= 50kb)
                if (_iconsSettings.CacheEnabled && (icon == null || icon.Image.Length <= 50012))
                {
                    _logger.LogInformation("Cache icon for {0}.", domain);
                    _memoryCache.Set(mappedDomain, icon, new MemoryCacheEntryOptions
                    {
                        AbsoluteExpirationRelativeToNow = new TimeSpan(_iconsSettings.CacheHours, 0, 0),
                        Size     = icon?.Image.Length ?? 0,
                        Priority = icon == null ? CacheItemPriority.High : CacheItemPriority.Normal
                    });
                }
            }

            if (icon == null)
            {
                return(new FileContentResult(_notFoundImage, "image/png"));
            }

            return(new FileContentResult(icon.Image, icon.Format));
        }
Exemple #2
0
        public async Task <IActionResult> Get(string hostname)
        {
            if (string.IsNullOrWhiteSpace(hostname) || !hostname.Contains("."))
            {
                return(new BadRequestResult());
            }

            var url = $"http://{hostname}";

            if (!Uri.TryCreate(url, UriKind.Absolute, out Uri uri))
            {
                return(new BadRequestResult());
            }

            var mappedDomain = _domainMappingService.MapDomain(uri.Host);

            if (!_memoryCache.TryGetValue(mappedDomain, out Icon icon))
            {
                var iconUrl = $"{_iconsSettings.BestIconBaseUrl}/icon?url={mappedDomain}&size=16..32..200" +
                              $"&fallback_icon_url=https://raw.githubusercontent.com/bitwarden/web/master/src/images/fa-globe.png";
                var response = await _httpClient.GetAsync(iconUrl);

                response = await FollowRedirectsAsync(response, 1);

                if (!response.IsSuccessStatusCode ||
                    !_allowedMediaTypes.Contains(response.Content.Headers.ContentType.MediaType))
                {
                    return(new NotFoundResult());
                }

                var image = await response.Content.ReadAsByteArrayAsync();

                icon = new Icon
                {
                    Image  = image,
                    Format = response.Content.Headers.ContentType.MediaType
                };

                // Only cache smaller images (<= 50kb)
                if (image.Length <= 50012)
                {
                    _memoryCache.Set(mappedDomain, icon, new MemoryCacheEntryOptions
                    {
                        AbsoluteExpirationRelativeToNow = new TimeSpan(_iconsSettings.CacheHours, 0, 0)
                    });
                }
            }

            return(new FileContentResult(icon.Image, icon.Format));
        }
        public async Task <IActionResult> Get(string hostname)
        {
            if (string.IsNullOrWhiteSpace(hostname) || !hostname.Contains("."))
            {
                return(new BadRequestResult());
            }

            var url = $"http://{hostname}";

            if (!Uri.TryCreate(url, UriKind.Absolute, out Uri uri))
            {
                return(new BadRequestResult());
            }

            var mappedDomain = _domainMappingService.MapDomain(uri.Host);

            if (!_memoryCache.TryGetValue(mappedDomain, out Icon icon))
            {
                var result = await _iconFetchingService.GetIconAsync(mappedDomain);

                if (result == null)
                {
                    return(new NotFoundResult());
                }

                icon = result.Icon;

                // Only cache smaller images (<= 50kb)
                if (icon.Image.Length <= 50012)
                {
                    _memoryCache.Set(mappedDomain, icon, new MemoryCacheEntryOptions
                    {
                        AbsoluteExpirationRelativeToNow = new TimeSpan(_iconsSettings.CacheHours, 0, 0)
                    });
                }
            }

            return(new FileContentResult(icon.Image, icon.Format));
        }
Exemple #4
0
        public async Task <IActionResult> Get(string hostname)
        {
            if (string.IsNullOrWhiteSpace(hostname) || !hostname.Contains("."))
            {
                return(new BadRequestResult());
            }

            var url = $"http://{hostname}";

            if (!Uri.TryCreate(url, UriKind.Absolute, out Uri uri))
            {
                return(new BadRequestResult());
            }

            var mappedDomain = _domainMappingService.MapDomain(uri.Host);

            if (!_memoryCache.TryGetValue(mappedDomain, out Icon icon))
            {
                var iconUrl = $"{_iconsSettings.BestIconBaseUrl}/icon?url={mappedDomain}&size=16..32..200" +
                              $"&fallback_icon_url=https://raw.githubusercontent.com/bitwarden/web/master/src/images/fa-globe.png";
                var response = await _httpClient.GetAsync(iconUrl);

                if (response.StatusCode == HttpStatusCode.Redirect && response.Headers.Contains("Location"))
                {
                    var locationHeader = response.Headers.GetValues("Location").FirstOrDefault();
                    if (!string.IsNullOrWhiteSpace(locationHeader) &&
                        Uri.TryCreate(locationHeader, UriKind.Absolute, out Uri location))
                    {
                        var message = new HttpRequestMessage
                        {
                            RequestUri = location,
                            Method     = HttpMethod.Get
                        };

                        // Let's add some headers to look like we're coming from a web browser request. Some websites
                        // will block our request without these.
                        message.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " +
                                            "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36");
                        message.Headers.Add("Accept-Language", "en-US,en;q=0.8");
                        message.Headers.Add("Cache-Control", "no-cache");
                        message.Headers.Add("Pragma", "no-cache");
                        message.Headers.Add("Accept", "image/webp,image/apng,image/*,*/*;q=0.8");

                        response = await _httpClient.SendAsync(message);
                    }
                }

                if (!response.IsSuccessStatusCode)
                {
                    return(new NotFoundResult());
                }

                var image = await response.Content.ReadAsByteArrayAsync();

                icon = new Icon
                {
                    Image  = image,
                    Format = response.Content.Headers.ContentType.MediaType
                };

                // Only cache smaller images (<= 50kb)
                if (image.Length <= 50012)
                {
                    _memoryCache.Set(mappedDomain, icon, new MemoryCacheEntryOptions
                    {
                        AbsoluteExpirationRelativeToNow = new TimeSpan(_iconsSettings.CacheHours, 0, 0)
                    });
                }
            }

            return(new FileContentResult(icon.Image, icon.Format));
        }