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
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

mux.go 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package hub
  2. import (
  3. "context"
  4. "mtg/conntypes"
  5. "mtg/protocol"
  6. )
  7. type muxNewRequest struct {
  8. req *protocol.TelegramRequest
  9. resp chan<- muxNewResponse
  10. }
  11. type muxNewResponse struct {
  12. conn *ProxyConn
  13. err error
  14. }
  15. type mux struct {
  16. connections connectionList
  17. clients map[string]*connection
  18. ctx context.Context
  19. channelClosed chan conntypes.ConnID
  20. channelNew chan muxNewRequest
  21. }
  22. func (m *mux) run() {
  23. for {
  24. select {
  25. case <-m.ctx.Done():
  26. for _, v := range m.clients {
  27. v.Close()
  28. }
  29. return
  30. case req := <-m.channelNew:
  31. proxyConn := newProxyConn(req.req, m.channelClosed)
  32. conn, err := m.connections.Get(proxyConn)
  33. if err == nil {
  34. m.clients[string(req.req.ConnID[:])] = conn
  35. }
  36. req.resp <- muxNewResponse{
  37. conn: proxyConn,
  38. err: err,
  39. }
  40. close(req.resp)
  41. case connID := <-m.channelClosed:
  42. if conn, ok := m.clients[string(connID[:])]; ok {
  43. conn.Detach(connID)
  44. delete(m.clients, string(connID[:]))
  45. }
  46. }
  47. }
  48. }
  49. func (m *mux) Get(req *protocol.TelegramRequest) (*ProxyConn, error) {
  50. resp := make(chan muxNewResponse)
  51. m.channelNew <- muxNewRequest{
  52. req: req,
  53. resp: resp,
  54. }
  55. rv := <-resp
  56. return rv.conn, rv.err
  57. }
  58. func newMux(ctx context.Context) *mux {
  59. m := &mux{
  60. ctx: ctx,
  61. clients: make(map[string]*connection),
  62. channelClosed: make(chan conntypes.ConnID, 1),
  63. channelNew: make(chan muxNewRequest),
  64. }
  65. go m.run()
  66. return m
  67. }