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 3.7KB

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