-
Notifications
You must be signed in to change notification settings - Fork 133
/
mtproto_utils.go
79 lines (64 loc) · 1.85 KB
/
mtproto_utils.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
// Copyright (c) 2020-2021 KHS Films
//
// This file is a part of mtproto package.
// See https://github.com/xelaj/mtproto/blob/master/LICENSE for details
package mtproto
import (
"reflect"
"github.com/pkg/errors"
"github.com/xelaj/mtproto/internal/encoding/tl"
"github.com/xelaj/mtproto/internal/session"
"github.com/xelaj/mtproto/internal/utils"
)
// helper methods
// GetSessionID returns the current session id 🧐
func (m *MTProto) GetSessionID() int64 {
return m.sessionId
}
// GetSeqNo returns seqno 🧐
func (m *MTProto) GetSeqNo() int32 {
return m.seqNo
}
// GetServerSalt returns current server salt 🧐
func (m *MTProto) GetServerSalt() int64 {
return m.serverSalt
}
// GetAuthKey returns decryption key of current session salt 🧐
func (m *MTProto) GetAuthKey() []byte {
return m.authKey
}
func (m *MTProto) SetAuthKey(key []byte) {
m.authKey = key
m.authKeyHash = utils.AuthKeyHash(m.authKey)
}
func (m *MTProto) MakeRequest(msg tl.Object) (any, error) {
return m.makeRequest(msg)
}
func (m *MTProto) MakeRequestWithHintToDecoder(msg tl.Object, expectedTypes ...reflect.Type) (any, error) {
if len(expectedTypes) == 0 {
return nil, errors.New("expected a few hints. If you don't need it, use m.MakeRequest")
}
return m.makeRequest(msg, expectedTypes...)
}
func (m *MTProto) AddCustomServerRequestHandler(handler customHandlerFunc) {
m.serverRequestHandlers = append(m.serverRequestHandlers, handler)
}
func (m *MTProto) warnError(err error) {
if m.Warnings != nil && err != nil {
m.Warnings <- err
}
}
func (m *MTProto) SaveSession() (err error) {
return m.tokensStorage.Store(&session.Session{
Key: m.authKey,
Hash: m.authKeyHash,
Salt: m.serverSalt,
Hostname: m.addr,
})
}
func (m *MTProto) LoadSession(s *session.Session) {
m.authKey = s.Key
m.authKeyHash = s.Hash
m.serverSalt = s.Salt
m.addr = s.Hostname
}