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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package packetack
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "fmt"
  6. "math/rand"
  7. "go.uber.org/zap"
  8. "github.com/9seconds/mtg/conntypes"
  9. )
  10. type wrapperClientIntermediateSecure struct {
  11. wrapperClientIntermediate
  12. }
  13. func (w *wrapperClientIntermediateSecure) Read(acks *conntypes.ConnectionAcks) (conntypes.Packet, error) {
  14. data, err := w.wrapperClientIntermediate.Read(acks)
  15. if err != nil {
  16. return nil, err
  17. }
  18. length := len(data) - (len(data) % 4)
  19. return data[:length], nil
  20. }
  21. func (w *wrapperClientIntermediateSecure) Write(packet conntypes.Packet, acks *conntypes.ConnectionAcks) error {
  22. if acks.Simple {
  23. if _, err := w.parent.Write(packet); err != nil {
  24. return fmt.Errorf("cannot send simpleacked packet: %w", err)
  25. }
  26. return nil
  27. }
  28. buf := bytes.Buffer{}
  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. }