change config file load logic

This commit is contained in:
yuzuki999 2022-09-13 11:08:20 +08:00
parent 53b330b4e8
commit 3682114f06
6 changed files with 105 additions and 113 deletions

View File

@ -1,13 +1,20 @@
package conf
import (
"fmt"
"gopkg.in/yaml.v3"
"os"
"path"
)
type Conf struct {
LogConfig *LogConfig `mapstructure:"Log"`
DnsConfigPath string `mapstructure:"DnsConfigPath"`
InboundConfigPath string `mapstructure:"InboundConfigPath"`
OutboundConfigPath string `mapstructure:"OutboundConfigPath"`
RouteConfigPath string `mapstructure:"RouteConfigPath"`
ConnectionConfig *ConnetionConfig `mapstructure:"ConnectionConfig"`
NodesConfig []*NodeConfig `mapstructure:"Nodes"`
LogConfig *LogConfig `yaml:"Log"`
DnsConfigPath string `yaml:"DnsConfigPath"`
InboundConfigPath string `yaml:"InboundConfigPath"`
OutboundConfigPath string `yaml:"OutboundConfigPath"`
RouteConfigPath string `yaml:"RouteConfigPath"`
ConnectionConfig *ConnetionConfig `yaml:"ConnectionConfig"`
NodesConfig []*NodeConfig `yaml:"Nodes"`
}
func New() *Conf {
@ -21,3 +28,18 @@ func New() *Conf {
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
}

View File

@ -1,11 +1,11 @@
package conf
type ConnetionConfig struct {
Handshake uint32 `mapstructure:"handshake"`
ConnIdle uint32 `mapstructure:"connIdle"`
UplinkOnly uint32 `mapstructure:"uplinkOnly"`
DownlinkOnly uint32 `mapstructure:"downlinkOnly"`
BufferSize int32 `mapstructure:"bufferSize"`
Handshake uint32 `yaml:"handshake"`
ConnIdle uint32 `yaml:"connIdle"`
UplinkOnly uint32 `yaml:"uplinkOnly"`
DownlinkOnly uint32 `yaml:"downlinkOnly"`
BufferSize int32 `yaml:"bufferSize"`
}
func NewConnetionConfig() *ConnetionConfig {

View File

@ -1,9 +1,9 @@
package conf
type LogConfig struct {
Level string `mapstructure:"Level"`
AccessPath string `mapstructure:"AccessPath"`
ErrorPath string `mapstructure:"ErrorPath"`
Level string `yaml:"Level"`
AccessPath string `yaml:"AccessPath"`
ErrorPath string `yaml:"ErrorPath"`
}
func NewLogConfig() *LogConfig {

View File

@ -1,75 +1,75 @@
package conf
type CertConfig struct {
CertMode string `mapstructure:"CertMode"` // none, file, http, dns
RejectUnknownSni bool `mapstructure:"RejectUnknownSni"`
CertDomain string `mapstructure:"CertDomain"`
CertFile string `mapstructure:"CertFile"`
KeyFile string `mapstructure:"KeyFile"`
Provider string `mapstructure:"Provider"` // alidns, cloudflare, gandi, godaddy....
Email string `mapstructure:"Email"`
DNSEnv map[string]string `mapstructure:"DNSEnv"`
CertMode string `yaml:"CertMode"` // none, file, http, dns
RejectUnknownSni bool `yaml:"RejectUnknownSni"`
CertDomain string `yaml:"CertDomain"`
CertFile string `yaml:"CertFile"`
KeyFile string `yaml:"KeyFile"`
Provider string `yaml:"Provider"` // alidns, cloudflare, gandi, godaddy....
Email string `yaml:"Email"`
DNSEnv map[string]string `yaml:"DNSEnv"`
}
type FallBackConfig struct {
SNI string `mapstructure:"SNI"`
Alpn string `mapstructure:"Alpn"`
Path string `mapstructure:"Path"`
Dest string `mapstructure:"Dest"`
ProxyProtocolVer uint64 `mapstructure:"ProxyProtocolVer"`
SNI string `yaml:"SNI"`
Alpn string `yaml:"Alpn"`
Path string `yaml:"Path"`
Dest string `yaml:"Dest"`
ProxyProtocolVer uint64 `yaml:"ProxyProtocolVer"`
}
type IpReportConfig struct {
Url string `mapstructure:"Url"`
Token string `mapstructure:"Token"`
Periodic int `mapstructure:"Periodic"`
Timeout int `mapstructure:"Timeout"`
EnableIpSync bool `mapstructure:"EnableIpSync"`
Url string `yaml:"Url"`
Token string `yaml:"Token"`
Periodic int `yaml:"Periodic"`
Timeout int `yaml:"Timeout"`
EnableIpSync bool `yaml:"EnableIpSync"`
}
type DynamicSpeedLimitConfig struct {
Periodic int `mapstructure:"Periodic"`
Traffic int64 `mapstructure:"Traffic"`
SpeedLimit uint64 `mapstructure:"SpeedLimit"`
ExpireTime int `mapstructure:"ExpireTime"`
Periodic int `yaml:"Periodic"`
Traffic int64 `yaml:"Traffic"`
SpeedLimit uint64 `yaml:"SpeedLimit"`
ExpireTime int `yaml:"ExpireTime"`
}
type ControllerConfig struct {
ListenIP string `mapstructure:"ListenIP"`
SendIP string `mapstructure:"SendIP"`
UpdatePeriodic int `mapstructure:"UpdatePeriodic"`
EnableDNS bool `mapstructure:"EnableDNS"`
DNSType string `mapstructure:"DNSType"`
DisableUploadTraffic bool `mapstructure:"DisableUploadTraffic"`
DisableGetRule bool `mapstructure:"DisableGetRule"`
EnableProxyProtocol bool `mapstructure:"EnableProxyProtocol"`
EnableFallback bool `mapstructure:"EnableFallback"`
DisableIVCheck bool `mapstructure:"DisableIVCheck"`
DisableSniffing bool `mapstructure:"DisableSniffing"`
FallBackConfigs []*FallBackConfig `mapstructure:"FallBackConfigs"`
EnableIpRecorder bool `mapstructure:"EnableIpRecorder"`
IpRecorderConfig *IpReportConfig `mapstructure:"IpRecorderConfig"`
EnableDynamicSpeedLimit bool `mapstructure:"EnableDynamicSpeedLimit"`
DynamicSpeedLimitConfig *DynamicSpeedLimitConfig `mapstructure:"DynamicSpeedLimitConfig"`
CertConfig *CertConfig `mapstructure:"CertConfig"`
ListenIP string `yaml:"ListenIP"`
SendIP string `yaml:"SendIP"`
UpdatePeriodic int `yaml:"UpdatePeriodic"`
EnableDNS bool `yaml:"EnableDNS"`
DNSType string `yaml:"DNSType"`
DisableUploadTraffic bool `yaml:"DisableUploadTraffic"`
DisableGetRule bool `yaml:"DisableGetRule"`
EnableProxyProtocol bool `yaml:"EnableProxyProtocol"`
EnableFallback bool `yaml:"EnableFallback"`
DisableIVCheck bool `yaml:"DisableIVCheck"`
DisableSniffing bool `yaml:"DisableSniffing"`
FallBackConfigs []*FallBackConfig `yaml:"FallBackConfigs"`
EnableIpRecorder bool `yaml:"EnableIpRecorder"`
IpRecorderConfig *IpReportConfig `yaml:"IpRecorderConfig"`
EnableDynamicSpeedLimit bool `yaml:"EnableDynamicSpeedLimit"`
DynamicSpeedLimitConfig *DynamicSpeedLimitConfig `yaml:"DynamicSpeedLimitConfig"`
CertConfig *CertConfig `yaml:"CertConfig"`
}
type ApiConfig struct {
APIHost string `mapstructure:"ApiHost"`
NodeID int `mapstructure:"NodeID"`
Key string `mapstructure:"ApiKey"`
NodeType string `mapstructure:"NodeType"`
EnableVless bool `mapstructure:"EnableVless"`
EnableXTLS bool `mapstructure:"EnableXTLS"`
//EnableSS2022 bool `mapstructure:"EnableSS2022"`
Timeout int `mapstructure:"Timeout"`
SpeedLimit float64 `mapstructure:"SpeedLimit"`
DeviceLimit int `mapstructure:"DeviceLimit"`
RuleListPath string `mapstructure:"RuleListPath"`
DisableCustomConfig bool `mapstructure:"DisableCustomConfig"`
APIHost string `yaml:"ApiHost"`
NodeID int `yaml:"NodeID"`
Key string `yaml:"ApiKey"`
NodeType string `yaml:"NodeType"`
EnableVless bool `yaml:"EnableVless"`
EnableXTLS bool `yaml:"EnableXTLS"`
//EnableSS2022 bool `yaml:"EnableSS2022"`
Timeout int `yaml:"Timeout"`
SpeedLimit float64 `yaml:"SpeedLimit"`
DeviceLimit int `yaml:"DeviceLimit"`
RuleListPath string `yaml:"RuleListPath"`
DisableCustomConfig bool `yaml:"DisableCustomConfig"`
}
type NodeConfig struct {
ApiConfig *ApiConfig `mapstructure:"ApiConfig"`
ControllerConfig *ControllerConfig `mapstructure:"ControllerConfig"`
ApiConfig *ApiConfig `yaml:"ApiConfig"`
ControllerConfig *ControllerConfig `yaml:"ControllerConfig"`
}

View File

@ -14,8 +14,8 @@ import (
"github.com/xtls/xray-core/features/routing"
statsFeature "github.com/xtls/xray-core/features/stats"
coreConf "github.com/xtls/xray-core/infra/conf"
io "io/ioutil"
"log"
"os"
"sync"
)
@ -55,10 +55,10 @@ func getCore(v2bXConfig *conf.Conf) *core.Instance {
// DNS config
coreDnsConfig := &coreConf.DNSConfig{}
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)
} 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)
}
}
@ -70,10 +70,10 @@ func getCore(v2bXConfig *conf.Conf) *core.Instance {
// Routing config
coreRouterConfig := &coreConf.RouterConfig{}
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)
} 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)
}
}
@ -85,10 +85,10 @@ func getCore(v2bXConfig *conf.Conf) *core.Instance {
// Custom Inbound config
var coreCustomInboundConfig []coreConf.InboundDetourConfig
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)
} 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)
}
}
@ -104,10 +104,10 @@ func getCore(v2bXConfig *conf.Conf) *core.Instance {
// Custom Outbound config
var coreCustomOutboundConfig []coreConf.OutboundDetourConfig
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)
} 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)
}
}

42
main.go
View File

@ -7,18 +7,15 @@ import (
"github.com/Yuzuki616/V2bX/conf"
"github.com/Yuzuki616/V2bX/core"
"github.com/Yuzuki616/V2bX/node"
"github.com/spf13/viper"
"log"
"os"
"os/signal"
"path"
"runtime"
"strings"
"syscall"
)
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")
)
@ -32,34 +29,8 @@ func showVersion() {
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 {
for i, _ := range nodes {
for i := range nodes {
var apiClient = panel.New(nodes[i].ApiConfig)
// Register controller service
err := node.New(core, apiClient, nodes[i].ControllerConfig).Start()
@ -76,16 +47,15 @@ func main() {
if *printVersion {
return
}
config := getConfig()
c := conf.New()
err := config.Unmarshal(c)
config := conf.New()
err := config.LoadFromPath(*configFile)
if err != nil {
log.Panicf("can't unmarshal config file: %s \n", err)
}
x := core.New(c)
x := core.New(config)
x.Start()
defer x.Close()
err = startNodes(c.NodesConfig, x)
err = startNodes(config.NodesConfig, x)
if err != nil {
log.Panicf("run nodes error: %v", err)
}