App.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. break;
  72. }
  73. if (address is not null)
  74. answer = new DnsResourceRecord(request.Question[0].Name, DnsResourceRecordType.AAAA, DnsClass.IN, appRecordTtl, new DnsAAAARecordData(address));
  75. }
  76. break;
  77. }
  78. if (answer is null)
  79. {
  80. //NODATA reponse
  81. DnsDatagram soaResponse = await _dnsServer.DirectQueryAsync(new DnsQuestionRecord(zoneName, DnsResourceRecordType.SOA, DnsClass.IN));
  82. return new DnsDatagram(request.Identifier, true, DnsOpcode.StandardQuery, true, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.NoError, request.Question, null, soaResponse.Answer);
  83. }
  84. return new DnsDatagram(request.Identifier, true, request.OPCODE, true, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.NoError, request.Question, new DnsResourceRecord[] { answer });
  85. }
  86. #endregion
  87. #region properties
  88. public string Description
  89. { get { return "Returns the IP address that was embedded in the subdomain name for A and AAAA queries. It works similar to sslip.io."; } }
  90. public string ApplicationRecordDataTemplate
  91. { get { return null; } }
  92. #endregion
  93. }
  94. }