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.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. 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. TolerateTimeSkewness string `toml:"tolerate-time-skewness" json:"tolerateTimeSkewness,omitempty"`
  17. Concurrency uint `toml:"concurrency" json:"concurrency,omitempty"`
  18. Defense struct {
  19. AntiReplay struct {
  20. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  21. MaxSize string `toml:"max-size" json:"maxSize,omitempty"`
  22. ErrorRate float64 `toml:"error-rate" json:"errorRate,omitempty"`
  23. } `toml:"anti-replay" json:"antiReplay,omitempty"`
  24. Blocklist struct {
  25. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  26. DownloadConcurrency uint `toml:"download-concurrency" json:"downloadConcurrency,omitempty"`
  27. URLs []string `toml:"urls" json:"urls,omitempty"`
  28. UpdateEach string `toml:"update-each" json:"updateEach,omitempty"`
  29. } `toml:"blocklist" json:"blocklist,omitempty"`
  30. Allowlist struct {
  31. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  32. DownloadConcurrency uint `toml:"download-concurrency" json:"downloadConcurrency,omitempty"`
  33. URLs []string `toml:"urls" json:"urls,omitempty"`
  34. UpdateEach string `toml:"update-each" json:"updateEach,omitempty"`
  35. } `toml:"allowlist" json:"allowlist,omitempty"`
  36. } `toml:"defense" json:"defense,omitempty"`
  37. Network struct {
  38. Timeout struct {
  39. TCP string `toml:"tcp" json:"tcp,omitempty"`
  40. HTTP string `toml:"http" json:"http,omitempty"`
  41. Idle string `toml:"idle" json:"idle,omitempty"`
  42. } `toml:"timeout" json:"timeout,omitempty"`
  43. DOHIP string `toml:"doh-ip" json:"dohIp,omitempty"`
  44. Proxies []string `toml:"proxies" json:"proxies,omitempty"`
  45. } `toml:"network" json:"network,omitempty"`
  46. Stats struct {
  47. StatsD struct {
  48. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  49. Address string `toml:"address" json:"address,omitempty"`
  50. MetricPrefix string `toml:"metric-prefix" json:"metricPrefix,omitempty"`
  51. TagFormat string `toml:"tag-format" json:"tagFormat,omitempty"`
  52. } `toml:"statsd" json:"statsd,omitempty"`
  53. Prometheus struct {
  54. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  55. BindTo string `toml:"bind-to" json:"bindTo,omitempty"`
  56. HTTPPath string `toml:"http-path" json:"httpPath,omitempty"`
  57. MetricPrefix string `toml:"metric-prefix" json:"metricPrefix,omitempty"`
  58. } `toml:"prometheus" json:"prometheus,omitempty"`
  59. } `toml:"stats" json:"stats,omitempty"`
  60. DCOverrides []struct {
  61. DC uint `toml:"dc" json:"dc"`
  62. IPs []string `toml:"ips" json:"ips"`
  63. } `toml:"dc-overrides" json:"dcOverrides,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. }