From edfda22766016995cec00a3d4b53bb4484006884 Mon Sep 17 00:00:00 2001 From: cubemaze Date: Fri, 28 Jul 2023 01:07:56 +0800 Subject: [PATCH 1/2] feat: remote dns configs --- api/panel/node.go | 101 +++++++++++++++++++++++++++++++--------------- conf/watch.go | 2 +- core/xray/xray.go | 2 +- node/task.go | 1 + 4 files changed, 71 insertions(+), 35 deletions(-) diff --git a/api/panel/node.go b/api/panel/node.go index 1d8fd0f..c51878e 100644 --- a/api/panel/node.go +++ b/api/panel/node.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/base64" "fmt" + coreConf "github.com/xtls/xray-core/infra/conf" "os" "reflect" "strconv" @@ -13,7 +14,6 @@ import ( "github.com/InazumaV/V2bX/common/crypt" "github.com/goccy/go-json" log "github.com/sirupsen/logrus" - coreConf "github.com/xtls/xray-core/infra/conf" ) type CommonNodeRsp struct { @@ -25,10 +25,10 @@ type CommonNodeRsp struct { } type Route struct { - Id int `json:"id"` - Match interface{} `json:"match"` - Action string `json:"action"` - //ActionValue interface{} `json:"action_value"` + Id int `json:"id"` + Match interface{} `json:"match"` + Action string `json:"action"` + ActionValue string `json:"action_value"` } type BaseConfig struct { PushInterval any `json:"push_interval"` @@ -96,6 +96,11 @@ type RealityConfig struct { ShortIds []string `yaml:"ShortIds" json:"ShortIds"` } +type DNSConfig struct { + Servers []interface{} `json:"servers"` + Tag string `json:"tag"` +} + func (c *Client) GetNodeInfo() (node *NodeInfo, err error) { const path = "/api/v1/server/UniProxy/config" r, err := c.client. @@ -118,7 +123,16 @@ func (c *Client) GetNodeInfo() (node *NodeInfo, err error) { if err != nil { return nil, fmt.Errorf("decode common params error: %s", err) } - for i := range common.Routes { // parse rules from routes + + dnsPath := os.Getenv("XRAY_DNS_PATH") + dnsConfig := DNSConfig{ + Servers: []interface{}{ + "1.1.1.1", + "localhost"}, + Tag: "dns_inbound", + } + var isDnsConfigUpdating bool + for i := range common.Routes { var matchs []string if _, ok := common.Routes[i].Match.(string); ok { matchs = strings.Split(common.Routes[i].Match.(string), ",") @@ -144,37 +158,31 @@ func (c *Client) GetNodeInfo() (node *NodeInfo, err error) { } case "dns": if matchs[0] != "main" { + var domains []string + for _, v := range matchs { + domains = append(domains, v) + } + dnsConfig.Servers = append(dnsConfig.Servers, + map[string]interface{}{ + "address": common.Routes[i].ActionValue, + "domains": domains, + }, + ) + isDnsConfigUpdating = true + } else { + dns := []byte(strings.Join(matchs[1:], "")) + saveDnsConfig(dns, dnsPath) 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") - } - } } } + if isDnsConfigUpdating { + dnsConfigJSON, err := json.MarshalIndent(dnsConfig, "", " ") + if err != nil { + fmt.Println("Error marshaling dnsConfig to JSON:", err) + } + saveDnsConfig(dnsConfigJSON, dnsPath) + } node.ServerName = common.ServerName node.Host = common.Host node.Port = common.ServerPort @@ -244,3 +252,30 @@ func intervalToTime(i interface{}) time.Duration { return time.Duration(reflect.ValueOf(i).Int()) * time.Second } } + +func saveDnsConfig(dns []byte, dnsPath string) { + currentData, err := os.ReadFile(dnsPath) + if err != nil { + log.WithField("err", err).Error("Failed to read XRAY_DNS_PATH") + return + } + if !bytes.Equal(currentData, dns) { + coreDnsConfig := &coreConf.DNSConfig{} + if err = json.NewDecoder(bytes.NewReader(dns)).Decode(coreDnsConfig); err != nil { + log.WithField("err", err).Error("Failed to unmarshal DNS config") + } + _, err := coreDnsConfig.Build() + if err != nil { + log.WithField("err", err).Error("Failed to understand DNS config, Please check: https://xtls.github.io/config/dns.html for help") + return + } + if err = os.Truncate(dnsPath, 0); err != nil { + log.WithField("err", err).Error("Failed to clear XRAY DNS PATH file") + } + if err = os.WriteFile(dnsPath, dns, 0644); err != nil { + log.WithField("err", err).Error("Failed to write DNS to XRAY DNS PATH file") + } + } + log.Println("reloading config") + time.Sleep(5 * time.Second) +} diff --git a/conf/watch.go b/conf/watch.go index 0a2d0d8..81c6440 100644 --- a/conf/watch.go +++ b/conf/watch.go @@ -27,7 +27,7 @@ func (p *Conf) Watch(filePath, dnsPath string, reload func()) error { } pre = time.Now() go func() { - time.Sleep(10 * time.Second) + time.Sleep(5 * time.Second) if e.Name == dnsPath { log.Println("DNS file changed, reloading...") } else { diff --git a/core/xray/xray.go b/core/xray/xray.go index 96bb466..f03102f 100644 --- a/core/xray/xray.go +++ b/core/xray/xray.go @@ -67,7 +67,7 @@ func getCore(c *conf.XrayConfig) *core.Instance { log.WithField("err", err).Panic("Failed to read DNS config file") } else { if err = json.NewDecoder(f).Decode(coreDnsConfig); err != nil { - log.WithField("err", err).Panic("Failed to unmarshal DNS config") + log.WithField("err", err).Error("Failed to unmarshal DNS config") } } os.Setenv("XRAY_DNS_PATH", c.DnsConfigPath) diff --git a/node/task.go b/node/task.go index 86297bb..155b981 100644 --- a/node/task.go +++ b/node/task.go @@ -45,6 +45,7 @@ func (c *Controller) startTasks(node *panel.NodeInfo) { Interval: time.Duration(c.LimitConfig.DynamicSpeedLimitConfig.Periodic) * time.Second, Execute: c.SpeedChecker, } + log.Printf("[%s: %d] Start dynamic speed limit", c.apiClient.NodeType, c.apiClient.NodeId) } } From eddcdaa5f0409a09215142b1547120f1a67beb66 Mon Sep 17 00:00:00 2001 From: cubemaze Date: Wed, 2 Aug 2023 14:06:13 +0800 Subject: [PATCH 2/2] fix: bug --- conf/option.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conf/option.go b/conf/option.go index 1fc389b..7073f25 100644 --- a/conf/option.go +++ b/conf/option.go @@ -5,8 +5,8 @@ type Options struct { SendIP string `yaml:"SendIP"` LimitConfig LimitConfig `yaml:"LimitConfig"` CertConfig *CertConfig `yaml:"CertConfig"` - XrayOptions XrayOptions `yaml:"-"` - HyOptions HyOptions `yaml:"-"` + XrayOptions XrayOptions `yaml:"XrayOptions"` + HyOptions HyOptions `yaml:"HyOptions"` } type XrayOptions struct {