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.

rpc_proxy_request.go 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package rpc
  2. import (
  3. "bytes"
  4. "crypto/rand"
  5. "encoding/binary"
  6. "net"
  7. "github.com/9seconds/mtg/mtproto"
  8. "github.com/9seconds/mtg/mtproto/bufferpool"
  9. "github.com/juju/errors"
  10. )
  11. const (
  12. rpcProxyRequestConnectionIDLength = 8
  13. rpcProxyRequestIPPortLength = 16 + 4
  14. )
  15. var (
  16. rpcProxyRequestTag = []byte{0xee, 0xf1, 0xce, 0x36}
  17. rpcProxyRequestExtraSize = []byte{0x18, 0x00, 0x00, 0x00}
  18. rpcProxyRequestProxyTag = []byte{0xae, 0x26, 0x1e, 0xdb}
  19. )
  20. type RPCProxyRequest struct {
  21. Flags RPCProxyRequestFlags
  22. ConnectionID [rpcProxyRequestConnectionIDLength]byte
  23. RemoteIPPort [rpcProxyRequestIPPortLength]byte
  24. LocalIPPort [rpcProxyRequestIPPortLength]byte
  25. ADTag []byte
  26. Message *bytes.Buffer
  27. Extras *mtproto.Extras
  28. }
  29. func (r *RPCProxyRequest) Bytes() *bytes.Buffer {
  30. buf := bufferpool.Get()
  31. flags := r.Flags
  32. if r.Extras.QuickAck {
  33. flags |= RPCProxyRequestFlagsQuickAck
  34. }
  35. messageBytes := r.Message.Bytes()
  36. if bytes.HasPrefix(messageBytes, rpcProxyRequestFlagsEncryptedPrefix[:]) {
  37. flags |= RPCProxyRequestFlagsEncrypted
  38. }
  39. buf.Write(rpcProxyRequestTag)
  40. buf.Write(flags.Bytes())
  41. buf.Write(r.ConnectionID[:])
  42. buf.Write(r.RemoteIPPort[:])
  43. buf.Write(r.LocalIPPort[:])
  44. buf.Write(rpcProxyRequestExtraSize)
  45. buf.Write(rpcProxyRequestProxyTag)
  46. buf.WriteByte(byte(len(r.ADTag)))
  47. buf.Write(r.ADTag)
  48. for i := 0; i < (buf.Len() % 4); i++ {
  49. buf.WriteByte(0x00)
  50. }
  51. if r.Message != nil {
  52. buf.Write(messageBytes)
  53. }
  54. return buf
  55. }
  56. func NewRPCProxyRequest(connectionType mtproto.ConnectionType, local, remote *net.TCPAddr, adTag []byte, extras *mtproto.Extras) (*RPCProxyRequest, error) {
  57. flags := RPCProxyRequestFlagsHasAdTag | RPCProxyRequestFlagsMagic | RPCProxyRequestFlagsExtMode2
  58. switch connectionType {
  59. case mtproto.ConnectionTypeAbridged:
  60. flags |= RPCProxyRequestFlagsAbdridged
  61. case mtproto.ConnectionTypeIntermediate:
  62. flags |= RPCProxyRequestFlagsIntermediate
  63. }
  64. request := RPCProxyRequest{
  65. Flags: flags,
  66. ADTag: adTag,
  67. Extras: extras,
  68. }
  69. if _, err := rand.Read(request.ConnectionID[:]); err != nil {
  70. return nil, errors.Annotate(err, "Cannot generate connection ID")
  71. }
  72. port := make([]byte, 4)
  73. copy(request.LocalIPPort[:], local.IP.To16())
  74. binary.LittleEndian.PutUint32(port, uint32(local.Port))
  75. copy(request.LocalIPPort[16:], port)
  76. copy(request.RemoteIPPort[:], remote.IP.To16())
  77. binary.LittleEndian.PutUint32(port, uint32(remote.Port))
  78. copy(request.RemoteIPPort[16:], port)
  79. return &request, nil
  80. }