refactor conf

This commit is contained in:
Yuzuki616
2023-08-20 15:13:52 +08:00
parent 42407d5c62
commit 214324496d
19 changed files with 346 additions and 287 deletions

View File

@@ -1,12 +1,12 @@
package conf
type CertConfig struct {
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"`
CertMode string `json:"CertMode"` // none, file, http, dns
RejectUnknownSni bool `json:"RejectUnknownSni"`
CertDomain string `json:"CertDomain"`
CertFile string `json:"CertFile"`
KeyFile string `json:"KeyFile"`
Provider string `json:"Provider"` // alidns, cloudflare, gandi, godaddy....
Email string `json:"Email"`
DNSEnv map[string]string `json:"DNSEnv"`
}

View File

@@ -2,25 +2,23 @@ package conf
import (
"fmt"
"io"
"os"
"gopkg.in/yaml.v3"
"github.com/goccy/go-json"
)
type Conf struct {
CoreConfig CoreConfig `yaml:"CoreConfig"`
NodesConfig []*NodeConfig `yaml:"Nodes"`
LogConfig LogConfig `json:"Log"`
CoresConfig []CoreConfig `json:"Cores"`
NodeConfig []NodeConfig `json:"Nodes"`
}
func New() *Conf {
return &Conf{
CoreConfig: CoreConfig{
Type: "xray",
XrayConfig: NewXrayConfig(),
SingConfig: NewSingConfig(),
LogConfig: LogConfig{
Level: "info",
Output: "",
},
NodesConfig: []*NodeConfig{},
}
}
@@ -30,13 +28,5 @@ func (p *Conf) LoadFromPath(filePath string) error {
return fmt.Errorf("open config file error: %s", err)
}
defer f.Close()
content, err := io.ReadAll(f)
if err != nil {
return fmt.Errorf("read file error: %s", err)
}
err = yaml.Unmarshal(content, p)
if err != nil {
return fmt.Errorf("decode config error: %s", err)
}
return nil
return json.NewDecoder(f).Decode(p)
}

View File

@@ -1,17 +1,16 @@
package conf
import (
"log"
"strings"
"testing"
)
func TestConf_LoadFromPath(t *testing.T) {
c := New()
t.Log(c.LoadFromPath("../example/config.yml.example"))
t.Log(c.LoadFromPath("./config.json"), c.NodeConfig)
}
func TestConf_Watch(t *testing.T) {
//c := New()
log.Println(strings.Split("aaaa", " "))
c := New()
t.Log(c.Watch("./1.json", "", func() {}))
select {}
}

View File

@@ -1,7 +1,29 @@
package conf
import (
"encoding/json"
)
type CoreConfig struct {
Type string `yaml:"Type"`
XrayConfig *XrayConfig `yaml:"XrayConfig"`
SingConfig *SingConfig `yaml:"SingConfig"`
Type string `json:"Type"`
XrayConfig *XrayConfig `json:"-"`
SingConfig *SingConfig `json:"-"`
}
type _CoreConfig CoreConfig
func (c *CoreConfig) UnmarshalJSON(b []byte) error {
err := json.Unmarshal(b, (*_CoreConfig)(c))
if err != nil {
return err
}
switch c.Type {
case "xray":
c.XrayConfig = NewXrayConfig()
return json.Unmarshal(b, c.XrayConfig)
case "sing":
c.SingConfig = NewSingConfig()
return json.Unmarshal(b, c.SingConfig)
}
return nil
}

View File

