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.

handshake_frame.go 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package obfuscated2
  2. import "encoding/binary"
  3. const (
  4. handshakeFrameLen = 64
  5. handshakeFrameLenKey = 32
  6. handshakeFrameLenIV = 16
  7. handshakeFrameLenConnectionType = 4
  8. handshakeFrameLenDC = 2
  9. handshakeFrameOffsetStart = 8
  10. handshakeFrameOffsetKey = handshakeFrameOffsetStart
  11. handshakeFrameOffsetIV = handshakeFrameOffsetKey + handshakeFrameLenKey
  12. handshakeFrameOffsetConnectionType = handshakeFrameOffsetIV + handshakeFrameLenIV
  13. handshakeFrameOffsetDC = handshakeFrameOffsetConnectionType + handshakeFrameLenConnectionType
  14. handshakeFrameOffsetEnd = handshakeFrameOffsetDC + handshakeFrameLenDC
  15. )
  16. // Connection-Type: Secure. We support only fake tls.
  17. var handshakeConnectionType = []byte{0xdd, 0xdd, 0xdd, 0xdd}
  18. // A structure of obfuscated2 handshake frame is following:
  19. //
  20. // [frameOffsetFirst:frameOffsetKey:frameOffsetIV:frameOffsetMagic:frameOffsetDC:frameOffsetEnd].
  21. //
  22. // - 8 bytes of noise
  23. // - 32 bytes of AES Key
  24. // - 16 bytes of AES IV
  25. // - 4 bytes of 'connection type' - this has some setting like a connection type
  26. // - 2 bytes of 'DC'. DC is little endian int16
  27. // - 2 bytes of noise
  28. type handshakeFrame struct {
  29. data [handshakeFrameLen]byte
  30. }
  31. func (h *handshakeFrame) dc() int {
  32. data := h.data[handshakeFrameOffsetDC:handshakeFrameOffsetEnd]
  33. idx := int16(binary.LittleEndian.Uint16(data))
  34. switch {
  35. case idx > 0:
  36. return int(idx) - 1
  37. case idx < 0:
  38. return -int(idx + 1)
  39. default:
  40. return 0
  41. }
  42. }
  43. func (h *handshakeFrame) key() []byte {
  44. return h.data[handshakeFrameOffsetKey:handshakeFrameOffsetIV]
  45. }
  46. func (h *handshakeFrame) iv() []byte {
  47. return h.data[handshakeFrameOffsetIV:handshakeFrameOffsetConnectionType]
  48. }
  49. func (h *handshakeFrame) connectionType() []byte {
  50. return h.data[handshakeFrameOffsetConnectionType:handshakeFrameOffsetDC]
  51. }
  52. func (h *handshakeFrame) invert() handshakeFrame {
  53. copyFrame := *h
  54. for i := 0; i < handshakeFrameLenKey+handshakeFrameLenIV; i++ {
  55. copyFrame.data[handshakeFrameOffsetKey+i] = h.data[handshakeFrameOffsetConnectionType-1-i]
  56. }
  57. return copyFrame
  58. }