-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcredentials.go
138 lines (126 loc) · 2.86 KB
/
credentials.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
package mtproto
import (
"encoding/base64"
"encoding/json"
"errors"
"os"
)
type Credentials struct {
Phone string
ApiID int32
ApiHash string
IP string
Port int
Salt []byte
AuthKey []byte
AuthKeyHash []byte
}
type credentialsJSON struct {
Phone string `json:"phone"`
ApiID int32 `json:"api_id"`
ApiHash string `json:"api_hash"`
IP string `json:"ip"`
Port int `json:"port"`
Salt string `json:"salt"`
AuthKey string `json:"auth_key"`
}
// SaveToFile stores the Credentials as a JSON file in the format of credentialsJson.
// It creates the file, if not exists, and overwrite the file, if exists.
// JSON turns the given credentials into JSON binary in the format of credentialsJSON
func (c *Credentials) JSON() ([]byte, error) {
return json.Marshal(credentialsJSON{
c.Phone,
c.ApiID,
c.ApiHash,
c.IP,
c.Port,
//string(c.Salt),
base64.StdEncoding.EncodeToString(c.Salt),
base64.StdEncoding.EncodeToString(c.AuthKey),
})
}
func NewCredentials(jsonInBytes []byte) (c *Credentials, err error) {
unmarshaled := &credentialsJSON{}
err = json.Unmarshal(jsonInBytes, unmarshaled)
if err != nil {
return
}
var salt, authKey, authKeyHash []byte
salt, err = base64.StdEncoding.DecodeString(unmarshaled.Salt)
if err != nil {
return
}
authKey, err = base64.StdEncoding.DecodeString(unmarshaled.AuthKey)
if err != nil {
return
}
authKeyHash = sha1(authKey)[12:20]
c = &Credentials{
unmarshaled.Phone,
unmarshaled.ApiID,
unmarshaled.ApiHash,
unmarshaled.IP,
unmarshaled.Port,
//[]byte(unmarshaled.Salt),
salt,
authKey,
authKeyHash,
}
return
}
// Save session
//TODO: save channel and datacenter information
func (c *Credentials) Save(f *os.File) (err error) {
/*session.encrypted = true
b := NewEncodeBuf(1024)
b.StringBytes(session.authKey)
b.StringBytes(session.authKeyHash)
b.StringBytes(session.serverSalt)
b.String(session.addr)
var useIPv6UInt uint32
if session.useIPv6 {
useIPv6UInt = 1
}
b.UInt(useIPv6UInt)
*/
var jsonBytes []byte
jsonBytes, err = c.JSON()
if err != nil {
return err
}
err = f.Truncate(0)
if err != nil {
return err
}
_, err = f.WriteAt(jsonBytes, 0)
if err != nil {
return err
}
return nil
}
func NewCredentialsFromFile(f *os.File) (*Credentials, error) {
// Decode session file
b := make([]byte, 1024*4)
n, err := f.ReadAt(b, 0)
if n <= 0 || (err != nil && err.Error() != "EOF") {
return nil, errors.New("nothing in the file")
}
return NewCredentials(b[:n])
//d := NewDecodeBuf(b)
//session.authKey = d.StringBytes()
//session.authKeyHash = d.StringBytes()
//session.serverSalt = d.StringBytes()
//session.addr = d.String()
//session.useIPv6 = false
//if d.UInt() == 1 {
// session.useIPv6 = true
//}
//
//if d.err != nil {
// // Failed to load session
// return d.err
//}
//
//session.encrypted = true
//return nil
}