|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+package main
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "bytes"
|
|
|
5
|
+ "encoding/json"
|
|
|
6
|
+ "fmt"
|
|
|
7
|
+ "io"
|
|
|
8
|
+
|
|
|
9
|
+ "github.com/9seconds/mtg/v2/mtglib"
|
|
|
10
|
+ "github.com/pelletier/go-toml"
|
|
|
11
|
+)
|
|
|
12
|
+
|
|
|
13
|
+type config struct {
|
|
|
14
|
+ Debug bool `json:"debug"`
|
|
|
15
|
+ Secret mtglib.Secret `json:"secret"`
|
|
|
16
|
+}
|
|
|
17
|
+
|
|
|
18
|
+type configRaw struct {
|
|
|
19
|
+ Debug bool `toml:"debug" json:"debug"`
|
|
|
20
|
+ Secret string `toml:"secret" json:"secret"`
|
|
|
21
|
+ BindTo string `toml:"bind-to" json:"bind-to"`
|
|
|
22
|
+ TCPBuffer string `toml:"tcp-buffer" json:"tcp-buffer"`
|
|
|
23
|
+ PreferIP string `toml:"prefer-ip" json:"prefer-ip"`
|
|
|
24
|
+ CloakPort uint `toml:"cloak-port" json:"cloak-port"`
|
|
|
25
|
+ Probes struct {
|
|
|
26
|
+ Time struct {
|
|
|
27
|
+ Enabled bool `toml:"enabled" json:"enabled"`
|
|
|
28
|
+ AllowSkewness string `toml:"allow-skewness" json:"allow-skewness"`
|
|
|
29
|
+ } `toml:"time" json:"time"`
|
|
|
30
|
+ AntiReplay struct {
|
|
|
31
|
+ Enabled bool `toml:"enabled" json:"enabled"`
|
|
|
32
|
+ MaxSize string `toml:"max-size" json:"max-size"`
|
|
|
33
|
+ TTL string `toml:"ttl" json:"ttl"`
|
|
|
34
|
+ } `toml:"anti-replay" json:"anti-replay"`
|
|
|
35
|
+ } `toml:"probes" json:"probes"`
|
|
|
36
|
+ Network struct {
|
|
|
37
|
+ PublicIP struct {
|
|
|
38
|
+ IPv4 string `toml:"ipv4" json:"ipv4"`
|
|
|
39
|
+ IPv6 string `toml:"ipv6" json:"ipv6"`
|
|
|
40
|
+ } `toml:"public-ip" json:"public-ip"`
|
|
|
41
|
+ DOHHostname string `toml:"doh-hostname" json:"doh-hostname"`
|
|
|
42
|
+ Proxies []string `toml:"proxies" json:"proxies"`
|
|
|
43
|
+ } `toml:"network" json:"network"`
|
|
|
44
|
+ Stats struct {
|
|
|
45
|
+ StatsD struct {
|
|
|
46
|
+ Enabled bool `toml:"enabled" json:"enabled"`
|
|
|
47
|
+ Address string `toml:"address" json:"address"`
|
|
|
48
|
+ MetricPrefix string `toml:"metric-prefix" json:"metric-prefix"`
|
|
|
49
|
+ } `toml:"statsd" json:"statsd"`
|
|
|
50
|
+ Prometheus struct {
|
|
|
51
|
+ Enabled bool `toml:"enabled" json:"enabled"`
|
|
|
52
|
+ BindTo string `toml:"bind-to" json:"bind-to"`
|
|
|
53
|
+ HTTPPath string `toml:"http-path" json:"http-path"`
|
|
|
54
|
+ MetricPrefix string `toml:"metric-prefix" json:"metric-prefix"`
|
|
|
55
|
+ } `toml:"prometheus" json:"prometheus"`
|
|
|
56
|
+ } `toml:"stats" json:"stats"`
|
|
|
57
|
+}
|
|
|
58
|
+
|
|
|
59
|
+func parseConfig(reader io.Reader) (*config, error) {
|
|
|
60
|
+ rawConf := &configRaw{}
|
|
|
61
|
+
|
|
|
62
|
+ if err := toml.NewDecoder(reader).Decode(rawConf); err != nil {
|
|
|
63
|
+ return nil, fmt.Errorf("cannot parse toml config: %w", err)
|
|
|
64
|
+ }
|
|
|
65
|
+
|
|
|
66
|
+ jsonBuf := &bytes.Buffer{}
|
|
|
67
|
+ jsonEncoder := json.NewEncoder(jsonBuf)
|
|
|
68
|
+
|
|
|
69
|
+ jsonEncoder.SetEscapeHTML(false)
|
|
|
70
|
+ jsonEncoder.SetIndent("", "")
|
|
|
71
|
+
|
|
|
72
|
+ if err := jsonEncoder.Encode(rawConf); err != nil {
|
|
|
73
|
+ return nil, fmt.Errorf("cannot dump into interim format: %w", err)
|
|
|
74
|
+ }
|
|
|
75
|
+
|
|
|
76
|
+ conf := &config{}
|
|
|
77
|
+
|
|
|
78
|
+ if err := json.NewDecoder(jsonBuf).Decode(conf); err != nil {
|
|
|
79
|
+ return nil, fmt.Errorf("cannot parse final config: %w", err)
|
|
|
80
|
+ }
|
|
|
81
|
+
|
|
|
82
|
+ return conf, nil
|
|
|
83
|
+}
|