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.

rpc_handshake_response.go 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package rpc
  2. import (
  3. "bytes"
  4. "github.com/juju/errors"
  5. )
  6. const rpcHandshakeResponseLength = rpcHandshakeRequestLength
  7. type RPCHandshakeResponse struct {
  8. Type [rpcHandshakeTagLength]byte
  9. Flags [rpcHandshakeFlagsLength]byte
  10. SenderPID [rpcHandshakeSenderPIDLength]byte
  11. PeerPID [rpcHandshakePeerPIDLength]byte
  12. }
  13. func (r *RPCHandshakeResponse) Bytes() []byte {
  14. buf := &bytes.Buffer{}
  15. buf.Grow(rpcHandshakeResponseLength)
  16. buf.Write(r.Type[:])
  17. buf.Write(r.Flags[:])
  18. buf.Write(r.SenderPID[:])
  19. buf.Write(r.PeerPID[:])
  20. return buf.Bytes()
  21. }
  22. func (r *RPCHandshakeResponse) Valid(req *RPCHandshakeRequest) error {
  23. if r.Type != rpcHandshakeTag {
  24. return errors.New("Unexpected handshake tag")
  25. }
  26. if r.PeerPID != rpcHandshakeSenderPID {
  27. return errors.New("Incorrect sender PID")
  28. }
  29. return nil
  30. }
  31. func NewRPCHandshakeResponse(data []byte) (*RPCHandshakeResponse, error) {
  32. if len(data) != rpcHandshakeResponseLength {
  33. return nil, errors.New("Incorrect handshake response length")
  34. }
  35. resp := RPCHandshakeResponse{}
  36. copy(resp.Type[:], data[:4])
  37. copy(resp.Flags[:], data[4:8])
  38. copy(resp.SenderPID[:], data[8:20])
  39. copy(resp.PeerPID[:], data[20:])
  40. return &resp, nil
  41. }