|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+package wrappers
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "bytes"
|
|
|
5
|
+ "crypto/aes"
|
|
|
6
|
+ "encoding/binary"
|
|
|
7
|
+ "hash/crc32"
|
|
|
8
|
+ "io"
|
|
|
9
|
+ "io/ioutil"
|
|
|
10
|
+ "net"
|
|
|
11
|
+
|
|
|
12
|
+ "github.com/juju/errors"
|
|
|
13
|
+)
|
|
|
14
|
+
|
|
|
15
|
+const (
|
|
|
16
|
+ mtprotoFrameMinMessageLength = 12
|
|
|
17
|
+ mtprotoFrameMaxMessageLength = 16777216
|
|
|
18
|
+)
|
|
|
19
|
+
|
|
|
20
|
+var mtprotoFramePadding = []byte{0x04, 0x00, 0x00, 0x00}
|
|
|
21
|
+
|
|
|
22
|
+type MTProtoFrame struct {
|
|
|
23
|
+ conn WrapStreamReadWriteCloser
|
|
|
24
|
+ readSeqNo int32
|
|
|
25
|
+ writeSeqNo int32
|
|
|
26
|
+}
|
|
|
27
|
+
|
|
|
28
|
+func (m *MTProtoFrame) Read() ([]byte, error) {
|
|
|
29
|
+ buf := &bytes.Buffer{}
|
|
|
30
|
+ sum := crc32.NewIEEE()
|
|
|
31
|
+ writer := io.MultiWriter(buf, sum)
|
|
|
32
|
+
|
|
|
33
|
+ for {
|
|
|
34
|
+ buf.Reset()
|
|
|
35
|
+ sum.Reset()
|
|
|
36
|
+ if _, err := io.CopyN(writer, m.conn, 4); err != nil {
|
|
|
37
|
+ return nil, errors.Annotate(err, "Cannot read frame padding")
|
|
|
38
|
+ }
|
|
|
39
|
+ if !bytes.Equal(buf.Bytes(), mtprotoFramePadding) {
|
|
|
40
|
+ break
|
|
|
41
|
+ }
|
|
|
42
|
+ }
|
|
|
43
|
+
|
|
|
44
|
+ messageLength := binary.LittleEndian.Uint32(buf.Bytes())
|
|
|
45
|
+ m.LogDebug("Read MTProto frame",
|
|
|
46
|
+ "messageLength", messageLength,
|
|
|
47
|
+ "sequence_number", m.readSeqNo,
|
|
|
48
|
+ )
|
|
|
49
|
+ if messageLength%4 != 0 || messageLength < mtprotoFrameMinMessageLength || messageLength > mtprotoFrameMaxMessageLength {
|
|
|
50
|
+ return nil, errors.Errorf("Incorrect frame message length %d", messageLength)
|
|
|
51
|
+ }
|
|
|
52
|
+
|
|
|
53
|
+ buf.Reset()
|
|
|
54
|
+ buf.Grow(int(messageLength) - 4 - 4)
|
|
|
55
|
+ if _, err := io.CopyN(writer, m.conn, int64(messageLength)-4-4); err != nil {
|
|
|
56
|
+ return nil, errors.Annotate(err, "Cannot read the message frame")
|
|
|
57
|
+ }
|
|
|
58
|
+
|
|
|
59
|
+ var seqNo int32
|
|
|
60
|
+ binary.Read(buf, binary.LittleEndian, &seqNo)
|
|
|
61
|
+ if seqNo != m.readSeqNo {
|
|
|
62
|
+ return nil, errors.Errorf("Unexpected sequence number %d (wait for %d)", seqNo, m.readSeqNo)
|
|
|
63
|
+ }
|
|
|
64
|
+
|
|
|
65
|
+ data, _ := ioutil.ReadAll(buf)
|
|
|
66
|
+ buf.Reset()
|
|
|
67
|
+ // write to buf, not to writer. This is because we are going to fetch
|
|
|
68
|
+ // crc32 checksum.
|
|
|
69
|
+ if _, err := io.CopyN(buf, m.conn, 4); err != nil {
|
|
|
70
|
+ return nil, errors.Annotate(err, "Cannot read checksum")
|
|
|
71
|
+ }
|
|
|
72
|
+
|
|
|
73
|
+ checksum := binary.LittleEndian.Uint32(buf.Bytes())
|
|
|
74
|
+ if checksum != sum.Sum32() {
|
|
|
75
|
+ return nil, errors.Errorf("CRC32 checksum mismatch. Wait for %d, got %d", sum.Sum32(), checksum)
|
|
|
76
|
+ }
|
|
|
77
|
+
|
|
|
78
|
+ m.LogDebug("Read MTProto frame",
|
|
|
79
|
+ "messageLength", messageLength,
|
|
|
80
|
+ "sequence_number", m.readSeqNo,
|
|
|
81
|
+ "dataLength", len(data),
|
|
|
82
|
+ "checksum", checksum,
|
|
|
83
|
+ )
|
|
|
84
|
+ m.readSeqNo++
|
|
|
85
|
+
|
|
|
86
|
+ return data, nil
|
|
|
87
|
+}
|
|
|
88
|
+
|
|
|
89
|
+func (m *MTProtoFrame) Write(p []byte) (int, error) {
|
|
|
90
|
+ messageLength := 4 + 4 + len(p) + 4
|
|
|
91
|
+ paddingLength := (aes.BlockSize - messageLength%aes.BlockSize) % aes.BlockSize
|
|
|
92
|
+
|
|
|
93
|
+ buf := &bytes.Buffer{}
|
|
|
94
|
+ buf.Grow(messageLength + paddingLength)
|
|
|
95
|
+
|
|
|
96
|
+ binary.Write(buf, binary.LittleEndian, uint32(messageLength))
|
|
|
97
|
+ binary.Write(buf, binary.LittleEndian, m.writeSeqNo)
|
|
|
98
|
+ buf.Write(p)
|
|
|
99
|
+
|
|
|
100
|
+ checksum := crc32.ChecksumIEEE(buf.Bytes())
|
|
|
101
|
+ binary.Write(buf, binary.LittleEndian, checksum)
|
|
|
102
|
+ buf.Write(bytes.Repeat(mtprotoFramePadding, paddingLength/4))
|
|
|
103
|
+
|
|
|
104
|
+ m.LogDebug("Write MTProto frame",
|
|
|
105
|
+ "length", len(p),
|
|
|
106
|
+ "sequence_number", m.writeSeqNo,
|
|
|
107
|
+ "crc32", checksum,
|
|
|
108
|
+ "frame_length", buf.Len(),
|
|
|
109
|
+ )
|
|
|
110
|
+ m.writeSeqNo++
|
|
|
111
|
+
|
|
|
112
|
+ _, err := m.conn.Write(buf.Bytes())
|
|
|
113
|
+
|
|
|
114
|
+ return len(p), err
|
|
|
115
|
+}
|
|
|
116
|
+
|
|
|
117
|
+func (m *MTProtoFrame) LogDebug(msg string, data ...interface{}) {
|
|
|
118
|
+ m.conn.LogDebug(msg, data...)
|
|
|
119
|
+}
|
|
|
120
|
+
|
|
|
121
|
+func (m *MTProtoFrame) LogInfo(msg string, data ...interface{}) {
|
|
|
122
|
+ m.conn.LogInfo(msg, data...)
|
|
|
123
|
+}
|
|
|
124
|
+
|
|
|
125
|
+func (m *MTProtoFrame) LogWarn(msg string, data ...interface{}) {
|
|
|
126
|
+ m.conn.LogWarn(msg, data...)
|
|
|
127
|
+}
|
|
|
128
|
+
|
|
|
129
|
+func (m *MTProtoFrame) LogError(msg string, data ...interface{}) {
|
|
|
130
|
+ m.conn.LogError(msg, data...)
|
|
|
131
|
+}
|
|
|
132
|
+
|
|
|
133
|
+func (m *MTProtoFrame) LocalAddr() *net.TCPAddr {
|
|
|
134
|
+ return m.conn.LocalAddr()
|
|
|
135
|
+}
|
|
|
136
|
+
|
|
|
137
|
+func (m *MTProtoFrame) RemoteAddr() *net.TCPAddr {
|
|
|
138
|
+ return m.conn.RemoteAddr()
|
|
|
139
|
+}
|
|
|
140
|
+
|
|
|
141
|
+func (m *MTProtoFrame) Close() error {
|
|
|
142
|
+ return m.conn.Close()
|
|
|
143
|
+}
|
|
|
144
|
+
|
|
|
145
|
+func NewMTProtoFrame(conn WrapStreamReadWriteCloser, seqNo int32) WrapPacketReadWriteCloser {
|
|
|
146
|
+ return &MTProtoFrame{
|
|
|
147
|
+ conn: conn,
|
|
|
148
|
+ readSeqNo: seqNo,
|
|
|
149
|
+ writeSeqNo: seqNo,
|
|
|
150
|
+ }
|
|
|
151
|
+}
|