-
Notifications
You must be signed in to change notification settings - Fork 2
/
registration.go
301 lines (253 loc) · 7.09 KB
/
registration.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
295
296
297
298
299
300
301
package sync
import (
"crypto/tls"
"fmt"
"log"
"os"
"path/filepath"
"reflect"
"strconv"
"time"
"github.com/google/uuid"
"github.com/quic-s/quics-client/pkg/db/badger"
"github.com/quic-s/quics-client/pkg/net/qclient"
"github.com/quic-s/quics-client/pkg/viper"
qp "github.com/quic-s/quics-protocol"
"github.com/quic-s/quics-protocol/pkg/stream"
qstypes "github.com/quic-s/quics/pkg/types"
)
var ClientPW string
//var PingCnt = 0
// @URL /api/v1/connect/server
// ex. RegisterClientRequest("password", "S_IP", "S_PORT")
func ClientRegistration(ClientPassword string, SIP string, SPort string) error {
if SIP != "" {
viper.WriteViperEnvVariables("QUICS_SERVER_IP", SIP)
}
if SPort != "" {
viper.WriteViperEnvVariables("QUICS_SERVER_PORT", SPort)
}
ClientPW = ClientPassword
// Check UUID is existed
UUID := ""
if badger.GetUUID() != "" {
UUID = badger.GetUUID()
} else {
UUID = uuid.New().String()
badger.Update("UUID", []byte(UUID))
}
InitQPClient()
var err error
Conn, err = ConnectServer(UUID)
if err != nil {
return err
}
viper.WriteViperEnvVariables("QUICS_SERVER_PASSWORD", ClientPassword)
go Rescan()
go Reconnect()
return nil
}
func CheckInternetConnection() bool {
err := Conn.OpenTransaction("PING", func(stream *stream.Stream, transactionName string, transactionID []byte) error {
res, err := qclient.SendPing(stream, badger.GetUUID())
if err != nil {
log.Println("error in transaction: ", err)
return err
}
if reflect.ValueOf(res).IsZero() {
return fmt.Errorf("ping do not success")
}
return nil
})
if err != nil {
log.Println("quics-client : [PING] ERROR ", err)
return false
}
//log.Println("PingCnt >> ", PingCnt)
//PingCnt++
return true
}
func Reconnect() {
for {
isOnline := CheckInternetConnection()
//if !isOnline || PingCnt == 80 {
if !isOnline {
//PingCnt = 0
ip := viper.GetViperEnvVariables("QUICS_SERVER_IP")
if ip == "" {
log.Println("quics-client : [RECONNECT] ERROR ", "server IP is not set")
continue
}
err := Conn.Close()
if err != nil {
log.Println("quics-client : [RECONNECT] ERROR ", err)
}
Conn = nil
err = QPClient.Close()
if err != nil {
log.Println("quics-client : [RECONNECT] ERROR ", err)
}
QPClient = nil
port := viper.GetViperEnvVariables("QUICS_SERVER_PORT")
err = ClientRegistration(ClientPW, ip, port)
if err != nil {
log.Println(err)
continue
}
return
}
time.Sleep(5 * time.Second)
}
}
func ConnectServer(uuid string) (*qp.Connection, error) {
tlsConf := &tls.Config{
InsecureSkipVerify: true,
NextProtos: []string{"quic-s"},
}
parsedPort, err := strconv.Atoi(viper.GetViperEnvVariables("QUICS_SERVER_PORT"))
if err != nil {
return nil, fmt.Errorf("[ClientRegistration] %s", err)
}
NewConn, err := QPClient.DialWithTransaction(
viper.GetViperEnvVariables("QUICS_SERVER_IP"),
parsedPort,
tlsConf,
qstypes.REGISTERCLIENT,
func(stream *qp.Stream, transactionName string, transactionID []byte) error {
clientRegisterRes, err := qclient.SendClientRegister(stream, uuid, ClientPW)
if err != nil {
return err
}
if clientRegisterRes.UUID == "" {
return fmt.Errorf("Server cannot register client")
}
return nil
})
if err != nil {
return nil, fmt.Errorf("[ClientRegistration] %s", err)
}
return NewConn, nil
}
// @URL /api/v1/connect/root/local
// ex. RegistRootDir("/home/ubuntu/rootDir", "password")
func RegistRootDir(LocalRootDir string, RootDirPW string, Side string) error {
dir, root := filepath.Split(LocalRootDir)
//In Remote Register case
_, err := os.Stat(LocalRootDir)
if os.IsNotExist(err) {
err := os.MkdirAll(LocalRootDir, 0755)
if err != nil {
return err
}
}
// Add To RootDirList
err = badger.AddRootDir(LocalRootDir)
if err != nil {
return fmt.Errorf("[RegisterRootDir] %s", err)
}
rootdir := badger.GetRootDir(LocalRootDir)
if reflect.ValueOf(rootdir).FieldByName("IsRegistered").Bool() == true {
return fmt.Errorf("[RegisterRootDir] Root Directory named ", rootdir.NickName, " is already registered")
}
// Named Transaction
transcationName := ""
if Side == "LOCAL" {
transcationName = qstypes.REGISTERROOTDIR
} else if Side == "REMOTE" {
transcationName = qstypes.SYNCROOTDIR
}
err = Conn.OpenTransaction(transcationName, func(stream *qp.Stream, transactionName string, transactionID []byte) error {
//TODO When get root folder from root then What about badger
registerRes, err := qclient.SendRootDirRegister(stream, badger.GetUUID(), RootDirPW, dir, "/"+root)
if err != nil {
return err
}
if reflect.ValueOf(registerRes).IsZero() {
return fmt.Errorf("server cannot register root directory")
}
err = badger.UpdateRootdirToRegistered(LocalRootDir)
if err != nil {
return err
}
DirWatchAdd(LocalRootDir)
return nil
})
if err != nil {
return fmt.Errorf("[RegisterRootDir] %s", err)
}
return nil
}
// @URL /api/v1/connect/list/remote
// ex. GetRemoteRootList()
func GetRemoteRootList() (qstypes.AskRootDirRes, error) {
rootList := &qstypes.AskRootDirRes{}
err := Conn.OpenTransaction(qstypes.GETROOTDIRS, func(stream *qp.Stream, transactionName string, transactionID []byte) error {
askRootDirRes, err := qclient.SendAskRootList(stream, badger.GetUUID())
if err != nil {
return err
}
rootList = askRootDirRes
return nil
})
if err != nil {
return *rootList, fmt.Errorf("[GetRemoteRootList] %s", err)
}
log.Println("rootList : ", rootList)
return *rootList, nil
}
// @URL /api/v1/disconnect/server
// ex. DisconnectClient()
func DisconnectClient() error {
err := Conn.OpenTransaction("DISCONNECTCLIENT", func(stream *stream.Stream, transactionName string, transactionID []byte) error {
res, err := qclient.SendDisconnectClient(stream, badger.GetUUID())
if err != nil {
return err
}
if reflect.ValueOf(res).IsZero() {
return fmt.Errorf("[DISCONNECTCLIENT] still registerd")
}
return nil
})
if err != nil {
return err
}
viper.DeleteViperVariablesByKey("QUICS_SERVER_IP")
viper.DeleteViperVariablesByKey("QUICS_SERVER_PORT")
viper.DeleteViperVariablesByKey("QUICS_SERVER_PASSWORD")
Conn.Close()
Conn = nil
QPClient.Close()
QPClient = nil
return nil
}
// @URL /api/v1/disconnect/root
// ex. DisConnectRootDir( "/home/ubuntu/rootDir")
func DisconnectRootDir(path string) (qstypes.DisconnectRootDirRes, error) {
_, after := badger.SplitBeforeAfterRoot(path)
result := qstypes.DisconnectRootDirRes{}
err := Conn.OpenTransaction(qstypes.DISCONNECTROOTDIR, func(stream *stream.Stream, transactionName string, transactionID []byte) error {
res, err := qclient.SendDisconnectRootDir(stream, badger.GetUUID(), after)
if err != nil {
return err
}
if reflect.ValueOf(res).IsZero() {
return fmt.Errorf("[DISCONNECTROOTDIR] still registerd")
}
result = res
DirWatchStop(path)
syncList, err := badger.GetAllSyncMetadataInRoot(path)
if err != nil {
log.Println(err)
}
for _, item := range syncList {
badger.Delete(filepath.Join(item.BeforePath, item.AfterPath))
}
badger.DeleteRootDir(path)
return nil
})
if err != nil {
log.Println("log")
return result, err
}
return result, nil
}