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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. PublicIPv4 TypeIP `json:"publicIpv4"`
  33. PublicIPv6 TypeIP `json:"publicIpv6"`
  34. DomainFronting struct {
  35. IP TypeIP `json:"ip"`
  36. Host TypeHost `json:"host"`
  37. Port TypePort `json:"port"`
  38. ProxyProtocol TypeBool `json:"proxyProtocol"`
  39. } `json:"domainFronting"`
  40. Defense struct {
  41. AntiReplay struct {
  42. Optional
  43. MaxSize TypeBytes `json:"maxSize"`
  44. ErrorRate TypeErrorRate `json:"errorRate"`
  45. } `json:"antiReplay"`
  46. Blocklist ListConfig `json:"blocklist"`
  47. Allowlist ListConfig `json:"allowlist"`
  48. Doppelganger struct {
  49. URLs []TypeHttpsURL `json:"urls"`
  50. Repeats TypeConcurrency `json:"repeats_per_raid"`
  51. UpdateEach TypeDuration `json:"raid_each"`
  52. DRS TypeBool `json:"drs"`
  53. } `json:"doppelganger"`
  54. } `json:"defense"`
  55. Network struct {
  56. Timeout struct {
  57. TCP TypeDuration `json:"tcp"`
  58. HTTP TypeDuration `json:"http"`
  59. Idle TypeDuration `json:"idle"`
  60. Handshake TypeDuration `json:"handshake"`
  61. } `json:"timeout"`
  62. KeepAlive struct {
  63. Disabled TypeBool `json:"disabled"`
  64. Idle TypeDuration `json:"idle"`
  65. Interval TypeDuration `json:"interval"`
  66. Count TypeConcurrency `json:"count"`
  67. } `json:"keepAlive"`
  68. DOHIP TypeIP `json:"dohIp"`
  69. DNS TypeDNSURI `json:"dns"`
  70. Proxies []TypeProxyURL `json:"proxies"`
  71. } `json:"network"`
  72. Stats struct {
  73. StatsD struct {
  74. Optional
  75. Address TypeHostPort `json:"address"`
  76. MetricPrefix TypeMetricPrefix `json:"metricPrefix"`
  77. TagFormat TypeStatsdTagFormat `json:"tagFormat"`
  78. } `json:"statsd"`
  79. Prometheus struct {
  80. Optional
  81. BindTo TypeHostPort `json:"bindTo"`
  82. HTTPPath TypeHTTPPath `json:"httpPath"`
  83. MetricPrefix TypeMetricPrefix `json:"metricPrefix"`
  84. } `json:"prometheus"`
  85. } `json:"stats"`
  86. }
  87. func (c *Config) GetConcurrency(defaultValue uint) uint {
  88. if concurrency := c.Concurrency.Get(0); concurrency != 0 {
  89. return concurrency
  90. }
  91. return c.Concurrency.Get(defaultValue)
  92. }
  93. func (c *Config) GetDNS() *url.URL {
  94. var dohURL *url.URL
  95. if dohIP := c.Network.DOHIP.Get(nil); dohIP != nil {
  96. dohURL, _ = url.Parse("https://" + dohIP.String())
  97. }
  98. return c.Network.DNS.Get(dohURL)
  99. }
  100. func (c *Config) GetDomainFrontingPort(defaultValue uint) uint {
  101. if port := c.DomainFronting.Port.Get(0); port != 0 {
  102. return port
  103. }
  104. return c.DomainFrontingPort.Get(defaultValue)
  105. }
  106. func (c *Config) GetDomainFrontingIP(defaultValue net.IP) string {
  107. if host := c.DomainFronting.Host.Get(""); host != "" {
  108. return host
  109. }
  110. if ip := c.DomainFronting.IP.Get(nil); ip != nil {
  111. return ip.String()
  112. }
  113. if ip := c.DomainFrontingIP.Get(defaultValue); ip != nil {
  114. return ip.String()
  115. }
  116. return ""
  117. }
  118. func (c *Config) GetDomainFrontingProxyProtocol(defaultValue bool) bool {
  119. return c.DomainFronting.ProxyProtocol.Get(false) || c.DomainFrontingProxyProtocol.Get(defaultValue)
  120. }
  121. func (c *Config) Validate() error {
  122. if !c.Secret.Valid() {
  123. return fmt.Errorf("invalid secret %s", c.Secret.String())
  124. }
  125. if c.BindTo.Get("") == "" {
  126. return fmt.Errorf("incorrect bind-to parameter %s", c.BindTo.String())
  127. }
  128. if c.DomainFronting.Host.Get("") != "" && c.DomainFronting.IP.Get(nil) != nil {
  129. return fmt.Errorf("[domain-fronting] host and ip are mutually exclusive; pick one")
  130. }
  131. return nil
  132. }
  133. func (c *Config) String() string {
  134. buf := &bytes.Buffer{}
  135. encoder := json.NewEncoder(buf)
  136. encoder.SetEscapeHTML(false)
  137. if err := encoder.Encode(c); err != nil {
  138. panic(err)
  139. }
  140. return buf.String()
  141. }