mirror of
https://github.com/bitwarden/server.git
synced 2025-04-17 19:18:16 -05:00
version info in response headers
This commit is contained in:
parent
0e2c86b1f5
commit
eb9bb315c3
@ -158,11 +158,8 @@ namespace Bit.Api
|
|||||||
})
|
})
|
||||||
.AddDebug();
|
.AddDebug();
|
||||||
|
|
||||||
// Forwarded headers
|
// Default Middleware
|
||||||
if(!env.IsDevelopment())
|
app.UseDefaultMiddleware(env);
|
||||||
{
|
|
||||||
app.UseForwardedHeadersForAzure();
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!globalSettings.SelfHosted)
|
if(!globalSettings.SelfHosted)
|
||||||
{
|
{
|
||||||
|
@ -69,11 +69,8 @@ namespace Bit.Billing
|
|||||||
app.UseDeveloperExceptionPage();
|
app.UseDeveloperExceptionPage();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Forwarded headers
|
// Default Middleware
|
||||||
if(!env.IsDevelopment())
|
app.UseDefaultMiddleware(env);
|
||||||
{
|
|
||||||
app.UseForwardedHeadersForAzure();
|
|
||||||
}
|
|
||||||
|
|
||||||
app.UseMvc();
|
app.UseMvc();
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Reflection;
|
using Bit.Core.Utilities;
|
||||||
|
|
||||||
namespace Bit.Core.Models.Api
|
namespace Bit.Core.Models.Api
|
||||||
{
|
{
|
||||||
@ -8,13 +8,12 @@ namespace Bit.Core.Models.Api
|
|||||||
public VersionResponseModel()
|
public VersionResponseModel()
|
||||||
: base("version")
|
: base("version")
|
||||||
{
|
{
|
||||||
Version = Assembly.GetEntryAssembly()
|
var info = CoreHelpers.GetVersionInfo();
|
||||||
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
|
Version = info.version;
|
||||||
.InformationalVersion;
|
VersionWeight = info.versionWeight;
|
||||||
VersionInt = Convert.ToInt32(Version.Replace(".", string.Empty));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Version { get; set; }
|
public string Version { get; set; }
|
||||||
public int VersionInt { get; set; }
|
public int VersionWeight { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,8 @@ namespace Bit.Core.Utilities
|
|||||||
private static readonly long _baseDateTicks = new DateTime(1900, 1, 1).Ticks;
|
private static readonly long _baseDateTicks = new DateTime(1900, 1, 1).Ticks;
|
||||||
private static readonly DateTime _epoc = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
private static readonly DateTime _epoc = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||||
private static readonly Random _random = new Random();
|
private static readonly Random _random = new Random();
|
||||||
|
private static string _version;
|
||||||
|
private static int _versionWeight;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Generate sequential Guid for Sql Server.
|
/// Generate sequential Guid for Sql Server.
|
||||||
@ -372,5 +374,26 @@ namespace Bit.Core.Utilities
|
|||||||
|
|
||||||
return val.ToString();
|
return val.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static (string version, int versionWeight) GetVersionInfo()
|
||||||
|
{
|
||||||
|
if(string.IsNullOrWhiteSpace(_version))
|
||||||
|
{
|
||||||
|
_version = Assembly.GetEntryAssembly()
|
||||||
|
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
|
||||||
|
.InformationalVersion;
|
||||||
|
|
||||||
|
var dashIndex = _version.IndexOf('-');
|
||||||
|
var trimmedVersion = dashIndex > 0 ? _version.Substring(0, dashIndex) : _version;
|
||||||
|
|
||||||
|
var semVerParts = trimmedVersion.Split('.').Reverse().ToArray();
|
||||||
|
for(var i = 0; i < semVerParts.Length; i++)
|
||||||
|
{
|
||||||
|
_versionWeight += (i + 1) * Convert.ToInt32(semVerParts[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (_version, _versionWeight);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,12 +15,14 @@ using Microsoft.AspNetCore.HttpOverrides;
|
|||||||
using Microsoft.AspNetCore.Identity;
|
using Microsoft.AspNetCore.Identity;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
#if NET461
|
#if NET461
|
||||||
using Microsoft.WindowsAzure.Storage;
|
using Microsoft.WindowsAzure.Storage;
|
||||||
#endif
|
#endif
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using SqlServerRepos = Bit.Core.Repositories.SqlServer;
|
using SqlServerRepos = Bit.Core.Repositories.SqlServer;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Bit.Core.Utilities
|
namespace Bit.Core.Utilities
|
||||||
{
|
{
|
||||||
@ -267,19 +269,35 @@ namespace Bit.Core.Utilities
|
|||||||
return globalSettings;
|
return globalSettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void UseForwardedHeadersForAzure(this IApplicationBuilder app)
|
public static void UseDefaultMiddleware(this IApplicationBuilder app, IHostingEnvironment env)
|
||||||
{
|
{
|
||||||
|
if(!env.IsDevelopment())
|
||||||
|
{
|
||||||
|
// Adjust headers for proxy.
|
||||||
// ref: https://github.com/aspnet/Docs/issues/2384
|
// ref: https://github.com/aspnet/Docs/issues/2384
|
||||||
var forwardOptions = new ForwardedHeadersOptions
|
var forwardOptions = new ForwardedHeadersOptions
|
||||||
{
|
{
|
||||||
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto,
|
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto,
|
||||||
RequireHeaderSymmetry = false
|
RequireHeaderSymmetry = false
|
||||||
};
|
};
|
||||||
|
|
||||||
forwardOptions.KnownNetworks.Clear();
|
forwardOptions.KnownNetworks.Clear();
|
||||||
forwardOptions.KnownProxies.Clear();
|
forwardOptions.KnownProxies.Clear();
|
||||||
|
|
||||||
app.UseForwardedHeaders(forwardOptions);
|
app.UseForwardedHeaders(forwardOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add version information to response headers
|
||||||
|
app.Use(async (httpContext, next) =>
|
||||||
|
{
|
||||||
|
httpContext.Response.OnStarting((state) =>
|
||||||
|
{
|
||||||
|
var info = CoreHelpers.GetVersionInfo();
|
||||||
|
httpContext.Response.Headers.Append("Version", info.version);
|
||||||
|
httpContext.Response.Headers.Append("VersionWeight", info.versionWeight.ToString());
|
||||||
|
return Task.FromResult(0);
|
||||||
|
}, null);
|
||||||
|
|
||||||
|
await next.Invoke();
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -72,11 +72,8 @@ namespace Bit.Identity
|
|||||||
.AddConsole()
|
.AddConsole()
|
||||||
.AddDebug();
|
.AddDebug();
|
||||||
|
|
||||||
// Forwarded headers
|
// Default Middleware
|
||||||
if(!env.IsDevelopment())
|
app.UseDefaultMiddleware(env);
|
||||||
{
|
|
||||||
app.UseForwardedHeadersForAzure();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add IdentityServer to the request pipeline.
|
// Add IdentityServer to the request pipeline.
|
||||||
app.UseIdentityServer();
|
app.UseIdentityServer();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user