From b07f70ab0b494a87ea9f20b52612f3895bcd7e5d Mon Sep 17 00:00:00 2001
From: Kr328 <kr328app@outlook.com>
Date: Sat, 11 Sep 2021 23:37:54 +0800
Subject: [PATCH] Improve: should not reload profile on network changed

---
 core/src/main/golang/app/dns.go               | 19 ++++++++++++-------
 core/src/main/golang/config/load.go           |  3 +++
 core/src/main/golang/config/process.go        |  3 +--
 core/src/main/golang/core/foss                |  2 +-
 core/src/main/golang/go.mod                   | 15 +++++++++++++++
 core/src/main/golang/tun/dns.go               |  5 +++--
 .../kr328/clash/service/ClashService.kt       |  2 --
 .../github/kr328/clash/service/TunService.kt  |  2 --
 .../clash/module/ConfigurationModule.kt       |  4 ----
 9 files changed, 35 insertions(+), 20 deletions(-)

diff --git a/core/src/main/golang/app/dns.go b/core/src/main/golang/app/dns.go
index 1cd5e916..fce76797 100644
--- a/core/src/main/golang/app/dns.go
+++ b/core/src/main/golang/app/dns.go
@@ -1,15 +1,20 @@
 package app
 
-import "strings"
+import (
+	"strings"
 
-var systemDns []string
+	"github.com/Dreamacro/clash/dns"
+)
 
 func NotifyDnsChanged(dnsList string) {
-	dns := strings.Split(dnsList, ",")
+	dL := strings.Split(dnsList, ",")
 
-	systemDns = dns
-}
+	ns := make([]dns.NameServer, 0, len(dnsList))
+	for _, d := range dL {
+		ns = append(ns, dns.NameServer{Addr: d})
+	}
 
-func SystemDns() []string {
-	return systemDns
+	dns.UpdateSystemDNS(dL)
+	dns.FlushCacheWithDefaultResolver()
 }
