-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.go
206 lines (182 loc) · 4.56 KB
/
group.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
package controller
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"
"github.com/Qingluan/FrameUtils/utils"
"github.com/pkg/sftp"
)
type Group struct {
Members []string
}
func (chat *ChatRoom) IsGroupName(name string) (string, bool) {
if strings.HasSuffix(name, GROUP_TAIL) {
return strings.TrimSuffix(name, GROUP_TAIL), true
} else if strings.HasSuffix(name, "_GroUP") {
return strings.TrimSuffix(name, "_GroUP"), true
}
return name, false
}
func (vps *Vps) GroupCheck(name string) (exists, verified bool) {
err := vps.WithSftp(func(client *sftp.Client) error {
groupDir := Join(ROOT, vps.GetGroupVpsName(name))
if f, err := client.Stat(groupDir); err != nil {
return err
} else {
if !f.IsDir() {
return nil
}
exists = true
}
gkeys := Join(groupDir, MSG_PRIVATE_KEY)
vps.WithSftpRead(gkeys, os.O_RDONLY|os.O_CREATE, func(fp io.ReadCloser) error {
buf, err := ioutil.ReadAll(fp)
if err != nil {
return err
}
for _, hmac := range strings.Split(string(buf), "\n") {
if strings.Contains(hmac, ":") {
fs := strings.SplitN(hmac, ":", 2)
kname := strings.TrimSpace(fs[0])
if strings.HasSuffix(kname, GROUP_TAIL) {
kname = vps.D(kname)
}
if ToMd5(GetGroupKey(kname)) == strings.TrimSpace(fs[1]) {
verified = true
break
}
}
}
return nil
})
return nil
})
if err != nil {
return
}
return
}
func (vps *Vps) CreateGroup(name string) {
rawName := name
name = vps.E(name)
newgroupKey := NewKey()
var err error
// if vps.session == nil {
_, session, err := vps.Connect()
// }
if err == nil {
fmt.Println(utils.Green("creating group:", rawName))
}
dpath := Join(ROOT, MSG_TMP_FILE)
name += GROUP_TAIL
nghome := Join(ROOT, name)
inits := fmt.Sprintf("mkdir -p %s ;mkdir -p %s && mkdir -p %s/%s && echo \"%s\" > %s/%s",
dpath,
nghome,
nghome,
MSG_FILE_ROOT,
vps.client.LocalAddr().String(),
nghome,
MSG_HELLO,
)
err = session.Run(inits)
if err != nil {
log.Println("create group base dir err:", err)
return
}
k := Join(nghome, "keys")
err = vps.WithSftpWrite(k, os.O_CREATE|os.O_TRUNC|os.O_RDWR, func(fp io.WriteCloser) error {
fp.Write([]byte(name + ":" + ToMd5(newgroupKey)))
return nil
})
if err != nil {
log.Println("create group err:", err)
return
}
SetGroupKey(rawName, newgroupKey)
}
func (vps *Vps) JoinGroup(name string) {
name = vps.GetGroupVpsName(name)
exist, verified := vps.GroupCheck(name)
if exist && !verified {
// vps.SendMsg()
vps.SendGroupMsg(name, "$want-join")
} else {
log.Println("exist:", exist, "verify:", verified)
}
}
func (vps *Vps) E(raw string) string {
return vps.steam.FlowEn(raw)
}
func (vps *Vps) D(en string) string {
if strings.HasSuffix(en, GROUP_TAIL) {
fs := strings.SplitN(en, GROUP_TAIL, 2)
en := fs[0]
return vps.steam.FlowDe(en) + GROUP_TAIL
}
return vps.steam.FlowDe(en)
}
func (vps *Vps) GetGroupVpsName(name string) string {
if strings.HasSuffix(name, GROUP_TAIL) {
return name
}
// if strings.HasSuffix(name, "_GroUP") {
name = strings.TrimSuffix(name, "_GroUP")
// }
n := vps.E(name)
return n + GROUP_TAIL
}
func (vps *Vps) GetGroupName(name string) string {
n := vps.D(name)
return strings.TrimSuffix(n, GROUP_TAIL)
}
func (vps *Vps) AllowJoinGroup(gname, uname string) {
key := GetGroupKey(gname)
gname = vps.GetGroupVpsName(gname)
uname = vps.E(uname)
if key != "" {
vps.SendKeyTo(uname, key, gname)
// vps.SendGroupMsg(msg.Group, "$permitted-"+key)
}
}
func (vps *Vps) GroupList() (groups []string) {
for _, h := range LocalGroupKeys() {
if strings.Contains(h, ":") {
n := strings.TrimSpace(strings.SplitN(h, ":", 2)[0])
n = strings.TrimSuffix(n, GROUP_TAIL)
n = vps.E(strings.TrimSuffix(n, "_GroUP")) + GROUP_TAIL
groups = append(groups, n)
}
}
return
}
func (chat *ChatRoom) CreateGroup(name string) {
chat.vps.CreateGroup(name)
}
func (vps *Vps) RemoveGroup(name string) {
encryptedname := vps.GetGroupVpsName(name)
// fmt.Println("remove vps:", name, encryptedname, vps.GetGroupVpsName(encryptedname))
exists, verify := vps.GroupCheck(encryptedname)
if exists && verify {
err := vps.TimerClear(3, encryptedname)
fmt.Println("remove vps:", name, vps.GetGroupVpsName(encryptedname))
if err != nil {
log.Println(err)
}
} else {
log.Println("you have no permitted to remove ")
}
// vps.Close()
}
func (chat *ChatRoom) RemoveGroup(name string) {
chat.vps.RemoveGroup(name)
}
func (chat *ChatRoom) JoinGroup(name string) {
chat.vps.JoinGroup(name)
}
func (chat *ChatRoom) Permitt(gname, uname string) {
chat.vps.AllowJoinGroup(gname, uname)
}