App.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. Technitium DNS Server
  3. Copyright (C) 2023 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 class App : IDnsApplication, IDnsAppRecordRequestHandler
  24. {
  25. #region variables
  26. IDnsServer _dnsServer;
  27. #endregion
  28. #region IDisposable
  29. public void Dispose()
  30. {
  31. //do nothing
  32. }
  33. #endregion
  34. #region public
  35. public Task InitializeAsync(IDnsServer dnsServer, string config)
  36. {
  37. _dnsServer = dnsServer;
  38. return Task.CompletedTask;
  39. }
  40. public async Task<DnsDatagram> ProcessRequestAsync(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, bool isRecursionAllowed, string zoneName, string appRecordName, uint appRecordTtl, string appRecordData)
  41. {
  42. string qname = request.Question[0].Name;
  43. if (qname.Length == appRecordName.Length)
  44. return null;
  45. DnsResourceRecord answer = null;
  46. switch (request.Question[0].Type)
  47. {
  48. case DnsResourceRecordType.A:
  49. {
  50. string subdomain = qname.Substring(0, qname.Length - appRecordName.Length);
  51. string[] parts = subdomain.Split(new char[] { '.', '-' }, StringSplitOptions.RemoveEmptyEntries);
  52. byte[] rawIp = new byte[4];
  53. int i = 0;
  54. for (int j = 0; (j < parts.Length) && (i < 4); j++)
  55. {
  56. if (byte.TryParse(parts[j], out byte x))
  57. rawIp[i++] = x;
  58. }
  59. if (i == 4)
  60. answer = new DnsResourceRecord(request.Question[0].Name, DnsResourceRecordType.A, DnsClass.IN, appRecordTtl, new DnsARecordData(new IPAddress(rawIp)));
  61. }
  62. break;
  63. case DnsResourceRecordType.AAAA:
  64. {
  65. string subdomain = qname.Substring(0, qname.Length - appRecordName.Length - 1);
  66. string[] parts = subdomain.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
  67. IPAddress address = null;
  68. foreach (string part in parts)
  69. {
  70. if (part.Contains('-') && IPAddress.TryParse(part.Replace('-', ':'), out address))
  71. {
  72. break;
  73. }
  74. else if (part.Length == 32)
  75. {
  76. string addr = null;
  77. for (int i = 0; i < 32; i += 4)
  78. {
  79. if (addr is null)
  80. addr = part.Substring(i, 4);
  81. else
  82. addr += ":" + part.Substring(i, 4);
  83. }
  84. if (IPAddress.TryParse(addr, out address))
  85. break;
  86. }
  87. }
  88. if (address is not null)
  89. answer = new DnsResourceRecord(request.Question[0].Name, DnsResourceRecordType.AAAA, DnsClass.IN, appRecordTtl, new DnsAAAARecordData(address));
  90. }
  91. break;
  92. }
  93. if (answer is null)
  94. {
  95. //NODATA reponse
  96. DnsDatagram soaResponse = await _dnsServer.DirectQueryAsync(new DnsQuestionRecord(zoneName, DnsResourceRecordType.SOA, DnsClass.IN));
  97. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, true, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.NoError, request.Question, null, soaResponse.Answer);
  98. }
  99. return new DnsDatagram(request.Identifier, true, request.OPCODE, true, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.NoError, request.Question, new DnsResourceRecord[] { answer });
  100. }
  101. #endregion
  102. #region properties
  103. public string Description
  104. { get { return "Returns the IP address that was embedded in the subdomain name for A and AAAA queries. It works similar to sslip.io."; } }
  105. public string ApplicationRecordDataTemplate
  106. { get { return null; } }
  107. #endregion
  108. }
  109. }