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
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

telegram_protocol.go 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package obfuscated2
  2. import (
  3. "crypto/rand"
  4. "fmt"
  5. "github.com/9seconds/mtg/protocol"
  6. "github.com/9seconds/mtg/telegram"
  7. "github.com/9seconds/mtg/utils"
  8. "github.com/9seconds/mtg/wrappers"
  9. )
  10. type TelegramProtocol struct{}
  11. func (t *TelegramProtocol) Handshake(req *protocol.TelegramRequest) (wrappers.Wrap, error) {
  12. socket, err := telegram.Direct.Dial(req.Ctx,
  13. req.Cancel,
  14. req.ClientProtocol.DC(),
  15. req.ClientProtocol.ConnectionProtocol())
  16. if err != nil {
  17. return nil, fmt.Errorf("cannot dial to telegram: %w", err)
  18. }
  19. fm := generateFrame(req.ClientProtocol)
  20. data := fm.Bytes()
  21. encryptor := utils.MakeStreamCipher(fm.Key(), fm.IV())
  22. decryptedFrame := fm.Invert()
  23. decryptor := utils.MakeStreamCipher(decryptedFrame.Key(), decryptedFrame.IV())
  24. copyFrame := make([]byte, frameLen)
  25. copy(copyFrame[:frameOffsetIV], data[:frameOffsetIV])
  26. encryptor.XORKeyStream(data, data)
  27. copy(data[:frameOffsetIV], copyFrame[:frameOffsetIV])
  28. if _, err := socket.Write(data); err != nil {
  29. return nil, fmt.Errorf("cannot write handshake frame to telegram: %w", err)
  30. }
  31. return wrappers.NewObfuscated2(socket, encryptor, decryptor), nil
  32. }
  33. func MakeTelegramProtocol() protocol.TelegramProtocol {
  34. return &TelegramProtocol{}
  35. }
  36. func generateFrame(cp protocol.ClientProtocol) (fm Frame) {
  37. data := fm.Bytes()
  38. for {
  39. if _, err := rand.Read(data); err != nil {
  40. continue
  41. }
  42. if data[0] == 0xef {
  43. continue
  44. }
  45. val := (uint32(data[3]) << 24) | (uint32(data[2]) << 16) | (uint32(data[1]) << 8) | uint32(data[0])
  46. if val == 0x44414548 || val == 0x54534f50 || val == 0x20544547 || val == 0x4954504f || val == 0xeeeeeeee {
  47. continue
  48. }
  49. val = (uint32(data[7]) << 24) | (uint32(data[6]) << 16) | (uint32(data[5]) << 8) | uint32(data[4])
  50. if val == 0x00000000 {
  51. continue
  52. }
  53. copy(fm.Magic(), cp.ConnectionType().Tag())
  54. return
  55. }
  56. }