-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtp_config.go
308 lines (263 loc) · 8.44 KB
/
mtp_config.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package tdesktop
import (
"github.com/go-faster/errors"
"github.com/KoNekoD/td/bin"
"github.com/KoNekoD/td/crypto"
)
// MTPConfigEnvironment is enum of config environment.
type MTPConfigEnvironment int32
func (e MTPConfigEnvironment) valid() bool {
return e == 0 || e == 1
}
// String implements fmt.Stringer.
func (e MTPConfigEnvironment) String() string {
switch e {
case 0:
return "production"
case 1:
return "test"
default:
return "unknown"
}
}
// Test denotes that environment is test.
func (e MTPConfigEnvironment) Test() bool {
return e == 1
}
// MTPConfig is a Telegram Desktop storage structure which stores MTProto config info.
//
// See https://github.com/telegramdesktop/tdesktop/blob/v2.9.8/Telegram/SourceFiles/mtproto/mtproto_config.h.
type MTPConfig struct {
Environment MTPConfigEnvironment
DCOptions MTPDCOptions
ChatSizeMax int32 // default: 200
MegagroupSizeMax int32 // default: 10000
ForwardedCountMax int32 // default: 100
OnlineUpdatePeriod int32 // default: 120000
OfflineBlurTimeout int32 // default: 5000
OfflineIdleTimeout int32 // default: 30000
OnlineFocusTimeout int32 // default: 1000
OnlineCloudTimeout int32 // default: 300000
NotifyCloudDelay int32 // default: 30000
NotifyDefaultDelay int32 // default: 1500
SavedGifsLimit int32 // default: 200
EditTimeLimit int32 // default: 172800
RevokeTimeLimit int32 // default: 172800
RevokePrivateTimeLimit int32 // default: 172800
RevokePrivateInbox bool // default: false
StickersRecentLimit int32 // default: 30
StickersFavedLimit int32 // default: 5
PinnedDialogsCountMax int32 // default: 5
PinnedDialogsInFolderMax int32 // default: 100
InternalLinksDomain string // default: "https://t.me/"
ChannelsReadMediaPeriod int32 // default: 86400 * 7
CallReceiveTimeoutMs int32 // default: 20000
CallRingTimeoutMs int32 // default: 90000
CallConnectTimeoutMs int32 // default: 30000
CallPacketTimeoutMs int32 // default: 10000
WebFileDCID int32 // default: 4
TxtDomainString string // default: ""
PhoneCallsEnabled bool // default: true
BlockedMode bool // default: false
CaptionLengthMax int32 // default: 1024
}
// See https://github.com/telegramdesktop/tdesktop/blob/v2.9.8/Telegram/SourceFiles/storage/storage_account.cpp#L938.
func readMTPConfig(tgf *tdesktopFile, localKey crypto.Key) (MTPConfig, error) {
encrypted, err := tgf.readArray()
if err != nil {
return MTPConfig{}, errors.Wrap(err, "read encrypted data")
}
decrypted, err := decryptLocal(encrypted, localKey)
if err != nil {
return MTPConfig{}, errors.Wrap(err, "decrypt data")
}
// Skip decrypted data length (uint32).
decrypted = decrypted[4:]
root := qtReader{buf: bin.Buffer{Buf: decrypted}}
cfgReader, err := root.subArray()
if err != nil {
return MTPConfig{}, errors.Wrap(err, "read config array")
}
var m MTPConfig
if err := m.deserialize(&cfgReader); err != nil {
return MTPConfig{}, errors.Wrap(err, "deserialize MTPConfig")
}
return m, err
}
func (m *MTPConfig) deserialize(r *qtReader) error {
version, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read version")
}
if version != kVersion {
return errors.Errorf("wrong version (expected %d, got %d)", kVersion, version)
}
environment, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read environment")
}
m.Environment = MTPConfigEnvironment(environment)
if !m.Environment.valid() {
return errors.Errorf("invalid environment %d", environment)
}
{
sub, err := r.subArray()
if err != nil {
return err
}
if err := m.DCOptions.deserialize(&sub); err != nil {
return errors.Wrap(err, "read DC options")
}
}
chatSizeMax, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read chatSizeMax")
}
m.ChatSizeMax = chatSizeMax
megagroupSizeMax, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read megagroupSizeMax")
}
m.MegagroupSizeMax = megagroupSizeMax
forwardedCountMax, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read forwardedCountMax")
}
m.ForwardedCountMax = forwardedCountMax
onlineUpdatePeriod, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read onlineUpdatePeriod")
}
m.OnlineUpdatePeriod = onlineUpdatePeriod
offlineBlurTimeout, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read offlineBlurTimeout")
}
m.OfflineBlurTimeout = offlineBlurTimeout
offlineIdleTimeout, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read offlineIdleTimeout")
}
m.OfflineIdleTimeout = offlineIdleTimeout
onlineFocusTimeout, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read onlineFocusTimeout")
}
m.OnlineFocusTimeout = onlineFocusTimeout
onlineCloudTimeout, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read onlineCloudTimeout")
}
m.OnlineCloudTimeout = onlineCloudTimeout
notifyCloudDelay, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read notifyCloudDelay")
}
m.NotifyCloudDelay = notifyCloudDelay
notifyDefaultDelay, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read notifyDefaultDelay")
}
m.NotifyDefaultDelay = notifyDefaultDelay
savedGifsLimit, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read savedGifsLimit")
}
m.SavedGifsLimit = savedGifsLimit
editTimeLimit, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read editTimeLimit")
}
m.EditTimeLimit = editTimeLimit
revokeTimeLimit, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read revokeTimeLimit")
}
m.RevokeTimeLimit = revokeTimeLimit
revokePrivateTimeLimit, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read revokePrivateTimeLimit")
}
m.RevokePrivateTimeLimit = revokePrivateTimeLimit
revokePrivateInbox, err := r.readUint32()
if err != nil {
return errors.Wrap(err, "read revokePrivateInbox")
}
m.RevokePrivateInbox = revokePrivateInbox == 1
stickersRecentLimit, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read stickersRecentLimit")
}
m.StickersRecentLimit = stickersRecentLimit
stickersFavedLimit, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read stickersFavedLimit")
}
m.StickersFavedLimit = stickersFavedLimit
pinnedDialogsCountMax, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read pinnedDialogsCountMax")
}
m.PinnedDialogsCountMax = pinnedDialogsCountMax
pinnedDialogsInFolderMax, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read pinnedDialogsInFolderMax")
}
m.PinnedDialogsInFolderMax = pinnedDialogsInFolderMax
internalLinksDomain, err := r.readString()
if err != nil {
return errors.Wrap(err, "read internalLinksDomain")
}
m.InternalLinksDomain = internalLinksDomain
channelsReadMediaPeriod, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read channelsReadMediaPeriod")
}
m.ChannelsReadMediaPeriod = channelsReadMediaPeriod
callReceiveTimeoutMs, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read callReceiveTimeoutMs")
}
m.CallReceiveTimeoutMs = callReceiveTimeoutMs
callRingTimeoutMs, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read callRingTimeoutMs")
}
m.CallRingTimeoutMs = callRingTimeoutMs
callConnectTimeoutMs, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read callConnectTimeoutMs")
}
m.CallConnectTimeoutMs = callConnectTimeoutMs
callPacketTimeoutMs, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read callPacketTimeoutMs")
}
m.CallPacketTimeoutMs = callPacketTimeoutMs
webFileDCID, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read webFileDCID")
}
m.WebFileDCID = webFileDCID
txtDomainString, err := r.readString()
if err != nil {
return errors.Wrap(err, "read txtDomainString")
}
m.TxtDomainString = txtDomainString
phoneCallsEnabled, err := r.readUint32()
if err != nil {
return errors.Wrap(err, "read phoneCallsEnabled")
}
m.PhoneCallsEnabled = phoneCallsEnabled == 1
blockedMode, err := r.readUint32()
if err != nil {
return errors.Wrap(err, "read blockedMode")
}
m.BlockedMode = blockedMode == 1
captionLengthMax, err := r.readInt32()
if err != nil {
return errors.Wrap(err, "read captionLengthMax")
}
m.CaptionLengthMax = captionLengthMax
return nil
}