/
mtproto_message.go
executable file
·118 lines (101 loc) · 2.42 KB
/
mtproto_message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Copyright (c) 2018, https://github.com/airwide-code
* All rights reserved.
*
*
*
*/
package mtproto
import (
"fmt"
// "encoding/binary"
)
type MessageBase interface {
Encode() []byte
Decode(b []byte) error
}
//
//type CodecAble interface {
// Encode() ([]byte, error)
// Decode(dbuf *DecodeBuf) error
//}
func NewMTPRawMessage(authKeyId int64, quickAckId int32) *MTPRawMessage {
return &MTPRawMessage{
AuthKeyId: authKeyId,
QuickAckId: quickAckId,
}
}
////////////////////////////////////////////////////////////////////////////
// 代理使用
type MTPRawMessage struct {
AuthKeyId int64 // 由原始数据解压获得
QuickAckId int32 // EncryptedMessage,则可能存在
// 原始数据
Payload []byte
}
func (m *MTPRawMessage) Encode() []byte {
return m.Payload
}
func (m *MTPRawMessage) Decode(b []byte) error {
m.Payload = b
return nil
}
////////////////////////////////////////////////////////////////////////////
func NewUnencryptedRawMessage() *UnencryptedRawMessage {
return &UnencryptedRawMessage{
AuthKeyId: 0,
}
}
type UnencryptedRawMessage struct {
// TODO(@benqi): reportAck and quickAck
// NeedAck bool
AuthKeyId int64
MessageId int64
MessageData []byte
}
func (m *UnencryptedRawMessage) Encode() []byte {
// 一次性分配
x := NewEncodeBuf(20 + len(m.MessageData))
x.Long(0)
m.MessageId = GenerateMessageId()
x.Long(m.MessageId)
x.Int(int32(len(m.MessageData)))
x.Bytes(m.MessageData)
return x.buf
}
func (m *UnencryptedRawMessage) Decode(b []byte) error {
dbuf := NewDecodeBuf(b)
m.MessageId = dbuf.Long()
messageLen := dbuf.Int()
if int(messageLen) != dbuf.size-12 {
return fmt.Errorf("invalid UnencryptedRawMessage len: %d (need %d)", messageLen, dbuf.size-12)
}
m.MessageData = dbuf.Bytes(int(messageLen))
return dbuf.err
}
type EncryptedRawMessage struct {
// TODO(@benqi): reportAck and quickAck
// NeedAck bool
AuthKeyId int64
MsgKey []byte
EncryptedData []byte
}
func NewEncryptedRawMessage(authKeyId int64) *EncryptedRawMessage {
return &EncryptedRawMessage{
AuthKeyId: authKeyId,
}
}
func (m *EncryptedRawMessage) Encode() []byte {
// 一次性分配
x := NewEncodeBuf(24 + len(m.EncryptedData))
x.Long(m.AuthKeyId)
x.Bytes(m.MsgKey)
x.Bytes(m.EncryptedData)
return x.buf
}
func (m *EncryptedRawMessage) Decode(b []byte) error {
dbuf := NewDecodeBuf(b)
m.MsgKey = dbuf.Bytes(16)
m.EncryptedData = dbuf.Bytes(len(b) - 16)
return dbuf.err
}