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
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.go 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. type configRaw struct {
  15. Debug bool `toml:"debug" json:"debug"`
  16. Secret string `toml:"secret" json:"secret"`
  17. BindTo string `toml:"bind-to" json:"bind-to"`
  18. TCPBuffer string `toml:"tcp-buffer" json:"tcp-buffer"`
  19. PreferIP string `toml:"prefer-ip" json:"prefer-ip"`
  20. CloakPort uint `toml:"cloak-port" json:"cloak-port"`
  21. Probes struct {
  22. Time struct {
  23. Enabled bool `toml:"enabled" json:"enabled"`
  24. AllowSkewness string `toml:"allow-skewness" json:"allow-skewness"`
  25. } `toml:"time" json:"time"`
  26. AntiReplay struct {
  27. Enabled bool `toml:"enabled" json:"enabled"`
  28. MaxSize string `toml:"max-size" json:"max-size"`
  29. TTL string `toml:"ttl" json:"ttl"`
  30. } `toml:"anti-replay" json:"anti-replay"`
  31. } `toml:"probes" json:"probes"`
  32. Network struct {
  33. PublicIP struct {
  34. IPv4 string `toml:"ipv4" json:"ipv4"`
  35. IPv6 string `toml:"ipv6" json:"ipv6"`
  36. } `toml:"public-ip" json:"public-ip"`
  37. DOHHostname string `toml:"doh-hostname" json:"doh-hostname"`
  38. Proxies []string `toml:"proxies" json:"proxies"`
  39. } `toml:"network" json:"network"`
  40. Stats struct {
  41. StatsD struct {
  42. Enabled bool `toml:"enabled" json:"enabled"`
  43. Address string `toml:"address" json:"address"`
  44. MetricPrefix string `toml:"metric-prefix" json:"metric-prefix"`
  45. } `toml:"statsd" json:"statsd"`
  46. Prometheus struct {
  47. Enabled bool `toml:"enabled" json:"enabled"`
  48. BindTo string `toml:"bind-to" json:"bind-to"`
  49. HTTPPath string `toml:"http-path" json:"http-path"`
  50. MetricPrefix string `toml:"metric-prefix" json:"metric-prefix"`
  51. } `toml:"prometheus" json:"prometheus"`
  52. } `toml:"stats" json:"stats"`
  53. }
  54. func parseConfig(reader io.Reader) (*config, error) {
  55. rawConf := &configRaw{}
  56. if err := toml.NewDecoder(reader).Decode(rawConf); err != nil {
  57. return nil, fmt.Errorf("cannot parse toml config: %w", err)
  58. }
  59. jsonBuf := &bytes.Buffer{}
  60. jsonEncoder := json.NewEncoder(jsonBuf)
  61. jsonEncoder.SetEscapeHTML(false)
  62. jsonEncoder.SetIndent("", "")
  63. if err := jsonEncoder.Encode(rawConf); err != nil {
  64. return nil, fmt.Errorf("cannot dump into interim format: %w", err)
  65. }
  66. conf := &config{}
  67. if err := json.NewDecoder(jsonBuf).Decode(conf); err != nil {
  68. return nil, fmt.Errorf("cannot parse final config: %w", err)
  69. }
  70. return conf, nil
  71. }