-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
208 lines (189 loc) · 5.16 KB
/
client.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
/*
* File: client.go
* Project: mrsign
* Created Date: Tuesday, October 19th 2021, 12:16:54 pm
* Authors: Marcello Russo, Fabio Zito
* -----
* Last Modified:
* Modified By:
* -----
* MIT License
*
* Copyright (c) 2021 MR&&Z
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* -----
* HISTORY:
* Date By Comments
* ---------- --- ----------------------------------------------------------
*/
package main
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"os"
)
/*
func getIps() ([]string, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
var out []string
for _, a := range addrs {
if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
if ipNet.IP.To4() != nil {
out = append(out, ipNet.IP.String())
}
}
}
return out, nil
}
*/
type Client struct {
urlChallenge string
urlRetrieve string
path string
storeFile string
serverStoreFile string
}
func NewClient(server string, path string, clientStoreFile string, serverStoreFilePath string) *Client {
if len(clientStoreFile) == 0 {
clientStoreFile = ClientStoreFile
}
return &Client{
urlChallenge: server + apiChallenge,
urlRetrieve: server + apiRetrieve,
path: path,
storeFile: path + string(os.PathSeparator) + clientStoreFile,
serverStoreFile: serverStoreFilePath + string(os.PathSeparator) + clientStoreFile,
}
}
func (c *Client) Generate(user string, _ string, hostname string) error {
reqNegotiate := NewMessageNegotiate()
reqNegotiate.UserName = user
reqNegotiate.HostName = hostname
reqNegotiate.FolderName = c.path
out := NewClientStore()
out.User = user
out.HostName = hostname
out.Path = c.path
out.ClientChallenge = reqNegotiate.ClientChallenge
if err := c.saveStore(out); err != nil {
return err
}
folderHash, err := c.createFolderHash()
if err != nil {
_ = os.Remove(c.storeFile)
return err
}
reqNegotiate.Hash = folderHash
reqNegotiateBody, err := reqNegotiate.Marshal()
if err != nil {
_ = os.Remove(c.storeFile)
return err
}
buf := bytes.NewReader(reqNegotiateBody)
resp, err := http.Post(c.urlChallenge, "application/octet-stream", buf)
if err != nil {
_ = os.Remove(c.storeFile)
return err
}
var reqChallengeBody []byte
if resp.Body != nil {
if reqChallengeBody, err = ioutil.ReadAll(resp.Body); err != nil {
_ = os.Remove(c.storeFile)
return err
}
}
if resp.StatusCode != 200 {
_ = os.Remove(c.storeFile)
if resp.StatusCode == 409 {
return errors.New("already exists")
}
return errors.New("invalid status code: " + string(reqChallengeBody))
}
return nil
}
func (c *Client) Exists() bool {
exists := false
if a, e := os.Stat(c.storeFile); e == nil {
if !a.IsDir() {
exists = true
}
}
return exists
}
func (c *Client) Restore() error {
store, err := c.loadStore()
if err != nil {
return err
}
folderHash, err := c.createFolderHash()
if err != nil {
return err
}
reqNegotiate := NewMessageNegotiate()
reqNegotiate.UserName = store.User
reqNegotiate.HostName = store.HostName
reqNegotiate.FolderName = store.Path
reqNegotiate.Hash = folderHash
reqNegotiate.ClientChallenge = store.ClientChallenge
reqNegotiateBody, err := reqNegotiate.Marshal()
if err != nil {
return err
}
buf := bytes.NewReader(reqNegotiateBody)
resp, err := http.Post(c.urlRetrieve, "application/octet-stream", buf)
if err != nil {
return err
}
var reqChallengeBody []byte
if resp.Body != nil {
if reqChallengeBody, err = ioutil.ReadAll(resp.Body); err != nil {
return err
}
}
if resp.StatusCode != 200 {
return errors.New("invalid status code: " + string(reqChallengeBody))
}
return nil
}
func (c *Client) saveStore(store *ClientStore) error {
pr, _ := json.MarshalIndent(store, "", "\t")
return ioutil.WriteFile(c.storeFile, pr, 0644)
}
func (c *Client) loadStore() (*ClientStore, error) {
body, err := ioutil.ReadFile(c.storeFile)
if err != nil {
return nil, err
}
store := NewClientStore()
err = json.Unmarshal(body, store)
if err != nil {
return nil, err
}
return store, nil
}
func (c *Client) createFolderHash() (string, error) {
return HashDir(c.path, "", []string{c.serverStoreFile}, Hash256)
}