using System.IO;
using System.Net;
namespace MQTTnet.Server.Configuration
{
///
/// Listen Entry Settings Model
///
public class TcpEndPointModel
{
///
/// Path to Certificate
///
public string CertificatePath { get; set; }
///
/// Enabled / Disable
///
public bool Enabled { get; set; } = true;
///
/// Listen Address
///
public string IPv4 { get; set; }
///
/// Listen Address
///
public string IPv6 { get; set; }
///
/// Listen Port
///
public int Port { get; set; } = 1883;
///
/// Read Certificate file
///
///
public byte[] ReadCertificate()
{
if (string.IsNullOrEmpty(CertificatePath) || string.IsNullOrWhiteSpace(CertificatePath))
{
throw new FileNotFoundException("No path set");
}
if (!File.Exists(CertificatePath))
{
throw new FileNotFoundException($"Could not find Certificate in path: {CertificatePath}");
}
return File.ReadAllBytes(CertificatePath);
}
///
/// Read IPv4
///
///
public bool TryReadIPv4(out IPAddress address)
{
if (IPv4 == "*")
{
address = IPAddress.Any;
return true;
}
if (IPv4 == "localhost")
{
address = IPAddress.Loopback;
return true;
}
if (IPv4 == "disable")
{
address = IPAddress.None;
return true;
}
if (IPAddress.TryParse(IPv4, out var ip))
{
address = ip;
return true;
}
throw new System.Exception($"Could not parse IPv4 address: {IPv4}");
}
///
/// Read IPv6
///
///
public bool TryReadIPv6(out IPAddress address)
{
if (IPv6 == "*")
{
address = IPAddress.IPv6Any;
return true;
}
if (IPv6 == "localhost")
{
address = IPAddress.IPv6Loopback;
return true;
}
if (IPv6 == "disable")
{
address = IPAddress.None;
return true;
}
if (IPAddress.TryParse(IPv6, out var ip))
{
address = ip;
return true;
}
throw new System.Exception($"Could not parse IPv6 address: {IPv6}");
}
}
}