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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. // GetURLs returns configured IPURLs instance with links to this server.
  50. func (c *Config) GetURLs() IPURLs {
  51. urls := IPURLs{}
  52. if c.PublicIPv4 != nil {
  53. urls.IPv4 = getURLs(c.PublicIPv4, c.PublicIPv4Port, c.Secret)
  54. }
  55. if c.PublicIPv6 != nil {
  56. urls.IPv6 = getURLs(c.PublicIPv6, c.PublicIPv6Port, c.Secret)
  57. }
  58. return urls
  59. }
  60. func getAddr(host fmt.Stringer, port uint16) string {
  61. return net.JoinHostPort(host.String(), strconv.Itoa(int(port)))
  62. }
  63. // NewConfig returns new configuration. If required, it manages and
  64. // fetches data from external sources. Parameters passed to this
  65. // function, should come from command line arguments.
  66. func NewConfig(debug, verbose bool, // nolint: gocyclo
  67. bindIP net.IP, bindPort uint16,
  68. publicIPv4 net.IP, PublicIPv4Port uint16,
  69. publicIPv6 net.IP, publicIPv6Port uint16,
  70. statsIP net.IP, statsPort uint16,
  71. secret, adtag string) (*Config, error) {
  72. if len(secret) != 32 {
  73. return nil, errors.New("Telegram demands secret of length 32")
  74. }
  75. secretBytes, err := hex.DecodeString(secret)
  76. if err != nil {
  77. return nil, errors.Annotate(err, "Cannot create config")
  78. }
  79. var adTagBytes []byte
  80. if len(adtag) != 0 {
  81. adTagBytes, err = hex.DecodeString(adtag)
  82. if err != nil {
  83. return nil, errors.Annotate(err, "Cannot create config")
  84. }
  85. }
  86. if publicIPv4 == nil {
  87. publicIPv4, err = getGlobalIPv4()
  88. if err != nil {
  89. publicIPv4 = nil
  90. } else if publicIPv4.To4() == nil {
  91. return nil, errors.Errorf("IP %s is not IPv4", publicIPv4.String())
  92. }
  93. }
  94. if PublicIPv4Port == 0 {
  95. PublicIPv4Port = bindPort
  96. }
  97. if publicIPv6 == nil {
  98. publicIPv6, err = getGlobalIPv6()
  99. if err != nil {
  100. publicIPv6 = nil
  101. } else if publicIPv6.To4() != nil {
  102. return nil, errors.Errorf("IP %s is not IPv6", publicIPv6.String())
  103. }
  104. }
  105. if publicIPv6Port == 0 {
  106. publicIPv6Port = bindPort
  107. }
  108. if statsIP == nil {
  109. statsIP = publicIPv4
  110. }
  111. conf := &Config{
  112. Debug: debug,
  113. Verbose: verbose,
  114. BindIP: bindIP,
  115. BindPort: bindPort,
  116. PublicIPv4: publicIPv4,
  117. PublicIPv4Port: PublicIPv4Port,
  118. PublicIPv6: publicIPv6,
  119. PublicIPv6Port: publicIPv6Port,
  120. StatsIP: statsIP,
  121. StatsPort: statsPort,
  122. Secret: secretBytes,
  123. AdTag: adTagBytes,
  124. }
  125. return conf, nil
  126. }
  127. // SetSocketOptions makes socket keepalive, sets buffer sizes
  128. func SetSocketOptions(conn net.Conn) error {
  129. socket := conn.(*net.TCPConn)
  130. if err := socket.SetReadBuffer(BufferReadSize); err != nil {
  131. return errors.Annotate(err, "Cannot set read buffer size")
  132. }
  133. if err := socket.SetWriteBuffer(BufferWriteSize); err != nil {
  134. return errors.Annotate(err, "Cannot set write buffer size")
  135. }
  136. if err := socket.SetNoDelay(true); err != nil {
  137. return errors.Annotate(err, "Cannot activate nodelay for the socket")
  138. }
  139. return nil
  140. }