Files
server/src/Admin/Controllers/HomeController.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

99 lines
3.8 KiB
C#
Raw Normal View History

using System.Diagnostics;
2018-03-23 12:50:20 -04:00
using System.Net.Http;
using System.Text.Json;
using System.Threading;
2018-03-23 12:50:20 -04:00
using System.Threading.Tasks;
2018-03-21 12:57:43 -04:00
using Bit.Admin.Models;
using Bit.Core.Settings;
2018-03-21 14:26:49 -04:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
2018-03-21 12:57:43 -04:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
2018-03-21 12:57:43 -04:00
namespace Bit.Admin.Controllers
{
public class HomeController : Controller
{
2018-03-23 12:50:20 -04:00
private readonly GlobalSettings _globalSettings;
private readonly HttpClient _httpClient = new HttpClient();
private readonly ILogger<HomeController> _logger;
2018-03-23 12:50:20 -04:00
public HomeController(GlobalSettings globalSettings, ILogger<HomeController> logger)
2018-03-23 12:50:20 -04:00
{
_globalSettings = globalSettings;
_logger = logger;
2018-03-23 12:50:20 -04:00
}
2018-03-21 14:26:49 -04:00
[Authorize]
2018-03-21 12:57:43 -04:00
public IActionResult Index()
{
2018-03-23 12:50:20 -04:00
return View(new HomeModel
{
GlobalSettings = _globalSettings,
CurrentVersion = Core.Utilities.CoreHelpers.GetVersion()
});
2018-03-21 12:57:43 -04:00
}
public IActionResult Error()
{
return View(new ErrorViewModel
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier
});
}
2018-03-23 12:50:20 -04:00
public async Task<IActionResult> GetLatestDockerHubVersion(string repository, CancellationToken cancellationToken)
2018-03-23 12:50:20 -04:00
{
var requestUri = $"https://hub.docker.com/v2/repositories/bitwarden/{repository}/tags/";
2018-03-23 12:50:20 -04:00
try
{
var response = await _httpClient.GetAsync(requestUri, cancellationToken);
if (response.IsSuccessStatusCode)
2018-03-23 12:50:20 -04:00
{
using var jsonDocument = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync(cancellationToken), cancellationToken: cancellationToken);
var root = jsonDocument.RootElement;
var results = root.GetProperty("results");
foreach (var result in results.EnumerateArray())
2018-03-23 12:50:20 -04:00
{
var name = result.GetProperty("name").GetString();
if (!string.IsNullOrWhiteSpace(name) && name.Length > 0 && char.IsNumber(name[0]))
2018-03-23 12:50:20 -04:00
{
return new JsonResult(name);
}
}
}
}
catch (HttpRequestException e)
{
_logger.LogError(e, $"Error encountered while sending GET request to {requestUri}");
return new JsonResult("Unable to fetch latest version") { StatusCode = StatusCodes.Status500InternalServerError };
}
2018-03-23 12:50:20 -04:00
return new JsonResult("-");
}
public async Task<IActionResult> GetInstalledWebVersion(CancellationToken cancellationToken)
2018-03-23 12:50:20 -04:00
{
var requestUri = $"{_globalSettings.BaseServiceUri.InternalVault}/version.json";
2018-03-23 12:50:20 -04:00
try
{
var response = await _httpClient.GetAsync(requestUri, cancellationToken);
if (response.IsSuccessStatusCode)
2018-03-23 12:50:20 -04:00
{
using var jsonDocument = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync(cancellationToken), cancellationToken: cancellationToken);
var root = jsonDocument.RootElement;
return new JsonResult(root.GetProperty("version").GetString());
2018-03-23 12:50:20 -04:00
}
}
catch (HttpRequestException e)
{
_logger.LogError(e, $"Error encountered while sending GET request to {requestUri}");
return new JsonResult("Unable to fetch installed version") { StatusCode = StatusCodes.Status500InternalServerError };
}
2018-03-23 12:50:20 -04:00
return new JsonResult("-");
}
2018-03-21 12:57:43 -04:00
}
}