Program.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Net.Sockets;
  9. using System.Text;
  10. namespace DnsForwarder
  11. {
  12. internal class Program
  13. {
  14. private static string _defaultDns;
  15. private static string _cnDns;
  16. private static IReadOnlyDictionary<string, int> _chinaList;
  17. private static string[] _chinaListDns;
  18. private static Socket _listener;
  19. private static BlockingCollection<string> _consoleOutout;
  20. private static void Main(string[] args)
  21. {
  22. if (args.Length != 3)
  23. {
  24. Console.WriteLine("<default dns server ip> <.CN dns server ip> <path to dnsmasq-china-list file>");
  25. Environment.Exit(-1);
  26. return;
  27. }
  28. Console.WriteLine("Starting...");
  29. Console.WriteLine($"Default Server:{_defaultDns = args[0]}");
  30. Console.WriteLine($"China DNS Server:{_cnDns = args[1]}");
  31. Console.WriteLine($"dnsmasq-china-list file:{args[2]}");
  32. Console.WriteLine("Loading list file...");
  33. LoadListFile(args[2]);
  34. Console.WriteLine("OK. Listening...");
  35. _listener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  36. _listener.Bind(new IPEndPoint(IPAddress.Any, 53));
  37. _consoleOutout = new BlockingCollection<string>();
  38. StartHandleRequest();
  39. while (true)
  40. {
  41. Console.WriteLine(_consoleOutout.Take());
  42. }
  43. }
  44. private static void StartHandleRequest()
  45. {
  46. var buf = new byte[1500];
  47. EndPoint from = new IPEndPoint(IPAddress.Any, 0);
  48. _listener.BeginReceiveFrom(buf, 0, buf.Length, SocketFlags.None, ref from, Callback, buf);
  49. }
  50. private static void Callback(IAsyncResult ar)
  51. {
  52. EndPoint from = new IPEndPoint(IPAddress.Any, 0);
  53. var count = _listener.EndReceiveFrom(ar, ref from);
  54. var buf = (byte[])ar.AsyncState;
  55. StartHandleRequest();
  56. var domain = ExtractDomainName(buf, count);
  57. var target = MatchServer(domain);
  58. _consoleOutout.Add($"{DateTime.Now:yyyyMMdd HH:mm:ss} {from} [{target}]\t{domain}");
  59. var dnsResponse = GetDnsResponse(buf, count, target);
  60. if (dnsResponse != null) _listener.SendTo(dnsResponse, from);
  61. }
  62. protected static string ExtractDomainName(byte[] buf, int count)
  63. {
  64. var lst = new List<byte[]>();
  65. var ptr = 12;
  66. while (buf[ptr] != 0)
  67. {
  68. ptr += buf[ptr] + 1;
  69. }
  70. var bufDomain = new byte[ptr - 12];
  71. Array.Copy(buf, 12, bufDomain, 0, bufDomain.Length);
  72. //dot fill
  73. ptr = 0;
  74. while (ptr < bufDomain.Length)
  75. {
  76. var b = bufDomain[ptr];
  77. bufDomain[ptr] = (byte)'.';
  78. ptr += b + 1;
  79. }
  80. return Encoding.ASCII.GetString(bufDomain, 1, bufDomain.Length - 1);
  81. }
  82. protected static byte[] GetDnsResponse(byte[] buf, int count, string host, int port = 53)
  83. {
  84. using var to = new UdpClient();
  85. to.Connect(host, 53);
  86. to.Send(buf, count);
  87. //Handle Upstream TimeOut
  88. var asyncResult = to.BeginReceive(null, null);
  89. asyncResult.AsyncWaitHandle.WaitOne(2000);
  90. if (asyncResult.IsCompleted)
  91. {
  92. IPEndPoint remoteEP = null;
  93. byte[] receivedData = to.EndReceive(asyncResult, ref remoteEP);
  94. return receivedData;
  95. }
  96. return null;
  97. }
  98. protected static string MatchServer(string domain)
  99. {
  100. var lower = domain.ToLower();
  101. if (lower.EndsWith(".cn")) return _cnDns;
  102. var parts = lower.Split('.').Reverse().ToArray();
  103. for (int i = parts.Length - 1; i >= 0; i--)
  104. {
  105. var d = string.Join(".", parts.Take(i + 1).Reverse());
  106. if (_chinaList.TryGetValue(d, out var tar)) return _chinaListDns[tar];
  107. }
  108. return _defaultDns;
  109. }
  110. protected static void LoadListFile(params string[] paths)
  111. {
  112. var lines = paths.SelectMany(File.ReadAllLines);
  113. var dic = new Dictionary<string, int>();
  114. var tar = new List<string>();
  115. foreach (var line in lines)
  116. {
  117. var p = line.Trim();
  118. if (p.StartsWith("#")) continue;
  119. var parts = p.Split('/');
  120. if (parts.Length != 3 && parts[0] != "server=") continue;
  121. var domain = parts[1];
  122. var dns = parts[2];
  123. var dnsIndex = tar.IndexOf(dns);
  124. if (dnsIndex == -1)
  125. {
  126. tar.Add(dns);
  127. dnsIndex = tar.Count - 1;
  128. }
  129. dic[domain] = dnsIndex;
  130. }
  131. _chinaListDns = tar.ToArray();
  132. _chinaList = new ReadOnlyDictionary<string, int>(dic);
  133. }
  134. }
  135. }