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

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