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 символов.

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