Refactor: tun config
This commit is contained in:
@@ -82,10 +82,9 @@ func ApplyConfig(cfg *config.Config, force bool) {
|
||||
updateHosts(cfg.Hosts)
|
||||
updateMitm(cfg.Mitm)
|
||||
updateProfile(cfg)
|
||||
updateDNS(cfg.DNS, cfg.Tun)
|
||||
updateDNS(cfg.DNS, cfg.General.Tun)
|
||||
updateGeneral(cfg.General, force)
|
||||
updateIPTables(cfg)
|
||||
updateTun(cfg.Tun, cfg.DNS)
|
||||
updateExperimental(cfg)
|
||||
|
||||
log.SetLevel(cfg.General.LogLevel)
|
||||
@@ -94,8 +93,8 @@ func ApplyConfig(cfg *config.Config, force bool) {
|
||||
func GetGeneral() *config.General {
|
||||
ports := P.GetPorts()
|
||||
authenticator := []string{}
|
||||
if auth := authStore.Authenticator(); auth != nil {
|
||||
authenticator = auth.Users()
|
||||
if authM := authStore.Authenticator(); authM != nil {
|
||||
authenticator = authM.Users()
|
||||
}
|
||||
|
||||
general := &config.General{
|
||||
@@ -119,9 +118,9 @@ func GetGeneral() *config.General {
|
||||
return general
|
||||
}
|
||||
|
||||
func updateExperimental(c *config.Config) {}
|
||||
func updateExperimental(_ *config.Config) {}
|
||||
|
||||
func updateDNS(c *config.DNS, t *config.Tun) {
|
||||
func updateDNS(c *config.DNS, t config.Tun) {
|
||||
cfg := dns.Config{
|
||||
Main: c.NameServer,
|
||||
Fallback: c.Fallback,
|
||||
@@ -172,6 +171,10 @@ func updateDNS(c *config.DNS, t *config.Tun) {
|
||||
}
|
||||
dns.ReCreateServer("", nil, nil)
|
||||
}
|
||||
|
||||
if cfg.Pool != nil {
|
||||
P.SetTunAddressPrefix(cfg.Pool.IPNet())
|
||||
}
|
||||
}
|
||||
|
||||
func updateHosts(tree *trie.DomainTrie[netip.Addr]) {
|
||||
@@ -186,15 +189,6 @@ func updateRules(rules []C.Rule) {
|
||||
tunnel.UpdateRules(rules)
|
||||
}
|
||||
|
||||
func updateTun(tun *config.Tun, dns *config.DNS) {
|
||||
var tunAddressPrefix *netip.Prefix
|
||||
if dns.FakeIPRange != nil {
|
||||
tunAddressPrefix = dns.FakeIPRange.IPNet()
|
||||
}
|
||||
|
||||
P.ReCreateTun(tun, tunAddressPrefix, tunnel.TCPIn(), tunnel.UDPIn())
|
||||
}
|
||||
|
||||
func updateGeneral(general *config.General, force bool) {
|
||||
tunnel.SetMode(general.Mode)
|
||||
resolver.DisableIPv6 = !general.IPv6
|
||||
@@ -232,6 +226,7 @@ func updateGeneral(general *config.General, force bool) {
|
||||
P.ReCreateTProxy(general.TProxyPort, tcpIn, udpIn)
|
||||
P.ReCreateMixed(general.MixedPort, tcpIn, udpIn)
|
||||
P.ReCreateMitm(general.MitmPort, tcpIn)
|
||||
P.ReCreateTun(&general.Tun, tcpIn, udpIn)
|
||||
}
|
||||
|
||||
func updateUsers(users []auth.AuthUser) {
|
||||
@@ -273,7 +268,7 @@ func patchSelectGroup(proxies map[string]C.Proxy) {
|
||||
continue
|
||||
}
|
||||
|
||||
selector.Set(selected)
|
||||
_ = selector.Set(selected)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
package route
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/Dreamacro/clash/component/dialer"
|
||||
"github.com/Dreamacro/clash/component/resolver"
|
||||
"github.com/Dreamacro/clash/config"
|
||||
"github.com/Dreamacro/clash/constant"
|
||||
"github.com/Dreamacro/clash/hub/executor"
|
||||
P "github.com/Dreamacro/clash/listener"
|
||||
"github.com/Dreamacro/clash/listener/tun/ipstack/commons"
|
||||
"github.com/Dreamacro/clash/log"
|
||||
"github.com/Dreamacro/clash/tunnel"
|
||||
|
||||
@@ -41,11 +43,12 @@ type configSchema struct {
|
||||
}
|
||||
|
||||
type tunConfigSchema struct {
|
||||
Enable *bool `json:"enable"`
|
||||
Device *string `json:"device"`
|
||||
Stack *constant.TUNStack `json:"stack"`
|
||||
DNSHijack *[]netip.AddrPort `json:"dns-hijack"`
|
||||
AutoRoute *bool `json:"auto-route"`
|
||||
Enable *bool `json:"enable,omitempty"`
|
||||
Device *string `json:"device,omitempty"`
|
||||
Stack *constant.TUNStack `json:"stack,omitempty"`
|
||||
DNSHijack *[]constant.DNSUrl `json:"dns-hijack,omitempty"`
|
||||
AutoRoute *bool `json:"auto-route,omitempty"`
|
||||
AutoDetectInterface *bool `json:"auto-detect-interface,omitempty"`
|
||||
}
|
||||
|
||||
func getConfigs(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -120,10 +123,26 @@ func patchConfigs(w http.ResponseWriter, r *http.Request) {
|
||||
if tunSchema.AutoRoute != nil {
|
||||
tunConf.AutoRoute = *tunSchema.AutoRoute
|
||||
}
|
||||
if tunSchema.AutoDetectInterface != nil {
|
||||
tunConf.AutoDetectInterface = *tunSchema.AutoDetectInterface
|
||||
}
|
||||
|
||||
P.ReCreateTun(&tunConf, nil, tcpIn, udpIn)
|
||||
if dialer.DefaultInterface.Load() == "" && tunConf.Enable {
|
||||
outboundInterface, err := commons.GetAutoDetectInterface()
|
||||
if err != nil {
|
||||
render.Status(r, http.StatusBadRequest)
|
||||
render.JSON(w, r, newError("Get auto detect interface fail: "+err.Error()))
|
||||
return
|
||||
}
|
||||
dialer.DefaultInterface.Store(outboundInterface)
|
||||
}
|
||||
|
||||
P.ReCreateTun(&tunConf, tcpIn, udpIn)
|
||||
}
|
||||
|
||||
msg, _ := json.Marshal(general)
|
||||
log.Warnln("[REST-API] patch config by: %s", string(msg))
|
||||
|
||||
render.NoContent(w, r)
|
||||
}
|
||||
|
||||
@@ -151,6 +170,7 @@ func updateConfigs(w http.ResponseWriter, r *http.Request) {
|
||||
render.JSON(w, r, newError(err.Error()))
|
||||
return
|
||||
}
|
||||
log.Warnln("[REST-API] update config by payload")
|
||||
} else {
|
||||
if req.Path == "" {
|
||||
req.Path = constant.Path.Config()
|
||||
@@ -167,6 +187,7 @@ func updateConfigs(w http.ResponseWriter, r *http.Request) {
|
||||
render.JSON(w, r, newError(err.Error()))
|
||||
return
|
||||
}
|
||||
log.Warnln("[REST-API] reload config from path: %s", req.Path)
|
||||
}
|
||||
|
||||
executor.ApplyConfig(cfg, force)
|
||||
|
||||
Reference in New Issue
Block a user