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 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. AutoUpdate bool `toml:"auto-update" json:"autoUpdate,omitempty"`
  16. DomainFrontingPort uint `toml:"domain-fronting-port" json:"domainFrontingPort,omitempty"`
  17. DomainFrontingIP string `toml:"domain-fronting-ip" json:"domainFrontingIp,omitempty"`
  18. DomainFrontingProxyProtocol bool `toml:"domain-fronting-proxy-protocol" json:"domainFrontingProxyProtocol,omitempty"`
  19. TolerateTimeSkewness string `toml:"tolerate-time-skewness" json:"tolerateTimeSkewness,omitempty"`
  20. Concurrency uint `toml:"concurrency" json:"concurrency,omitempty"`
  21. PublicIPv4 string `toml:"public-ipv4" json:"publicIpv4,omitempty"`
  22. PublicIPv6 string `toml:"public-ipv6" json:"publicIpv6,omitempty"`
  23. DomainFronting struct {
  24. IP string `toml:"ip" json:"ip,omitempty"`
  25. Host string `toml:"host" json:"host,omitempty"`
  26. Port uint `toml:"port" json:"port,omitempty"`
  27. ProxyProtocol bool `toml:"proxy-protocol" json:"proxyProtocol,omitempty"`
  28. } `toml:"domain-fronting" json:"domainFronting,omitempty"`
  29. Defense struct {
  30. AntiReplay struct {
  31. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  32. MaxSize string `toml:"max-size" json:"maxSize,omitempty"`
  33. ErrorRate float64 `toml:"error-rate" json:"errorRate,omitempty"`
  34. } `toml:"anti-replay" json:"antiReplay,omitempty"`
  35. Blocklist struct {
  36. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  37. DownloadConcurrency uint `toml:"download-concurrency" json:"downloadConcurrency,omitempty"`
  38. URLs []string `toml:"urls" json:"urls,omitempty"`
  39. UpdateEach string `toml:"update-each" json:"updateEach,omitempty"`
  40. } `toml:"blocklist" json:"blocklist,omitempty"`
  41. Allowlist struct {
  42. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  43. DownloadConcurrency uint `toml:"download-concurrency" json:"downloadConcurrency,omitempty"`
  44. URLs []string `toml:"urls" json:"urls,omitempty"`
  45. UpdateEach string `toml:"update-each" json:"updateEach,omitempty"`
  46. } `toml:"allowlist" json:"allowlist,omitempty"`
  47. Doppelganger struct {
  48. URLs []string `toml:"urls" json:"urls,omitempty"`
  49. Repeats uint `toml:"repeats-per-raid" json:"repeats_per_raid,omitempty"`
  50. UpdateEach string `toml:"raid-each" json:"raid_each,omitempty"`
  51. DRS bool `toml:"drs" json:"drs,omitempty"`
  52. } `toml:"doppelganger" json:"doppelganger,omitempty"`
  53. } `toml:"defense" json:"defense,omitempty"`
  54. Network struct {
  55. Timeout struct {
  56. TCP string `toml:"tcp" json:"tcp,omitempty"`
  57. HTTP string `toml:"http" json:"http,omitempty"`
  58. Idle string `toml:"idle" json:"idle,omitempty"`
  59. Handshake string `toml:"handshake" json:"handshake,omitempty"`
  60. } `toml:"timeout" json:"timeout,omitempty"`
  61. KeepAlive struct {
  62. Disabled bool `toml:"disabled" json:"disabled,omitempty"`
  63. Idle string `toml:"idle" json:"idle,omitempty"`
  64. Interval string `toml:"interval" json:"interval,omitempty"`
  65. Count uint `toml:"count" json:"count,omitempty"`
  66. } `toml:"keep-alive" json:"keepAlive,omitempty"`
  67. DOHIP string `toml:"doh-ip" json:"dohIp,omitempty"`
  68. DNS string `toml:"dns" json:"dns,omitempty"`
  69. Proxies []string `toml:"proxies" json:"proxies,omitempty"`
  70. } `toml:"network" json:"network,omitempty"`
  71. Stats struct {
  72. StatsD struct {
  73. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  74. Address string `toml:"address" json:"address,omitempty"`
  75. MetricPrefix string `toml:"metric-prefix" json:"metricPrefix,omitempty"`
  76. TagFormat string `toml:"tag-format" json:"tagFormat,omitempty"`
  77. } `toml:"statsd" json:"statsd,omitempty"`
  78. Prometheus struct {
  79. Enabled bool `toml:"enabled" json:"enabled,omitempty"`
  80. BindTo string `toml:"bind-to" json:"bindTo,omitempty"`
  81. HTTPPath string `toml:"http-path" json:"httpPath,omitempty"`
  82. MetricPrefix string `toml:"metric-prefix" json:"metricPrefix,omitempty"`
  83. } `toml:"prometheus" json:"prometheus,omitempty"`
  84. } `toml:"stats" json:"stats,omitempty"`
  85. }
  86. func Parse(rawData []byte) (*Config, error) {
  87. tomlConf := &tomlConfig{}
  88. jsonBuf := &bytes.Buffer{}
  89. conf := &Config{}
  90. jsonEncoder := json.NewEncoder(jsonBuf)
  91. jsonEncoder.SetEscapeHTML(false)
  92. jsonEncoder.SetIndent("", "")
  93. if err := toml.Unmarshal(rawData, tomlConf); err != nil {
  94. return nil, fmt.Errorf("cannot parse toml config: %w", err)
  95. }
  96. if err := jsonEncoder.Encode(tomlConf); err != nil {
  97. panic(err)
  98. }
  99. if err := json.NewDecoder(jsonBuf).Decode(conf); err != nil {
  100. return nil, fmt.Errorf("cannot parse a config: %w", err)
  101. }
  102. return conf, nil
  103. }