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
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

parse.go 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. TCPBuffer string `toml:"tcp-buffer" json:"tcpBuffer,omitempty"`
  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. } `toml:"defense" json:"defense,omitempty"`
  31. Network struct {
  32. Timeout struct {
  33. TCP string `toml:"tcp" json:"tcp,omitempty"`
  34. HTTP string `toml:"http" json:"http,omitempty"`
  35. Idle string `toml:"idle" json:"idle,omitempty"`
  36. } `toml:"timeout" json:"timeout,omitempty"`
  37. DOHIP string `toml:"doh-ip" json:"dohIp,omitempty"`
  38. Proxies []string `toml:"proxies" json:"proxies,omitempty"`
  39. } `toml:"network" json:"network,omitempty"`
  40. Stats struct {
  41. StatsD struct {
  42. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  43. Address string `toml:"address" json:"address,omitempty"`
  44. MetricPrefix string `toml:"metric-prefix" json:"metricPrefix,omitempty"`
  45. TagFormat string `toml:"tag-format" json:"tagFormat,omitempty"`
  46. } `toml:"statsd" json:"statsd,omitempty"`
  47. Prometheus struct {
  48. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  49. BindTo string `toml:"bind-to" json:"bindTo,omitempty"`
  50. HTTPPath string `toml:"http-path" json:"httpPath,omitempty"`
  51. MetricPrefix string `toml:"metric-prefix" json:"metricPrefix,omitempty"`
  52. } `toml:"prometheus" json:"prometheus,omitempty"`
  53. } `toml:"stats" json:"stats,omitempty"`
  54. }
  55. func Parse(rawData []byte) (*Config, error) {
  56. tomlConf := &tomlConfig{}
  57. jsonBuf := &bytes.Buffer{}
  58. conf := &Config{}
  59. jsonEncoder := json.NewEncoder(jsonBuf)
  60. jsonEncoder.SetEscapeHTML(false)
  61. jsonEncoder.SetIndent("", "")
  62. if err := toml.Unmarshal(rawData, tomlConf); err != nil {
  63. return nil, fmt.Errorf("cannot parse toml config: %w", err)
  64. }
  65. if err := jsonEncoder.Encode(tomlConf); err != nil {
  66. panic(err)
  67. }
  68. if err := json.NewDecoder(jsonBuf).Decode(conf); err != nil {
  69. return nil, fmt.Errorf("cannot parse a config: %w", err)
  70. }
  71. return conf, nil
  72. }