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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package config
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "net"
  6. "strconv"
  7. "time"
  8. "github.com/juju/errors"
  9. )
  10. // Config represents common configuration of mtg.
  11. type Config struct {
  12. Debug bool
  13. Verbose bool
  14. BindPort uint16
  15. PublicIPv4Port uint16
  16. PublicIPv6Port uint16
  17. StatsPort uint16
  18. TimeoutRead time.Duration
  19. TimeoutWrite time.Duration
  20. BindIP net.IP
  21. PublicIPv4 net.IP
  22. PublicIPv6 net.IP
  23. StatsIP net.IP
  24. Secret []byte
  25. }
  26. // URLs contains links to the proxy (tg://, t.me) and their QR codes.
  27. type URLs struct {
  28. TG string `json:"tg_url"`
  29. TMe string `json:"tme_url"`
  30. TGQRCode string `json:"tg_qrcode"`
  31. TMeQRCode string `json:"tme_qrcode"`
  32. }
  33. // IPURLs contains links to both ipv4 and ipv6 of the proxy.
  34. type IPURLs struct {
  35. IPv4 URLs `json:"ipv4"`
  36. IPv6 URLs `json:"ipv6"`
  37. }
  38. // BindAddr returns connection for this server to bind to.
  39. func (c *Config) BindAddr() string {
  40. return getAddr(c.BindIP, c.BindPort)
  41. }
  42. // IPv4Addr returns connection string to ipv6 for mtproto proxy.
  43. func (c *Config) IPv4Addr() string {
  44. return getAddr(c.PublicIPv4, c.PublicIPv4Port)
  45. }
  46. // IPv6Addr returns connection string to ipv6 for mtproto proxy.
  47. func (c *Config) IPv6Addr() string {
  48. return getAddr(c.PublicIPv6, c.PublicIPv6Port)
  49. }
  50. // StatAddr returns connection string to the stats API.
  51. func (c *Config) StatAddr() string {
  52. return getAddr(c.StatsIP, c.StatsPort)
  53. }
  54. // GetURLs returns configured IPURLs instance with links to this server.
  55. func (c *Config) GetURLs() IPURLs {
  56. return IPURLs{
  57. IPv4: getURLs(c.PublicIPv4, c.PublicIPv4Port, c.Secret),
  58. IPv6: getURLs(c.PublicIPv6, c.PublicIPv6Port, c.Secret),
  59. }
  60. }
  61. func getAddr(host fmt.Stringer, port uint16) string {
  62. return net.JoinHostPort(host.String(), strconv.Itoa(int(port)))
  63. }
  64. // NewConfig returns new configuration. If required, it manages and
  65. // fetches data from external sources. Parameters passed to this
  66. // function, should come from command line arguments.
  67. func NewConfig(debug, verbose bool, // nolint: gocyclo
  68. bindIP net.IP, bindPort uint16,
  69. publicIPv4 net.IP, PublicIPv4Port uint16,
  70. publicIPv6 net.IP, publicIPv6Port uint16,
  71. statsIP net.IP, statsPort uint16,
  72. timeoutRead, timeoutWrite time.Duration,
  73. secret string) (*Config, error) {
  74. secretBytes, err := hex.DecodeString(secret)
  75. if err != nil {
  76. return nil, errors.Annotate(err, "Cannot create config")
  77. }
  78. if publicIPv4 == nil {
  79. publicIPv4, err = getGlobalIPv4()
  80. if err != nil {
  81. return nil, errors.Errorf("Cannot get public IP")
  82. }
  83. }
  84. if publicIPv4.To4() == nil {
  85. return nil, errors.Errorf("IP %s is not IPv4", publicIPv4.String())
  86. }
  87. if PublicIPv4Port == 0 {
  88. PublicIPv4Port = bindPort
  89. }
  90. if publicIPv6 == nil {
  91. publicIPv6, err = getGlobalIPv6()
  92. if err != nil {
  93. publicIPv6 = publicIPv4
  94. }
  95. }
  96. if publicIPv6.To16() == nil {
  97. return nil, errors.Errorf("IP %s is not IPv6", publicIPv6.String())
  98. }
  99. if publicIPv6Port == 0 {
  100. publicIPv6Port = bindPort
  101. }
  102. if statsIP == nil {
  103. statsIP = publicIPv4
  104. }
  105. conf := &Config{
  106. Debug: debug,
  107. Verbose: verbose,
  108. BindIP: bindIP,
  109. BindPort: bindPort,
  110. PublicIPv4: publicIPv4,
  111. PublicIPv4Port: PublicIPv4Port,
  112. PublicIPv6: publicIPv6,
  113. PublicIPv6Port: publicIPv6Port,
  114. StatsIP: statsIP,
  115. StatsPort: statsPort,
  116. TimeoutRead: timeoutRead,
  117. TimeoutWrite: timeoutWrite,
  118. Secret: secretBytes,
  119. }
  120. return conf, nil
  121. }