Highly-opinionated (ex-bullshit-free) MTPROTO proxy for Telegram. If you use v1.0 or upgrade broke you proxy, please read the chapter Version 2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.go 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package config
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/9seconds/mtg/v2/mtglib"
  7. "github.com/pelletier/go-toml"
  8. )
  9. type Config struct {
  10. Debug bool `json:"debug"`
  11. Secret mtglib.Secret `json:"secret"`
  12. BindTo TypeHostPort `json:"bind-to"`
  13. TCPBuffer TypeBytes `json:"tcp-buffer"`
  14. PreferIP TypePreferIP `json:"prefer-ip"`
  15. CloakPort TypePort `json:"cloak-port"`
  16. Defense struct {
  17. Time struct {
  18. Enabled bool `json:"enabled"`
  19. AllowSkewness TypeDuration `json:"allow-skewness"`
  20. } `json:"time"`
  21. AntiReplay struct {
  22. Enabled bool `json:"enabled"`
  23. MaxSize TypeBytes `json:"max-size"`
  24. ErrorRate TypeErrorRate `json:"error-rate"`
  25. } `json:"anti-replay"`
  26. } `json:"defense"`
  27. Network struct {
  28. PublicIP struct {
  29. IPv4 TypeIP `json:"ipv4"`
  30. IPv6 TypeIP `json:"ipv6"`
  31. } `json:"public-ip"`
  32. Timeout struct {
  33. TCP TypeDuration `json:"tcp"`
  34. HTTP TypeDuration `json:"http"`
  35. Idle TypeDuration `json:"idle"`
  36. } `json:"timeout"`
  37. DOHIP TypeIP `json:"doh-ip"`
  38. Proxies []TypeURL `json:"proxies"`
  39. } `json:"network"`
  40. Stats struct {
  41. StatsD struct {
  42. Enabled bool `json:"enabled"`
  43. Address TypeHostPort `json:"address"`
  44. MetricPrefix TypeMetricPrefix `json:"metric-prefix"`
  45. } `json:"statsd"`
  46. Prometheus struct {
  47. Enabled bool `json:"enabled"`
  48. BindTo TypeHostPort `json:"bind-to"`
  49. HTTPPath TypeHTTPPath `json:"http-path"`
  50. MetricPrefix TypeMetricPrefix `json:"metric-prefix"`
  51. } `json:"prometheus"`
  52. } `json:"stats"`
  53. }
  54. func (c *Config) Validate() error {
  55. if !c.Secret.Valid() {
  56. return fmt.Errorf("invalid secret %s", c.Secret.String())
  57. }
  58. if len(c.BindTo.HostValue(nil)) == 0 || c.BindTo.PortValue(0) == 0 {
  59. return fmt.Errorf("incorrect bind-to parameter %s", c.BindTo.String())
  60. }
  61. return nil
  62. }
  63. func (c *Config) String() string {
  64. buf := &bytes.Buffer{}
  65. encoder := json.NewEncoder(buf)
  66. encoder.SetEscapeHTML(false)
  67. if err := encoder.Encode(c); err != nil {
  68. panic(err)
  69. }
  70. return buf.String()
  71. }
  72. type configRaw struct {
  73. Debug bool `toml:"debug" json:"debug,omitempty"`
  74. Secret string `toml:"secret" json:"secret"`
  75. BindTo string `toml:"bind-to" json:"bind-to"`
  76. TCPBuffer string `toml:"tcp-buffer" json:"tcp-buffer,omitempty"`
  77. PreferIP string `toml:"prefer-ip" json:"prefer-ip,omitempty"`
  78. CloakPort uint `toml:"cloak-port" json:"cloak-port,omitempty"`
  79. Defense struct {
  80. Time struct {
  81. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  82. AllowSkewness string `toml:"allow-skewness" json:"allow-skewness,omitempty"`
  83. } `toml:"time" json:"time,omitempty"`
  84. AntiReplay struct {
  85. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  86. MaxSize string `toml:"max-size" json:"max-size,omitempty"`
  87. ErrorRate float64 `toml:"error-rate" json:"error-rate,omitempty"`
  88. } `toml:"anti-replay" json:"anti-replay,omitempty"`
  89. } `toml:"defense" json:"defense,omitempty"`
  90. Network struct {
  91. PublicIP struct {
  92. IPv4 string `toml:"ipv4" json:"ipv4,omitempty"`
  93. IPv6 string `toml:"ipv6" json:"ipv6,omitempty"`
  94. } `toml:"public-ip" json:"public-ip,omitempty"`
  95. Timeout struct {
  96. TCP string `toml:"tcp" json:"tcp,omitempty"`
  97. HTTP string `toml:"http" json:"http,omitempty"`
  98. Idle string `toml:"idle" json:"idle,omitempty"`
  99. } `toml:"timeout" json:"timeout,omitempty"`
  100. DOHIP string `toml:"doh-ip" json:"doh-ip,omitempty"`
  101. Proxies []string `toml:"proxies" json:"proxies,omitempty"`
  102. } `toml:"network" json:"network,omitempty"`
  103. Stats struct {
  104. StatsD struct {
  105. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  106. Address string `toml:"address" json:"address,omitempty"`
  107. MetricPrefix string `toml:"metric-prefix" json:"metric-prefix,omitempty"`
  108. } `toml:"statsd" json:"statsd,omitempty"`
  109. Prometheus struct {
  110. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  111. BindTo string `toml:"bind-to" json:"bind-to,omitempty"`
  112. HTTPPath string `toml:"http-path" json:"http-path,omitempty"`
  113. MetricPrefix string `toml:"metric-prefix" json:"metric-prefix,omitempty"`
  114. } `toml:"prometheus" json:"prometheus,omitempty"`
  115. } `toml:"stats" json:"stats,omitempty"`
  116. }
  117. func Parse(rawData []byte) (*Config, error) {
  118. rawConf := &configRaw{}
  119. jsonBuf := &bytes.Buffer{}
  120. conf := &Config{}
  121. jsonEncoder := json.NewEncoder(jsonBuf)
  122. jsonEncoder.SetEscapeHTML(false)
  123. jsonEncoder.SetIndent("", "")
  124. if err := toml.Unmarshal(rawData, rawConf); err != nil {
  125. return nil, fmt.Errorf("cannot parse toml config: %w", err)
  126. }
  127. if err := jsonEncoder.Encode(rawConf); err != nil {
  128. return nil, fmt.Errorf("cannot dump into interim format: %w", err)
  129. }
  130. if err := json.NewDecoder(jsonBuf).Decode(conf); err != nil {
  131. return nil, fmt.Errorf("cannot parse final config: %w", err)
  132. }
  133. if err := conf.Validate(); err != nil {
  134. return nil, fmt.Errorf("cannot validate config: %w", err)
  135. }
  136. return conf, nil
  137. }