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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package config
  2. import (
  3. "encoding/hex"
  4. "fmt"
  5. "net"
  6. "strconv"
  7. "time"
  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. BufferSizeCopy = 32 * 1024
  15. keepAlivePeriod = 20 * time.Second
  16. )
  17. // Config represents common configuration of mtg.
  18. type Config struct {
  19. Debug bool
  20. Verbose bool
  21. BindPort uint16
  22. PublicIPv4Port uint16
  23. PublicIPv6Port uint16
  24. StatsPort uint16
  25. BindIP net.IP
  26. PublicIPv4 net.IP
  27. PublicIPv6 net.IP
  28. StatsIP net.IP
  29. Secret []byte
  30. }
  31. // URLs contains links to the proxy (tg://, t.me) and their QR codes.
  32. type URLs struct {
  33. TG string `json:"tg_url"`
  34. TMe string `json:"tme_url"`
  35. TGQRCode string `json:"tg_qrcode"`
  36. TMeQRCode string `json:"tme_qrcode"`
  37. }
  38. // IPURLs contains links to both ipv4 and ipv6 of the proxy.
  39. type IPURLs struct {
  40. IPv4 URLs `json:"ipv4"`
  41. IPv6 URLs `json:"ipv6"`
  42. }
  43. // BindAddr returns connection for this server to bind to.
  44. func (c *Config) BindAddr() string {
  45. return getAddr(c.BindIP, c.BindPort)
  46. }
  47. // IPv4Addr returns connection string to ipv6 for mtproto proxy.
  48. func (c *Config) IPv4Addr() string {
  49. return getAddr(c.PublicIPv4, c.PublicIPv4Port)
  50. }
  51. // IPv6Addr returns connection string to ipv6 for mtproto proxy.
  52. func (c *Config) IPv6Addr() string {
  53. return getAddr(c.PublicIPv6, c.PublicIPv6Port)
  54. }
  55. // StatAddr returns connection string to the stats API.
  56. func (c *Config) StatAddr() string {
  57. return getAddr(c.StatsIP, c.StatsPort)
  58. }
  59. // GetURLs returns configured IPURLs instance with links to this server.
  60. func (c *Config) GetURLs() IPURLs {
  61. return IPURLs{
  62. IPv4: getURLs(c.PublicIPv4, c.PublicIPv4Port, c.Secret),
  63. IPv6: getURLs(c.PublicIPv6, c.PublicIPv6Port, c.Secret),
  64. }
  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 string) (*Config, error) {
  78. if len(secret) != 32 {
  79. return nil, errors.New("Telegram demands secret of length 32")
  80. }
  81. secretBytes, err := hex.DecodeString(secret)
  82. if err != nil {
  83. return nil, errors.Annotate(err, "Cannot create config")
  84. }
  85. if publicIPv4 == nil {
  86. publicIPv4, err = getGlobalIPv4()
  87. if err != nil {
  88. return nil, errors.Errorf("Cannot get public IP")
  89. }
  90. }
  91. if publicIPv4.To4() == nil {
  92. return nil, errors.Errorf("IP %s is not IPv4", publicIPv4.String())
  93. }
  94. if PublicIPv4Port == 0 {
  95. PublicIPv4Port = bindPort
  96. }
  97. if publicIPv6 == nil {
  98. publicIPv6, err = getGlobalIPv6()
  99. if err != nil {
  100. publicIPv6 = publicIPv4
  101. }
  102. }
  103. if publicIPv6.To16() == nil {
  104. return nil, errors.Errorf("IP %s is not IPv6", publicIPv6.String())
  105. }
  106. if publicIPv6Port == 0 {
  107. publicIPv6Port = bindPort
  108. }
  109. if statsIP == nil {
  110. statsIP = publicIPv4
  111. }
  112. conf := &Config{
  113. Debug: debug,
  114. Verbose: verbose,
  115. BindIP: bindIP,
  116. BindPort: bindPort,
  117. PublicIPv4: publicIPv4,
  118. PublicIPv4Port: PublicIPv4Port,
  119. PublicIPv6: publicIPv6,
  120. PublicIPv6Port: publicIPv6Port,
  121. StatsIP: statsIP,
  122. StatsPort: statsPort,
  123. Secret: secretBytes,
  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.SetKeepAlive(true); err != nil {
  137. return errors.Annotate(err, "Cannot make socket keepalive")
  138. }
  139. if err := socket.SetKeepAlivePeriod(keepAlivePeriod); err != nil {
  140. return errors.Annotate(err, "Cannot set keepalive period")
  141. }
  142. if err := socket.SetNoDelay(true); err != nil {
  143. return errors.Annotate(err, "Cannot activate nodelay for the socket")
  144. }
  145. return nil
  146. }