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.

connection.go 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package hub
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "sync"
  6. "github.com/9seconds/mtg/conntypes"
  7. "github.com/9seconds/mtg/mtproto"
  8. "github.com/9seconds/mtg/mtproto/rpc"
  9. "github.com/9seconds/mtg/protocol"
  10. )
  11. type connection struct {
  12. conn conntypes.PacketReadWriteCloser
  13. mutex sync.RWMutex
  14. shutdownOnce sync.Once
  15. hub *connectionHub
  16. id int
  17. pending uint
  18. done chan struct{}
  19. }
  20. func (c *connection) read() (conntypes.Packet, error) {
  21. packet, err := c.conn.Read()
  22. c.mutex.Lock()
  23. if err != nil {
  24. c.pending--
  25. } else {
  26. c.pending = 0
  27. }
  28. c.mutex.Unlock()
  29. return packet, err
  30. }
  31. func (c *connection) write(packet conntypes.Packet) error {
  32. err := c.conn.Write(packet)
  33. if err != nil {
  34. // if we tried to write into a socket and it was broken, it is
  35. // a time to reconsider the prescence of this socket at all.
  36. //
  37. // probably we need to remove it completely because it seems
  38. // that connection is broken.
  39. c.mutex.Lock()
  40. c.pending = 0
  41. c.mutex.Unlock()
  42. }
  43. return err
  44. }
  45. func (c *connection) shutdown() {
  46. c.shutdownOnce.Do(func() {
  47. close(c.done)
  48. c.hub.channelBrokenSockets <- c.id
  49. })
  50. }
  51. func (c *connection) closed() bool {
  52. select {
  53. case <-c.done:
  54. return true
  55. default:
  56. return false
  57. }
  58. }
  59. func (c *connection) idle() bool {
  60. c.mutex.RLock()
  61. defer c.mutex.RUnlock()
  62. return c.pending == 0
  63. }
  64. func (c *connection) run() {
  65. logger := c.hub.logger.Named("connection").With("id", c.id)
  66. for {
  67. packet, err := c.read()
  68. if err != nil {
  69. c.shutdown()
  70. return
  71. }
  72. response, err := rpc.ParseProxyResponse(packet)
  73. if err != nil {
  74. logger.Debugw("Failed response", "error", err)
  75. continue
  76. }
  77. if response.Type == rpc.ProxyResponseTypeCloseExt {
  78. logger.Debugw("Proxy has closed connection")
  79. return
  80. }
  81. if channel, ok := Registry.getChannel(response.ConnID); ok {
  82. go channel.sendBack(response) // nolint: errcheck
  83. }
  84. }
  85. }
  86. func newConnection(req *protocol.TelegramRequest, hub *connectionHub) (*connection, error) {
  87. conn, err := mtproto.TelegramProtocol(req)
  88. if err != nil {
  89. return nil, fmt.Errorf("cannot create a new connection: %w", err)
  90. }
  91. rv := &connection{
  92. conn: conn,
  93. hub: hub,
  94. id: rand.Int(), // nolint: gosec
  95. }
  96. go rv.run()
  97. return rv, nil
  98. }