mirror of
https://github.com/wyx2685/V2bX.git
synced 2025-01-22 09:58:14 -05:00
feat: remote dns configs
This commit is contained in:
parent
a18cd2f134
commit
edfda22766
@ -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)
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user