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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "github.com/9seconds/mtg/v2/mtglib"
  8. "github.com/pelletier/go-toml"
  9. )
  10. type config struct {
  11. Debug bool `json:"debug"`
  12. Secret mtglib.Secret `json:"secret"`
  13. }
  14. func (c *config) Validate() error {
  15. if len(c.Secret.Key) == 0 || c.Secret.Host == "" {
  16. return fmt.Errorf("incorrect secret %s", c.Secret.String())
  17. }
  18. return nil
  19. }
  20. type configRaw struct {
  21. Debug bool `toml:"debug" json:"debug"`
  22. Secret string `toml:"secret" json:"secret"`
  23. BindTo string `toml:"bind-to" json:"bind-to"`
  24. TCPBuffer string `toml:"tcp-buffer" json:"tcp-buffer"`
  25. PreferIP string `toml:"prefer-ip" json:"prefer-ip"`
  26. CloakPort uint `toml:"cloak-port" json:"cloak-port"`
  27. Probes struct {
  28. Time struct {
  29. Enabled bool `toml:"enabled" json:"enabled"`
  30. AllowSkewness string `toml:"allow-skewness" json:"allow-skewness"`
  31. } `toml:"time" json:"time"`
  32. AntiReplay struct {
  33. Enabled bool `toml:"enabled" json:"enabled"`
  34. MaxSize string `toml:"max-size" json:"max-size"`
  35. TTL string `toml:"ttl" json:"ttl"`
  36. } `toml:"anti-replay" json:"anti-replay"`
  37. } `toml:"probes" json:"probes"`
  38. Network struct {
  39. PublicIP struct {
  40. IPv4 string `toml:"ipv4" json:"ipv4"`
  41. IPv6 string `toml:"ipv6" json:"ipv6"`
  42. } `toml:"public-ip" json:"public-ip"`
  43. DOHHostname string `toml:"doh-hostname" json:"doh-hostname"`
  44. Proxies []string `toml:"proxies" json:"proxies"`
  45. } `toml:"network" json:"network"`
  46. Stats struct {
  47. StatsD struct {
  48. Enabled bool `toml:"enabled" json:"enabled"`
  49. Address string `toml:"address" json:"address"`
  50. MetricPrefix string `toml:"metric-prefix" json:"metric-prefix"`
  51. } `toml:"statsd" json:"statsd"`
  52. Prometheus struct {
  53. Enabled bool `toml:"enabled" json:"enabled"`
  54. BindTo string `toml:"bind-to" json:"bind-to"`
  55. HTTPPath string `toml:"http-path" json:"http-path"`
  56. MetricPrefix string `toml:"metric-prefix" json:"metric-prefix"`
  57. } `toml:"prometheus" json:"prometheus"`
  58. } `toml:"stats" json:"stats"`
  59. }
  60. func parseConfig(reader io.Reader) (*config, error) {
  61. rawConf := &configRaw{}
  62. if err := toml.NewDecoder(reader).Decode(rawConf); err != nil {
  63. return nil, fmt.Errorf("cannot parse toml config: %w", err)
  64. }
  65. jsonBuf := &bytes.Buffer{}
  66. jsonEncoder := json.NewEncoder(jsonBuf)
  67. jsonEncoder.SetEscapeHTML(false)
  68. jsonEncoder.SetIndent("", "")
  69. if err := jsonEncoder.Encode(rawConf); err != nil {
  70. return nil, fmt.Errorf("cannot dump into interim format: %w", err)
  71. }
  72. conf := &config{}
  73. if err := json.NewDecoder(jsonBuf).Decode(conf); err != nil {
  74. return nil, fmt.Errorf("cannot parse final config: %w", err)
  75. }
  76. if err := conf.Validate(); err != nil {
  77. return nil, fmt.Errorf("cannot validate config: %w", err)
  78. }
  79. return conf, nil
  80. }