mirror of
https://github.com/bitwarden/server.git
synced 2024-11-23 12:25:16 +01:00
domain mapping service and more cleanup
This commit is contained in:
parent
fc3425dfb7
commit
4d7bd85490
@ -2,6 +2,7 @@
|
|||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Bit.Icons.Models;
|
using Bit.Icons.Models;
|
||||||
|
using Bit.Icons.Services;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
@ -11,14 +12,18 @@ namespace Bit.Icons.Controllers
|
|||||||
[Route("")]
|
[Route("")]
|
||||||
public class IconsController : Controller
|
public class IconsController : Controller
|
||||||
{
|
{
|
||||||
|
private static readonly HttpClient _httpClient = new HttpClient();
|
||||||
private readonly IMemoryCache _memoryCache;
|
private readonly IMemoryCache _memoryCache;
|
||||||
|
private readonly IDomainMappingService _domainMappingService;
|
||||||
private readonly IconsSettings _iconsSettings;
|
private readonly IconsSettings _iconsSettings;
|
||||||
|
|
||||||
public IconsController(
|
public IconsController(
|
||||||
IMemoryCache memoryCache,
|
IMemoryCache memoryCache,
|
||||||
|
IDomainMappingService domainMappingService,
|
||||||
IOptions<IconsSettings> iconsSettingsOptions)
|
IOptions<IconsSettings> iconsSettingsOptions)
|
||||||
{
|
{
|
||||||
_memoryCache = memoryCache;
|
_memoryCache = memoryCache;
|
||||||
|
_domainMappingService = domainMappingService;
|
||||||
_iconsSettings = iconsSettingsOptions.Value;
|
_iconsSettings = iconsSettingsOptions.Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,13 +40,13 @@ namespace Bit.Icons.Controllers
|
|||||||
return new BadRequestResult();
|
return new BadRequestResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
var iconUrl = BuildIconUrl(uri);
|
var mappedDomain = _domainMappingService.MapDomain(uri.Host);
|
||||||
var icon = await _memoryCache.GetOrCreateAsync(domain, async entry =>
|
var icon = await _memoryCache.GetOrCreateAsync(mappedDomain, async entry =>
|
||||||
{
|
{
|
||||||
entry.AbsoluteExpiration = DateTime.Now.AddDays(1);
|
entry.AbsoluteExpiration = DateTime.UtcNow.AddHours(_iconsSettings.CacheHours);
|
||||||
|
|
||||||
var httpClient = new HttpClient();
|
var iconUrl = $"{_iconsSettings.BestIconBaseUrl}/icon?url={mappedDomain}&size=16..24..200";
|
||||||
var response = await httpClient.GetAsync(iconUrl);
|
var response = await _httpClient.GetAsync(iconUrl);
|
||||||
if(!response.IsSuccessStatusCode)
|
if(!response.IsSuccessStatusCode)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
@ -61,10 +66,5 @@ namespace Bit.Icons.Controllers
|
|||||||
|
|
||||||
return new FileContentResult(icon.Image, icon.Format);
|
return new FileContentResult(icon.Image, icon.Format);
|
||||||
}
|
}
|
||||||
|
|
||||||
private string BuildIconUrl(Uri uri)
|
|
||||||
{
|
|
||||||
return $"{_iconsSettings.BestIconBaseUrl}/icon?url={uri.Host}&size=16..24..200";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,5 +3,6 @@
|
|||||||
public class IconsSettings
|
public class IconsSettings
|
||||||
{
|
{
|
||||||
public virtual string BestIconBaseUrl { get; set; }
|
public virtual string BestIconBaseUrl { get; set; }
|
||||||
|
public virtual int CacheHours { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
24
src/Icons/Services/DomainMappingService.cs
Normal file
24
src/Icons/Services/DomainMappingService.cs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace Bit.Icons.Services
|
||||||
|
{
|
||||||
|
public class DomainMappingService : IDomainMappingService
|
||||||
|
{
|
||||||
|
private readonly Dictionary<string, string> _map = new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
["vault.bitwarden.com"] = "bitwarden.com",
|
||||||
|
["accounts.google.com"] = "google.com",
|
||||||
|
// TODO: Add others here
|
||||||
|
};
|
||||||
|
|
||||||
|
public string MapDomain(string hostname)
|
||||||
|
{
|
||||||
|
if(_map.ContainsKey(hostname))
|
||||||
|
{
|
||||||
|
return _map[hostname];
|
||||||
|
}
|
||||||
|
|
||||||
|
return hostname;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
7
src/Icons/Services/IDomainMappingService.cs
Normal file
7
src/Icons/Services/IDomainMappingService.cs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
namespace Bit.Icons.Services
|
||||||
|
{
|
||||||
|
public interface IDomainMappingService
|
||||||
|
{
|
||||||
|
string MapDomain(string hostname);
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using Bit.Icons.Services;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
@ -20,6 +21,7 @@ namespace Bit.Icons
|
|||||||
services.AddOptions();
|
services.AddOptions();
|
||||||
services.Configure<IconsSettings>(Configuration.GetSection("IconsSettings"));
|
services.Configure<IconsSettings>(Configuration.GetSection("IconsSettings"));
|
||||||
services.AddMemoryCache();
|
services.AddMemoryCache();
|
||||||
|
services.AddSingleton<IDomainMappingService, DomainMappingService>();
|
||||||
services.AddMvc();
|
services.AddMvc();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"iconsSettings": {
|
"iconsSettings": {
|
||||||
"BestIconBaseUrl": "https://icons.better-idea.org"
|
"BestIconBaseUrl": "https://icons.better-idea.org",
|
||||||
|
"CacheHours": 24
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user