Files
server/src/Icons/Controllers/IconsController.cs

127 lines
5.0 KiB
C#
Raw Normal View History

using System;
2018-03-03 14:12:57 -05:00
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
2017-10-09 13:35:07 -04:00
using Bit.Icons.Models;
using Bit.Icons.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
2017-10-09 13:35:07 -04:00
namespace Bit.Icons.Controllers
{
2017-10-09 13:35:07 -04:00
[Route("")]
2017-10-09 14:02:57 -04:00
public class IconsController : Controller
{
private static readonly HttpClient _httpClient = new HttpClient(new HttpClientHandler
{
2017-10-21 23:04:51 -04:00
AllowAutoRedirect = false,
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
});
2018-03-03 14:12:57 -05:00
private static readonly HashSet<string> _allowedMediaTypes = new HashSet<string>{
"image/png",
"image/x-icon",
"image/jpeg"
};
2017-10-09 13:35:07 -04:00
private readonly IMemoryCache _memoryCache;
private readonly IDomainMappingService _domainMappingService;
2017-10-09 14:02:57 -04:00
private readonly IconsSettings _iconsSettings;
2017-10-09 14:02:57 -04:00
public IconsController(
IMemoryCache memoryCache,
IDomainMappingService domainMappingService,
2017-10-09 14:54:32 -04:00
IconsSettings iconsSettings)
{
2017-10-09 13:35:07 -04:00
_memoryCache = memoryCache;
_domainMappingService = domainMappingService;
2017-10-09 14:54:32 -04:00
_iconsSettings = iconsSettings;
}
2017-10-12 10:00:44 -04:00
[HttpGet("{hostname}/icon.png")]
2017-10-25 12:52:55 -04:00
[ResponseCache(Duration = 604800 /*7 days*/)]
2017-10-12 10:00:44 -04:00
public async Task<IActionResult> Get(string hostname)
{
2017-10-12 10:00:44 -04:00
if(string.IsNullOrWhiteSpace(hostname) || !hostname.Contains("."))
2017-10-09 14:34:44 -04:00
{
return new BadRequestResult();
}
2017-10-12 10:00:44 -04:00
var url = $"http://{hostname}";
2017-10-09 16:21:37 -04:00
if(!Uri.TryCreate(url, UriKind.Absolute, out Uri uri))
2017-10-09 13:35:07 -04:00
{
return new BadRequestResult();
}
var mappedDomain = _domainMappingService.MapDomain(uri.Host);
2017-10-12 10:41:13 -04:00
if(!_memoryCache.TryGetValue(mappedDomain, out Icon icon))
{
2017-10-25 12:54:00 -04:00
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);
2017-11-10 21:48:49 -05:00
response = await FollowRedirectsAsync(response, 1);
2018-03-03 14:12:57 -05:00
if(!response.IsSuccessStatusCode ||
!_allowedMediaTypes.Contains(response.Content.Headers.ContentType.MediaType))
{
2017-10-12 10:41:13 -04:00
return new NotFoundResult();
}
2017-10-12 10:41:13 -04:00
var image = await response.Content.ReadAsByteArrayAsync();
icon = new Icon
2017-10-09 13:35:07 -04:00
{
2017-10-12 10:41:13 -04:00
Image = image,
2017-10-09 13:35:07 -04:00
Format = response.Content.Headers.ContentType.MediaType
};
2017-10-12 10:41:13 -04:00
// 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);
}
2017-11-10 21:48:49 -05:00
private async Task<HttpResponseMessage> FollowRedirectsAsync(HttpResponseMessage response, int followCount)
{
if(response.IsSuccessStatusCode || followCount > 2)
{
return response;
}
if((response.StatusCode == HttpStatusCode.Redirect || response.StatusCode == HttpStatusCode.MovedPermanently) &&
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);
response = await FollowRedirectsAsync(response, followCount++);
}
}
return response;
}
}
}