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.

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