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
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

config.go 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. } `json:"defense"`
  28. Network struct {
  29. PublicIP struct {
  30. IPv4 TypeIP `json:"ipv4"`
  31. IPv6 TypeIP `json:"ipv6"`
  32. } `json:"public-ip"`
  33. Timeout struct {
  34. TCP TypeDuration `json:"tcp"`
  35. HTTP TypeDuration `json:"http"`
  36. Idle TypeDuration `json:"idle"`
  37. } `json:"timeout"`
  38. DOHIP TypeIP `json:"doh-ip"`
  39. Proxies []TypeURL `json:"proxies"`
  40. } `json:"network"`
  41. Stats struct {
  42. StatsD struct {
  43. Enabled bool `json:"enabled"`
  44. Address TypeHostPort `json:"address"`
  45. MetricPrefix TypeMetricPrefix `json:"metric-prefix"`
  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. CloakPort uint `toml:"cloak-port" json:"cloak-port,omitempty"`
  80. Concurrency uint `toml:"concurrency" json:"concurrency,omitempty"`
  81. Defense struct {
  82. Time struct {
  83. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  84. AllowSkewness string `toml:"allow-skewness" json:"allow-skewness,omitempty"`
  85. } `toml:"time" json:"time,omitempty"`
  86. AntiReplay struct {
  87. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  88. MaxSize string `toml:"max-size" json:"max-size,omitempty"`
  89. ErrorRate float64 `toml:"error-rate" json:"error-rate,omitempty"`
  90. } `toml:"anti-replay" json:"anti-replay,omitempty"`
  91. } `toml:"defense" json:"defense,omitempty"`
  92. Network struct {
  93. PublicIP struct {
  94. IPv4 string `toml:"ipv4" json:"ipv4,omitempty"`
  95. IPv6 string `toml:"ipv6" json:"ipv6,omitempty"`
  96. } `toml:"public-ip" json:"public-ip,omitempty"`
  97. Timeout struct {
  98. TCP string `toml:"tcp" json:"tcp,omitempty"`
  99. HTTP string `toml:"http" json:"http,omitempty"`
  100. Idle string `toml:"idle" json:"idle,omitempty"`
  101. } `toml:"timeout" json:"timeout,omitempty"`
  102. DOHIP string `toml:"doh-ip" json:"doh-ip,omitempty"`
  103. Proxies []string `toml:"proxies" json:"proxies,omitempty"`
  104. } `toml:"network" json:"network,omitempty"`
  105. Stats struct {
  106. StatsD struct {
  107. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  108. Address string `toml:"address" json:"address,omitempty"`
  109. MetricPrefix string `toml:"metric-prefix" json:"metric-prefix,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. return nil, fmt.Errorf("cannot dump into interim format: %w", err)
  131. }
  132. if err := json.NewDecoder(jsonBuf).Decode(conf); err != nil {
  133. return nil, fmt.Errorf("cannot parse final 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. }