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 kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

parse.go 4.6KB

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