update config parsing

This commit is contained in:
ginuerzh
2022-02-07 22:15:16 +08:00
parent 0983ecc52f
commit 1ec10ff7dd
23 changed files with 846 additions and 534 deletions

View File

@ -1,6 +1,7 @@
package config
import (
"encoding/json"
"io"
"time"
@ -20,148 +21,148 @@ func init() {
}
type LogConfig struct {
Output string `yaml:",omitempty"`
Level string `yaml:",omitempty"`
Format string `yaml:",omitempty"`
Output string `yaml:",omitempty" json:"output,omitempty"`
Level string `yaml:",omitempty" json:"level,omitempty"`
Format string `yaml:",omitempty" json:"format,omitempty"`
}
type ProfilingConfig struct {
Addr string
Enabled bool
Addr string `json:"addr"`
Enabled bool `json:"enabled"`
}
type TLSConfig struct {
CertFile string `yaml:"certFile,omitempty"`
KeyFile string `yaml:"keyFile,omitempty"`
CAFile string `yaml:"caFile,omitempty"`
Secure bool `yaml:",omitempty"`
ServerName string `yaml:"serverName,omitempty"`
CertFile string `yaml:"certFile,omitempty" json:"certFile,omitempty"`
KeyFile string `yaml:"keyFile,omitempty" json:"keyFile,omitempty"`
CAFile string `yaml:"caFile,omitempty" json:"caFile,omitempty"`
Secure bool `yaml:",omitempty" json:"secure,omitempty"`
ServerName string `yaml:"serverName,omitempty" json:"serverName,omitempty"`
}
type AuthConfig struct {
Username string
Password string
Username string `json:"username"`
Password string `yaml:",omitempty" json:"password,omitempty"`
}
type SelectorConfig struct {
Strategy string
MaxFails int `yaml:"maxFails"`
FailTimeout time.Duration `yaml:"failTimeout"`
Strategy string `json:"strategy"`
MaxFails int `yaml:"maxFails" json:"maxFails"`
FailTimeout time.Duration `yaml:"failTimeout" json:"failTimeout"`
}
type BypassConfig struct {
Name string
Reverse bool `yaml:",omitempty"`
Matchers []string
Name string `json:"name"`
Reverse bool `yaml:",omitempty" json:"reverse,omitempty"`
Matchers []string `json:"matchers"`
}
type NameserverConfig struct {
Addr string
Chain string `yaml:",omitempty"`
Prefer string `yaml:",omitempty"`
ClientIP string `yaml:"clientIP,omitempty"`
Hostname string `yaml:",omitempty"`
TTL time.Duration `yaml:",omitempty"`
Timeout time.Duration `yaml:",omitempty"`
Addr string `json:"addr"`
Chain string `yaml:",omitempty" json:"chain,omitempty"`
Prefer string `yaml:",omitempty" json:"prefer,omitempty"`
ClientIP string `yaml:"clientIP,omitempty" json:"clientIP,omitempty"`
Hostname string `yaml:",omitempty" json:"hostname,omitempty"`
TTL time.Duration `yaml:",omitempty" json:"ttl,omitempty"`
Timeout time.Duration `yaml:",omitempty" json:"timeout,omitempty"`
}
type ResolverConfig struct {
Name string
Nameservers []NameserverConfig
Name string `json:"name"`
Nameservers []NameserverConfig `json:"nameservers"`
}
type HostMappingConfig struct {
IP string
Hostname string
Aliases []string `yaml:",omitempty"`
IP string `json:"ip"`
Hostname string `json:"hostname"`
Aliases []string `yaml:",omitempty" json:"aliases,omitempty"`
}
type HostsConfig struct {
Name string
Mappings []HostMappingConfig
Name string `json:"name"`
Mappings []HostMappingConfig `json:"mappings"`
}
type ListenerConfig struct {
Type string
Chain string `yaml:",omitempty"`
Auths []*AuthConfig `yaml:",omitempty"`
TLS *TLSConfig `yaml:",omitempty"`
Metadata map[string]interface{} `yaml:",omitempty"`
Type string `json:"type"`
Chain string `yaml:",omitempty" json:"chain,omitempty"`
Auths []*AuthConfig `yaml:",omitempty" json:"auths,omitempty"`
TLS *TLSConfig `yaml:",omitempty" json:"tls,omitempty"`
Metadata map[string]interface{} `yaml:",omitempty" json:"metadata,omitempty"`
}
type HandlerConfig struct {
Type string
Retries int `yaml:",omitempty"`
Chain string `yaml:",omitempty"`
Auths []*AuthConfig `yaml:",omitempty"`
TLS *TLSConfig `yaml:",omitempty"`
Metadata map[string]interface{} `yaml:",omitempty"`
Type string `json:"type"`
Retries int `yaml:",omitempty" json:"retries,omitempty"`
Chain string `yaml:",omitempty" json:"chain,omitempty"`
Auths []*AuthConfig `yaml:",omitempty" json:"auths,omitempty"`
TLS *TLSConfig `yaml:",omitempty" json:"tls,omitempty"`
Metadata map[string]interface{} `yaml:",omitempty" json:"metadata,omitempty"`
}
type ForwarderConfig struct {
Targets []string
Selector *SelectorConfig `yaml:",omitempty"`
Targets []string `json:"targets"`
Selector *SelectorConfig `yaml:",omitempty" json:"selector,omitempty"`
}
type DialerConfig struct {
Type string
Auth *AuthConfig `yaml:",omitempty"`
TLS *TLSConfig `yaml:",omitempty"`
Metadata map[string]interface{} `yaml:",omitempty"`
Type string `json:"type"`
Auth *AuthConfig `yaml:",omitempty" json:"auth,omitempty"`
TLS *TLSConfig `yaml:",omitempty" json:"tls,omitempty"`
Metadata map[string]interface{} `yaml:",omitempty" json:"metadata,omitempty"`
}
type ConnectorConfig struct {
Type string
Auth *AuthConfig `yaml:",omitempty"`
TLS *TLSConfig `yaml:",omitempty"`
Metadata map[string]interface{} `yaml:",omitempty"`
Type string `json:"type"`
Auth *AuthConfig `yaml:",omitempty" json:"auth,omitempty"`
TLS *TLSConfig `yaml:",omitempty" json:"tls,omitempty"`
Metadata map[string]interface{} `yaml:",omitempty" json:"metadata,omitempty"`
}
type ServiceConfig struct {
Name string
Addr string `yaml:",omitempty"`
Bypass string `yaml:",omitempty"`
Resolver string `yaml:",omitempty"`
Hosts string `yaml:",omitempty"`
Handler *HandlerConfig `yaml:",omitempty"`
Listener *ListenerConfig `yaml:",omitempty"`
Forwarder *ForwarderConfig `yaml:",omitempty"`
Name string `json:"name"`
Addr string `yaml:",omitempty" json:"addr,omitempty"`
Bypass string `yaml:",omitempty" json:"bypass,omitempty"`
Resolver string `yaml:",omitempty" json:"resolver,omitempty"`
Hosts string `yaml:",omitempty" json:"hosts,omitempty"`
Handler *HandlerConfig `yaml:",omitempty" json:"handler,omitempty"`
Listener *ListenerConfig `yaml:",omitempty" json:"listener,omitempty"`
Forwarder *ForwarderConfig `yaml:",omitempty" json:"forwarder,omitempty"`
}
type ChainConfig struct {
Name string
Selector *SelectorConfig `yaml:",omitempty"`
Hops []*HopConfig
Name string `json:"name"`
Selector *SelectorConfig `yaml:",omitempty" json:"selector,omitempty"`
Hops []*HopConfig `json:"hops"`
}
type HopConfig struct {
Name string
Selector *SelectorConfig `yaml:",omitempty"`
Bypass string `yaml:",omitempty"`
Resolver string `yaml:",omitempty"`
Hosts string `yaml:",omitempty"`
Nodes []*NodeConfig
Name string `json:"name"`
Selector *SelectorConfig `yaml:",omitempty" json:"selector,omitempty"`
Bypass string `yaml:",omitempty" json:"bypass,omitempty"`
Resolver string `yaml:",omitempty" json:"resolver,omitempty"`
Hosts string `yaml:",omitempty" json:"hosts,omitempty"`
Nodes []*NodeConfig `json:"nodes"`
}
type NodeConfig struct {
Name string
Addr string `yaml:",omitempty"`
Bypass string `yaml:",omitempty"`
Resolver string `yaml:",omitempty"`
Hosts string `yaml:",omitempty"`
Connector *ConnectorConfig `yaml:",omitempty"`
Dialer *DialerConfig `yaml:",omitempty"`
Name string `json:"name"`
Addr string `yaml:",omitempty" json:"addr,omitempty"`
Bypass string `yaml:",omitempty" json:"bypass,omitempty"`
Resolver string `yaml:",omitempty" json:"resolver,omitempty"`
Hosts string `yaml:",omitempty" json:"hosts,omitempty"`
Connector *ConnectorConfig `yaml:",omitempty" json:"connector,omitempty"`
Dialer *DialerConfig `yaml:",omitempty" json:"dialer,omitempty"`
}
type Config struct {
Services []*ServiceConfig
Chains []*ChainConfig `yaml:",omitempty"`
Bypasses []*BypassConfig `yaml:",omitempty"`
Resolvers []*ResolverConfig `yaml:",omitempty"`
Hosts []*HostsConfig `yaml:",omitempty"`
TLS *TLSConfig `yaml:",omitempty"`
Log *LogConfig `yaml:",omitempty"`
Profiling *ProfilingConfig `yaml:",omitempty"`
Services []*ServiceConfig `json:"services"`
Chains []*ChainConfig `yaml:",omitempty" json:"chains,omitempty"`
Bypasses []*BypassConfig `yaml:",omitempty" json:"bypasses,omitempty"`
Resolvers []*ResolverConfig `yaml:",omitempty" json:"resolvers,omitempty"`
Hosts []*HostsConfig `yaml:",omitempty" json:"hosts,omitempty"`
TLS *TLSConfig `yaml:",omitempty" json:"tls,omitempty"`
Log *LogConfig `yaml:",omitempty" json:"log,omitempty"`
Profiling *ProfilingConfig `yaml:",omitempty" json:"profiling,omitempty"`
}
func (c *Config) Load() error {
@ -188,9 +189,19 @@ func (c *Config) ReadFile(file string) error {
return v.Unmarshal(c)
}
func (c *Config) Write(w io.Writer) error {
enc := yaml.NewEncoder(w)
defer enc.Close()
func (c *Config) Write(w io.Writer, format string) error {
switch format {
case "json":
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
enc.Encode(c)
return nil
case "yaml":
fallthrough
default:
enc := yaml.NewEncoder(w)
defer enc.Close()
return enc.Encode(c)
return enc.Encode(c)
}
}