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

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