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
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

config.go 5.7KB

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