App.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.Net.Sockets;
  19. using System.Threading.Tasks;
  20. using TechnitiumLibrary.Net.Dns;
  21. using TechnitiumLibrary.Net.Dns.ResourceRecords;
  22. namespace WhatIsMyDns
  23. {
  24. public class App : IDnsApplication, IDnsAppRecordRequestHandler
  25. {
  26. #region IDisposable
  27. public void Dispose()
  28. {
  29. //do nothing
  30. }
  31. #endregion
  32. #region public
  33. public Task InitializeAsync(IDnsServer dnsServer, string config)
  34. {
  35. //do nothing
  36. return Task.CompletedTask;
  37. }
  38. public Task<DnsDatagram> ProcessRequestAsync(DnsDatagram request, IPEndPoint remoteEP, DnsTransportProtocol protocol, bool isRecursionAllowed, string zoneName, string appRecordName, uint appRecordTtl, string appRecordData)
  39. {
  40. DnsQuestionRecord question = request.Question[0];
  41. if (!question.Name.Equals(appRecordName, StringComparison.OrdinalIgnoreCase))
  42. return Task.FromResult<DnsDatagram>(null);
  43. DnsResourceRecord answer;
  44. switch (question.Type)
  45. {
  46. case DnsResourceRecordType.A:
  47. if (remoteEP.AddressFamily != AddressFamily.InterNetwork)
  48. return Task.FromResult<DnsDatagram>(null);
  49. answer = new DnsResourceRecord(question.Name, DnsResourceRecordType.A, DnsClass.IN, appRecordTtl, new DnsARecordData(remoteEP.Address));
  50. break;
  51. case DnsResourceRecordType.AAAA:
  52. if (remoteEP.AddressFamily != AddressFamily.InterNetworkV6)
  53. return Task.FromResult<DnsDatagram>(null);
  54. answer = new DnsResourceRecord(question.Name, DnsResourceRecordType.AAAA, DnsClass.IN, appRecordTtl, new DnsAAAARecordData(remoteEP.Address));
  55. break;
  56. case DnsResourceRecordType.TXT:
  57. answer = new DnsResourceRecord(question.Name, DnsResourceRecordType.TXT, DnsClass.IN, appRecordTtl, new DnsTXTRecordData(remoteEP.Address.ToString()));
  58. break;
  59. default:
  60. return Task.FromResult<DnsDatagram>(null);
  61. }
  62. return Task.FromResult(new DnsDatagram(request.Identifier, true, request.OPCODE, true, false, request.RecursionDesired, isRecursionAllowed, false, false, DnsResponseCode.NoError, request.Question, new DnsResourceRecord[] { answer }));
  63. }
  64. #endregion
  65. #region properties
  66. public string Description
  67. { get { return "Returns the IP address of the user's DNS Server for A, AAAA, and TXT queries."; } }
  68. public string ApplicationRecordDataTemplate
  69. { get { return null; } }
  70. #endregion
  71. }
  72. }