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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. DomainFrontingPort TypePort `json:"domainFrontingPort"`
  26. DomainFrontingIP TypeIP `json:"domainFrontingIp"`
  27. DomainFrontingProxyProtocol TypeBool `json:"domainFrontingProxyProtocol"`
  28. TolerateTimeSkewness TypeDuration `json:"tolerateTimeSkewness"`
  29. Concurrency TypeConcurrency `json:"concurrency"`
  30. DomainFronting struct {
  31. IP TypeIP `json:"ip"`
  32. Port TypePort `json:"port"`
  33. ProxyProtocol TypeBool `json:"proxyProtocol"`
  34. } `json:"domainFronting"`
  35. Defense struct {
  36. AntiReplay struct {
  37. Optional
  38. MaxSize TypeBytes `json:"maxSize"`
  39. ErrorRate TypeErrorRate `json:"errorRate"`
  40. } `json:"antiReplay"`
  41. Blocklist ListConfig `json:"blocklist"`
  42. Allowlist ListConfig `json:"allowlist"`
  43. } `json:"defense"`
  44. Network struct {
  45. Timeout struct {
  46. TCP TypeDuration `json:"tcp"`
  47. HTTP TypeDuration `json:"http"`
  48. Idle TypeDuration `json:"idle"`
  49. } `json:"timeout"`
  50. DOHIP TypeIP `json:"dohIp"`
  51. Proxies []TypeProxyURL `json:"proxies"`
  52. } `json:"network"`
  53. Stats struct {
  54. StatsD struct {
  55. Optional
  56. Address TypeHostPort `json:"address"`
  57. MetricPrefix TypeMetricPrefix `json:"metricPrefix"`
  58. TagFormat TypeStatsdTagFormat `json:"tagFormat"`
  59. } `json:"statsd"`
  60. Prometheus struct {
  61. Optional
  62. BindTo TypeHostPort `json:"bindTo"`
  63. HTTPPath TypeHTTPPath `json:"httpPath"`
  64. MetricPrefix TypeMetricPrefix `json:"metricPrefix"`
  65. } `json:"prometheus"`
  66. } `json:"stats"`
  67. }
  68. func (c *Config) GetDomainFrontingPort(defaultValue uint) uint {
  69. if port := c.DomainFronting.Port.Get(0); port != 0 {
  70. return port
  71. }
  72. return c.DomainFrontingPort.Get(defaultValue)
  73. }
  74. func (c *Config) GetDomainFrontingIP(defaultValue net.IP) string {
  75. if ip := c.DomainFronting.IP.Get(nil); ip != nil {
  76. return ip.String()
  77. }
  78. if ip := c.DomainFrontingIP.Get(defaultValue); ip != nil {
  79. return ip.String()
  80. }
  81. return ""
  82. }
  83. func (c *Config) GetDomainFrontingProxyProtocol(defaultValue bool) bool {
  84. return c.DomainFronting.ProxyProtocol.Get(false) || c.DomainFrontingProxyProtocol.Get(defaultValue)
  85. }
  86. func (c *Config) Validate() error {
  87. if !c.Secret.Valid() {
  88. return fmt.Errorf("invalid secret %s", c.Secret.String())
  89. }
  90. if c.BindTo.Get("") == "" {
  91. return fmt.Errorf("incorrect bind-to parameter %s", c.BindTo.String())
  92. }
  93. return nil
  94. }
  95. func (c *Config) String() string {
  96. buf := &bytes.Buffer{}
  97. encoder := json.NewEncoder(buf)
  98. encoder.SetEscapeHTML(false)
  99. if err := encoder.Encode(c); err != nil {
  100. panic(err)
  101. }
  102. return buf.String()
  103. }