+
diff --git a/core/src/main/golang/config/load.go b/core/src/main/golang/config/load.go
index 2b5b1cdd..b11c6ea3 100644
--- a/core/src/main/golang/config/load.go
+++ b/core/src/main/golang/config/load.go
@@ -3,6 +3,7 @@ package config
 import (
 	"io/ioutil"
 	P "path"
+	"runtime"
 	"strings"
 
 	"gopkg.in/yaml.v2"
@@ -81,6 +82,8 @@ func Load(path string) error {
 
 	app.ApplySubtitlePattern(rawCfg.ClashForAndroid.UiSubtitlePattern)
 
+	runtime.GC()
+
 	return nil
 }
 
diff --git a/core/src/main/golang/config/process.go b/core/src/main/golang/config/process.go
index 8244e2bf..1b55d765 100644
--- a/core/src/main/golang/config/process.go
+++ b/core/src/main/golang/config/process.go
@@ -10,7 +10,6 @@ import (
 	"github.com/Dreamacro/clash/log"
 	"github.com/dlclark/regexp2"
 
-	"cfa/app"
 	"cfa/common"
 
 	"github.com/Dreamacro/clash/config"
@@ -76,7 +75,7 @@ func patchDns(cfg *config.RawConfig, _ string) error {
 	}
 
 	if cfg.ClashForAndroid.AppendSystemDNS {
-		cfg.DNS.NameServer = append(cfg.DNS.NameServer, app.SystemDns()...)
+		cfg.DNS.NameServer = append(cfg.DNS.NameServer, "dhcp://" + dns.SystemDNSPlaceholder)
 	}
 
 	return nil
diff --git a/core/src/main/golang/core/foss b/core/src/main/golang/core/foss
index a9186991..006bce44 160000
--- a/core/src/main/golang/core/foss
+++ b/core/src/main/golang/core/foss
@@ -1 +1 @@
-Subproject commit a918699140e9169f76600f2e7837ec6d69957fb5
+Subproject commit 006bce4436065b2173dad16ac154cc3824a34c79
diff --git a/core/src/main/golang/go.mod b/core/src/main/golang/go.mod
index 29bce892..8436c94a 100644
--- a/core/src/main/golang/go.mod
+++ b/core/src/main/golang/go.mod
@@ -13,6 +13,21 @@ require (
 	gopkg.in/yaml.v2 v2.4.0
 )
 
+require (
+	github.com/Dreamacro/go-shadowsocks2 v0.1.7 // indirect
+	github.com/gofrs/uuid v4.0.0+incompatible // indirect
+	github.com/gorilla/websocket v1.4.2 // indirect
+	github.com/insomniacslk/dhcp v0.0.0-20210827173440-b95caade3eac // indirect
+	github.com/oschwald/maxminddb-golang v1.8.0 // indirect
+	github.com/sirupsen/logrus v1.8.1 // indirect
+	github.com/u-root/uio v0.0.0-20210528114334-82958018845c // indirect
+	go.uber.org/atomic v1.9.0 // indirect
+	golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
+	golang.org/x/net v0.0.0-20210903162142-ad29c8ab022f // indirect
+	golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34 // indirect
+	golang.org/x/text v0.3.6 // indirect
+)
+
 replace github.com/Dreamacro/clash => ./core/foss
 
 replace cfa/blob => ../../../build/intermediates/golang_blob
diff --git a/core/src/main/golang/tun/dns.go b/core/src/main/golang/tun/dns.go
index a5180f42..a62518bf 100644
--- a/core/src/main/golang/tun/dns.go
+++ b/core/src/main/golang/tun/dns.go
@@ -3,7 +3,8 @@ package tun
 import (
 	"net"
 
-	"github.com/Dreamacro/clash/component/resolver"
+	"github.com/Dreamacro/clash/dns"
+
 	D "github.com/miekg/dns"
 )
 
@@ -21,7 +22,7 @@ func relayDns(payload []byte) ([]byte, error) {
 		return nil, err
 	}
 
-	r, err := resolver.ServeMsg(msg)
+	r, err := dns.ServeDNSWithDefaultServer(msg)
 	if err != nil {
 		return nil, err
 	}
diff --git a/service/src/main/java/com/github/kr328/clash/service/ClashService.kt b/service/src/main/java/com/github/kr328/clash/service/ClashService.kt
index 48764bfc..dd58a644 100644
--- a/service/src/main/java/com/github/kr328/clash/service/ClashService.kt
+++ b/service/src/main/java/com/github/kr328/clash/service/ClashService.kt
@@ -54,8 +54,6 @@ class ClashService : BaseService() {
                         true
                     }
                     network.onEvent {
-                        config.reload()
-
                         false
                     }
                 }
diff --git a/service/src/main/java/com/github/kr328/clash/service/TunService.kt b/service/src/main/java/com/github/kr328/clash/service/TunService.kt
index 57900e9c..f861145d 100644
--- a/service/src/main/java/com/github/kr328/clash/service/TunService.kt
+++ b/service/src/main/java/com/github/kr328/clash/service/TunService.kt
@@ -65,8 +65,6 @@ class TunService : VpnService(), CoroutineScope by CoroutineScope(Dispatchers.De
                             setUnderlyingNetworks(e.network?.let { arrayOf(it) })
                         }
 
-                        config.reload()
-
                         false
                     }
                 }
diff --git a/service/src/main/java/com/github/kr328/clash/service/clash/module/ConfigurationModule.kt b/service/src/main/java/com/github/kr328/clash/service/clash/module/ConfigurationModule.kt
index 7be05436..4e384b46 100644
--- a/service/src/main/java/com/github/kr328/clash/service/clash/module/ConfigurationModule.kt
+++ b/service/src/main/java/com/github/kr328/clash/service/clash/module/ConfigurationModule.kt
@@ -73,8 +73,4 @@ class ConfigurationModule(service: Service) : Module<ConfigurationModule.LoadExc
             }
         }
     }
-
-    fun reload() {
-        reload.trySend(Unit)
-    }
 }
\ No newline at end of file
-- 
GitLab