package mtproto
import (
"context"
"github.com/go-faster/errors"
"github.com/gotd/log"
"github.com/gotd/td/bin"
"github.com/gotd/td/crypto"
"github.com/gotd/td/proto"
)
func (c *Conn ) newEncryptedMessage (id int64 , seq int32 , payload bin .Encoder , b *bin .Buffer ) error {
s := c .session ()
var (
d crypto .EncryptedMessageData
logger = c .log
)
if c .compressThreshold <= 0 && c .wire == nil {
if obj , ok := payload .(interface { TypeID () uint32 }); ok {
logger = c .logWithTypeID (obj .TypeID ())
}
d = crypto .EncryptedMessageData {
SessionID : s .ID ,
Salt : s .Salt ,
MessageID : id ,
SeqNo : seq ,
Message : payload ,
}
} else {
payloadBuf := bufPool .Get ()
defer bufPool .Put (payloadBuf )
if err := payload .Encode (payloadBuf ); err != nil {
return errors .Wrap (err , "encode payload" )
}
logger = c .logWithType (payloadBuf )
body := payloadBuf
compress := c .compressThreshold > 0 && payloadBuf .Len () > c .compressThreshold
switch {
case compress && c .wire == nil :
d = crypto .EncryptedMessageData {
SessionID : s .ID ,
Salt : s .Salt ,
MessageID : id ,
SeqNo : seq ,
Message : proto .GZIP {Data : payloadBuf .Raw ()},
}
default :
if compress {
gzipBuf := bufPool .Get ()
defer bufPool .Put (gzipBuf )
if err := (proto .GZIP {Data : payloadBuf .Raw ()}).Encode (gzipBuf ); err != nil {
return errors .Wrap (err , "encode gzip" )
}
body = gzipBuf
}
if c .wire != nil {
c .traceMessage (WireOut , s .Key , s .Salt , s .ID , id , seq , body .Raw ())
}
d = crypto .EncryptedMessageData {
SessionID : s .ID ,
Salt : s .Salt ,
MessageID : id ,
SeqNo : seq ,
MessageDataLen : int32 (body .Len ()),
MessageDataWithPadding : body .Buf ,
}
}
}
logger .Debug (context .Background (), "Request" , log .Int64 ("msg_id" , id ))
if err := c .cipher .Encrypt (s .Key , d , b ); err != nil {
return errors .Wrap (err , "encrypt" )
}
return nil
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .