forked from teamgram/teamgram-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmtproto_abridged_codec.go
157 lines (135 loc) · 3.9 KB
/
mtproto_abridged_codec.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright (c) 2018-present, NebulaChat Studio (https://nebula.chat).
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Author: Benqi ([email protected])
package mtproto
import (
"encoding/binary"
"encoding/hex"
"fmt"
"github.com/golang/glog"
"github.com/nebula-chat/chatengine/pkg/net2"
"io"
)
// https://core.telegram.org/mtproto#tcp-transport
//
// There is an abridged version of the same protocol:
// if the client sends 0xef as the first byte (**important:** only prior to the very first data packet),
// then packet length is encoded by a single byte (0x01..0x7e = data length divided by 4;
// or 0x7f followed by 3 length bytes (little endian) divided by 4) followed
// by the data themselves (sequence number and CRC32 not added).
// In this case, server responses look the same (the server does not send 0xefas the first byte).
//
type MTProtoAbridgedCodec struct {
conn *net2.BufferedConn
}
func NewMTProtoAbridgedCodec(conn *net2.BufferedConn) *MTProtoAbridgedCodec {
return &MTProtoAbridgedCodec{
conn: conn,
}
}
func (c *MTProtoAbridgedCodec) Receive() (interface{}, error) {
// minus padding
//size := len(x.buf) / 4 - 1
//
//if size < 127 {
// x.buf[3] = byte(size)
// x.buf = x.buf[3:]
//} else {
// binary.LittleEndian.PutUint32(x.buf, uint32(size << 8 | 127))
//}
//_, err := m.conn.Write(x.buf)
//if err != nil {
// return err
//}
var size int
var n int
var err error
b := make([]byte, 1)
n, err = io.ReadFull(c.conn, b)
if err != nil {
return nil, err
}
// glog.Info("first_byte: ", hex.EncodeToString(b[:1]))
needAck := bool(b[0]>>7 == 1)
_ = needAck
b[0] = b[0] & 0x7f
// glog.Info("first_byte2: ", hex.EncodeToString(b[:1]))
if b[0] < 0x7f {
size = int(b[0]) << 2
glog.Info("size1: ", size)
if size == 0 {
return nil, nil
}
} else {
glog.Info("first_byte2: ", hex.EncodeToString(b[:1]))
b2 := make([]byte, 3)
n, err = io.ReadFull(c.conn, b2)
if err != nil {
return nil, err
}
size = (int(b2[0]) | int(b2[1])<<8 | int(b2[2])<<16) << 2
glog.Info("size2: ", size)
}
left := size
buf := make([]byte, size)
for left > 0 {
n, err = io.ReadFull(c.conn, buf[size-left:])
if err != nil {
glog.Error("ReadFull2 error: ", err)
return nil, err
}
left -= n
}
if size > 10240 {
glog.Info("ReadFull2: ", hex.EncodeToString(buf[:256]))
}
// TODO(@benqi): process report ack and quickack
// 截断QuickAck消息,客户端有问题
if size == 4 {
glog.Errorf("Server response error: ", int32(binary.LittleEndian.Uint32(buf)))
// return nil, fmt.Errorf("Recv QuickAckMessage, ignore!!!!") // connId: ", c.stream, ", by client ", m.RemoteAddr())
return nil, nil
}
authKeyId := int64(binary.LittleEndian.Uint64(buf))
message := NewMTPRawMessage(authKeyId, 0, TRANSPORT_TCP)
message.Decode(buf)
return message, nil
}
func (c *MTProtoAbridgedCodec) Send(msg interface{}) error {
message, ok := msg.(*MTPRawMessage)
if !ok {
err := fmt.Errorf("msg type error, only MTPRawMessage, msg: {%v}", msg)
glog.Error(err)
return err
}
b := message.Encode()
sb := make([]byte, 4)
// minus padding
size := len(b) / 4
if size < 127 {
sb = []byte{byte(size)}
} else {
binary.LittleEndian.PutUint32(sb, uint32(size<<8|127))
}
b = append(sb, b...)
_, err := c.conn.Write(b)
if err != nil {
glog.Errorf("Send msg error: %s", err)
}
return err
}
func (c *MTProtoAbridgedCodec) Close() error {
return c.conn.Close()
}