feat: remote dns config

This commit is contained in:
cubemaze 2023-07-19 12:03:09 +08:00
parent 7a1be2e594
commit a67ef8f692
4 changed files with 52 additions and 6 deletions

View File

@ -1,8 +1,12 @@
package panel package panel
import ( import (
"bytes"
"encoding/base64" "encoding/base64"
"fmt" "fmt"
log "github.com/sirupsen/logrus"
coreConf "github.com/xtls/xray-core/infra/conf"
"os"
"reflect" "reflect"
"regexp" "regexp"
"strconv" "strconv"
@ -130,6 +134,36 @@ func (c *Client) GetNodeInfo() (node *NodeInfo, err error) {
node.Rules = append(node.Rules, regexp.MustCompile(v)) node.Rules = append(node.Rules, regexp.MustCompile(v))
} }
case "dns": case "dns":
if matchs[0] != "main" {
break
}
dnsPath := os.Getenv("XRAY_DNS_PATH")
if dnsPath == "" {
break
}
dns := []byte(strings.Join(matchs[1:], ""))
currentData, err := os.ReadFile(dnsPath)
if err != nil {
log.WithField("err", err).Panic("Failed to read XRAY_DNS_PATH")
break
}
if !bytes.Equal(currentData, dns) {
coreDnsConfig := &coreConf.DNSConfig{}
if err = json.NewDecoder(bytes.NewReader(dns)).Decode(coreDnsConfig); err != nil {
log.WithField("err", err).Panic("Failed to unmarshal DNS config")
}
_, err := coreDnsConfig.Build()
if err != nil {
log.WithField("err", err).Panic("Failed to understand DNS config, Please check: https://xtls.github.io/config/dns.html for help")
break
}
if err = os.Truncate(dnsPath, 0); err != nil {
log.WithField("err", err).Panic("Failed to clear XRAY DNS PATH file")
}
if err = os.WriteFile(dnsPath, dns, 0644); err != nil {
log.WithField("err", err).Panic("Failed to write DNS to XRAY DNS PATH file")
}
}
} }
} }
node.ServerName = common.ServerName node.ServerName = common.ServerName

View File

@ -64,8 +64,9 @@ func serverHandle(_ *cobra.Command, _ []string) {
log.WithField("err", err).Error("Run nodes failed") log.WithField("err", err).Error("Run nodes failed")
return return
} }
dns := os.Getenv("XRAY_DNS_PATH")
if watch { if watch {
err = c.Watch(config, func() { err = c.Watch(config, dns, func() {
nodes.Close() nodes.Close()
err = vc.Close() err = vc.Close()
if err != nil { if err != nil {

View File

@ -8,7 +8,7 @@ import (
"time" "time"
) )
func (p *Conf) Watch(filePath string, reload func()) error { func (p *Conf) Watch(filePath, dnsPath string, reload func()) error {
watcher, err := fsnotify.NewWatcher() watcher, err := fsnotify.NewWatcher()
if err != nil { if err != nil {
return fmt.Errorf("new watcher error: %s", err) return fmt.Errorf("new watcher error: %s", err)
@ -28,7 +28,11 @@ func (p *Conf) Watch(filePath string, reload func()) error {
pre = time.Now() pre = time.Now()
go func() { go func() {
time.Sleep(10 * time.Second) time.Sleep(10 * time.Second)
if e.Name == dnsPath {
log.Println("DNS file changed, reloading...")
} else {
log.Println("config dir changed, reloading...") log.Println("config dir changed, reloading...")
}
*p = *New() *p = *New()
err := p.LoadFromPath(filePath) err := p.LoadFromPath(filePath)
if err != nil { if err != nil {
@ -48,5 +52,11 @@ func (p *Conf) Watch(filePath string, reload func()) error {
if err != nil { if err != nil {
return fmt.Errorf("watch file error: %s", err) return fmt.Errorf("watch file error: %s", err)
} }
if dnsPath != "" {
err = watcher.Add(path.Dir(dnsPath))
if err != nil {
return fmt.Errorf("watch dns file error: %s", err)
}
}
return nil return nil
} }

View File

@ -1,9 +1,6 @@
package xray package xray
import ( import (
"os"
"sync"
"github.com/Yuzuki616/V2bX/conf" "github.com/Yuzuki616/V2bX/conf"
vCore "github.com/Yuzuki616/V2bX/core" vCore "github.com/Yuzuki616/V2bX/core"
"github.com/Yuzuki616/V2bX/core/xray/app/dispatcher" "github.com/Yuzuki616/V2bX/core/xray/app/dispatcher"
@ -19,6 +16,8 @@ import (
"github.com/xtls/xray-core/features/routing" "github.com/xtls/xray-core/features/routing"
statsFeature "github.com/xtls/xray-core/features/stats" statsFeature "github.com/xtls/xray-core/features/stats"
coreConf "github.com/xtls/xray-core/infra/conf" coreConf "github.com/xtls/xray-core/infra/conf"
"os"
"sync"
) )
func init() { func init() {
@ -61,6 +60,7 @@ func getCore(c *conf.XrayConfig) *core.Instance {
coreLogConfig.ErrorLog = c.LogConfig.ErrorPath coreLogConfig.ErrorLog = c.LogConfig.ErrorPath
// DNS config // DNS config
coreDnsConfig := &coreConf.DNSConfig{} coreDnsConfig := &coreConf.DNSConfig{}
os.Setenv("XRAY_DNS_PATH", "")
if c.DnsConfigPath != "" { if c.DnsConfigPath != "" {
if f, err := os.Open(c.DnsConfigPath); err != nil { if f, err := os.Open(c.DnsConfigPath); err != nil {
log.WithField("err", err).Panic("Failed to read DNS config file") log.WithField("err", err).Panic("Failed to read DNS config file")
@ -69,6 +69,7 @@ func getCore(c *conf.XrayConfig) *core.Instance {
log.WithField("err", err).Panic("Failed to unmarshal DNS config") log.WithField("err", err).Panic("Failed to unmarshal DNS config")
} }
} }
os.Setenv("XRAY_DNS_PATH", c.DnsConfigPath)
} }
dnsConfig, err := coreDnsConfig.Build() dnsConfig, err := coreDnsConfig.Build()
if err != nil { if err != nil {