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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package config
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "net"
  7. "github.com/9seconds/mtg/v2/mtglib"
  8. )
  9. type Optional struct {
  10. Enabled TypeBool `json:"enabled"`
  11. }
  12. type ListConfig struct {
  13. Optional
  14. DownloadConcurrency TypeConcurrency `json:"downloadConcurrency"`
  15. URLs []TypeBlocklistURI `json:"urls"`
  16. UpdateEach TypeDuration `json:"updateEach"`
  17. }
  18. type Config struct {
  19. Debug TypeBool `json:"debug"`
  20. AllowFallbackOnUnknownDC TypeBool `json:"allowFallbackOnUnknownDc"`
  21. Secret mtglib.Secret `json:"secret"`
  22. BindTo TypeHostPort `json:"bindTo"`
  23. ProxyProtocolListener TypeBool `json:"proxyProtocolListener"`
  24. PreferIP TypePreferIP `json:"preferIp"`
  25. AutoUpdate TypeBool `json:"autoUpdate"`
  26. DomainFrontingPort TypePort `json:"domainFrontingPort"`
  27. DomainFrontingIP TypeIP `json:"domainFrontingIp"`
  28. DomainFrontingProxyProtocol TypeBool `json:"domainFrontingProxyProtocol"`
  29. TolerateTimeSkewness TypeDuration `json:"tolerateTimeSkewness"`
  30. Concurrency TypeConcurrency `json:"concurrency"`
  31. DomainFronting struct {
  32. IP TypeIP `json:"ip"`
  33. Port TypePort `json:"port"`
  34. ProxyProtocol TypeBool `json:"proxyProtocol"`
  35. } `json:"domainFronting"`
  36. Defense struct {
  37. AntiReplay struct {
  38. Optional
  39. MaxSize TypeBytes `json:"maxSize"`
  40. ErrorRate TypeErrorRate `json:"errorRate"`
  41. } `json:"antiReplay"`
  42. Blocklist ListConfig `json:"blocklist"`
  43. Allowlist ListConfig `json:"allowlist"`
  44. } `json:"defense"`
  45. Network struct {
  46. Timeout struct {
  47. TCP TypeDuration `json:"tcp"`
  48. HTTP TypeDuration `json:"http"`
  49. Idle TypeDuration `json:"idle"`
  50. } `json:"timeout"`
  51. DOHIP TypeIP `json:"dohIp"`
  52. Proxies []TypeProxyURL `json:"proxies"`
  53. } `json:"network"`
  54. Stats struct {
  55. StatsD struct {
  56. Optional
  57. Address TypeHostPort `json:"address"`
  58. MetricPrefix TypeMetricPrefix `json:"metricPrefix"`
  59. TagFormat TypeStatsdTagFormat `json:"tagFormat"`
  60. } `json:"statsd"`
  61. Prometheus struct {
  62. Optional
  63. BindTo TypeHostPort `json:"bindTo"`
  64. HTTPPath TypeHTTPPath `json:"httpPath"`
  65. MetricPrefix TypeMetricPrefix `json:"metricPrefix"`
  66. } `json:"prometheus"`
  67. } `json:"stats"`
  68. }
  69. func (c *Config) GetDomainFrontingPort(defaultValue uint) uint {
  70. if port := c.DomainFronting.Port.Get(0); port != 0 {
  71. return port
  72. }
  73. return c.DomainFrontingPort.Get(defaultValue)
  74. }
  75. func (c *Config) GetDomainFrontingIP(defaultValue net.IP) string {
  76. if ip := c.DomainFronting.IP.Get(nil); ip != nil {
  77. return ip.String()
  78. }
  79. if ip := c.DomainFrontingIP.Get(defaultValue); ip != nil {
  80. return ip.String()
  81. }
  82. return ""
  83. }
  84. func (c *Config) GetDomainFrontingProxyProtocol(defaultValue bool) bool {
  85. return c.DomainFronting.ProxyProtocol.Get(false) || c.DomainFrontingProxyProtocol.Get(defaultValue)
  86. }
  87. func (c *Config) Validate() error {
  88. if !c.Secret.Valid() {
  89. return fmt.Errorf("invalid secret %s", c.Secret.String())
  90. }
  91. if c.BindTo.Get("") == "" {
  92. return fmt.Errorf("incorrect bind-to parameter %s", c.BindTo.String())
  93. }
  94. return nil
  95. }
  96. func (c *Config) String() string {
  97. buf := &bytes.Buffer{}
  98. encoder := json.NewEncoder(buf)
  99. encoder.SetEscapeHTML(false)
  100. if err := encoder.Encode(c); err != nil {
  101. panic(err)
  102. }
  103. return buf.String()
  104. }