btcpayserver/BTCPayServer/Services/DynamicDnsSettings.cs

96 lines
3.6 KiB
C#
Raw Normal View History

2019-07-24 10:59:30 +02:00
using System;
using System.Reflection;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using BTCPayServer.Hosting;
using NBitcoin.DataEncoders;
using Newtonsoft.Json;
namespace BTCPayServer.Services
{
public class DynamicDnsSettings
2019-07-25 11:29:18 +02:00
{
public List<DynamicDnsService> Services { get; set; } = new List<DynamicDnsService>();
2019-09-06 10:49:38 +02:00
public override string ToString()
{
return String.Empty;
}
2019-07-25 11:29:18 +02:00
}
public class DynamicDnsService
2019-07-24 10:59:30 +02:00
{
[Display(Name = "Url of the Dynamic DNS service you are using")]
2019-07-25 11:29:18 +02:00
[Required]
2019-07-24 10:59:30 +02:00
public string ServiceUrl { get; set; }
public string Login { get; set; }
[DataType(DataType.Password)]
public string Password { get; set; }
[Display(Name = "Your dynamic DNS hostname")]
2019-07-25 11:29:18 +02:00
[Required]
2019-07-24 10:59:30 +02:00
public string Hostname { get; set; }
2019-07-25 11:29:18 +02:00
public bool Enabled { get; set; } = true;
2019-07-24 10:59:30 +02:00
[JsonConverter(typeof(NBitcoin.JsonConverters.DateTimeToUnixTimeConverter))]
public DateTimeOffset? LastUpdated { get; set; }
public async Task<string> SendUpdateRequest(HttpClient httpClient)
{
string errorMessage = null;
try
{
var result = await httpClient.SendAsync(CreateUpdateRequest());
if (!result.IsSuccessStatusCode)
{
try
{
errorMessage = await result.Content.ReadAsStringAsync();
}
catch { }
2019-07-25 12:36:03 +02:00
errorMessage = $"Error: Invalid return code {result.StatusCode}, expected 200 ({errorMessage.Trim()}) for hostname '{Hostname}'";
2019-07-24 10:59:30 +02:00
}
}
catch (Exception ex)
{
2019-07-25 12:36:03 +02:00
errorMessage = $"Error: While querying the Dynamic DNS service ({ex.Message}) for hostname '{Hostname}'";
2019-07-24 10:59:30 +02:00
}
return errorMessage;
}
public HttpRequestMessage CreateUpdateRequest()
{
HttpRequestMessage webRequest = new HttpRequestMessage();
if (!Uri.TryCreate(ServiceUrl, UriKind.Absolute, out var uri) || uri.HostNameType == UriHostNameType.Unknown)
{
2019-07-25 11:29:18 +02:00
throw new FormatException($"Invalid service url");
2019-07-24 10:59:30 +02:00
}
var builder = new UriBuilder(uri);
if (!string.IsNullOrEmpty(Login))
{
builder.UserName = Login;
}
if (!string.IsNullOrEmpty(Password))
{
builder.Password = Password;
}
builder.UserName = builder.UserName ?? string.Empty;
builder.Password = builder.Password ?? string.Empty;
builder.Query = $"hostname={Hostname}";
webRequest.Headers.Authorization = new AuthenticationHeaderValue("Basic", Encoders.Base64.EncodeData(new UTF8Encoding(false).GetBytes($"{builder.UserName}:{builder.Password}")));
webRequest.Headers.TryAddWithoutValidation("User-Agent", $"BTCPayServer/{GetVersion()} btcpayserver@gmail.com");
webRequest.Method = HttpMethod.Get;
2019-07-25 16:26:56 +02:00
builder.UserName = string.Empty;
builder.Password = string.Empty;
2019-07-24 10:59:30 +02:00
webRequest.RequestUri = builder.Uri;
return webRequest;
}
private string GetVersion()
{
return typeof(BTCPayServerEnvironment).GetTypeInfo().Assembly.GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
}
}
}