-
Notifications
You must be signed in to change notification settings - Fork 14
/
editor.go
147 lines (122 loc) 路 3.21 KB
/
editor.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
package config
import (
"os"
"sync"
"github.com/number571/go-peer/pkg/crypto/asymmetric"
"github.com/number571/go-peer/pkg/encoding"
"github.com/number571/go-peer/pkg/utils"
)
var (
_ IEditor = &sEditor{}
)
type sEditor struct {
fMutex sync.Mutex
fConfig *SConfig
}
func newEditor(pCfg IConfig) IEditor {
if pCfg == nil {
panic("cfg = nil")
}
v, ok := pCfg.(*SConfig)
if !ok {
panic("cfg is invalid")
}
return &sEditor{
fConfig: v,
}
}
func (p *sEditor) UpdateNetworkKey(pNetworkKey string) error {
p.fMutex.Lock()
defer p.fMutex.Unlock()
filepath := p.fConfig.fFilepath
icfg, err := LoadConfig(filepath)
if err != nil {
return utils.MergeErrors(ErrLoadConfig, err)
}
cfg := icfg.(*SConfig)
cfg.FSettings.FNetworkKey = pNetworkKey
if err := os.WriteFile(filepath, encoding.SerializeYAML(cfg), 0o600); err != nil {
return utils.MergeErrors(ErrWriteConfig, err)
}
p.fConfig.FSettings.fMutex.Lock()
defer p.fConfig.FSettings.fMutex.Unlock()
p.fConfig.FSettings.FNetworkKey = cfg.FSettings.FNetworkKey
return nil
}
func (p *sEditor) UpdateConnections(pConns []string) error {
p.fMutex.Lock()
defer p.fMutex.Unlock()
filepath := p.fConfig.fFilepath
icfg, err := LoadConfig(filepath)
if err != nil {
return utils.MergeErrors(ErrLoadConfig, err)
}
cfg := icfg.(*SConfig)
cfg.FConnections = deleteDuplicateStrings(pConns)
if err := os.WriteFile(filepath, encoding.SerializeYAML(cfg), 0o600); err != nil {
return utils.MergeErrors(ErrWriteConfig, err)
}
p.fConfig.fMutex.Lock()
defer p.fConfig.fMutex.Unlock()
p.fConfig.FConnections = cfg.FConnections
return nil
}
func (p *sEditor) UpdateFriends(pFriends map[string]asymmetric.IPubKey) error {
p.fMutex.Lock()
defer p.fMutex.Unlock()
for _, pubKey := range pFriends {
if pubKey.GetSize() == p.fConfig.GetSettings().GetKeySizeBits() {
continue
}
return ErrNotSupportedKeySize
}
filepath := p.fConfig.fFilepath
icfg, err := LoadConfig(filepath)
if err != nil {
return utils.MergeErrors(ErrLoadConfig, err)
}
if hasDuplicatePubKeys(pFriends) {
return ErrDuplicatePublicKey
}
cfg := icfg.(*SConfig)
cfg.fFriends = pFriends
cfg.FFriends = pubKeysToStrings(pFriends)
if err := os.WriteFile(filepath, encoding.SerializeYAML(cfg), 0o600); err != nil {
return utils.MergeErrors(ErrWriteConfig, err)
}
p.fConfig.fMutex.Lock()
defer p.fConfig.fMutex.Unlock()
p.fConfig.fFriends = cfg.fFriends
p.fConfig.FFriends = cfg.FFriends
return nil
}
func pubKeysToStrings(pPubKeys map[string]asymmetric.IPubKey) map[string]string {
result := make(map[string]string, len(pPubKeys))
for name, pubKey := range pPubKeys {
result[name] = pubKey.ToString()
}
return result
}
func hasDuplicatePubKeys(pPubKeys map[string]asymmetric.IPubKey) bool {
mapping := make(map[string]struct{})
for _, pubKey := range pPubKeys {
pubStr := pubKey.GetHasher().ToString()
if _, ok := mapping[pubStr]; ok {
return true
}
mapping[pubStr] = struct{}{}
}
return false
}
func deleteDuplicateStrings(pStrs []string) []string {
result := make([]string, 0, len(pStrs))
mapping := make(map[string]struct{})
for _, s := range pStrs {
if _, ok := mapping[s]; ok {
continue
}
mapping[s] = struct{}{}
result = append(result, s)
}
return result
}