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.

parse.go 3.7KB

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