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
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

config.go 3.9KB

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