mirror of
https://github.com/wyx2685/V2bX.git
synced 2026-02-04 04:30:08 +00:00
chore: move xray dns configuration processing
This commit is contained in:
63
core/xray/dns.go
Normal file
63
core/xray/dns.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package xray
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/InazumaV/V2bX/api/panel"
|
||||
"github.com/goccy/go-json"
|
||||
log "github.com/sirupsen/logrus"
|
||||
coreConf "github.com/xtls/xray-core/infra/conf"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func updateDNSConfig(node *panel.NodeInfo) (err error) {
|
||||
dnsPath := os.Getenv("XRAY_DNS_PATH")
|
||||
if len(node.RawDNS.DNSJson) != 0 {
|
||||
err = saveDnsConfig(node.RawDNS.DNSJson, dnsPath)
|
||||
} else if len(node.RawDNS.DNSMap) != 0 {
|
||||
dnsConfig := DNSConfig{
|
||||
Servers: []interface{}{
|
||||
"1.1.1.1",
|
||||
"localhost"},
|
||||
Tag: "dns_inbound",
|
||||
}
|
||||
for _, value := range node.RawDNS.DNSMap {
|
||||
dnsConfig.Servers = append(dnsConfig.Servers, value)
|
||||
}
|
||||
dnsConfigJSON, err := json.MarshalIndent(dnsConfig, "", " ")
|
||||
if err != nil {
|
||||
log.WithField("err", err).Error("Error marshaling dnsConfig to JSON")
|
||||
return
|
||||
}
|
||||
err = saveDnsConfig(dnsConfigJSON, dnsPath)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func saveDnsConfig(dns []byte, dnsPath string) (err error) {
|
||||
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)
|
||||
return err
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package xray
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/InazumaV/V2bX/api/panel"
|
||||
"github.com/InazumaV/V2bX/conf"
|
||||
"github.com/xtls/xray-core/core"
|
||||
@@ -11,7 +10,16 @@ import (
|
||||
"github.com/xtls/xray-core/features/outbound"
|
||||
)
|
||||
|
||||
type DNSConfig struct {
|
||||
Servers []interface{} `json:"servers"`
|
||||
Tag string `json:"tag"`
|
||||
}
|
||||
|
||||
func (c *Core) AddNode(tag string, info *panel.NodeInfo, config *conf.Options) error {
|
||||
err := updateDNSConfig(info)
|
||||
if err != nil {
|
||||
return fmt.Errorf("build dns error: %s", err)
|
||||
}
|
||||
inboundConfig, err := buildInbound(config, info, tag)
|
||||
if err != nil {
|
||||
return fmt.Errorf("build inbound error: %s", err)
|
||||
|
||||
Reference in New Issue
Block a user