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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package config
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "net"
  7. "net/url"
  8. "github.com/dolonet/mtg-multi/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. Secrets map[string]mtglib.Secret `json:"secrets"`
  24. BindTo TypeHostPort `json:"bindTo"`
  25. ProxyProtocolListener TypeBool `json:"proxyProtocolListener"`
  26. PreferIP TypePreferIP `json:"preferIp"`
  27. AutoUpdate TypeBool `json:"autoUpdate"`
  28. DomainFrontingPort TypePort `json:"domainFrontingPort"`
  29. DomainFrontingIP TypeIP `json:"domainFrontingIp"`
  30. DomainFrontingProxyProtocol TypeBool `json:"domainFrontingProxyProtocol"`
  31. TolerateTimeSkewness TypeDuration `json:"tolerateTimeSkewness"`
  32. Concurrency TypeConcurrency `json:"concurrency"`
  33. PublicIPv4 TypeIP `json:"publicIpv4"`
  34. PublicIPv6 TypeIP `json:"publicIpv6"`
  35. DomainFronting struct {
  36. IP TypeIP `json:"ip"`
  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. } `json:"timeout"`
  61. DOHIP TypeIP `json:"dohIp"`
  62. DNS TypeDNSURI `json:"dns"`
  63. Proxies []TypeProxyURL `json:"proxies"`
  64. } `json:"network"`
  65. APIBindTo TypeHostPort `json:"apiBindTo"`
  66. Stats struct {
  67. StatsD struct {
  68. Optional
  69. Address TypeHostPort `json:"address"`
  70. MetricPrefix TypeMetricPrefix `json:"metricPrefix"`
  71. TagFormat TypeStatsdTagFormat `json:"tagFormat"`
  72. } `json:"statsd"`
  73. Prometheus struct {
  74. Optional
  75. BindTo TypeHostPort `json:"bindTo"`
  76. HTTPPath TypeHTTPPath `json:"httpPath"`
  77. MetricPrefix TypeMetricPrefix `json:"metricPrefix"`
  78. } `json:"prometheus"`
  79. } `json:"stats"`
  80. }
  81. func (c *Config) GetConcurrency(defaultValue uint) uint {
  82. if concurrency := c.Concurrency.Get(0); concurrency != 0 {
  83. return concurrency
  84. }
  85. return c.Concurrency.Get(defaultValue)
  86. }
  87. func (c *Config) GetDNS() *url.URL {
  88. var dohURL *url.URL
  89. if dohIP := c.Network.DOHIP.Get(nil); dohIP != nil {
  90. dohURL, _ = url.Parse("https://" + dohIP.String())
  91. }
  92. return c.Network.DNS.Get(dohURL)
  93. }
  94. func (c *Config) GetDomainFrontingPort(defaultValue uint) uint {
  95. if port := c.DomainFronting.Port.Get(0); port != 0 {
  96. return port
  97. }
  98. return c.DomainFrontingPort.Get(defaultValue)
  99. }
  100. func (c *Config) GetDomainFrontingIP(defaultValue net.IP) string {
  101. if ip := c.DomainFronting.IP.Get(nil); ip != nil {
  102. return ip.String()
  103. }
  104. if ip := c.DomainFrontingIP.Get(defaultValue); ip != nil {
  105. return ip.String()
  106. }
  107. return ""
  108. }
  109. func (c *Config) GetDomainFrontingProxyProtocol(defaultValue bool) bool {
  110. return c.DomainFronting.ProxyProtocol.Get(false) || c.DomainFrontingProxyProtocol.Get(defaultValue)
  111. }
  112. func (c *Config) Validate() error {
  113. if len(c.Secrets) == 0 {
  114. if !c.Secret.Valid() {
  115. return fmt.Errorf("invalid secret %s", c.Secret.String())
  116. }
  117. } else {
  118. for name, s := range c.Secrets {
  119. if !s.Valid() {
  120. return fmt.Errorf("invalid secret %q: %s", name, s.String())
  121. }
  122. }
  123. }
  124. if c.BindTo.Get("") == "" {
  125. return fmt.Errorf("incorrect bind-to parameter %s", c.BindTo.String())
  126. }
  127. return nil
  128. }
  129. // GetSecrets returns all secrets as a map. If the new [secrets] section is used,
  130. // returns that map. Otherwise, wraps the single Secret as {"default": Secret}.
  131. func (c *Config) GetSecrets() map[string]mtglib.Secret {
  132. if len(c.Secrets) > 0 {
  133. return c.Secrets
  134. }
  135. return map[string]mtglib.Secret{"default": c.Secret}
  136. }
  137. func (c *Config) String() string {
  138. buf := &bytes.Buffer{}
  139. encoder := json.NewEncoder(buf)
  140. encoder.SetEscapeHTML(false)
  141. if err := encoder.Encode(c); err != nil {
  142. panic(err)
  143. }
  144. return buf.String()
  145. }