-
-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathfile.go
More file actions
executable file
·222 lines (182 loc) · 4.78 KB
/
Copy pathfile.go
File metadata and controls
executable file
·222 lines (182 loc) · 4.78 KB
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
// Copyright (c) 2025 @AmarnathCJD
package session
import (
"encoding/base64"
"encoding/binary"
"encoding/json"
"fmt"
"os"
"path/filepath"
"sync"
"time"
"errors"
aes "github.com/amarnathcjd/gogram/internal/aes_ige"
"github.com/amarnathcjd/gogram/internal/encoding/tl"
)
// defaultAESKey is used when no AuthAESKey is supplied. The session file
// stays obfuscated, not encrypted, when this key is used — anyone with
// the file can decrypt it. Set Config.AuthAESKey to a random 16+ char
// secret for real protection.
const defaultAESKey = "1234567890123456"
type genericFileSessionLoader struct {
path string
lastEdited time.Time
cached *Session
key string
}
var _ SessionLoader = (*genericFileSessionLoader)(nil)
func NewFromFile(path string, authAesKey string) SessionLoader {
if authAesKey == "" {
authAesKey = defaultAESKey
}
return &genericFileSessionLoader{path: path, key: authAesKey}
}
func (l *genericFileSessionLoader) Path() string {
return l.path
}
func (l *genericFileSessionLoader) Key() string {
return l.key
}
func (l *genericFileSessionLoader) Exists() bool {
_, err := os.Stat(l.path)
return err == nil
}
func (l *genericFileSessionLoader) Load() (*Session, error) {
info, err := os.Stat(l.path)
switch {
case err == nil:
case errors.Is(err, os.ErrNotExist):
return nil, fmt.Errorf("file not found: %w", err)
default:
return nil, err
}
if info.ModTime().Equal(l.lastEdited) && l.cached != nil {
return l.cached, nil
}
data, err := os.ReadFile(l.path)
if err != nil {
return nil, fmt.Errorf("reading file: %w", err)
}
data, err = decodeBytes(data, l.key)
if err != nil {
return nil, fmt.Errorf("decrypting session: %w", err)
}
file := new(tokenStorageFormat)
err = json.Unmarshal(data, file)
if err != nil {
return nil, fmt.Errorf("parsing file: %w", err)
}
s, err := file.readSession()
if err != nil {
return nil, err
}
l.cached = s
l.lastEdited = info.ModTime()
return s, nil
}
func (l *genericFileSessionLoader) Store(s *Session) error {
dir, _ := filepath.Split(l.path)
if dir != "" {
if err := os.MkdirAll(dir, 0700); err != nil {
return fmt.Errorf("creating session directory: %w", err)
}
}
file := new(tokenStorageFormat)
file.writeSession(s)
data, _ := json.Marshal(file)
encrypted, err := encodeBytes(data, l.key)
if err != nil {
return fmt.Errorf("encrypting session: %w", err)
}
return os.WriteFile(l.path, encrypted, 0600)
}
func (l *genericFileSessionLoader) Delete() error {
return os.Remove(l.path)
}
type tokenStorageFormat struct {
Key string `json:"key"`
Hash string `json:"hash"`
Salt string `json:"salt"`
Hostname string `json:"hostname"`
AppID int32 `json:"app_id"`
}
func (t *tokenStorageFormat) writeSession(s *Session) {
t.Key = base64.StdEncoding.EncodeToString(s.Key)
t.Hash = base64.StdEncoding.EncodeToString(s.Hash)
t.Salt = encodeInt64ToBase64(s.Salt)
t.Hostname = s.Hostname
t.AppID = s.AppID
}
func (t *tokenStorageFormat) readSession() (*Session, error) {
s := new(Session)
var err error
s.Key, err = base64.StdEncoding.DecodeString(t.Key)
if err != nil {
return nil, fmt.Errorf("invalid binary data of 'key': %w", err)
}
s.Hash, err = base64.StdEncoding.DecodeString(t.Hash)
if err != nil {
return nil, fmt.Errorf("invalid binary data of 'hash': %w", err)
}
s.Salt, err = decodeInt64ToBase64(t.Salt)
if err != nil {
return nil, fmt.Errorf("invalid binary data of 'salt': %w", err)
}
s.Hostname = t.Hostname
s.AppID = t.AppID
return s, nil
}
func encodeInt64ToBase64(i int64) string {
buf := make([]byte, tl.LongLen)
binary.LittleEndian.PutUint64(buf, uint64(i))
return base64.StdEncoding.EncodeToString(buf)
}
func decodeInt64ToBase64(i string) (int64, error) {
buf, err := base64.StdEncoding.DecodeString(i)
if err != nil {
return 0, err
}
return int64(binary.LittleEndian.Uint64(buf)), nil
}
func encodeBytes(b []byte, key string) ([]byte, error) {
return aes.EncryptAES(b, key)
}
func decodeBytes(b []byte, key string) ([]byte, error) {
return aes.DecryptAES(b, key)
}
func NewInMemory() SessionLoader {
return &inMemorySessionLoader{}
}
type inMemorySessionLoader struct {
mu sync.RWMutex
s *Session
}
var _ SessionLoader = (*inMemorySessionLoader)(nil)
func (l *inMemorySessionLoader) Path() string {
return ":memory:"
}
func (l *inMemorySessionLoader) Key() string {
return "in-memory"
}
func (l *inMemorySessionLoader) Exists() bool {
l.mu.RLock()
defer l.mu.RUnlock()
return l.s != nil
}
func (l *inMemorySessionLoader) Load() (*Session, error) {
l.mu.RLock()
defer l.mu.RUnlock()
return l.s, nil
}
func (l *inMemorySessionLoader) Store(s *Session) error {
l.mu.Lock()
defer l.mu.Unlock()
l.s = s
return nil
}
func (l *inMemorySessionLoader) Delete() error {
l.mu.Lock()
defer l.mu.Unlock()
l.s = nil
return nil
}