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.

TcpEndpointModel.cs 2.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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.Any;
  55. return true;
  56. }
  57. if (IPv4 == "localhost")
  58. {
  59. address = IPAddress.Loopback;
  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. throw new System.Exception($"Could not parse IPv4 address: {IPv4}");
  73. }
  74. /// <summary>
  75. /// Read IPv6
  76. /// </summary>
  77. /// <returns></returns>
  78. public bool TryReadIPv6(out IPAddress address)
  79. {
  80. if (IPv6 == "*")
  81. {
  82. address = IPAddress.IPv6Any;
  83. return true;
  84. }
  85. if (IPv6 == "localhost")
  86. {
  87. address = IPAddress.IPv6Loopback;
  88. return true;
  89. }
  90. if (IPv6 == "disable")
  91. {
  92. address = IPAddress.None;
  93. return true;
  94. }
  95. if (IPAddress.TryParse(IPv6, out var ip))
  96. {
  97. address = ip;
  98. return true;
  99. }
  100. throw new System.Exception($"Could not parse IPv6 address: {IPv6}");
  101. }
  102. }
  103. }