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

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