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

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