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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. Throttle struct {
  67. MaxConnections TypeConcurrency `json:"maxConnections"`
  68. CheckInterval TypeDuration `json:"checkInterval"`
  69. } `json:"throttle"`
  70. Stats struct {
  71. StatsD struct {
  72. Optional
  73. Address TypeHostPort `json:"address"`
  74. MetricPrefix TypeMetricPrefix `json:"metricPrefix"`
  75. TagFormat TypeStatsdTagFormat `json:"tagFormat"`
  76. } `json:"statsd"`
  77. Prometheus struct {
  78. Optional
  79. BindTo TypeHostPort `json:"bindTo"`
  80. HTTPPath TypeHTTPPath `json:"httpPath"`
  81. MetricPrefix TypeMetricPrefix `json:"metricPrefix"`
  82. } `json:"prometheus"`
  83. } `json:"stats"`
  84. }
  85. func (c *Config) GetConcurrency(defaultValue uint) uint {
  86. if concurrency := c.Concurrency.Get(0); concurrency != 0 {
  87. return concurrency
  88. }
  89. return c.Concurrency.Get(defaultValue)
  90. }
  91. func (c *Config) GetDNS() *url.URL {
  92. var dohURL *url.URL
  93. if dohIP := c.Network.DOHIP.Get(nil); dohIP != nil {
  94. dohURL, _ = url.Parse("https://" + dohIP.String())
  95. }
  96. return c.Network.DNS.Get(dohURL)
  97. }
  98. func (c *Config) GetDomainFrontingPort(defaultValue uint) uint {
  99. if port := c.DomainFronting.Port.Get(0); port != 0 {
  100. return port
  101. }
  102. return c.DomainFrontingPort.Get(defaultValue)
  103. }
  104. func (c *Config) GetDomainFrontingIP(defaultValue net.IP) string {
  105. if ip := c.DomainFronting.IP.Get(nil); ip != nil {
  106. return ip.String()
  107. }
  108. if ip := c.DomainFrontingIP.Get(defaultValue); ip != nil {
  109. return ip.String()
  110. }
  111. return ""
  112. }
  113. func (c *Config) GetDomainFrontingProxyProtocol(defaultValue bool) bool {
  114. return c.DomainFronting.ProxyProtocol.Get(false) || c.DomainFrontingProxyProtocol.Get(defaultValue)
  115. }
  116. func (c *Config) Validate() error {
  117. if len(c.Secrets) == 0 {
  118. if !c.Secret.Valid() {
  119. return fmt.Errorf("invalid secret %s", c.Secret.String())
  120. }
  121. } else {
  122. for name, s := range c.Secrets {
  123. if !s.Valid() {
  124. return fmt.Errorf("invalid secret %q: %s", name, s.String())
  125. }
  126. }
  127. }
  128. if c.BindTo.Get("") == "" {
  129. return fmt.Errorf("incorrect bind-to parameter %s", c.BindTo.String())
  130. }
  131. return nil
  132. }
  133. // GetSecrets returns all secrets as a map. If the new [secrets] section is used,
  134. // returns that map. Otherwise, wraps the single Secret as {"default": Secret}.
  135. func (c *Config) GetSecrets() map[string]mtglib.Secret {
  136. if len(c.Secrets) > 0 {
  137. return c.Secrets
  138. }
  139. return map[string]mtglib.Secret{"default": c.Secret}
  140. }
  141. func (c *Config) String() string {
  142. buf := &bytes.Buffer{}
  143. encoder := json.NewEncoder(buf)
  144. encoder.SetEscapeHTML(false)
  145. if err := encoder.Encode(c); err != nil {
  146. panic(err)
  147. }
  148. return buf.String()
  149. }