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.

scout_conn.go 1.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package doppel
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "io"
  6. "github.com/9seconds/mtg/v2/essentials"
  7. "github.com/9seconds/mtg/v2/mtglib/internal/tls"
  8. )
  9. type ScoutConn struct {
  10. tls.Conn
  11. results *ScoutConnCollected
  12. rawBuf *bytes.Buffer
  13. }
  14. func (s ScoutConn) Read(p []byte) (int, error) {
  15. buf := &bytes.Buffer{}
  16. for {
  17. if n, err := s.rawBuf.Read(p); err == nil {
  18. return n, nil
  19. }
  20. s.rawBuf.Reset()
  21. recordType, length, err := tls.ReadRecord(s.Conn, buf)
  22. if err != nil {
  23. return 0, err
  24. }
  25. s.results.Add(recordType)
  26. s.rawBuf.Write([]byte{recordType})
  27. s.rawBuf.Write(tls.TLSVersion[:])
  28. if err := binary.Write(s.rawBuf, binary.BigEndian, uint16(length)); err != nil {
  29. return 0, err
  30. }
  31. if _, err := io.Copy(s.rawBuf, buf); err != nil {
  32. return 0, err
  33. }
  34. }
  35. }
  36. func NewScoutConn(conn essentials.Conn, results *ScoutConnCollected) ScoutConn {
  37. rawBuf := &bytes.Buffer{}
  38. rawBuf.Grow(tls.MaxRecordSize)
  39. return ScoutConn{
  40. Conn: tls.New(conn, false, false),
  41. results: results,
  42. rawBuf: rawBuf,
  43. }
  44. }