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"`
}