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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. TimeoutRead = time.Minute
  16. TimeoutWrite = time.Minute
  17. keepAlivePeriod = 20 * time.Second
  18. )
  19. // Config represents common configuration of mtg.
  20. type Config struct {
  21. Debug bool
  22. Verbose bool
  23. BindPort uint16
  24. PublicIPv4Port uint16
  25. PublicIPv6Port uint16
  26. StatsPort uint16
  27. BindIP net.IP
  28. PublicIPv4 net.IP
  29. PublicIPv6 net.IP
  30. StatsIP net.IP
  31. Secret []byte
  32. }
  33. // URLs contains links to the proxy (tg://, t.me) and their QR codes.
  34. type URLs struct {
  35. TG string `json:"tg_url"`
  36. TMe string `json:"tme_url"`
  37. TGQRCode string `json:"tg_qrcode"`
  38. TMeQRCode string `json:"tme_qrcode"`
  39. }
  40. // IPURLs contains links to both ipv4 and ipv6 of the proxy.
  41. type IPURLs struct {
  42. IPv4 URLs `json:"ipv4"`
  43. IPv6 URLs `json:"ipv6"`
  44. }
  45. // BindAddr returns connection for this server to bind to.
  46. func (c *Config) BindAddr() string {
  47. return getAddr(c.BindIP, c.BindPort)
  48. }
  49. // IPv4Addr returns connection string to ipv6 for mtproto proxy.
  50. func (c *Config) IPv4Addr() string {
  51. return getAddr(c.PublicIPv4, c.PublicIPv4Port)
  52. }
  53. // IPv6Addr returns connection string to ipv6 for mtproto proxy.
  54. func (c *Config) IPv6Addr() string {
  55. return getAddr(c.PublicIPv6, c.PublicIPv6Port)
  56. }
  57. // StatAddr returns connection string to the stats API.
  58. func (c *Config) StatAddr() string {
  59. return getAddr(c.StatsIP, c.StatsPort)
  60. }
  61. // GetURLs returns configured IPURLs instance with links to this server.
  62. func (c *Config) GetURLs() IPURLs {
  63. return IPURLs{
  64. IPv4: getURLs(c.PublicIPv4, c.PublicIPv4Port, c.Secret),
  65. IPv6: getURLs(c.PublicIPv6, c.PublicIPv6Port, c.Secret),
  66. }
  67. }
  68. func getAddr(host fmt.Stringer, port uint16) string {
  69. return net.JoinHostPort(host.String(), strconv.Itoa(int(port)))
  70. }
  71. // NewConfig returns new configuration. If required, it manages and
  72. // fetches data from external sources. Parameters passed to this
  73. // function, should come from command line arguments.
  74. func NewConfig(debug, verbose bool, // nolint: gocyclo
  75. bindIP net.IP, bindPort uint16,
  76. publicIPv4 net.IP, PublicIPv4Port uint16,
  77. publicIPv6 net.IP, publicIPv6Port uint16,
  78. statsIP net.IP, statsPort uint16,
  79. secret string) (*Config, error) {
  80. if len(secret) != 32 {
  81. return nil, errors.New("Telegram demands secret of length 32")
  82. }
  83. secretBytes, err := hex.DecodeString(secret)
  84. if err != nil {
  85. return nil, errors.Annotate(err, "Cannot create config")
  86. }
  87. if publicIPv4 == nil {
  88. publicIPv4, err = getGlobalIPv4()
  89. if err != nil {
  90. return nil, errors.Errorf("Cannot get public IP")
  91. }
  92. }
  93. if publicIPv4.To4() == nil {
  94. return nil, errors.Errorf("IP %s is not IPv4", publicIPv4.String())
  95. }
  96. if PublicIPv4Port == 0 {
  97. PublicIPv4Port = bindPort
  98. }
  99. if publicIPv6 == nil {
  100. publicIPv6, err = getGlobalIPv6()
  101. if err != nil {
  102. publicIPv6 = publicIPv4
  103. }
  104. }
  105. if publicIPv6.To16() == nil {
  106. return nil, errors.Errorf("IP %s is not IPv6", publicIPv6.String())
  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. }
  127. return conf, nil
  128. }
  129. // SetSocketOptions makes socket keepalive, sets buffer sizes
  130. func SetSocketOptions(conn net.Conn) error {
  131. socket := conn.(*net.TCPConn)
  132. if err := socket.SetReadBuffer(BufferReadSize); err != nil {
  133. return errors.Annotate(err, "Cannot set read buffer size")
  134. }
  135. if err := socket.SetWriteBuffer(BufferWriteSize); err != nil {
  136. return errors.Annotate(err, "Cannot set write buffer size")
  137. }
  138. if err := socket.SetKeepAlive(true); err != nil {
  139. return errors.Annotate(err, "Cannot make socket keepalive")
  140. }
  141. if err := socket.SetKeepAlivePeriod(keepAlivePeriod); err != nil {
  142. return errors.Annotate(err, "Cannot set keepalive period")
  143. }
  144. if err := socket.SetNoDelay(true); err != nil {
  145. return errors.Annotate(err, "Cannot activate nodelay for the socket")
  146. }
  147. return nil
  148. }