mirror of
https://github.com/wyx2685/V2bX.git
synced 2025-01-22 18:08:14 -05:00
Merge pull request #5 from stitchrs/dev_remoteDns
feat: remote dns configs
This commit is contained in:
commit
d659971c71
@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
coreConf "github.com/xtls/xray-core/infra/conf"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -13,7 +14,6 @@ import (
|
|||||||
"github.com/InazumaV/V2bX/common/crypt"
|
"github.com/InazumaV/V2bX/common/crypt"
|
||||||
"github.com/goccy/go-json"
|
"github.com/goccy/go-json"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
coreConf "github.com/xtls/xray-core/infra/conf"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type CommonNodeRsp struct {
|
type CommonNodeRsp struct {
|
||||||
@ -25,10 +25,10 @@ type CommonNodeRsp struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Route struct {
|
type Route struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
Match interface{} `json:"match"`
|
Match interface{} `json:"match"`
|
||||||
Action string `json:"action"`
|
Action string `json:"action"`
|
||||||
//ActionValue interface{} `json:"action_value"`
|
ActionValue string `json:"action_value"`
|
||||||
}
|
}
|
||||||
type BaseConfig struct {
|
type BaseConfig struct {
|
||||||
PushInterval any `json:"push_interval"`
|
PushInterval any `json:"push_interval"`
|
||||||
@ -96,6 +96,11 @@ type RealityConfig struct {
|
|||||||
ShortIds []string `yaml:"ShortIds" json:"ShortIds"`
|
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) {
|
func (c *Client) GetNodeInfo() (node *NodeInfo, err error) {
|
||||||
const path = "/api/v1/server/UniProxy/config"
|
const path = "/api/v1/server/UniProxy/config"
|
||||||
r, err := c.client.
|
r, err := c.client.
|
||||||
@ -118,7 +123,16 @@ func (c *Client) GetNodeInfo() (node *NodeInfo, err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("decode common params error: %s", err)
|
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
|
var matchs []string
|
||||||
if _, ok := common.Routes[i].Match.(string); ok {
|
if _, ok := common.Routes[i].Match.(string); ok {
|
||||||
matchs = strings.Split(common.Routes[i].Match.(string), ",")
|
matchs = strings.Split(common.Routes[i].Match.(string), ",")
|
||||||
@ -144,37 +158,31 @@ func (c *Client) GetNodeInfo() (node *NodeInfo, err error) {
|
|||||||
}
|
}
|
||||||
case "dns":
|
case "dns":
|
||||||
if matchs[0] != "main" {
|
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
|
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.ServerName = common.ServerName
|
||||||
node.Host = common.Host
|
node.Host = common.Host
|
||||||
node.Port = common.ServerPort
|
node.Port = common.ServerPort
|
||||||
@ -244,3 +252,30 @@ func intervalToTime(i interface{}) time.Duration {
|
|||||||
return time.Duration(reflect.ValueOf(i).Int()) * time.Second
|
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)
|
||||||
|
}
|
||||||
|
@ -5,8 +5,8 @@ type Options struct {
|
|||||||
SendIP string `yaml:"SendIP"`
|
SendIP string `yaml:"SendIP"`
|
||||||
LimitConfig LimitConfig `yaml:"LimitConfig"`
|
LimitConfig LimitConfig `yaml:"LimitConfig"`
|
||||||
CertConfig *CertConfig `yaml:"CertConfig"`
|
CertConfig *CertConfig `yaml:"CertConfig"`
|
||||||
XrayOptions XrayOptions `yaml:"-"`
|
XrayOptions XrayOptions `yaml:"XrayOptions"`
|
||||||
HyOptions HyOptions `yaml:"-"`
|
HyOptions HyOptions `yaml:"HyOptions"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type XrayOptions struct {
|
type XrayOptions struct {
|
||||||
|
@ -27,7 +27,7 @@ func (p *Conf) Watch(filePath, dnsPath string, reload func()) error {
|
|||||||
}
|
}
|
||||||
pre = time.Now()
|
pre = time.Now()
|
||||||
go func() {
|
go func() {
|
||||||
time.Sleep(10 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
if e.Name == dnsPath {
|
if e.Name == dnsPath {
|
||||||
log.Println("DNS file changed, reloading...")
|
log.Println("DNS file changed, reloading...")
|
||||||
} else {
|
} else {
|
||||||
|
@ -67,7 +67,7 @@ func getCore(c *conf.XrayConfig) *core.Instance {
|
|||||||
log.WithField("err", err).Panic("Failed to read DNS config file")
|
log.WithField("err", err).Panic("Failed to read DNS config file")
|
||||||
} else {
|
} else {
|
||||||
if err = json.NewDecoder(f).Decode(coreDnsConfig); err != nil {
|
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)
|
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,
|
Interval: time.Duration(c.LimitConfig.DynamicSpeedLimitConfig.Periodic) * time.Second,
|
||||||
Execute: c.SpeedChecker,
|
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