@@ -1,40 +1,40 @@
package conf
type LimitConfig struct {
EnableRealtime bool `yaml:"EnableRealtime"`
SpeedLimit int `yaml:"SpeedLimit"`
IPLimit int `yaml:"DeviceLimit"`
ConnLimit int `yaml:"ConnLimit"`
EnableIpRecorder bool `yaml:"EnableIpRecorder"`
IpRecorderConfig *IpReportConfig `yaml:"IpRecorderConfig"`
EnableDynamicSpeedLimit bool `yaml:"EnableDynamicSpeedLimit"`
DynamicSpeedLimitConfig *DynamicSpeedLimitConfig `yaml:"DynamicSpeedLimitConfig"`
EnableRealtime bool `json:"EnableRealtime"`
SpeedLimit int `json:"SpeedLimit"`
IPLimit int `json:"DeviceLimit"`
ConnLimit int `json:"ConnLimit"`
EnableIpRecorder bool `json:"EnableIpRecorder"`
IpRecorderConfig *IpReportConfig `json:"IpRecorderConfig"`
EnableDynamicSpeedLimit bool `json:"EnableDynamicSpeedLimit"`
DynamicSpeedLimitConfig *DynamicSpeedLimitConfig `json:"DynamicSpeedLimitConfig"`
}
type RecorderConfig struct {
Url string `yaml:"Url"`
Token string `yaml:"Token"`
Timeout int `yaml:"Timeout"`
Url string `json:"Url"`
Token string `json:"Token"`
Timeout int `json:"Timeout"`
}
type RedisConfig struct {
Address string `yaml:"Address"`
Password string `yaml:"Password"`
Db int `yaml:"Db"`
Address string `json:"Address"`
Password string `json:"Password"`
Db int `json:"Db"`
Expiry int `json:"Expiry"`
}
type IpReportConfig struct {
Periodic int `yaml:"Periodic"`
Type string `yaml:"Type"`
RecorderConfig *RecorderConfig `yaml:"RecorderConfig"`
RedisConfig *RedisConfig `yaml:"RedisConfig"`
EnableIpSync bool `yaml:"EnableIpSync"`
Periodic int `json:"Periodic"`
Type string `json:"Type"`
RecorderConfig *RecorderConfig `json:"RecorderConfig"`
RedisConfig *RedisConfig `json:"RedisConfig"`
EnableIpSync bool `json:"EnableIpSync"`
}
type DynamicSpeedLimitConfig struct {
Periodic int `yaml:"Periodic"`
Traffic int64 `yaml:"Traffic"`
SpeedLimit int `yaml:"SpeedLimit"`
ExpireTime int `yaml:"ExpireTime"`
Periodic int `json:"Periodic"`
Traffic int64 `json:"Traffic"`
SpeedLimit int `json:"SpeedLimit"`
ExpireTime int `json:"ExpireTime"`
}

6
conf/log.go Normal file
View File

@@ -0,0 +1,6 @@
package conf
type LogConfig struct {
Level string `json:"Level"`
Output string `json:"Output"`
}

View File

@@ -1,15 +1,82 @@
package conf
import (
"github.com/goccy/go-json"
)
type NodeConfig struct {
ApiConfig *ApiConfig `yaml:"ApiConfig"`
Options *Options `yaml:"Options"`
ApiConfig ApiConfig `json:"-"`
Options Options `json:"-"`
}
type rawNodeConfig struct {
ApiRaw *json.RawMessage `json:"ApiConfig"`
OptRaw *json.RawMessage `json:"Options"`
}
type ApiConfig struct {
APIHost string `yaml:"ApiHost"`
NodeID int `yaml:"NodeID"`
Key string `yaml:"ApiKey"`
NodeType string `yaml:"NodeType"`
Timeout int `yaml:"Timeout"`
RuleListPath string `yaml:"RuleListPath"`
APIHost string `json:"ApiHost"`
NodeID int `json:"NodeID"`
Key string `json:"ApiKey"`
NodeType string `json:"NodeType"`
Timeout int `json:"Timeout"`
RuleListPath string `json:"RuleListPath"`
}
type Options struct {
Core string `json:"Core"`
ListenIP string `json:"ListenIP"`
SendIP string `json:"SendIP"`
LimitConfig LimitConfig `json:"LimitConfig"`
XrayOptions *XrayOptions `json:"XrayOptions"`
SingOptions *SingOptions `json:"SingOptions"`
CertConfig *CertConfig `json:"CertConfig"`
}
func (n *NodeConfig) UnmarshalJSON(b []byte) (err error) {
r := rawNodeConfig{}
err = json.Unmarshal(b, &r)
if err != nil {
return err
}
if r.ApiRaw != nil {
err = json.Unmarshal(*r.ApiRaw, &n.ApiConfig)
if err != nil {
return
}
} else {
n.ApiConfig = ApiConfig{
Timeout: 30,
}
err = json.Unmarshal(b, &n.ApiConfig)
if err != nil {
return
}
}
if r.OptRaw != nil {
err = json.Unmarshal(*r.OptRaw, &n.Options)
if err != nil {
return
}
} else {
n.Options = Options{
Core: "xray",
ListenIP: "0.0.0.0",
SendIP: "0.0.0.0",
}
err = json.Unmarshal(b, &n.Options)
if err != nil {
return
}
switch n.Options.Core {
case "xray":
n.Options.XrayOptions = NewXrayOptions()
return json.Unmarshal(b, n.Options.XrayOptions)
case "sing":
n.Options.SingOptions = NewSingOptions()
return json.Unmarshal(b, n.Options.SingOptions)
}
}
return
}

