-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
294 lines (243 loc) · 6.1 KB
/
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
// Copyright 2021-22 Kirill Scherba <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Teonet config module
package teonet
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path"
"strings"
"sync"
"github.com/teonet-go/tru"
)
var nMODULEconf = "Config"
const (
ConfigDir = "teonet"
configFile = "teonet.conf"
configBufferSize = 2048
)
// config teonet config struct and methods receiver
type config struct {
TrudpPrivateKeyData []byte `json:"trudp_private_key"`
PrivateKeyData KeyData `json:"private_key"`
ServerPublicKeyData []byte `json:"server_key"`
Address string `json:"address"`
trudpPrivateKey *rsa.PrivateKey `json:"-"`
appName string `json:"-"`
osConfigDir string `json:"-"`
m *sync.RWMutex `json:"-"`
}
type KeyData []byte
func (k KeyData) String() string {
return base64.StdEncoding.EncodeToString(k)
}
// OsConfigDir used in teonet.New parameter to define os config directory
type OsConfigDir string
// newConfig create new config holder
func (teo *Teonet) newConfig(appName string, osConfigDir string) (err error) {
teo.config = &config{appName: appName, osConfigDir: osConfigDir}
teo.config.m = new(sync.RWMutex)
// Check config file exists and create and save new config if does not exists
if !teo.config.exists() {
log.Error.Println(nMODULEconf, err)
err = teo.config.create()
if err != nil {
return
}
log.Error.Println(nMODULEconf, "new keys and config file created")
}
// Read config file
err = teo.config.read()
return
}
// marshal config
func (c config) marshal() (data []byte, err error) {
data, err = json.MarshalIndent(c, "", " ")
if err != nil {
return
}
return
}
// unmarshal config
func (c *config) unmarshal(data []byte) error {
return json.Unmarshal(data, c)
}
// file return file name or error
func (c config) file() (res string, err error) {
return c.configFile(c.appName, configFile)
}
// configFile return config file full name (path + name)
// TODO: if os.UserConfigDir() return err - do something right
func (c config) configFile(appName string, file string) (res string, err error) {
if len(c.osConfigDir) > 0 {
res = c.osConfigDir
} else {
res, err = os.UserConfigDir()
if err != nil {
return
}
}
res += "/" + ConfigDir + "/" + appName + "/" + file
return
}
// ConfigFile return full path to config file
func (teo Teonet) ConfigFile(appName string, file string) (res string, err error) {
return teo.config.configFile(appName, file)
}
// save config to file
func (c config) save() (err error) {
file, err := c.file()
if err != nil {
return
}
f, err := os.Create(file)
if err != nil {
return
}
data, err := c.marshal()
if err != nil {
return
}
_, err = f.Write(data)
return
}
// exists return true if config file exists
func (c config) exists() bool {
file, err := c.file()
if err != nil {
return false
}
_, err = os.Stat(file)
return err == nil
}
// read config file and parse private keys
func (c *config) read() (err error) {
// Get file name
file, err := c.file()
if err != nil {
return
}
// Open file
f, err := os.Open(file)
if err != nil {
return
}
// Read file data
data := make([]byte, configBufferSize)
n, err := f.Read(data)
if err != nil {
return
}
if n == configBufferSize {
err = errors.New("too small read buffer")
return
}
// Unmarshal config data
err = c.unmarshal(data[:n])
if err != nil {
return
}
// Parse trudp private key
c.trudpPrivateKey, err = x509.ParsePKCS1PrivateKey(c.TrudpPrivateKeyData)
if err != nil {
return
}
// Get teonet address
c.Address, err = c.makeAddress(c.PrivateKeyData)
if err != nil {
return
}
return
}
// create new config with new private keys and save it to config folder
func (c *config) create() (err error) {
file, err := c.file()
if err != nil {
return
}
err = os.MkdirAll(path.Dir(file), os.ModePerm)
if err != nil {
return
}
// Create config keys
err = c.createKeys()
if err != nil {
return
}
return c.save()
}
// createKeys create new trudp and teonet private keys
func (c *config) createKeys() (err error) {
// Create trudp rsa private key
c.trudpPrivateKey, err = tru.GeneratePrivateKey()
if err != nil {
return
}
c.TrudpPrivateKeyData = x509.MarshalPKCS1PrivateKey(c.trudpPrivateKey)
// Create teonet (address) private key
c.PrivateKeyData = c.generatePrivateKey()
fmt.Printf("new private key hex: %s\n", c.PrivateKeyData)
return
}
// generatePrivateKey create new teonet private key
func (c config) generatePrivateKey() (key []byte) {
buf := make([]byte, 512)
io.ReadFull(rand.Reader, buf)
h := sha256.New()
h.Write(buf)
key = h.Sum(nil)
return
}
// getPublicKey get teonet public key from private key
func (c config) getPublicKey() (key []byte) {
h := sha256.New()
h.Write(c.PrivateKeyData)
key = h.Sum(nil)
return
}
// GetPrivateKey get teonet private key
func (teo Teonet) GetPrivateKey() (key KeyData) {
return teo.config.PrivateKeyData
}
// GetPublicKey get teonet public key from private key
func (t Teonet) GetPublicKey() []byte {
return t.config.getPublicKey()
}
// makeAddress get teonet address from private key
func (c config) makeAddress(keyData []byte) (addr string, err error) {
const addrLen = 35
var escaper = strings.NewReplacer("+", "", "/", "", "=", "")
addr = base64.StdEncoding.EncodeToString(keyData)
addr = escaper.Replace(addr)
if len(addr) < addrLen {
err = errors.New("too low address len")
return
}
addr = addr[:addrLen]
return
}
// Address get teonet address
func (t Teonet) Address() (addr string) {
t.config.m.RLock()
defer t.config.m.RUnlock()
return t.config.Address
}
// setAddress set teonet address
func (t Teonet) setAddress(addr string) {
t.config.m.Lock()
defer t.config.m.Unlock()
t.config.Address = addr
}
// MakeAddress make teonet address from key
func (t Teonet) MakeAddress(keyData []byte) (addr string, err error) {
return t.config.makeAddress(keyData)
}