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

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