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.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package config
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/pelletier/go-toml"
  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. }
  60. func Parse(rawData []byte) (*Config, error) {
  61. tomlConf := &tomlConfig{}
  62. jsonBuf := &bytes.Buffer{}
  63. conf := &Config{}
  64. jsonEncoder := json.NewEncoder(jsonBuf)
  65. jsonEncoder.SetEscapeHTML(false)
  66. jsonEncoder.SetIndent("", "")
  67. if err := toml.Unmarshal(rawData, tomlConf); err != nil {
  68. return nil, fmt.Errorf("cannot parse toml config: %w", err)
  69. }
  70. if err := jsonEncoder.Encode(tomlConf); err != nil {
  71. panic(err)
  72. }
  73. if err := json.NewDecoder(jsonBuf).Decode(conf); err != nil {
  74. return nil, fmt.Errorf("cannot parse a config: %w", err)
  75. }
  76. return conf, nil
  77. }