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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. PreferIP string `toml:"prefer-ip" json:"preferIp,omitempty"`
  14. DomainFrontingPort uint `toml:"domain-fronting-port" json:"domainFrontingPort,omitempty"`
  15. TolerateTimeSkewness string `toml:"tolerate-time-skewness" json:"tolerateTimeSkewness,omitempty"`
  16. Concurrency uint `toml:"concurrency" json:"concurrency,omitempty"`
  17. Defense struct {
  18. AntiReplay struct {
  19. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  20. MaxSize string `toml:"max-size" json:"maxSize,omitempty"`
  21. ErrorRate float64 `toml:"error-rate" json:"errorRate,omitempty"`
  22. } `toml:"anti-replay" json:"antiReplay,omitempty"`
  23. Blocklist struct {
  24. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  25. DownloadConcurrency uint `toml:"download-concurrency" json:"downloadConcurrency,omitempty"`
  26. URLs []string `toml:"urls" json:"urls,omitempty"`
  27. UpdateEach string `toml:"update-each" json:"updateEach,omitempty"`
  28. } `toml:"blocklist" json:"blocklist,omitempty"`
  29. Allowlist struct {
  30. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  31. DownloadConcurrency uint `toml:"download-concurrency" json:"downloadConcurrency,omitempty"`
  32. URLs []string `toml:"urls" json:"urls,omitempty"`
  33. UpdateEach string `toml:"update-each" json:"updateEach,omitempty"`
  34. } `toml:"allowlist" json:"allowlist,omitempty"`
  35. } `toml:"defense" json:"defense,omitempty"`
  36. Network struct {
  37. Timeout struct {
  38. TCP string `toml:"tcp" json:"tcp,omitempty"`
  39. HTTP string `toml:"http" json:"http,omitempty"`
  40. Idle string `toml:"idle" json:"idle,omitempty"`
  41. } `toml:"timeout" json:"timeout,omitempty"`
  42. DOHIP string `toml:"doh-ip" json:"dohIp,omitempty"`
  43. Proxies []string `toml:"proxies" json:"proxies,omitempty"`
  44. } `toml:"network" json:"network,omitempty"`
  45. Stats struct {
  46. StatsD struct {
  47. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  48. Address string `toml:"address" json:"address,omitempty"`
  49. MetricPrefix string `toml:"metric-prefix" json:"metricPrefix,omitempty"`
  50. TagFormat string `toml:"tag-format" json:"tagFormat,omitempty"`
  51. } `toml:"statsd" json:"statsd,omitempty"`
  52. Prometheus struct {
  53. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  54. BindTo string `toml:"bind-to" json:"bindTo,omitempty"`
  55. HTTPPath string `toml:"http-path" json:"httpPath,omitempty"`
  56. MetricPrefix string `toml:"metric-prefix" json:"metricPrefix,omitempty"`
  57. } `toml:"prometheus" json:"prometheus,omitempty"`
  58. } `toml:"stats" json:"stats,omitempty"`
  59. DCOverrides []struct {
  60. DC int `toml:"dc" json:"dc"`
  61. IPs []string `toml:"ips" json:"ips"`
  62. } `toml:"dc-overrides" json:"dcOverrides,omitempty"`
  63. DCUpdateEach string `toml:"dc-update-each" json:"dcUpdateEach,omitempty"`
  64. }
  65. func Parse(rawData []byte) (*Config, error) {
  66. tomlConf := &tomlConfig{}
  67. jsonBuf := &bytes.Buffer{}
  68. conf := &Config{}
  69. jsonEncoder := json.NewEncoder(jsonBuf)
  70. jsonEncoder.SetEscapeHTML(false)
  71. jsonEncoder.SetIndent("", "")
  72. if err := toml.Unmarshal(rawData, tomlConf); err != nil {
  73. return nil, fmt.Errorf("cannot parse toml config: %w", err)
  74. }
  75. if err := jsonEncoder.Encode(tomlConf); err != nil {
  76. panic(err)
  77. }
  78. if err := json.NewDecoder(jsonBuf).Decode(conf); err != nil {
  79. return nil, fmt.Errorf("cannot parse a config: %w", err)
  80. }
  81. return conf, nil
  82. }