2020-06-28 21:44:35 -05:00
|
|
|
using System;
|
2021-12-27 13:46:31 +09:00
|
|
|
using System.Globalization;
|
2017-10-27 12:08:18 +09:00
|
|
|
using System.Linq;
|
2023-04-19 21:13:31 +09:00
|
|
|
using System.Net.Http;
|
2017-10-27 12:08:18 +09:00
|
|
|
using System.Reflection;
|
|
|
|
using System.Text;
|
2021-10-11 12:32:09 +09:00
|
|
|
using BTCPayServer.Configuration;
|
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;
|
2021-10-11 12:32:09 +09:00
|
|
|
using Microsoft.Extensions.Options;
|
2020-06-28 17:55:27 +09:00
|
|
|
using NBitcoin;
|
2017-10-27 12:08:18 +09:00
|
|
|
|
|
|
|
namespace BTCPayServer.Services
|
|
|
|
{
|
|
|
|
public class BTCPayServerEnvironment
|
|
|
|
{
|
2020-06-28 22:07:48 -05:00
|
|
|
readonly TorServices torServices;
|
2022-11-22 03:32:19 +09:00
|
|
|
public BTCPayServerEnvironment(IWebHostEnvironment env, BTCPayNetworkProvider provider, TorServices torServices, BTCPayServerOptions opts)
|
2017-10-27 17:53:04 +09:00
|
|
|
{
|
2023-04-19 21:13:31 +09:00
|
|
|
Version = GetInformationalVersion();
|
2022-11-22 21:37:07 +09:00
|
|
|
Commit = typeof(BTCPayServerEnvironment).GetTypeInfo().Assembly.GetCustomAttribute<GitCommitAttribute>()?.ShortSHA;
|
2017-10-27 12:08:18 +09:00
|
|
|
#if DEBUG
|
2017-10-27 17:53:04 +09:00
|
|
|
Build = "Debug";
|
2017-10-27 12:08:18 +09:00
|
|
|
#else
|
|
|
|
Build = "Release";
|
|
|
|
#endif
|
2020-07-30 22:06:54 +09:00
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
Environment = env;
|
2018-04-19 16:54:25 +09:00
|
|
|
NetworkType = provider.NetworkType;
|
2019-03-18 17:07:39 +09:00
|
|
|
this.torServices = torServices;
|
2021-10-11 12:32:09 +09:00
|
|
|
CheatMode = opts.CheatMode;
|
2017-10-27 17:53:04 +09:00
|
|
|
}
|
2023-04-19 21:13:31 +09:00
|
|
|
|
|
|
|
internal static string GetInformationalVersion()
|
|
|
|
{
|
|
|
|
return typeof(BTCPayServerEnvironment).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
|
|
|
|
}
|
|
|
|
|
2019-10-03 17:06:49 +09:00
|
|
|
public IWebHostEnvironment Environment
|
2017-10-27 17:53:04 +09:00
|
|
|
{
|
|
|
|
get; set;
|
|
|
|
}
|
2018-07-11 22:40:10 +09:00
|
|
|
|
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-07-11 22:40:10 +09:00
|
|
|
|
2021-10-11 12:32:09 +09:00
|
|
|
public bool CheatMode { get; set; }
|
2021-01-27 14:39:38 +09:00
|
|
|
public ChainName NetworkType { get; set; }
|
2017-10-27 17:53:04 +09:00
|
|
|
public string Version
|
|
|
|
{
|
|
|
|
get; set;
|
|
|
|
}
|
|
|
|
public string Build
|
|
|
|
{
|
|
|
|
get; set;
|
|
|
|
}
|
2018-02-26 00:48:12 +09:00
|
|
|
|
2020-07-30 20:43:44 -05:00
|
|
|
public bool IsDeveloping
|
2018-02-26 00:48:12 +09:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2021-03-02 11:11:58 +09:00
|
|
|
return NetworkType == ChainName.Regtest && Environment.IsDevelopment();
|
2018-02-26 00:48:12 +09:00
|
|
|
}
|
|
|
|
}
|
2019-04-04 14:28:11 +09:00
|
|
|
|
2022-11-22 03:32:19 +09:00
|
|
|
public bool IsSecure(HttpContext httpContext)
|
2019-04-04 14:28:11 +09:00
|
|
|
{
|
2022-11-22 03:32:19 +09:00
|
|
|
return NetworkType != ChainName.Mainnet ||
|
|
|
|
httpContext.Request.Scheme == "https" ||
|
|
|
|
httpContext.Request.Host.Host.EndsWith(".onion", StringComparison.OrdinalIgnoreCase) ||
|
|
|
|
Extensions.IsLocalNetwork(httpContext.Request.Host.Host);
|
2019-04-04 14:28:11 +09:00
|
|
|
}
|
|
|
|
|
2022-11-22 21:37:07 +09:00
|
|
|
public string Commit { get; set; }
|
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
StringBuilder txt = new StringBuilder();
|
2022-01-24 12:01:17 +01:00
|
|
|
txt.Append(CultureInfo.InvariantCulture, $"© BTCPay Server v{Version}");
|
2022-11-22 21:37:07 +09:00
|
|
|
if (Commit != null)
|
|
|
|
txt.Append($"+{Commit}");
|
2017-12-17 22:50:05 +09:00
|
|
|
if (!Environment.IsProduction() || !Build.Equals("Release", StringComparison.OrdinalIgnoreCase))
|
2017-10-27 17:53:04 +09:00
|
|
|
{
|
2022-01-24 12:01:17 +01:00
|
|
|
txt.Append(CultureInfo.InvariantCulture, $" Environment: {Environment.EnvironmentName} ({Build})");
|
2017-10-27 17:53:04 +09:00
|
|
|
}
|
|
|
|
return txt.ToString();
|
|
|
|
}
|
|
|
|
}
|
2017-10-27 12:08:18 +09:00
|
|
|
}
|