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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package config
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/9seconds/mtg/v2/mtglib"
  7. )
  8. type Optional struct {
  9. Enabled TypeBool `json:"enabled"`
  10. }
  11. type ListConfig struct {
  12. Optional
  13. DownloadConcurrency TypeConcurrency `json:"downloadConcurrency"`
  14. URLs []TypeBlocklistURI `json:"urls"`
  15. UpdateEach TypeDuration `json:"updateEach"`
  16. }
  17. type Config struct {
  18. Debug TypeBool `json:"debug"`
  19. AllowFallbackOnUnknownDC TypeBool `json:"allowFallbackOnUnknownDc"`
  20. Secret mtglib.Secret `json:"secret"`
  21. BindTo TypeHostPort `json:"bindTo"`
  22. PreferIP TypePreferIP `json:"preferIp"`
  23. DomainFrontingPort TypePort `json:"domainFrontingPort"`
  24. TolerateTimeSkewness TypeDuration `json:"tolerateTimeSkewness"`
  25. Concurrency TypeConcurrency `json:"concurrency"`
  26. Defense struct {
  27. AntiReplay struct {
  28. Optional
  29. MaxSize TypeBytes `json:"maxSize"`
  30. ErrorRate TypeErrorRate `json:"errorRate"`
  31. } `json:"antiReplay"`
  32. Blocklist ListConfig `json:"blocklist"`
  33. Allowlist ListConfig `json:"allowlist"`
  34. } `json:"defense"`
  35. Network struct {
  36. Timeout struct {
  37. TCP TypeDuration `json:"tcp"`
  38. HTTP TypeDuration `json:"http"`
  39. Idle TypeDuration `json:"idle"`
  40. } `json:"timeout"`
  41. DOHIP TypeIP `json:"dohIp"`
  42. Proxies []TypeProxyURL `json:"proxies"`
  43. } `json:"network"`
  44. Stats struct {
  45. StatsD struct {
  46. Optional
  47. Address TypeHostPort `json:"address"`
  48. MetricPrefix TypeMetricPrefix `json:"metricPrefix"`
  49. TagFormat TypeStatsdTagFormat `json:"tagFormat"`
  50. } `json:"statsd"`
  51. Prometheus struct {
  52. Optional
  53. BindTo TypeHostPort `json:"bindTo"`
  54. HTTPPath TypeHTTPPath `json:"httpPath"`
  55. MetricPrefix TypeMetricPrefix `json:"metricPrefix"`
  56. } `json:"prometheus"`
  57. } `json:"stats"`
  58. DCOverrides []struct {
  59. DC TypeDC
  60. IPs []TypeHostPort `json:"ips"`
  61. } `json:"dc_overrides"`
  62. }
  63. func (c *Config) Validate() error {
  64. if !c.Secret.Valid() {
  65. return fmt.Errorf("invalid secret %s", c.Secret.String())
  66. }
  67. if c.BindTo.Get("") == "" {
  68. return fmt.Errorf("incorrect bind-to parameter %s", c.BindTo.String())
  69. }
  70. return nil
  71. }
  72. func (c *Config) String() string {
  73. buf := &bytes.Buffer{}
  74. encoder := json.NewEncoder(buf)
  75. encoder.SetEscapeHTML(false)
  76. if err := encoder.Encode(c); err != nil {
  77. panic(err)
  78. }
  79. return buf.String()
  80. }