forked from shelomentsevd/mtproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
170 lines (136 loc) · 2.92 KB
/
session.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
package mtproto
import (
"errors"
"os"
"math/rand"
"time"
)
// Session storage interface
type ISession interface {
// Load is deserialization method
Load() error
// Save is serialization method
Save() error
IsIPv6() bool
// IsEncrypted returns true if AuthKey, ServerSalt and SessionID fields aren't empty
IsEncrypted() bool
GetAddress() string
GetAuthKey() []byte
GetAuthKeyHash() []byte
GetServerSalt() []byte
GetSessionID() int64
SetAddress(string)
SetAuthKey([]byte)
SetAuthKeyHash([]byte)
SetServerSalt([]byte)
SetSessionID(int64)
UseIPv6(bool)
Encrypted(bool)
}
type Session struct {
// TODO: ReaderWriter interface
file *os.File
address string
authKey []byte
authKeyHash []byte
serverSalt []byte
sessionId int64
useIPv6 bool
encrypted bool
}
func NewSession(file *os.File) ISession {
session := &Session{
file: file,
}
rand.Seed(time.Now().UnixNano())
session.SetSessionID(rand.Int63())
return session
}
func (s *Session) Load() error {
// TODO: Magic number
buffer := make([]byte, 1024*4)
n, _ := s.file.ReadAt(buffer, 0)
if n <= 0 {
return errors.New("New session")
}
decoder := NewDecodeBuf(buffer)
s.authKey = decoder.StringBytes()
s.authKeyHash = decoder.StringBytes()
s.serverSalt = decoder.StringBytes()
s.address = decoder.String()
s.useIPv6 = false
if decoder.UInt() == 1 {
s.useIPv6 = true
}
if decoder.err != nil {
return decoder.err
}
return nil
}
func (s Session) Save() error {
// TODO: Magic number
buffer := NewEncodeBuf(1024)
buffer.StringBytes(s.authKey)
buffer.StringBytes(s.authKeyHash)
buffer.StringBytes(s.serverSalt)
buffer.String(s.address)
var useIPv6UInt uint32
if s.useIPv6 {
useIPv6UInt = 1
}
buffer.UInt(useIPv6UInt)
err := s.file.Truncate(0)
if err != nil {
return err
}
_, err = s.file.WriteAt(buffer.buf, 0)
if err != nil {
return err
}
return nil
}
func (s Session) IsIPv6() bool {
return s.useIPv6
}
func (s Session) IsEncrypted() bool {
return s.encrypted
}
func (s Session) GetAddress() string {
return s.address
}
func (s Session) GetAuthKey() []byte {
return s.authKey
}
func (s Session) GetAuthKeyHash() []byte {
return s.authKeyHash
}
func (s Session) GetServerSalt() []byte {
return s.serverSalt
}
func (s Session) GetSessionID() int64 {
return s.sessionId
}
func (s *Session) SetAddress(address string) {
s.address = address
}
func (s *Session) SetAuthKey(authKey []byte) {
s.authKey = make([]byte, len(authKey))
copy(s.authKey, authKey)
}
func (s *Session) SetAuthKeyHash(authKeyHash []byte) {
s.authKeyHash = make([]byte, len(authKeyHash))
copy(s.authKeyHash, authKeyHash)
}
func (s *Session) SetServerSalt(salt []byte) {
s.serverSalt = make([]byte, len(salt))
copy(s.serverSalt, salt)
}
func (s *Session) SetSessionID(ID int64) {
s.sessionId = ID
}
func (s *Session) UseIPv6(useIPv6 bool) {
s.useIPv6 = useIPv6
}
func (s *Session) Encrypted(encrypted bool) {
s.encrypted = encrypted
}