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
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

handshake_frame.go 2.0KB

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