From 406135b04bdca568b2499fcd33730b9bdac4adf2 Mon Sep 17 00:00:00 2001
From: NarukawaHime <NarukawaHime@outlook.com>
Date: Thu, 9 Jun 2022 21:42:27 +0800
Subject: [PATCH] Fix DNS issue in TUN mode (#824)

---
 Netch/Controllers/TUNController.cs | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/Netch/Controllers/TUNController.cs b/Netch/Controllers/TUNController.cs
index 249d014249..686cc60cc5 100644
--- a/Netch/Controllers/TUNController.cs
+++ b/Netch/Controllers/TUNController.cs
@@ -93,7 +93,7 @@ public async Task StartAsync(Socks5Server server, Mode mode)
 
             if (_tunConfig.UseCustomDNS)
             {
-                Dial(NameList.TYPE_DNSADDR, _tunConfig.DNS);
+                Dial(NameList.TYPE_DNSADDR, DnsUtils.AppendPort(_tunConfig.DNS));
             }
             else
             {
@@ -175,8 +175,13 @@ private void SetupRouteTable()
 
             if (_tunConfig.UseCustomDNS)
             {
-                // NOTICE: DNS metric is network interface metric
-                RouteUtils.CreateRoute(_tun.FillTemplate(_tunConfig.DNS, 32));
+                if (_tunConfig.ProxyDNS)
+                {
+                    // NOTICE: DNS metric is network interface metric
+                    RouteUtils.CreateRoute(_tun.FillTemplate(_tunConfig.DNS, 32));
+                }
+
+                tunNetworkInterface.SetDns(_tunConfig.DNS);
             }
             else
             {