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

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