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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package config
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/pelletier/go-toml/v2"
  7. )
  8. type tomlConfig struct {
  9. Debug bool `toml:"debug" json:"debug,omitempty"`
  10. AllowFallbackOnUnknownDC bool `toml:"allow-fallback-on-unknown-dc" json:"allowFallbackOnUnknownDc,omitempty"`
  11. Secret string `toml:"secret" json:"secret,omitempty"`
  12. Secrets map[string]string `toml:"secrets" json:"secrets,omitempty"`
  13. BindTo string `toml:"bind-to" json:"bindTo"`
  14. ProxyProtocolListener bool `toml:"proxy-protocol-listener" json:"proxyProtocolListener"`
  15. PreferIP string `toml:"prefer-ip" json:"preferIp,omitempty"`
  16. AutoUpdate bool `toml:"auto-update" json:"autoUpdate,omitempty"`
  17. DomainFrontingPort uint `toml:"domain-fronting-port" json:"domainFrontingPort,omitempty"`
  18. DomainFrontingIP string `toml:"domain-fronting-ip" json:"domainFrontingIp,omitempty"`
  19. DomainFrontingProxyProtocol bool `toml:"domain-fronting-proxy-protocol" json:"domainFrontingProxyProtocol,omitempty"`
  20. TolerateTimeSkewness string `toml:"tolerate-time-skewness" json:"tolerateTimeSkewness,omitempty"`
  21. Concurrency uint `toml:"concurrency" json:"concurrency,omitempty"`
  22. PublicIPv4 string `toml:"public-ipv4" json:"publicIpv4,omitempty"`
  23. PublicIPv6 string `toml:"public-ipv6" json:"publicIpv6,omitempty"`
  24. DomainFronting struct {
  25. IP string `toml:"ip" json:"ip,omitempty"`
  26. Port uint `toml:"port" json:"port,omitempty"`
  27. ProxyProtocol bool `toml:"proxy-protocol" json:"proxyProtocol,omitempty"`
  28. } `toml:"domain-fronting" json:"domainFronting,omitempty"`
  29. Defense struct {
  30. AntiReplay struct {
  31. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  32. MaxSize string `toml:"max-size" json:"maxSize,omitempty"`
  33. ErrorRate float64 `toml:"error-rate" json:"errorRate,omitempty"`
  34. } `toml:"anti-replay" json:"antiReplay,omitempty"`
  35. Blocklist struct {
  36. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  37. DownloadConcurrency uint `toml:"download-concurrency" json:"downloadConcurrency,omitempty"`
  38. URLs []string `toml:"urls" json:"urls,omitempty"`
  39. UpdateEach string `toml:"update-each" json:"updateEach,omitempty"`
  40. } `toml:"blocklist" json:"blocklist,omitempty"`
  41. Allowlist struct {
  42. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  43. DownloadConcurrency uint `toml:"download-concurrency" json:"downloadConcurrency,omitempty"`
  44. URLs []string `toml:"urls" json:"urls,omitempty"`
  45. UpdateEach string `toml:"update-each" json:"updateEach,omitempty"`
  46. } `toml:"allowlist" json:"allowlist,omitempty"`
  47. Doppelganger struct {
  48. URLs []string `toml:"urls" json:"urls,omitempty"`
  49. Repeats uint `toml:"repeats-per-raid" json:"repeats_per_raid,omitempty"`
  50. UpdateEach string `toml:"raid-each" json:"raid_each,omitempty"`
  51. DRS bool `toml:"drs" json:"drs,omitempty"`
  52. } `toml:"doppelganger" json:"doppelganger,omitempty"`
  53. } `toml:"defense" json:"defense,omitempty"`
  54. Network struct {
  55. Timeout struct {
  56. TCP string `toml:"tcp" json:"tcp,omitempty"`
  57. HTTP string `toml:"http" json:"http,omitempty"`
  58. Idle string `toml:"idle" json:"idle,omitempty"`
  59. } `toml:"timeout" json:"timeout,omitempty"`
  60. DOHIP string `toml:"doh-ip" json:"dohIp,omitempty"`
  61. DNS string `toml:"dns" json:"dns,omitempty"`
  62. Proxies []string `toml:"proxies" json:"proxies,omitempty"`
  63. } `toml:"network" json:"network,omitempty"`
  64. APIBindTo string `toml:"api-bind-to" json:"apiBindTo,omitempty"`
  65. Stats struct {
  66. StatsD struct {
  67. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  68. Address string `toml:"address" json:"address,omitempty"`
  69. MetricPrefix string `toml:"metric-prefix" json:"metricPrefix,omitempty"`
  70. TagFormat string `toml:"tag-format" json:"tagFormat,omitempty"`
  71. } `toml:"statsd" json:"statsd,omitempty"`
  72. Prometheus struct {
  73. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  74. BindTo string `toml:"bind-to" json:"bindTo,omitempty"`
  75. HTTPPath string `toml:"http-path" json:"httpPath,omitempty"`
  76. MetricPrefix string `toml:"metric-prefix" json:"metricPrefix,omitempty"`
  77. } `toml:"prometheus" json:"prometheus,omitempty"`
  78. } `toml:"stats" json:"stats,omitempty"`
  79. }
  80. func Parse(rawData []byte) (*Config, error) {
  81. tomlConf := &tomlConfig{}
  82. jsonBuf := &bytes.Buffer{}
  83. conf := &Config{}
  84. jsonEncoder := json.NewEncoder(jsonBuf)
  85. jsonEncoder.SetEscapeHTML(false)
  86. jsonEncoder.SetIndent("", "")
  87. if err := toml.Unmarshal(rawData, tomlConf); err != nil {
  88. return nil, fmt.Errorf("cannot parse toml config: %w", err)
  89. }
  90. if err := jsonEncoder.Encode(tomlConf); err != nil {
  91. panic(err)
  92. }
  93. if err := json.NewDecoder(jsonBuf).Decode(conf); err != nil {
  94. return nil, fmt.Errorf("cannot parse a config: %w", err)
  95. }
  96. return conf, nil
  97. }