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
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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