View File

@@ -1,48 +0,0 @@
package conf
type Options struct {
ListenIP string `yaml:"ListenIP"`
SendIP string `yaml:"SendIP"`
LimitConfig LimitConfig `yaml:"LimitConfig"`
CertConfig *CertConfig `yaml:"CertConfig"`
XrayOptions XrayOptions `yaml:"XrayOptions"`
SingOptions SingOptions `yaml:"SingOptions"`
}
type XrayOptions struct {
EnableProxyProtocol bool `yaml:"EnableProxyProtocol"`
EnableDNS bool `yaml:"EnableDNS"`
DNSType string `yaml:"DNSType"`
EnableUot bool `yaml:"EnableUot"`
EnableTFO bool `yaml:"EnableTFO"`
DisableIVCheck bool `yaml:"DisableIVCheck"`
DisableSniffing bool `yaml:"DisableSniffing"`
EnableFallback bool `yaml:"EnableFallback"`
FallBackConfigs []FallBackConfigForXray `yaml:"FallBackConfigs"`
}
type SingOptions struct {
EnableProxyProtocol bool `yaml:"EnableProxyProtocol"`
TCPFastOpen bool `yaml:"EnableTFO"`
SniffEnabled bool `yaml:"EnableSniff"`
SniffOverrideDestination bool `yaml:"SniffOverrideDestination"`
FallBackConfigs *FallBackConfigForSing `yaml:"FallBackConfigs"`
}
type FallBackConfigForXray struct {
SNI string `yaml:"SNI"`
Alpn string `yaml:"Alpn"`
Path string `yaml:"Path"`
Dest string `yaml:"Dest"`
ProxyProtocolVer uint64 `yaml:"ProxyProtocolVer"`
}
type FallBackConfigForSing struct {
// sing-box
FallBack FallBack `yaml:"FallBack"`
FallBackForALPN map[string]FallBack `yaml:"FallBackForALPN"`
}
type FallBack struct {
Server string `yaml:"Server"`
ServerPort string `yaml:"ServerPort"`
}

View File

@@ -1,15 +1,15 @@
package conf
type SingConfig struct {
LogConfig SingLogConfig `yaml:"LogConfig"`
OriginalPath string `yaml:"OriginalPath"`
LogConfig SingLogConfig `json:"Log"`
OriginalPath string `json:"OriginalPath"`
}
type SingLogConfig struct {
Disabled bool `yaml:"Disable"`
Level string `yaml:"Level"`
Output string `yaml:"Output"`
Timestamp bool `yaml:"Timestamp"`
Disabled bool `json:"Disable"`
Level string `json:"Level"`
Output string `json:"Output"`
Timestamp bool `json:"Timestamp"`
}
func NewSingConfig() *SingConfig {
@@ -20,3 +20,32 @@ func NewSingConfig() *SingConfig {
},
}
}
type SingOptions struct {
EnableProxyProtocol bool `json:"EnableProxyProtocol"`
TCPFastOpen bool `json:"EnableTFO"`
SniffEnabled bool `json:"EnableSniff"`
SniffOverrideDestination bool `json:"SniffOverrideDestination"`
FallBackConfigs *FallBackConfigForSing `json:"FallBackConfigs"`
}
type FallBackConfigForSing struct {
// sing-box
FallBack FallBack `json:"FallBack"`
FallBackForALPN map[string]FallBack `json:"FallBackForALPN"`
}
type FallBack struct {
Server string `json:"Server"`
ServerPort string `json:"ServerPort"`
}
func NewSingOptions() *SingOptions {
return &SingOptions{
EnableProxyProtocol: false,
TCPFastOpen: false,
SniffEnabled: true,
SniffOverrideDestination: true,
FallBackConfigs: &FallBackConfigForSing{},
}
}

