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
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

connection.go 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. c.conn.Close()
  48. close(c.done)
  49. c.hub.channelBrokenSockets <- c.id
  50. })
  51. }
  52. func (c *connection) closed() bool {
  53. select {
  54. case <-c.done:
  55. return true
  56. default:
  57. return false
  58. }
  59. }
  60. func (c *connection) idle() bool {
  61. c.mutex.RLock()
  62. defer c.mutex.RUnlock()
  63. return c.pending == 0
  64. }
  65. func (c *connection) run() {
  66. logger := c.hub.logger.Named("connection").With("id", c.id)
  67. for {
  68. packet, err := c.read()
  69. if err != nil {
  70. c.shutdown()
  71. return
  72. }
  73. response, err := rpc.ParseProxyResponse(packet)
  74. if err != nil {
  75. logger.Debugw("Failed response", "error", err)
  76. continue
  77. }
  78. if response.Type == rpc.ProxyResponseTypeCloseExt {
  79. logger.Debugw("Proxy has closed connection")
  80. return
  81. }
  82. if channel, ok := Registry.getChannel(response.ConnID); ok {
  83. go channel.sendBack(response) // nolint: errcheck
  84. }
  85. }
  86. }
  87. func newConnection(req *protocol.TelegramRequest, hub *connectionHub) (*connection, error) {
  88. conn, err := mtproto.TelegramProtocol(req)
  89. if err != nil {
  90. return nil, fmt.Errorf("cannot create a new connection: %w", err)
  91. }
  92. rv := &connection{
  93. conn: conn,
  94. hub: hub,
  95. id: rand.Int(), // nolint: gosec
  96. done: make(chan struct{}),
  97. }
  98. go rv.run()
  99. return rv, nil
  100. }