mirror of
https://github.com/wyx2685/V2bX.git
synced 2025-01-22 09:58:14 -05:00
refine remote dns
This commit is contained in:
parent
99e68846d7
commit
ebefcd6343
@ -12,7 +12,11 @@ import (
|
||||
func updateDNSConfig(node *panel.NodeInfo) (err error) {
|
||||
dnsPath := os.Getenv("SING_DNS_PATH")
|
||||
if len(node.RawDNS.DNSJson) != 0 {
|
||||
err = saveDnsConfig(node.RawDNS.DNSJson, dnsPath)
|
||||
var prettyJSON bytes.Buffer
|
||||
if err := json.Indent(&prettyJSON, node.RawDNS.DNSJson, "", " "); err != nil {
|
||||
return err
|
||||
}
|
||||
err = saveDnsConfig(prettyJSON.Bytes(), dnsPath)
|
||||
} else if len(node.RawDNS.DNSMap) != 0 {
|
||||
dnsConfig := DNSConfig{
|
||||
Servers: []map[string]interface{}{
|
||||
|
@ -3,6 +3,9 @@ package sing
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/inazumav/sing-box/constant"
|
||||
"github.com/inazumav/sing-box/log"
|
||||
vlog "github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
@ -75,13 +78,16 @@ func New(c *conf.CoreConfig) (vCore.Core, error) {
|
||||
},
|
||||
}
|
||||
os.Setenv("SING_DNS_PATH", "")
|
||||
options.DNS = &option.DNSOptions{}
|
||||
if c.SingConfig.DnsConfigPath != "" {
|
||||
if f, err := os.Open(c.SingConfig.DnsConfigPath); err != nil {
|
||||
log.Error("Failed to read DNS config file")
|
||||
} else {
|
||||
if err = json.NewDecoder(f).Decode(&option.DNSOptions{}); err != nil {
|
||||
log.Error("Failed to unmarshal DNS config")
|
||||
}
|
||||
f, err := os.OpenFile(c.SingConfig.DnsConfigPath, os.O_RDWR|os.O_CREATE, 0755)
|
||||
if err != nil {
|
||||
log.Error("Failed to open or create sing dns config file: %s", err)
|
||||
}
|
||||
defer f.Close()
|
||||
if err := json.NewDecoder(f).Decode(options.DNS); err != nil {
|
||||
log.Error(fmt.Sprintf("Failed to unmarshal sing dns config from file '%v': %v. Using default DNS options.", f.Name(), err))
|
||||
options.DNS = &option.DNSOptions{}
|
||||
}
|
||||
os.Setenv("SING_DNS_PATH", c.SingConfig.DnsConfigPath)
|
||||
}
|
||||
@ -230,6 +236,7 @@ func (b *Box) preStart() error {
|
||||
}
|
||||
|
||||
func (b *Box) start() error {
|
||||
vlog.Info("Sing Core Version: ", constant.Version)
|
||||
err := b.preStart()
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -6,13 +6,20 @@ import (
|
||||
"github.com/goccy/go-json"
|
||||
log "github.com/sirupsen/logrus"
|
||||
coreConf "github.com/xtls/xray-core/infra/conf"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
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)
|
||||
var prettyJSON bytes.Buffer
|
||||
if err := json.Indent(&prettyJSON, node.RawDNS.DNSJson, "", " "); err != nil {
|
||||
return err
|
||||
}
|
||||
err = saveDnsConfig(prettyJSON.Bytes(), dnsPath)
|
||||
} else if len(node.RawDNS.DNSMap) != 0 {
|
||||
dnsConfig := DNSConfig{
|
||||
Servers: []interface{}{
|
||||
@ -21,7 +28,21 @@ func updateDNSConfig(node *panel.NodeInfo) (err error) {
|
||||
Tag: "dns_inbound",
|
||||
}
|
||||
for _, value := range node.RawDNS.DNSMap {
|
||||
address := value["address"].(string)
|
||||
if strings.Contains(address, ":") && !strings.Contains(address, "/") {
|
||||
host, port, err := net.SplitHostPort(address)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var uint16Port uint16
|
||||
if port, err := strconv.ParseUint(port, 10, 16); err == nil {
|
||||
uint16Port = uint16(port)
|
||||
}
|
||||
value["address"] = host
|
||||
value["port"] = uint16Port
|
||||
}
|
||||
dnsConfig.Servers = append(dnsConfig.Servers, value)
|
||||
|
||||
}
|
||||
dnsConfigJSON, err := json.MarshalIndent(dnsConfig, "", " ")
|
||||
if err != nil {
|
||||
|
@ -1,6 +1,7 @@
|
||||
package xray
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
@ -63,12 +64,14 @@ func getCore(c *conf.XrayConfig) *core.Instance {
|
||||
coreDnsConfig := &coreConf.DNSConfig{}
|
||||
os.Setenv("XRAY_DNS_PATH", "")
|
||||
if c.DnsConfigPath != "" {
|
||||
if f, err := os.Open(c.DnsConfigPath); err != nil {
|
||||
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).Error("Failed to unmarshal DNS config")
|
||||
}
|
||||
f, err := os.OpenFile(c.DnsConfigPath, os.O_RDWR|os.O_CREATE, 0755)
|
||||
if err != nil {
|
||||
log.Error("Failed to open or create xray dns config file: %v", err)
|
||||
}
|
||||
defer f.Close()
|
||||
if err := json.NewDecoder(f).Decode(coreDnsConfig); err != nil {
|
||||
log.Error(fmt.Sprintf("Failed to unmarshal xray dns config from file '%v': %v. Using default DNS options.", f.Name(), err))
|
||||
coreDnsConfig = &coreConf.DNSConfig{}
|
||||
}
|
||||
os.Setenv("XRAY_DNS_PATH", c.DnsConfigPath)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user