You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

125 lines
3.1 KiB

  1. using System.IO;
  2. using System.Net;
  3. namespace MQTTnet.Server.Configuration
  4. {
  5. /// <summary>
  6. /// Listen Entry Settings Model
  7. /// </summary>
  8. public class TcpEndPointModel
  9. {
  10. /// <summary>
  11. /// Path to Certificate
  12. /// </summary>
  13. public string CertificatePath { get; set; }
  14. /// <summary>
  15. /// Enabled / Disable
  16. /// </summary>
  17. public bool Enabled { get; set; } = true;
  18. /// <summary>
  19. /// Listen Address
  20. /// </summary>
  21. public string IPv4 { get; set; }
  22. /// <summary>
  23. /// Listen Address
  24. /// </summary>
  25. public string IPv6 { get; set; }
  26. /// <summary>
  27. /// Listen Port
  28. /// </summary>
  29. public int Port { get; set; } = 1883;
  30. /// <summary>
  31. /// Read Certificate file
  32. /// </summary>
  33. /// <returns></returns>
  34. public byte[] ReadCertificate()
  35. {
  36. if (string.IsNullOrEmpty(CertificatePath) || string.IsNullOrWhiteSpace(CertificatePath))
  37. {
  38. throw new FileNotFoundException("No path set");
  39. }
  40. if (!File.Exists(CertificatePath))
  41. {
  42. throw new FileNotFoundException($"Could not find Certificate in path: {CertificatePath}");
  43. }
  44. return File.ReadAllBytes(CertificatePath);
  45. }
  46. /// <summary>
  47. /// Read IPv4
  48. /// </summary>
  49. /// <returns></returns>
  50. public bool TryReadIPv4(out IPAddress address)
  51. {
  52. if (IPv4 == "*")
  53. {
  54. address = IPAddress.Parse("0.0.0.0");
  55. return true;
  56. }
  57. if (IPv4 == "localhost")
  58. {
  59. address = IPAddress.Parse("127.0.0.1");
  60. return true;
  61. }
  62. if (IPv4 == "disable")
  63. {
  64. address = IPAddress.None;
  65. return true;
  66. }
  67. if (IPAddress.TryParse(IPv4, out var ip))
  68. {
  69. address = ip;
  70. return true;
  71. }
  72. else
  73. {
  74. throw new System.Exception($"Could not parse IPv4 address: {IPv4}");
  75. }
  76. }
  77. /// <summary>
  78. /// Read IPv6
  79. /// </summary>
  80. /// <returns></returns>
  81. public bool TryReadIPv6(out IPAddress address)
  82. {
  83. if (IPv6 == "*")
  84. {
  85. address = IPAddress.Parse("::");
  86. return true;
  87. }
  88. if (IPv6 == "localhost")
  89. {
  90. address = IPAddress.Parse("::1");
  91. return true;
  92. }
  93. if (IPv6 == "disable")
  94. {
  95. address = IPAddress.None;
  96. return true;
  97. }
  98. if (IPAddress.TryParse(IPv6, out var ip))
  99. {
  100. address = ip;
  101. return true;
  102. }
  103. else
  104. {
  105. throw new System.Exception($"Could not parse IPv6 address: {IPv6}");
  106. }
  107. }
  108. }
  109. }