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
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.go 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. NoiseProbeCount TypeConcurrency `json:"noise_probe_count"`
  51. NoiseCacheTTL TypeDuration `json:"noise_cache_ttl"`
  52. NoiseCachePath string `json:"noise_cache_path"`
  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. } `json:"timeout"`
  61. DOHIP TypeIP `json:"dohIp"`
  62. DNS TypeDNSURI `json:"dns"`
  63. Proxies []TypeProxyURL `json:"proxies"`
  64. } `json:"network"`
  65. Stats struct {
  66. StatsD struct {
  67. Optional
  68. Address TypeHostPort `json:"address"`
  69. MetricPrefix TypeMetricPrefix `json:"metricPrefix"`
  70. TagFormat TypeStatsdTagFormat `json:"tagFormat"`
  71. } `json:"statsd"`
  72. Prometheus struct {
  73. Optional
  74. BindTo TypeHostPort `json:"bindTo"`
  75. HTTPPath TypeHTTPPath `json:"httpPath"`
  76. MetricPrefix TypeMetricPrefix `json:"metricPrefix"`
  77. } `json:"prometheus"`
  78. } `json:"stats"`
  79. }
  80. func (c *Config) GetConcurrency(defaultValue uint) uint {
  81. if concurrency := c.Concurrency.Get(0); concurrency != 0 {
  82. return concurrency
  83. }
  84. return c.Concurrency.Get(defaultValue)
  85. }
  86. func (c *Config) GetDNS() *url.URL {
  87. var dohURL *url.URL
  88. if dohIP := c.Network.DOHIP.Get(nil); dohIP != nil {
  89. dohURL, _ = url.Parse("https://" + dohIP.String())
  90. }
  91. return c.Network.DNS.Get(dohURL)
  92. }
  93. func (c *Config) GetDomainFrontingPort(defaultValue uint) uint {
  94. if port := c.DomainFronting.Port.Get(0); port != 0 {
  95. return port
  96. }
  97. return c.DomainFrontingPort.Get(defaultValue)
  98. }
  99. func (c *Config) GetDomainFrontingIP(defaultValue net.IP) string {
  100. if ip := c.DomainFronting.IP.Get(nil); ip != nil {
  101. return ip.String()
  102. }
  103. if ip := c.DomainFrontingIP.Get(defaultValue); ip != nil {
  104. return ip.String()
  105. }
  106. return ""
  107. }
  108. func (c *Config) GetDomainFrontingProxyProtocol(defaultValue bool) bool {
  109. return c.DomainFronting.ProxyProtocol.Get(false) || c.DomainFrontingProxyProtocol.Get(defaultValue)
  110. }
  111. func (c *Config) Validate() error {
  112. if !c.Secret.Valid() {
  113. return fmt.Errorf("invalid secret %s", c.Secret.String())
  114. }
  115. if c.BindTo.Get("") == "" {
  116. return fmt.Errorf("incorrect bind-to parameter %s", c.BindTo.String())
  117. }
  118. return nil
  119. }
  120. func (c *Config) String() string {
  121. buf := &bytes.Buffer{}
  122. encoder := json.NewEncoder(buf)
  123. encoder.SetEscapeHTML(false)
  124. if err := encoder.Encode(c); err != nil {
  125. panic(err)
  126. }
  127. return buf.String()
  128. }