mirror of
https://github.com/wyx2685/V2bX.git
synced 2025-01-22 18:08:14 -05:00
change config file load logic
This commit is contained in:
parent
53b330b4e8
commit
3682114f06
36
conf/conf.go
36
conf/conf.go
@ -1,13 +1,20 @@
|
|||||||
package conf
|
package conf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
)
|
||||||
|
|
||||||
type Conf struct {
|
type Conf struct {
|
||||||
LogConfig *LogConfig `mapstructure:"Log"`
|
LogConfig *LogConfig `yaml:"Log"`
|
||||||
DnsConfigPath string `mapstructure:"DnsConfigPath"`
|
DnsConfigPath string `yaml:"DnsConfigPath"`
|
||||||
InboundConfigPath string `mapstructure:"InboundConfigPath"`
|
InboundConfigPath string `yaml:"InboundConfigPath"`
|
||||||
OutboundConfigPath string `mapstructure:"OutboundConfigPath"`
|
OutboundConfigPath string `yaml:"OutboundConfigPath"`
|
||||||
RouteConfigPath string `mapstructure:"RouteConfigPath"`
|
RouteConfigPath string `yaml:"RouteConfigPath"`
|
||||||
ConnectionConfig *ConnetionConfig `mapstructure:"ConnectionConfig"`
|
ConnectionConfig *ConnetionConfig `yaml:"ConnectionConfig"`
|
||||||
NodesConfig []*NodeConfig `mapstructure:"Nodes"`
|
NodesConfig []*NodeConfig `yaml:"Nodes"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() *Conf {
|
func New() *Conf {
|
||||||
@ -21,3 +28,18 @@ func New() *Conf {
|
|||||||
NodesConfig: []*NodeConfig{},
|
NodesConfig: []*NodeConfig{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Conf) LoadFromPath(filePath string) error {
|
||||||
|
confPath := path.Dir(filePath)
|
||||||
|
os.Setenv("XRAY_LOCATION_ASSET", confPath)
|
||||||
|
os.Setenv("XRAY_LOCATION_CONFIG", confPath)
|
||||||
|
f, err := os.Open(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("open config file error: %v", err)
|
||||||
|
}
|
||||||
|
err = yaml.NewDecoder(f).Decode(p)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("decode config error: %v", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
package conf
|
package conf
|
||||||
|
|
||||||
type ConnetionConfig struct {
|
type ConnetionConfig struct {
|
||||||
Handshake uint32 `mapstructure:"handshake"`
|
Handshake uint32 `yaml:"handshake"`
|
||||||
ConnIdle uint32 `mapstructure:"connIdle"`
|
ConnIdle uint32 `yaml:"connIdle"`
|
||||||
UplinkOnly uint32 `mapstructure:"uplinkOnly"`
|
UplinkOnly uint32 `yaml:"uplinkOnly"`
|
||||||
DownlinkOnly uint32 `mapstructure:"downlinkOnly"`
|
DownlinkOnly uint32 `yaml:"downlinkOnly"`
|
||||||
BufferSize int32 `mapstructure:"bufferSize"`
|
BufferSize int32 `yaml:"bufferSize"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConnetionConfig() *ConnetionConfig {
|
func NewConnetionConfig() *ConnetionConfig {
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
package conf
|
package conf
|
||||||
|
|
||||||
type LogConfig struct {
|
type LogConfig struct {
|
||||||
Level string `mapstructure:"Level"`
|
Level string `yaml:"Level"`
|
||||||
AccessPath string `mapstructure:"AccessPath"`
|
AccessPath string `yaml:"AccessPath"`
|
||||||
ErrorPath string `mapstructure:"ErrorPath"`
|
ErrorPath string `yaml:"ErrorPath"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLogConfig() *LogConfig {
|
func NewLogConfig() *LogConfig {
|
||||||
|
106
conf/node.go
106
conf/node.go
@ -1,75 +1,75 @@
|
|||||||
package conf
|
package conf
|
||||||
|
|
||||||
type CertConfig struct {
|
type CertConfig struct {
|
||||||
CertMode string `mapstructure:"CertMode"` // none, file, http, dns
|
CertMode string `yaml:"CertMode"` // none, file, http, dns
|
||||||
RejectUnknownSni bool `mapstructure:"RejectUnknownSni"`
|
RejectUnknownSni bool `yaml:"RejectUnknownSni"`
|
||||||
CertDomain string `mapstructure:"CertDomain"`
|
CertDomain string `yaml:"CertDomain"`
|
||||||
CertFile string `mapstructure:"CertFile"`
|
CertFile string `yaml:"CertFile"`
|
||||||
KeyFile string `mapstructure:"KeyFile"`
|
KeyFile string `yaml:"KeyFile"`
|
||||||
Provider string `mapstructure:"Provider"` // alidns, cloudflare, gandi, godaddy....
|
Provider string `yaml:"Provider"` // alidns, cloudflare, gandi, godaddy....
|
||||||
Email string `mapstructure:"Email"`
|
Email string `yaml:"Email"`
|
||||||
DNSEnv map[string]string `mapstructure:"DNSEnv"`
|
DNSEnv map[string]string `yaml:"DNSEnv"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type FallBackConfig struct {
|
type FallBackConfig struct {
|
||||||
SNI string `mapstructure:"SNI"`
|
SNI string `yaml:"SNI"`
|
||||||
Alpn string `mapstructure:"Alpn"`
|
Alpn string `yaml:"Alpn"`
|
||||||
Path string `mapstructure:"Path"`
|
Path string `yaml:"Path"`
|
||||||
Dest string `mapstructure:"Dest"`
|
Dest string `yaml:"Dest"`
|
||||||
ProxyProtocolVer uint64 `mapstructure:"ProxyProtocolVer"`
|
ProxyProtocolVer uint64 `yaml:"ProxyProtocolVer"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type IpReportConfig struct {
|
type IpReportConfig struct {
|
||||||
Url string `mapstructure:"Url"`
|
Url string `yaml:"Url"`
|
||||||
Token string `mapstructure:"Token"`
|
Token string `yaml:"Token"`
|
||||||
Periodic int `mapstructure:"Periodic"`
|
Periodic int `yaml:"Periodic"`
|
||||||
Timeout int `mapstructure:"Timeout"`
|
Timeout int `yaml:"Timeout"`
|
||||||
EnableIpSync bool `mapstructure:"EnableIpSync"`
|
EnableIpSync bool `yaml:"EnableIpSync"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type DynamicSpeedLimitConfig struct {
|
type DynamicSpeedLimitConfig struct {
|
||||||
Periodic int `mapstructure:"Periodic"`
|
Periodic int `yaml:"Periodic"`
|
||||||
Traffic int64 `mapstructure:"Traffic"`
|
Traffic int64 `yaml:"Traffic"`
|
||||||
SpeedLimit uint64 `mapstructure:"SpeedLimit"`
|
SpeedLimit uint64 `yaml:"SpeedLimit"`
|
||||||
ExpireTime int `mapstructure:"ExpireTime"`
|
ExpireTime int `yaml:"ExpireTime"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ControllerConfig struct {
|
type ControllerConfig struct {
|
||||||
ListenIP string `mapstructure:"ListenIP"`
|
ListenIP string `yaml:"ListenIP"`
|
||||||
SendIP string `mapstructure:"SendIP"`
|
SendIP string `yaml:"SendIP"`
|
||||||
UpdatePeriodic int `mapstructure:"UpdatePeriodic"`
|
UpdatePeriodic int `yaml:"UpdatePeriodic"`
|
||||||
EnableDNS bool `mapstructure:"EnableDNS"`
|
EnableDNS bool `yaml:"EnableDNS"`
|
||||||
DNSType string `mapstructure:"DNSType"`
|
DNSType string `yaml:"DNSType"`
|
||||||
DisableUploadTraffic bool `mapstructure:"DisableUploadTraffic"`
|
DisableUploadTraffic bool `yaml:"DisableUploadTraffic"`
|
||||||
DisableGetRule bool `mapstructure:"DisableGetRule"`
|
DisableGetRule bool `yaml:"DisableGetRule"`
|
||||||
EnableProxyProtocol bool `mapstructure:"EnableProxyProtocol"`
|
EnableProxyProtocol bool `yaml:"EnableProxyProtocol"`
|
||||||
EnableFallback bool `mapstructure:"EnableFallback"`
|
EnableFallback bool `yaml:"EnableFallback"`
|
||||||
DisableIVCheck bool `mapstructure:"DisableIVCheck"`
|
DisableIVCheck bool `yaml:"DisableIVCheck"`
|
||||||
DisableSniffing bool `mapstructure:"DisableSniffing"`
|
DisableSniffing bool `yaml:"DisableSniffing"`
|
||||||
FallBackConfigs []*FallBackConfig `mapstructure:"FallBackConfigs"`
|
FallBackConfigs []*FallBackConfig `yaml:"FallBackConfigs"`
|
||||||
EnableIpRecorder bool `mapstructure:"EnableIpRecorder"`
|
EnableIpRecorder bool `yaml:"EnableIpRecorder"`
|
||||||
IpRecorderConfig *IpReportConfig `mapstructure:"IpRecorderConfig"`
|
IpRecorderConfig *IpReportConfig `yaml:"IpRecorderConfig"`
|
||||||
EnableDynamicSpeedLimit bool `mapstructure:"EnableDynamicSpeedLimit"`
|
EnableDynamicSpeedLimit bool `yaml:"EnableDynamicSpeedLimit"`
|
||||||
DynamicSpeedLimitConfig *DynamicSpeedLimitConfig `mapstructure:"DynamicSpeedLimitConfig"`
|
DynamicSpeedLimitConfig *DynamicSpeedLimitConfig `yaml:"DynamicSpeedLimitConfig"`
|
||||||
CertConfig *CertConfig `mapstructure:"CertConfig"`
|
CertConfig *CertConfig `yaml:"CertConfig"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ApiConfig struct {
|
type ApiConfig struct {
|
||||||
APIHost string `mapstructure:"ApiHost"`
|
APIHost string `yaml:"ApiHost"`
|
||||||
NodeID int `mapstructure:"NodeID"`
|
NodeID int `yaml:"NodeID"`
|
||||||
Key string `mapstructure:"ApiKey"`
|
Key string `yaml:"ApiKey"`
|
||||||
NodeType string `mapstructure:"NodeType"`
|
NodeType string `yaml:"NodeType"`
|
||||||
EnableVless bool `mapstructure:"EnableVless"`
|
EnableVless bool `yaml:"EnableVless"`
|
||||||
EnableXTLS bool `mapstructure:"EnableXTLS"`
|
EnableXTLS bool `yaml:"EnableXTLS"`
|
||||||
//EnableSS2022 bool `mapstructure:"EnableSS2022"`
|
//EnableSS2022 bool `yaml:"EnableSS2022"`
|
||||||
Timeout int `mapstructure:"Timeout"`
|
Timeout int `yaml:"Timeout"`
|
||||||
SpeedLimit float64 `mapstructure:"SpeedLimit"`
|
SpeedLimit float64 `yaml:"SpeedLimit"`
|
||||||
DeviceLimit int `mapstructure:"DeviceLimit"`
|
DeviceLimit int `yaml:"DeviceLimit"`
|
||||||
RuleListPath string `mapstructure:"RuleListPath"`
|
RuleListPath string `yaml:"RuleListPath"`
|
||||||
DisableCustomConfig bool `mapstructure:"DisableCustomConfig"`
|
DisableCustomConfig bool `yaml:"DisableCustomConfig"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type NodeConfig struct {
|
type NodeConfig struct {
|
||||||
ApiConfig *ApiConfig `mapstructure:"ApiConfig"`
|
ApiConfig *ApiConfig `yaml:"ApiConfig"`
|
||||||
ControllerConfig *ControllerConfig `mapstructure:"ControllerConfig"`
|
ControllerConfig *ControllerConfig `yaml:"ControllerConfig"`
|
||||||
}
|
}
|
||||||
|
18
core/core.go
18
core/core.go
@ -14,8 +14,8 @@ import (
|
|||||||
"github.com/xtls/xray-core/features/routing"
|
"github.com/xtls/xray-core/features/routing"
|
||||||
statsFeature "github.com/xtls/xray-core/features/stats"
|
statsFeature "github.com/xtls/xray-core/features/stats"
|
||||||
coreConf "github.com/xtls/xray-core/infra/conf"
|
coreConf "github.com/xtls/xray-core/infra/conf"
|
||||||
io "io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -55,10 +55,10 @@ func getCore(v2bXConfig *conf.Conf) *core.Instance {
|
|||||||
// DNS config
|
// DNS config
|
||||||
coreDnsConfig := &coreConf.DNSConfig{}
|
coreDnsConfig := &coreConf.DNSConfig{}
|
||||||
if v2bXConfig.DnsConfigPath != "" {
|
if v2bXConfig.DnsConfigPath != "" {
|
||||||
if data, err := io.ReadFile(v2bXConfig.DnsConfigPath); err != nil {
|
if f, err := os.Open(v2bXConfig.DnsConfigPath); err != nil {
|
||||||
log.Panicf("Failed to read DNS config file at: %s", v2bXConfig.DnsConfigPath)
|
log.Panicf("Failed to read DNS config file at: %s", v2bXConfig.DnsConfigPath)
|
||||||
} else {
|
} else {
|
||||||
if err = json.Unmarshal(data, coreDnsConfig); err != nil {
|
if err = json.NewDecoder(f).Decode(coreDnsConfig); err != nil {
|
||||||
log.Panicf("Failed to unmarshal DNS config: %s", v2bXConfig.DnsConfigPath)
|
log.Panicf("Failed to unmarshal DNS config: %s", v2bXConfig.DnsConfigPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -70,10 +70,10 @@ func getCore(v2bXConfig *conf.Conf) *core.Instance {
|
|||||||
// Routing config
|
// Routing config
|
||||||
coreRouterConfig := &coreConf.RouterConfig{}
|
coreRouterConfig := &coreConf.RouterConfig{}
|
||||||
if v2bXConfig.RouteConfigPath != "" {
|
if v2bXConfig.RouteConfigPath != "" {
|
||||||
if data, err := io.ReadFile(v2bXConfig.RouteConfigPath); err != nil {
|
if f, err := os.Open(v2bXConfig.RouteConfigPath); err != nil {
|
||||||
log.Panicf("Failed to read Routing config file at: %s", v2bXConfig.RouteConfigPath)
|
log.Panicf("Failed to read Routing config file at: %s", v2bXConfig.RouteConfigPath)
|
||||||
} else {
|
} else {
|
||||||
if err = json.Unmarshal(data, coreRouterConfig); err != nil {
|
if err = json.NewDecoder(f).Decode(coreRouterConfig); err != nil {
|
||||||
log.Panicf("Failed to unmarshal Routing config: %s", v2bXConfig.RouteConfigPath)
|
log.Panicf("Failed to unmarshal Routing config: %s", v2bXConfig.RouteConfigPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -85,10 +85,10 @@ func getCore(v2bXConfig *conf.Conf) *core.Instance {
|
|||||||
// Custom Inbound config
|
// Custom Inbound config
|
||||||
var coreCustomInboundConfig []coreConf.InboundDetourConfig
|
var coreCustomInboundConfig []coreConf.InboundDetourConfig
|
||||||
if v2bXConfig.InboundConfigPath != "" {
|
if v2bXConfig.InboundConfigPath != "" {
|
||||||
if data, err := io.ReadFile(v2bXConfig.InboundConfigPath); err != nil {
|
if f, err := os.Open(v2bXConfig.InboundConfigPath); err != nil {
|
||||||
log.Panicf("Failed to read Custom Inbound config file at: %s", v2bXConfig.OutboundConfigPath)
|
log.Panicf("Failed to read Custom Inbound config file at: %s", v2bXConfig.OutboundConfigPath)
|
||||||
} else {
|
} else {
|
||||||
if err = json.Unmarshal(data, &coreCustomInboundConfig); err != nil {
|
if err = json.NewDecoder(f).Decode(&coreCustomInboundConfig); err != nil {
|
||||||
log.Panicf("Failed to unmarshal Custom Inbound config: %s", v2bXConfig.OutboundConfigPath)
|
log.Panicf("Failed to unmarshal Custom Inbound config: %s", v2bXConfig.OutboundConfigPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -104,10 +104,10 @@ func getCore(v2bXConfig *conf.Conf) *core.Instance {
|
|||||||
// Custom Outbound config
|
// Custom Outbound config
|
||||||
var coreCustomOutboundConfig []coreConf.OutboundDetourConfig
|
var coreCustomOutboundConfig []coreConf.OutboundDetourConfig
|
||||||
if v2bXConfig.OutboundConfigPath != "" {
|
if v2bXConfig.OutboundConfigPath != "" {
|
||||||
if data, err := io.ReadFile(v2bXConfig.OutboundConfigPath); err != nil {
|
if f, err := os.Open(v2bXConfig.OutboundConfigPath); err != nil {
|
||||||
log.Panicf("Failed to read Custom Outbound config file at: %s", v2bXConfig.OutboundConfigPath)
|
log.Panicf("Failed to read Custom Outbound config file at: %s", v2bXConfig.OutboundConfigPath)
|
||||||
} else {
|
} else {
|
||||||
if err = json.Unmarshal(data, &coreCustomOutboundConfig); err != nil {
|
if err = json.NewDecoder(f).Decode(&coreCustomOutboundConfig); err != nil {
|
||||||
log.Panicf("Failed to unmarshal Custom Outbound config: %s", v2bXConfig.OutboundConfigPath)
|
log.Panicf("Failed to unmarshal Custom Outbound config: %s", v2bXConfig.OutboundConfigPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
42
main.go
42
main.go
@ -7,18 +7,15 @@ import (
|
|||||||
"github.com/Yuzuki616/V2bX/conf"
|
"github.com/Yuzuki616/V2bX/conf"
|
||||||
"github.com/Yuzuki616/V2bX/core"
|
"github.com/Yuzuki616/V2bX/core"
|
||||||
"github.com/Yuzuki616/V2bX/node"
|
"github.com/Yuzuki616/V2bX/node"
|
||||||
"github.com/spf13/viper"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"path"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
configFile = flag.String("config", "", "Config file for XrayR.")
|
configFile = flag.String("config", "/etc/V2bX/config.yml", "Config file for V2bX.")
|
||||||
printVersion = flag.Bool("version", false, "show version")
|
printVersion = flag.Bool("version", false, "show version")
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -32,34 +29,8 @@ func showVersion() {
|
|||||||
fmt.Printf("%s %s (%s) \n", codename, version, intro)
|
fmt.Printf("%s %s (%s) \n", codename, version, intro)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getConfig() *viper.Viper {
|
|
||||||
config := viper.New()
|
|
||||||
// Set custom path and name
|
|
||||||
if *configFile != "" {
|
|
||||||
configName := path.Base(*configFile)
|
|
||||||
configFileExt := path.Ext(*configFile)
|
|
||||||
configNameOnly := strings.TrimSuffix(configName, configFileExt)
|
|
||||||
configPath := path.Dir(*configFile)
|
|
||||||
config.SetConfigName(configNameOnly)
|
|
||||||
config.SetConfigType(strings.TrimPrefix(configFileExt, "."))
|
|
||||||
config.AddConfigPath(configPath)
|
|
||||||
// Set ASSET Path and Config Path for XrayR
|
|
||||||
os.Setenv("XRAY_LOCATION_ASSET", configPath)
|
|
||||||
os.Setenv("XRAY_LOCATION_CONFIG", configPath)
|
|
||||||
} else {
|
|
||||||
// Set default config path
|
|
||||||
config.SetConfigName("config")
|
|
||||||
config.SetConfigType("yml")
|
|
||||||
config.AddConfigPath(".")
|
|
||||||
}
|
|
||||||
if err := config.ReadInConfig(); err != nil {
|
|
||||||
log.Panicf("Fatal error config file: %s \n", err)
|
|
||||||
}
|
|
||||||
return config
|
|
||||||
}
|
|
||||||
|
|
||||||
func startNodes(nodes []*conf.NodeConfig, core *core.Core) error {
|
func startNodes(nodes []*conf.NodeConfig, core *core.Core) error {
|
||||||
for i, _ := range nodes {
|
for i := range nodes {
|
||||||
var apiClient = panel.New(nodes[i].ApiConfig)
|
var apiClient = panel.New(nodes[i].ApiConfig)
|
||||||
// Register controller service
|
// Register controller service
|
||||||
err := node.New(core, apiClient, nodes[i].ControllerConfig).Start()
|
err := node.New(core, apiClient, nodes[i].ControllerConfig).Start()
|
||||||
@ -76,16 +47,15 @@ func main() {
|
|||||||
if *printVersion {
|
if *printVersion {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
config := getConfig()
|
config := conf.New()
|
||||||
c := conf.New()
|
err := config.LoadFromPath(*configFile)
|
||||||
err := config.Unmarshal(c)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("can't unmarshal config file: %s \n", err)
|
log.Panicf("can't unmarshal config file: %s \n", err)
|
||||||
}
|
}
|
||||||
x := core.New(c)
|
x := core.New(config)
|
||||||
x.Start()
|
x.Start()
|
||||||
defer x.Close()
|
defer x.Close()
|
||||||
err = startNodes(c.NodesConfig, x)
|
err = startNodes(config.NodesConfig, x)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("run nodes error: %v", err)
|
log.Panicf("run nodes error: %v", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user