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 символов.

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