View File

@@ -1,27 +1,27 @@
package conf
type XrayConfig struct {
LogConfig *XrayLogConfig `yaml:"Log"`
AssetPath string `yaml:"AssetPath"`
DnsConfigPath string `yaml:"DnsConfigPath"`
RouteConfigPath string `yaml:"RouteConfigPath"`
ConnectionConfig *XrayConnectionConfig `yaml:"XrayConnectionConfig"`
InboundConfigPath string `yaml:"InboundConfigPath"`
OutboundConfigPath string `yaml:"OutboundConfigPath"`
LogConfig *XrayLogConfig `json:"Log"`
AssetPath string `json:"AssetPath"`
DnsConfigPath string `json:"DnsConfigPath"`
RouteConfigPath string `json:"RouteConfigPath"`
ConnectionConfig *XrayConnectionConfig `json:"XrayConnectionConfig"`
InboundConfigPath string `json:"InboundConfigPath"`
OutboundConfigPath string `json:"OutboundConfigPath"`
}
type XrayLogConfig struct {
Level string `yaml:"Level"`
AccessPath string `yaml:"AccessPath"`
ErrorPath string `yaml:"ErrorPath"`
Level string `json:"Level"`
AccessPath string `json:"AccessPath"`
ErrorPath string `json:"ErrorPath"`
}
type XrayConnectionConfig struct {
Handshake uint32 `yaml:"handshake"`
ConnIdle uint32 `yaml:"connIdle"`
UplinkOnly uint32 `yaml:"uplinkOnly"`
DownlinkOnly uint32 `yaml:"downlinkOnly"`
BufferSize int32 `yaml:"bufferSize"`
Handshake uint32 `json:"handshake"`
ConnIdle uint32 `json:"connIdle"`
UplinkOnly uint32 `json:"uplinkOnly"`
DownlinkOnly uint32 `json:"downlinkOnly"`
BufferSize int32 `json:"bufferSize"`
}
func NewXrayConfig() *XrayConfig {
@@ -45,3 +45,36 @@ func NewXrayConfig() *XrayConfig {
},
}
}
type XrayOptions struct {
EnableProxyProtocol bool `json:"EnableProxyProtocol"`
EnableDNS bool `json:"EnableDNS"`
DNSType string `json:"DNSType"`
EnableUot bool `json:"EnableUot"`
EnableTFO bool `json:"EnableTFO"`
DisableIVCheck bool `json:"DisableIVCheck"`
DisableSniffing bool `json:"DisableSniffing"`
EnableFallback bool `json:"EnableFallback"`
FallBackConfigs []FallBackConfigForXray `json:"FallBackConfigs"`
}
type FallBackConfigForXray struct {
SNI string `json:"SNI"`
Alpn string `json:"Alpn"`
Path string `json:"Path"`
Dest string `json:"Dest"`
ProxyProtocolVer uint64 `json:"ProxyProtocolVer"`
}
func NewXrayOptions() *XrayOptions {
return &XrayOptions{
EnableProxyProtocol: false,
EnableDNS: false,
DNSType: "AsIs",
EnableUot: false,
EnableTFO: false,
DisableIVCheck: false,
DisableSniffing: false,
EnableFallback: false,
}
}