mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 09:04:01 +03:00
Move config into separate package
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/9seconds/mtg/v2/mtglib"
|
||||
"github.com/pelletier/go-toml"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Debug bool `json:"debug"`
|
||||
Secret mtglib.Secret `json:"secret"`
|
||||
BindTo TypeHostPort `json:"bind-to"`
|
||||
TCPBuffer TypeBytes `json:"tcp-buffer"`
|
||||
PreferIP TypePreferIP `json:"prefer-ip"`
|
||||
CloakPort TypePort `json:"cloak-port"`
|
||||
Probes struct {
|
||||
Time struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
AllowSkewness TypeDuration `json:"allow-skewness"`
|
||||
} `json:"time"`
|
||||
AntiReplay struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
MaxSize TypeBytes `json:"max-size"`
|
||||
ErrorRate TypeFloat `json:"error-rate"`
|
||||
} `json:"anti-replay"`
|
||||
} `json:"probes"`
|
||||
Network struct {
|
||||
PublicIP struct {
|
||||
IPv4 TypeIP `json:"ipv4"`
|
||||
IPv6 TypeIP `json:"ipv6"`
|
||||
} `json:"public-ip"`
|
||||
Timeout struct {
|
||||
TCP TypeDuration `json:"tcp"`
|
||||
Idle TypeDuration `json:"idle"`
|
||||
} `json:"timeout"`
|
||||
DOHIP TypeIP `json:"doh-ip"`
|
||||
Proxies []TypeURL `json:"proxies"`
|
||||
} `json:"network"`
|
||||
Stats struct {
|
||||
StatsD struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
Address TypeHostPort `json:"address"`
|
||||
MetricPrefix TypeMetricPrefix `json:"metric-prefix"`
|
||||
} `json:"statsd"`
|
||||
Prometheus struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
BindTo TypeHostPort `json:"bind-to"`
|
||||
HTTPPath TypeHTTPPath `json:"http-path"`
|
||||
MetricPrefix TypeMetricPrefix `json:"metric-prefix"`
|
||||
} `json:"prometheus"`
|
||||
} `json:"stats"`
|
||||
}
|
||||
|
||||
func (c *Config) Validate() error {
|
||||
if len(c.Secret.Key) == 0 || c.Secret.Host == "" {
|
||||
return fmt.Errorf("incorrect secret %s", c.Secret.String())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Config) String() string {
|
||||
buf := &bytes.Buffer{}
|
||||
encoder := json.NewEncoder(buf)
|
||||
|
||||
encoder.SetEscapeHTML(false)
|
||||
|
||||
if err := encoder.Encode(c); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
type configRaw struct {
|
||||
Debug bool `toml:"debug" json:"debug"`
|
||||
Secret string `toml:"secret" json:"secret"`
|
||||
BindTo string `toml:"bind-to" json:"bind-to"`
|
||||
TCPBuffer string `toml:"tcp-buffer" json:"tcp-buffer"`
|
||||
PreferIP string `toml:"prefer-ip" json:"prefer-ip"`
|
||||
CloakPort uint `toml:"cloak-port" json:"cloak-port"`
|
||||
Probes struct {
|
||||
Time struct {
|
||||
Enabled bool `toml:"enabled" json:"enabled"`
|
||||
AllowSkewness string `toml:"allow-skewness" json:"allow-skewness"`
|
||||
} `toml:"time" json:"time"`
|
||||
AntiReplay struct {
|
||||
Enabled bool `toml:"enabled" json:"enabled"`
|
||||
MaxSize string `toml:"max-size" json:"max-size"`
|
||||
ErrorRate float64 `toml:"error-rate" json:"error-rate"`
|
||||
} `toml:"anti-replay" json:"anti-replay"`
|
||||
} `toml:"probes" json:"probes"`
|
||||
Network struct {
|
||||
PublicIP struct {
|
||||
IPv4 string `toml:"ipv4" json:"ipv4"`
|
||||
IPv6 string `toml:"ipv6" json:"ipv6"`
|
||||
} `toml:"public-ip" json:"public-ip"`
|
||||
Timeout struct {
|
||||
TCP string `toml:"tcp" json:"tcp"`
|
||||
Idle string `toml:"idle" json:"idle"`
|
||||
} `toml:"timeout" json:"timeout"`
|
||||
DOHIP string `toml:"doh-ip" json:"doh-ip"`
|
||||
Proxies []string `toml:"proxies" json:"proxies"`
|
||||
} `toml:"network" json:"network"`
|
||||
Stats struct {
|
||||
StatsD struct {
|
||||
Enabled bool `toml:"enabled" json:"enabled"`
|
||||
Address string `toml:"address" json:"address"`
|
||||
MetricPrefix string `toml:"metric-prefix" json:"metric-prefix"`
|
||||
} `toml:"statsd" json:"statsd"`
|
||||
Prometheus struct {
|
||||
Enabled bool `toml:"enabled" json:"enabled"`
|
||||
BindTo string `toml:"bind-to" json:"bind-to"`
|
||||
HTTPPath string `toml:"http-path" json:"http-path"`
|
||||
MetricPrefix string `toml:"metric-prefix" json:"metric-prefix"`
|
||||
} `toml:"prometheus" json:"prometheus"`
|
||||
} `toml:"stats" json:"stats"`
|
||||
}
|
||||
|
||||
func Parse(rawData []byte) (*Config, error) {
|
||||
rawConf := &configRaw{}
|
||||
|
||||
if err := toml.Unmarshal(rawData, rawConf); err != nil {
|
||||
return nil, fmt.Errorf("cannot parse toml config: %w", err)
|
||||
}
|
||||
|
||||
jsonBuf := &bytes.Buffer{}
|
||||
jsonEncoder := json.NewEncoder(jsonBuf)
|
||||
|
||||
jsonEncoder.SetEscapeHTML(false)
|
||||
jsonEncoder.SetIndent("", "")
|
||||
|
||||
if err := jsonEncoder.Encode(rawConf); err != nil {
|
||||
return nil, fmt.Errorf("cannot dump into interim format: %w", err)
|
||||
}
|
||||
|
||||
conf := &Config{}
|
||||
|
||||
if err := json.NewDecoder(jsonBuf).Decode(conf); err != nil {
|
||||
return nil, fmt.Errorf("cannot parse final config: %w", err)
|
||||
}
|
||||
|
||||
if err := conf.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("cannot validate config: %w", err)
|
||||
}
|
||||
|
||||
return conf, nil
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/alecthomas/units"
|
||||
)
|
||||
|
||||
type TypeBytes struct {
|
||||
value uint
|
||||
}
|
||||
|
||||
func (c *TypeBytes) UnmarshalText(data []byte) error {
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
value, err := units.ParseStrictBytes(strings.ToUpper(string(data)))
|
||||
if err != nil {
|
||||
return fmt.Errorf("incorrect bytes value: %w", err)
|
||||
}
|
||||
|
||||
if value < 0 {
|
||||
return fmt.Errorf("%d should be positive number", value)
|
||||
}
|
||||
|
||||
c.value = uint(value)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c TypeBytes) MarshalText() ([]byte, error) { // nolint: unparam
|
||||
return []byte(c.String()), nil
|
||||
}
|
||||
|
||||
func (c TypeBytes) String() string {
|
||||
return units.ToString(int64(c.value), 1024, "ib", "b")
|
||||
}
|
||||
|
||||
func (c TypeBytes) Value(defaultValue uint) uint {
|
||||
if c.value == 0 {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
return c.value
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type TypeDuration struct {
|
||||
value time.Duration
|
||||
}
|
||||
|
||||
func (c *TypeDuration) UnmarshalText(data []byte) error {
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
dur, err := time.ParseDuration(strings.ToLower(string(data)))
|
||||
if err != nil {
|
||||
return fmt.Errorf("incorrect duration: %w", err)
|
||||
}
|
||||
|
||||
if dur < 0 {
|
||||
return fmt.Errorf("%s should be positive duration", dur)
|
||||
}
|
||||
|
||||
c.value = dur
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c TypeDuration) MarshalText() ([]byte, error) { // nolint: unparam
|
||||
return []byte(c.value.String()), nil
|
||||
}
|
||||
|
||||
func (c TypeDuration) String() string {
|
||||
return c.value.String()
|
||||
}
|
||||
|
||||
func (c TypeDuration) Value(defaultValue time.Duration) time.Duration {
|
||||
if c.value == 0 {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
return c.value
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type TypeFloat struct {
|
||||
value float64
|
||||
}
|
||||
|
||||
func (c *TypeFloat) UnmarshalJSON(data []byte) error {
|
||||
value, err := strconv.ParseFloat(string(data), 64)
|
||||
if err != nil {
|
||||
return fmt.Errorf("incorrect float value: %w", err)
|
||||
}
|
||||
|
||||
if value < 0 {
|
||||
return fmt.Errorf("%f should be positive", value)
|
||||
}
|
||||
|
||||
c.value = value
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *TypeFloat) MarshalText() ([]byte, error) { // nolint: unparam
|
||||
return []byte(c.String()), nil
|
||||
}
|
||||
|
||||
func (c TypeFloat) String() string {
|
||||
return strconv.FormatFloat(c.value, 'f', -1, 64)
|
||||
}
|
||||
|
||||
func (c TypeFloat) Value(defaultValue float64) float64 {
|
||||
if c.value < 0.00001 {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
return c.value
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type TypeHostPort struct {
|
||||
host TypeIP
|
||||
port TypePort
|
||||
}
|
||||
|
||||
func (c *TypeHostPort) UnmarshalText(data []byte) error {
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
host, port, err := net.SplitHostPort(string(data))
|
||||
if err != nil {
|
||||
return fmt.Errorf("incorrect host:port syntax: %w", err)
|
||||
}
|
||||
|
||||
if err := c.port.UnmarshalJSON([]byte(port)); err != nil {
|
||||
return fmt.Errorf("incorrect port in host:port: %w", err)
|
||||
}
|
||||
|
||||
if err := c.host.UnmarshalText([]byte(host)); err != nil {
|
||||
return fmt.Errorf("incorrect host: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c TypeHostPort) MarshalText() ([]byte, error) { // nolint: unparam
|
||||
return []byte(c.String()), nil
|
||||
}
|
||||
|
||||
func (c TypeHostPort) String() string {
|
||||
return c.Value(net.IP{}, 0)
|
||||
}
|
||||
|
||||
func (c TypeHostPort) HostValue(defaultValue net.IP) net.IP {
|
||||
return c.host.Value(defaultValue)
|
||||
}
|
||||
|
||||
func (c TypeHostPort) PortValue(defaultValue uint) uint {
|
||||
return c.port.Value(defaultValue)
|
||||
}
|
||||
|
||||
func (c TypeHostPort) Value(defaultHostValue net.IP, defaultPortValue uint) string {
|
||||
host := c.HostValue(defaultHostValue)
|
||||
port := c.PortValue(defaultPortValue)
|
||||
|
||||
return net.JoinHostPort(host.String(), strconv.Itoa(int(port)))
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package config
|
||||
|
||||
import "strings"
|
||||
|
||||
type TypeHTTPPath struct {
|
||||
value string
|
||||
}
|
||||
|
||||
func (c *TypeHTTPPath) UnmarshalText(data []byte) error { // nolint: unparam
|
||||
if len(data) > 0 {
|
||||
c.value = "/" + strings.Trim(string(data), "/")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c TypeHTTPPath) MarshalText() ([]byte, error) { // nolint: unparam
|
||||
return []byte(c.String()), nil
|
||||
}
|
||||
|
||||
func (c TypeHTTPPath) String() string {
|
||||
return c.value
|
||||
}
|
||||
|
||||
func (c TypeHTTPPath) Value(defaultValue string) string {
|
||||
if c.value == "" {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
return c.value
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
)
|
||||
|
||||
type TypeIP struct {
|
||||
value net.IP
|
||||
}
|
||||
|
||||
func (c *TypeIP) UnmarshalText(data []byte) error {
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
ip := net.ParseIP(string(data))
|
||||
if ip == nil {
|
||||
return fmt.Errorf("incorrect ip address: %s", string(data))
|
||||
}
|
||||
|
||||
c.value = ip
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *TypeIP) MarshalText() ([]byte, error) { // nolint: unparam
|
||||
return []byte(c.String()), nil
|
||||
}
|
||||
|
||||
func (c TypeIP) String() string {
|
||||
if c.value == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return c.value.String()
|
||||
}
|
||||
|
||||
func (c TypeIP) Value(defaultValue net.IP) net.IP {
|
||||
if c.value == nil {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
return c.value
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
type TypeMetricPrefix struct {
|
||||
value string
|
||||
}
|
||||
|
||||
func (c *TypeMetricPrefix) UnmarshalText(data []byte) error {
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
prefix := string(data)
|
||||
|
||||
if ok, err := regexp.MatchString("^[a-z0-9]+$", prefix); !ok || err != nil {
|
||||
return fmt.Errorf("incorrect metric prefix: %s", prefix)
|
||||
}
|
||||
|
||||
c.value = prefix
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c TypeMetricPrefix) MarshalText() ([]byte, error) { // nolint: unparam
|
||||
return []byte(c.String()), nil
|
||||
}
|
||||
|
||||
func (c TypeMetricPrefix) String() string {
|
||||
return c.value
|
||||
}
|
||||
|
||||
func (c TypeMetricPrefix) Value(defaultValue string) string {
|
||||
if c.value == "" {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
return c.value
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type TypePort struct {
|
||||
value uint
|
||||
}
|
||||
|
||||
func (c *TypePort) UnmarshalJSON(data []byte) error {
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
intValue, err := strconv.ParseUint(string(data), 10, 16)
|
||||
if err != nil {
|
||||
return fmt.Errorf("port number is not a number: %w", err)
|
||||
}
|
||||
|
||||
if intValue == 0 || intValue > 65536 {
|
||||
return fmt.Errorf("port number should be 0 < portNo < 65536: %d", intValue)
|
||||
}
|
||||
|
||||
c.value = uint(intValue)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *TypePort) MarshalJSON() ([]byte, error) { // nolint: unparam
|
||||
return []byte(c.String()), nil
|
||||
}
|
||||
|
||||
func (c TypePort) String() string {
|
||||
return strconv.Itoa(int(c.value))
|
||||
}
|
||||
|
||||
func (c TypePort) Value(defaultValue uint) uint {
|
||||
if c.value == 0 {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
return c.value
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type TypePreferIP struct {
|
||||
value string
|
||||
}
|
||||
|
||||
func (c *TypePreferIP) UnmarshalText(data []byte) error {
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
text := strings.ToLower(string(data))
|
||||
|
||||
switch text {
|
||||
case "prefer-ipv4", "prefer-ipv6", "only-ipv4", "only-ipv6":
|
||||
c.value = text
|
||||
default:
|
||||
return fmt.Errorf("incorrect prefer-ip value: %s", string(data))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c TypePreferIP) MarshalText() ([]byte, error) { // nolint: unparam
|
||||
return []byte(c.value), nil
|
||||
}
|
||||
|
||||
func (c *TypePreferIP) String() string {
|
||||
return c.value
|
||||
}
|
||||
|
||||
func (c *TypePreferIP) Value(defaultValue string) string {
|
||||
if c.value == "" {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
return c.value
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type TypeURL struct {
|
||||
value *url.URL
|
||||
}
|
||||
|
||||
func (c *TypeURL) UnmarshalText(data []byte) error {
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
value, err := url.Parse(string(data))
|
||||
if err != nil {
|
||||
return fmt.Errorf("incorrect URL: %w", err)
|
||||
}
|
||||
|
||||
c.value = value
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *TypeURL) MarshalText() ([]byte, error) { // nolint: unparam
|
||||
return []byte(c.String()), nil
|
||||
}
|
||||
|
||||
func (c TypeURL) String() string {
|
||||
if c.value == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return c.value.String()
|
||||
}
|
||||
|
||||
func (c TypeURL) Value(defaultValue *url.URL) *url.URL {
|
||||
if c.value == nil {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
return c.value
|
||||
}
|
||||
Reference in New Issue
Block a user