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
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

parse.go 5.2KB

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