Feature: add default-nameserver and outbound interface

This commit is contained in:
Dreamacro
2020-02-15 21:42:46 +08:00
parent f69f635e0b
commit d75cb069d9
28 changed files with 578 additions and 347 deletions

View File

@ -8,14 +8,16 @@ import (
"github.com/Dreamacro/clash/adapters/provider"
"github.com/Dreamacro/clash/component/auth"
"github.com/Dreamacro/clash/component/dialer"
trie "github.com/Dreamacro/clash/component/domain-trie"
"github.com/Dreamacro/clash/component/resolver"
"github.com/Dreamacro/clash/config"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/dns"
"github.com/Dreamacro/clash/log"
P "github.com/Dreamacro/clash/proxy"
authStore "github.com/Dreamacro/clash/proxy/auth"
T "github.com/Dreamacro/clash/tunnel"
"github.com/Dreamacro/clash/tunnel"
)
// forward compatibility before 1.0
@ -83,7 +85,7 @@ func ApplyConfig(cfg *config.Config, force bool) {
updateRules(cfg.Rules)
updateDNS(cfg.DNS)
updateHosts(cfg.Hosts)
updateExperimental(cfg.Experimental)
updateExperimental(cfg)
}
func GetGeneral() *config.General {
@ -100,20 +102,30 @@ func GetGeneral() *config.General {
Authentication: authenticator,
AllowLan: P.AllowLan(),
BindAddress: P.BindAddress(),
Mode: T.Instance().Mode(),
Mode: tunnel.Mode(),
LogLevel: log.Level(),
}
return general
}
func updateExperimental(c *config.Experimental) {
T.Instance().UpdateExperimental(c.IgnoreResolveFail)
func updateExperimental(c *config.Config) {
cfg := c.Experimental
tunnel.UpdateExperimental(cfg.IgnoreResolveFail)
if cfg.Interface != "" && c.DNS.Enable {
dialer.DialHook = dialer.DialerWithInterface(cfg.Interface)
dialer.ListenPacketHook = dialer.ListenPacketWithInterface(cfg.Interface)
} else {
dialer.DialHook = nil
dialer.ListenPacketHook = nil
}
}
func updateDNS(c *config.DNS) {
if c.Enable == false {
dns.DefaultResolver = nil
resolver.DefaultResolver = nil
tunnel.SetResolver(nil)
dns.ReCreateServer("", nil)
return
}
@ -127,8 +139,10 @@ func updateDNS(c *config.DNS) {
GeoIP: c.FallbackFilter.GeoIP,
IPCIDR: c.FallbackFilter.IPCIDR,
},
Default: c.DefaultNameserver,
})
dns.DefaultResolver = r
resolver.DefaultResolver = r
tunnel.SetResolver(r)
if err := dns.ReCreateServer(c.Listen, r); err != nil {
log.Errorln("Start DNS server error: %s", err.Error())
return
@ -140,11 +154,10 @@ func updateDNS(c *config.DNS) {
}
func updateHosts(tree *trie.Trie) {
dns.DefaultHosts = tree
resolver.DefaultHosts = tree
}
func updateProxies(proxies map[string]C.Proxy, providers map[string]provider.ProxyProvider) {
tunnel := T.Instance()
oldProviders := tunnel.Providers()
// close providers goroutine
@ -156,12 +169,12 @@ func updateProxies(proxies map[string]C.Proxy, providers map[string]provider.Pro
}
func updateRules(rules []C.Rule) {
T.Instance().UpdateRules(rules)
tunnel.UpdateRules(rules)
}
func updateGeneral(general *config.General) {
log.SetLevel(general.LogLevel)
T.Instance().SetMode(general.Mode)
tunnel.SetMode(general.Mode)
allowLan := general.AllowLan
P.SetAllowLan(allowLan)

View File

@ -8,7 +8,7 @@ import (
"github.com/Dreamacro/clash/hub/executor"
"github.com/Dreamacro/clash/log"
P "github.com/Dreamacro/clash/proxy"
T "github.com/Dreamacro/clash/tunnel"
"github.com/Dreamacro/clash/tunnel"
"github.com/go-chi/chi"
"github.com/go-chi/render"
@ -23,13 +23,13 @@ func configRouter() http.Handler {
}
type configSchema struct {
Port *int `json:"port"`
SocksPort *int `json:"socks-port"`
RedirPort *int `json:"redir-port"`
AllowLan *bool `json:"allow-lan"`
BindAddress *string `json:"bind-address"`
Mode *T.Mode `json:"mode"`
LogLevel *log.LogLevel `json:"log-level"`
Port *int `json:"port"`
SocksPort *int `json:"socks-port"`
RedirPort *int `json:"redir-port"`
AllowLan *bool `json:"allow-lan"`
BindAddress *string `json:"bind-address"`
Mode *tunnel.TunnelMode `json:"mode"`
LogLevel *log.LogLevel `json:"log-level"`
}
func getConfigs(w http.ResponseWriter, r *http.Request) {
@ -67,7 +67,7 @@ func patchConfigs(w http.ResponseWriter, r *http.Request) {
P.ReCreateRedir(pointerOrDefault(general.RedirPort, ports.RedirPort))
if general.Mode != nil {
T.Instance().SetMode(*general.Mode)
tunnel.SetMode(*general.Mode)
}
if general.LogLevel != nil {

View File

@ -5,7 +5,7 @@ import (
"net/http"
"github.com/Dreamacro/clash/adapters/provider"
T "github.com/Dreamacro/clash/tunnel"
"github.com/Dreamacro/clash/tunnel"
"github.com/go-chi/chi"
"github.com/go-chi/render"
@ -25,7 +25,7 @@ func proxyProviderRouter() http.Handler {
}
func getProviders(w http.ResponseWriter, r *http.Request) {
providers := T.Instance().Providers()
providers := tunnel.Providers()
render.JSON(w, r, render.M{
"providers": providers,
})
@ -63,7 +63,7 @@ func parseProviderName(next http.Handler) http.Handler {
func findProviderByName(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
name := r.Context().Value(CtxKeyProviderName).(string)
providers := T.Instance().Providers()
providers := tunnel.Providers()
provider, exist := providers[name]
if !exist {
render.Status(r, http.StatusNotFound)

View File

@ -10,7 +10,7 @@ import (
"github.com/Dreamacro/clash/adapters/outbound"
"github.com/Dreamacro/clash/adapters/outboundgroup"
C "github.com/Dreamacro/clash/constant"
T "github.com/Dreamacro/clash/tunnel"
"github.com/Dreamacro/clash/tunnel"
"github.com/go-chi/chi"
"github.com/go-chi/render"
@ -40,7 +40,7 @@ func parseProxyName(next http.Handler) http.Handler {
func findProxyByName(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
name := r.Context().Value(CtxKeyProxyName).(string)
proxies := T.Instance().Proxies()
proxies := tunnel.Proxies()
proxy, exist := proxies[name]
if !exist {
render.Status(r, http.StatusNotFound)
@ -54,7 +54,7 @@ func findProxyByName(next http.Handler) http.Handler {
}
func getProxies(w http.ResponseWriter, r *http.Request) {
proxies := T.Instance().Proxies()
proxies := tunnel.Proxies()
render.JSON(w, r, render.M{
"proxies": proxies,
})

View File

@ -3,7 +3,7 @@ package route
import (
"net/http"
T "github.com/Dreamacro/clash/tunnel"
"github.com/Dreamacro/clash/tunnel"
"github.com/go-chi/chi"
"github.com/go-chi/render"
@ -22,7 +22,7 @@ type Rule struct {
}
func getRules(w http.ResponseWriter, r *http.Request) {
rawRules := T.Instance().Rules()
rawRules := tunnel.Rules()
rules := []Rule{}
for _, rule := range rawRules {