-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
209 lines (160 loc) · 4.05 KB
/
main.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
// This package provides a ligth client for syncthing, for when you need to upload/download but don't want a full sync.
package syncthingclient
import (
"crypto/tls"
"errors"
"github.com/syncthing/syncthing/lib/protocol"
"log"
"os"
"strings"
)
type Client struct {
Remote Remote // The remote syncthing instance
Cert tls.Certificate // Your certificate
DeviceID protocol.DeviceID // Your device ID
}
// LoadCertificate loads the syncthing certificate from file
func (c *Client) LoadCertificate(certFile, keyFile string) error {
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err == nil {
c.Cert = cert
}
return err
}
// Upload uploades the "from" file to the remote "to" path specified
func (c *Client) Upload(from, to string) error {
c.Remote.connect(c.Cert)
return errors.New("Not implemented")
}
// Download retrieves the specified file and stores it in "to"
func (c *Client) Download(from, to string) error {
c.Remote.Callback.IndexRecived = make(chan bool)
root := from[:strings.Index(from, "/")]
file := from[strings.Index(from, "/")+1:]
c.Remote.connect(c.Cert)
defer c.Remote.close()
log.Println("Sending cluster config")
cc, err := c.Remote.MakeClusterConfig(root)
if err != nil {
return err
}
err = c.Remote.Send(cc)
if err != nil {
return err
}
// Wait for the callback
<-c.Remote.Callback.IndexRecived
fileIndex := -1
for i, f := range c.Remote.Index.Files {
if f.Name == file {
fileIndex = i
break
}
}
if fileIndex == -1 {
return errors.New("File not found on remote")
}
log.Printf("Downloading %v blocks", len(c.Remote.Index.Files[fileIndex].Blocks))
c.Remote.Callback.ResponseRecived = make(chan *protocol.Response)
// Create the file
f, err := os.Create(to)
if err != nil {
return err
}
var id int32 = 0
for _, b := range c.Remote.Index.Files[fileIndex].Blocks {
req, err := MakeRequst(id, root, file, b.Offset, b.Size, b.Hash)
if err != nil {
return err
}
err = c.Remote.Send(req)
if err != nil {
return err
}
// Wait for response
res := <-c.Remote.Callback.ResponseRecived
_, err = f.Write(res.Data)
if err != nil {
return err
}
id++
}
f.Close()
return nil
}
type FileInfo struct {
Name string
Size int64
Type string
}
// GetFileList retrieves a file list for the folder.
func (c *Client) GetFileList(folder string) ([]FileInfo, error) {
c.Remote.Callback.IndexRecived = make(chan bool)
c.Remote.connect(c.Cert)
defer c.Remote.close()
log.Println("Sending cluster config")
cc, err := c.Remote.MakeClusterConfig(folder)
if err != nil {
return nil, err
}
err = c.Remote.Send(cc)
if err != nil {
return nil, err
}
// Wait for the callback
<-c.Remote.Callback.IndexRecived
fileList := make([]FileInfo, len(c.Remote.Index.Files))
for i, f := range c.Remote.Index.Files {
fileList[i] = FileInfo{
Name: folder + "/" + f.Name,
Size: f.Size,
}
switch f.Type {
case 0:
fileList[i].Type = "FILE"
case 1:
fileList[i].Type = "DIRECTORY"
default:
fileList[i].Type = "SYMLINK"
}
}
// Remote the callback
c.Remote.Callback.IndexRecived = nil
return fileList, nil
}
// GetSharedFolders retrieves the folders that are shared with this client
func (c *Client) GetSharedFolders() ([]string, error) {
c.Remote.Callback.CCrecived = make(chan bool)
c.Remote.connect(c.Cert)
defer c.Remote.close()
log.Println("Sending cluster config")
cc, err := c.Remote.MakeClusterConfig("")
if err != nil {
return nil, err
}
err = c.Remote.Send(cc)
if err != nil {
return nil, err
}
// Wait for the callback
<-c.Remote.Callback.CCrecived
folders := make([]string, len(c.Remote.ClusterConfig.Folders))
for i, f := range c.Remote.ClusterConfig.Folders {
folders[i] = f.ID
}
// Remote the callback
c.Remote.Callback.CCrecived = nil
return folders, nil
}
// AddRemote adds a new remote syncthing client
func (c *Client) AddRemote(deviceID string) error {
id, err := protocol.DeviceIDFromString(deviceID)
if err != nil {
return err
}
c.Remote = Remote{
DeviceID: id,
Callback: Callback{},
}
return nil
}