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 5.5KB

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