App.cs 3.3 KB

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