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.

nonce_request.go 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package rpc
  2. import (
  3. "bytes"
  4. "crypto/rand"
  5. "encoding/binary"
  6. "time"
  7. "github.com/juju/errors"
  8. )
  9. // NonceRequest is the data type which contains all the data for correct
  10. // nonce request.
  11. type NonceRequest struct {
  12. KeySelector []byte
  13. CryptoTS []byte
  14. Nonce []byte
  15. }
  16. // Bytes returns serialized nonce request.
  17. func (r *NonceRequest) Bytes() []byte {
  18. buf := &bytes.Buffer{}
  19. buf.Write(TagNonce) // nolint: gosec
  20. buf.Write(r.KeySelector) // nolint: gosec
  21. buf.Write(NonceCryptoAES) // nolint: gosec
  22. buf.Write(r.CryptoTS) // nolint: gosec
  23. buf.Write(r.Nonce) // nolint: gosec
  24. return buf.Bytes()
  25. }
  26. // NewNonceRequest builds new none request based on proxy secret.
  27. func NewNonceRequest(proxySecret []byte) (*NonceRequest, error) {
  28. nonce := make([]byte, 16)
  29. keySelector := make([]byte, 4)
  30. cryptoTS := make([]byte, 4)
  31. if _, err := rand.Read(nonce); err != nil {
  32. return nil, errors.Annotate(err, "Cannot generate nonce")
  33. }
  34. copy(keySelector, proxySecret)
  35. timestamp := time.Now().Truncate(time.Second).Unix() % 4294967296 // 256 ^ 4 - do not know how to name
  36. binary.LittleEndian.PutUint32(cryptoTS, uint32(timestamp))
  37. return &NonceRequest{
  38. KeySelector: keySelector,
  39. CryptoTS: cryptoTS,
  40. Nonce: nonce,
  41. }, nil
  42. }