2020-06-29 04:44:35 +02:00
|
|
|
using System;
|
2017-10-27 05:08:18 +02:00
|
|
|
using System.Linq;
|
|
|
|
using System.Reflection;
|
|
|
|
using System.Text;
|
2021-10-11 05:32:09 +02:00
|
|
|
using BTCPayServer.Configuration;
|
2019-10-03 10:06:49 +02:00
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2020-06-28 10:55:27 +02:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2019-10-03 11:00:07 +02:00
|
|
|
using Microsoft.Extensions.Hosting;
|
2021-10-11 05:32:09 +02:00
|
|
|
using Microsoft.Extensions.Options;
|
2020-06-28 10:55:27 +02:00
|
|
|
using NBitcoin;
|
2017-10-27 05:08:18 +02:00
|
|
|
|
|
|
|
namespace BTCPayServer.Services
|
|
|
|
{
|
|
|
|
public class BTCPayServerEnvironment
|
|
|
|
{
|
2020-06-29 05:07:48 +02:00
|
|
|
readonly IHttpContextAccessor httpContext;
|
|
|
|
readonly TorServices torServices;
|
2021-10-11 05:32:09 +02:00
|
|
|
public BTCPayServerEnvironment(IWebHostEnvironment env, BTCPayNetworkProvider provider, IHttpContextAccessor httpContext, TorServices torServices, BTCPayServerOptions opts)
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
2019-03-04 10:33:57 +01:00
|
|
|
this.httpContext = httpContext;
|
2017-10-27 10:53:04 +02:00
|
|
|
Version = typeof(BTCPayServerEnvironment).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
|
2017-10-27 05:08:18 +02:00
|
|
|
#if DEBUG
|
2017-10-27 10:53:04 +02:00
|
|
|
Build = "Debug";
|
2017-10-27 05:08:18 +02:00
|
|
|
#else
|
|
|
|
Build = "Release";
|
|
|
|
#endif
|
2020-07-30 15:06:54 +02:00
|
|
|
#if ALTCOINS
|
|
|
|
AltcoinsVersion = true;
|
|
|
|
#else
|
|
|
|
AltcoinsVersion = false;
|
|
|
|
#endif
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
Environment = env;
|
2018-04-19 09:54:25 +02:00
|
|
|
NetworkType = provider.NetworkType;
|
2019-03-18 09:07:39 +01:00
|
|
|
this.torServices = torServices;
|
2021-10-11 05:32:09 +02:00
|
|
|
CheatMode = opts.CheatMode;
|
2017-10-27 10:53:04 +02:00
|
|
|
}
|
2019-10-03 10:06:49 +02:00
|
|
|
public IWebHostEnvironment Environment
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
|
|
|
get; set;
|
|
|
|
}
|
2018-07-11 15:40:10 +02:00
|
|
|
|
2019-03-04 10:33:57 +01:00
|
|
|
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 09:07:39 +01:00
|
|
|
public string OnionUrl => this.torServices.Services.Where(s => s.ServiceType == TorServiceType.BTCPayServer)
|
|
|
|
.Select(s => $"http://{s.OnionHost}").FirstOrDefault();
|
2018-07-11 15:40:10 +02:00
|
|
|
|
2021-10-11 05:32:09 +02:00
|
|
|
public bool CheatMode { get; set; }
|
2021-01-27 06:39:38 +01:00
|
|
|
public ChainName NetworkType { get; set; }
|
2017-10-27 10:53:04 +02:00
|
|
|
public string Version
|
|
|
|
{
|
|
|
|
get; set;
|
|
|
|
}
|
|
|
|
public string Build
|
|
|
|
{
|
|
|
|
get; set;
|
|
|
|
}
|
2020-07-30 15:06:54 +02:00
|
|
|
public bool AltcoinsVersion { get; set; }
|
2018-02-25 16:48:12 +01:00
|
|
|
|
2020-07-31 03:43:44 +02:00
|
|
|
public bool IsDeveloping
|
2018-02-25 16:48:12 +01:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2021-03-02 03:11:58 +01:00
|
|
|
return NetworkType == ChainName.Regtest && Environment.IsDevelopment();
|
2018-02-25 16:48:12 +01:00
|
|
|
}
|
|
|
|
}
|
2019-04-04 07:28:11 +02:00
|
|
|
|
|
|
|
public bool IsSecure
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2021-01-27 06:39:38 +01:00
|
|
|
return NetworkType != ChainName.Mainnet ||
|
2019-04-04 07:28:11 +02:00
|
|
|
httpContext.HttpContext.Request.Scheme == "https" ||
|
|
|
|
httpContext.HttpContext.Request.Host.Host.EndsWith(".onion", StringComparison.OrdinalIgnoreCase) ||
|
|
|
|
Extensions.IsLocalNetwork(httpContext.HttpContext.Request.Host.Host);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 10:55:27 +02:00
|
|
|
public HttpContext Context => httpContext.HttpContext;
|
2019-05-29 16:33:31 +02:00
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
StringBuilder txt = new StringBuilder();
|
|
|
|
txt.Append($"@Copyright BTCPayServer v{Version}");
|
2020-07-30 15:06:54 +02:00
|
|
|
if (AltcoinsVersion)
|
|
|
|
txt.Append($" (altcoins)");
|
2017-12-17 14:50:05 +01:00
|
|
|
if (!Environment.IsProduction() || !Build.Equals("Release", StringComparison.OrdinalIgnoreCase))
|
2017-10-27 10:53:04 +02:00
|
|
|
{
|
|
|
|
txt.Append($" Environment: {Environment.EnvironmentName} Build: {Build}");
|
|
|
|
}
|
|
|
|
return txt.ToString();
|
|
|
|
}
|
|
|
|
}
|
2017-10-27 05:08:18 +02:00
|
|
|
}
|