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

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

87 lines
3.0 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;
2018-03-21 12:57:43 -04:00
using Microsoft.AspNetCore.Mvc;
namespace Bit.Admin.Controllers
{
public class HomeController : Controller
{
2018-03-23 12:50:20 -04:00
private readonly GlobalSettings _globalSettings;
private HttpClient _httpClient = new HttpClient();
public HomeController(GlobalSettings globalSettings)
{
_globalSettings = globalSettings;
}
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
{
try
{
var response = await _httpClient.GetAsync(
$"https://hub.docker.com/v2/repositories/bitwarden/{repository}/tags/", 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) { }
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
{
try
{
var response = await _httpClient.GetAsync(
$"{_globalSettings.BaseServiceUri.InternalVault}/version.json", 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) { }
2018-03-23 12:50:20 -04:00
return new JsonResult("-");
}
2018-03-21 12:57:43 -04:00
}
}