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文字以内のものにしてください。

config.go 5.5KB

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