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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package obfuscated2
  2. const (
  3. // DefaultDC defines a number of the default DC to use. This value used
  4. // only if a value from obfuscated2 handshake frame is 0 (default).
  5. DefaultDC = 2
  6. handshakeFrameLen = 64
  7. handshakeFrameLenKey = 32
  8. handshakeFrameLenIV = 16
  9. handshakeFrameLenConnectionType = 4
  10. handshakeFrameOffsetStart = 8
  11. handshakeFrameOffsetKey = handshakeFrameOffsetStart
  12. handshakeFrameOffsetIV = handshakeFrameOffsetKey + handshakeFrameLenKey
  13. handshakeFrameOffsetConnectionType = handshakeFrameOffsetIV + handshakeFrameLenIV
  14. handshakeFrameOffsetDC = handshakeFrameOffsetConnectionType + handshakeFrameLenConnectionType
  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. idx := int16(h.data[handshakeFrameOffsetDC]) | int16(h.data[handshakeFrameOffsetDC+1])<<8 //nolint: lll // little endian for int16 is here
  33. switch {
  34. case idx > 0:
  35. return int(idx)
  36. case idx < 0:
  37. return -int(idx)
  38. default:
  39. return DefaultDC
  40. }
  41. }
  42. func (h *handshakeFrame) key() []byte {
  43. return h.data[handshakeFrameOffsetKey:handshakeFrameOffsetIV]
  44. }
  45. func (h *handshakeFrame) iv() []byte {
  46. return h.data[handshakeFrameOffsetIV:handshakeFrameOffsetConnectionType]
  47. }
  48. func (h *handshakeFrame) connectionType() []byte {
  49. return h.data[handshakeFrameOffsetConnectionType:handshakeFrameOffsetDC]
  50. }
  51. func (h *handshakeFrame) invert() handshakeFrame {
  52. copyFrame := *h
  53. for i := 0; i < handshakeFrameLenKey+handshakeFrameLenIV; i++ {
  54. copyFrame.data[handshakeFrameOffsetKey+i] = h.data[handshakeFrameOffsetConnectionType-1-i]
  55. }
  56. return copyFrame
  57. }