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.

client_intermediate_secure.go 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package packetack
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "math/rand"
  6. "go.uber.org/zap"
  7. "github.com/9seconds/mtg/conntypes"
  8. )
  9. type wrapperClientIntermediateSecure struct {
  10. wrapperClientIntermediate
  11. }
  12. func (w *wrapperClientIntermediateSecure) Read(acks *conntypes.ConnectionAcks) (conntypes.Packet, error) {
  13. data, err := w.wrapperClientIntermediate.Read(acks)
  14. if err != nil {
  15. return nil, err
  16. }
  17. length := len(data) - (len(data) % 4)
  18. return data[:length], nil
  19. }
  20. func (w *wrapperClientIntermediateSecure) Write(packet conntypes.Packet, acks *conntypes.ConnectionAcks) error {
  21. if acks.Simple {
  22. if _, err := w.parent.Write(packet); err != nil {
  23. return fmt.Errorf("cannot send simpleacked packet: %w", err)
  24. }
  25. return nil
  26. }
  27. buf := acquireClientBytesBuffer()
  28. defer releaseClientBytesBuffer(buf)
  29. paddingLength := rand.Intn(4)
  30. buf.Grow(4 + len(packet) + paddingLength)
  31. binary.Write(buf, binary.LittleEndian, uint32(len(packet)+paddingLength)) // nolint: errcheck
  32. buf.Write(packet)
  33. buf.Write(make([]byte, paddingLength))
  34. if _, err := w.parent.Write(buf.Bytes()); err != nil {
  35. return fmt.Errorf("cannot send packet: %w", err)
  36. }
  37. return nil
  38. }
  39. func (w *wrapperClientIntermediateSecure) Logger() *zap.SugaredLogger {
  40. return w.parent.Logger().Named("client-intermediate-secure")
  41. }
  42. func NewClientIntermediateSecure(parent conntypes.StreamReadWriteCloser) conntypes.PacketAckFullReadWriteCloser {
  43. return &wrapperClientIntermediateSecure{
  44. wrapperClientIntermediate: wrapperClientIntermediate{
  45. parent: parent,
  46. },
  47. }
  48. }