App.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2024 Shreyas Zare (shreyas@technitium.com)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. using DnsServerCore.ApplicationCommon;
  16. using System;
  17. using System.Net;
  18. using System.Threading.Tasks;
  19. using TechnitiumLibrary.Net.Dns;
  20. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  21. namespace WildIp
  22. {
  23. public sealed class App : IDnsApplication, IDnsAppRecordRequestHandler
  24. {
  25. #region variables
  26. static readonly char[] aRecordSeparator = new char[] { '.', '-' };
  27. static readonly char[] aaaaRecordSeparator = new char[] { '.' };
  28. IDnsServer _dnsServer;
  29. #endregion
  30. #region IDisposable
  31. public void Dispose()
  32. {
  33. //do nothing
  34. }
  35. #endregion
  36. #region public
  37. public Task InitializeAsync(IDnsServer dnsServer, string config)
  38. {
  39. _dnsServer = dnsServer;
  40. return Task.CompletedTask;
  41. }
  42. public async Task<DnsDatagram> ProcessRequestAsync(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, bool isRecursionAllowed, string zoneName, string appRecordName, uint appRecordTtl, string appRecordData)
  43. {
  44. string qname = request.Question[0].Name;
  45. if (qname.Length == appRecordName.Length)
  46. return null;
  47. DnsResourceRecord answer = null;
  48. switch (request.Question[0].Type)
  49. {
  50. case DnsResourceRecordType.A:
  51. {
  52. string subdomain = qname.Substring(0, qname.Length - appRecordName.Length);
  53. string[] parts = subdomain.Split(aRecordSeparator, StringSplitOptions.RemoveEmptyEntries);
  54. byte[] rawIp = new byte[4];
  55. int i = 0;
  56. for (int j = 0; (j < parts.Length) && (i < 4); j++)
  57. {
  58. if (byte.TryParse(parts[j], out byte x))
  59. rawIp[i++] = x;
  60. }
  61. if (i == 4)
  62. answer = new DnsResourceRecord(request.Question[0].Name, DnsResourceRecordType.A, DnsClass.IN, appRecordTtl, new DnsARecordData(new IPAddress(rawIp)));
  63. }
  64. break;
  65. case DnsResourceRecordType.AAAA:
  66. {
  67. string subdomain = qname.Substring(0, qname.Length - appRecordName.Length - 1);
  68. string[] parts = subdomain.Split(aaaaRecordSeparator, StringSplitOptions.RemoveEmptyEntries);
  69. IPAddress address = null;
  70. foreach (string part in parts)
  71. {
  72. if (part.Contains('-') && IPAddress.TryParse(part.Replace('-', ':'), out address))
  73. {
  74. break;
  75. }
  76. else if (part.Length == 32)
  77. {
  78. string addr = null;
  79. for (int i = 0; i < 32; i += 4)
  80. {
  81. if (addr is null)
  82. addr = part.Substring(i, 4);
  83. else
  84. addr += string.Concat(":", part.AsSpan(i, 4));
  85. }
  86. if (IPAddress.TryParse(addr, out address))
  87. break;
  88. }
  89. }
  90. if (address is not null)
  91. answer = new DnsResourceRecord(request.Question[0].Name, DnsResourceRecordType.AAAA, DnsClass.IN, appRecordTtl, new DnsAAAARecordData(address));
  92. }
  93. break;
  94. }
  95. if (answer is null)
  96. {
  97. //NODATA reponse
  98. DnsDatagram soaResponse = await _dnsServer.DirectQueryAsync(new DnsQuestionRecord(zoneName, DnsResourceRecordType.SOA, DnsClass.IN));
  99. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, true, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.NoError, request.Question, null, soaResponse.Answer);
  100. }
  101. return new DnsDatagram(request.Identifier, true, request.OPCODE, true, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.NoError, request.Question, new DnsResourceRecord[] { answer });
  102. }
  103. #endregion
  104. #region properties
  105. public string Description
  106. { get { return "Returns the IP address that was embedded in the subdomain name for A and AAAA queries. It works similar to sslip.io."; } }
  107. public string ApplicationRecordDataTemplate
  108. { get { return null; } }
  109. #endregion
  110. }
  111. }