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
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

parse.go 4.2KB

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