btcpayserver/BTCPayServer/Services/BTCPayServerEnvironment.cs

94 lines
3.1 KiB
C#
Raw Normal View History

2020-06-28 21:44:35 -05:00
using System;
using System.Linq;
using System.Reflection;
using System.Text;
2019-10-03 17:06:49 +09:00
using Microsoft.AspNetCore.Hosting;
2020-06-28 17:55:27 +09:00
using Microsoft.AspNetCore.Http;
2019-10-03 18:00:07 +09:00
using Microsoft.Extensions.Hosting;
2020-06-28 17:55:27 +09:00
using NBitcoin;
namespace BTCPayServer.Services
{
public class BTCPayServerEnvironment
{
readonly IHttpContextAccessor httpContext;
readonly TorServices torServices;
2019-10-03 17:06:49 +09:00
public BTCPayServerEnvironment(IWebHostEnvironment env, BTCPayNetworkProvider provider, IHttpContextAccessor httpContext, TorServices torServices)
{
this.httpContext = httpContext;
Version = typeof(BTCPayServerEnvironment).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
#if DEBUG
Build = "Debug";
#else
Build = "Release";
#endif
2020-07-30 22:06:54 +09:00
#if ALTCOINS
AltcoinsVersion = true;
#else
AltcoinsVersion = false;
#endif
Environment = env;
2018-04-19 16:54:25 +09:00
NetworkType = provider.NetworkType;
2019-03-18 17:07:39 +09:00
this.torServices = torServices;
}
2019-10-03 17:06:49 +09:00
public IWebHostEnvironment Environment
{
get; set;
}
public string ExpectedDomain => httpContext.HttpContext.Request.Host.Host;
public string ExpectedHost => httpContext.HttpContext.Request.Host.Value;
public string ExpectedProtocol => httpContext.HttpContext.Request.Scheme;
2019-03-18 17:07:39 +09:00
public string OnionUrl => this.torServices.Services.Where(s => s.ServiceType == TorServiceType.BTCPayServer)
.Select(s => $"http://{s.OnionHost}").FirstOrDefault();
2018-04-19 16:54:25 +09:00
public NetworkType NetworkType { get; set; }
public string Version
{
get; set;
}
public string Build
{
get; set;
}
2020-07-30 22:06:54 +09:00
public bool AltcoinsVersion { get; set; }
2020-07-30 20:43:44 -05:00
public bool IsDeveloping
{
get
{
return DevelopmentOverride?? NetworkType == NetworkType.Regtest && Environment.IsDevelopment();
}
}
public bool IsSecure
{
get
{
return NetworkType != NetworkType.Mainnet ||
httpContext.HttpContext.Request.Scheme == "https" ||
httpContext.HttpContext.Request.Host.Host.EndsWith(".onion", StringComparison.OrdinalIgnoreCase) ||
Extensions.IsLocalNetwork(httpContext.HttpContext.Request.Host.Host);
}
}
2020-06-28 17:55:27 +09:00
public HttpContext Context => httpContext.HttpContext;
public override string ToString()
{
StringBuilder txt = new StringBuilder();
txt.Append($"@Copyright BTCPayServer v{Version}");
2020-07-30 22:06:54 +09:00
if (AltcoinsVersion)
txt.Append($" (altcoins)");
if (!Environment.IsProduction() || !Build.Equals("Release", StringComparison.OrdinalIgnoreCase))
{
txt.Append($" Environment: {Environment.EnvironmentName} Build: {Build}");
}
return txt.ToString();
}
public bool? DevelopmentOverride;
}
}