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
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

parse.go 5.4KB

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