forked from nocalhost/nocalhost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncthing.go
360 lines (293 loc) · 9.74 KB
/
syncthing.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
* This source code is licensed under the Apache License Version 2.0.
*/
// syncthing module: thanks to okteto given our inspired
package syncthing
import (
"bytes"
"context"
"fmt"
_const "github.com/lsutils/nocalhost/internal/nhctl/const"
"github.com/pkg/errors"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"text/template"
ps "github.com/mitchellh/go-ps"
"github.com/lsutils/nocalhost/internal/nhctl/syncthing/local"
"github.com/lsutils/nocalhost/internal/nhctl/syncthing/terminate"
"github.com/lsutils/nocalhost/pkg/nhctl/log"
)
var (
ignoredFileTemplate = template.Must(template.New("ignoredFileTemplate").Parse(local.IgnoredFileTemplate))
)
const (
SyncSecretName = "nocalhost-syncthing-secret"
syncthing = "syncthing"
RemoteHome = "/var/syncthing/config"
Bind = "127.0.0.1"
Nocalhost = "nocalhost"
certFile = "cert.pem"
keyFile = "key.pem"
configFile = "config.xml"
LogFile = "syncthing.log"
IgnoredFIle = ".nhignore"
syncthingPidFile = "syncthing.pid"
// Use to access syncthing API
DefaultAPIKey = "nocalhost"
// DefaultRemoteDeviceID remote syncthing ID
DefaultRemoteDeviceID = "MDPJNTF-OSPJC65-LZNCQGD-3AWRUW6-BYJULSS-GOCA2TU-5DWWBNC-TKM4VQ5"
localDeviceID = "SJTYMUE-DI3REKX-JCLCRXU-F6UJHCG-XQGHAZJ-5O5D3JR-LALGSBC-TJ4I4QO"
// DefaultFileWatcherDelay how much to wait before starting a sync after a file change
DefaultFileWatcherDelay = 5
// may result bug due to syncthing config changing
DefaultFolderName = "nh-1"
// ClusterPort is the port used by syncthing in the cluster
ClusterPort = 22000
// GUIPort is the port used by syncthing in the cluster for the http endpoint
GUIPort = 8384
EnableParseFromGitIgnore = "#enableParseFromGitIgnore"
DisableParseFromGitIgnore = "#disableParseFromGitIgnore"
)
// Folder represents a sync folder
type Folder struct {
Name string `yaml:"name"`
LocalPath string `yaml:"localPath"`
RemotePath string `yaml:"remotePath"`
Retries int `yaml:"-"`
SentStIgnore bool `yaml:"-"`
}
// Ignores represents the .stignore file
type Ignores struct {
Ignore []string `json:"ignore"`
}
type Syncthing struct {
APIKey string `yaml:"apikey"`
GUIPassword string `yaml:"password"`
GUIPasswordHash string `yaml:"-"`
BinPath string `yaml:"-"`
Client *http.Client `yaml:"-"`
cmd *exec.Cmd `yaml:"-"`
Folders []*Folder `yaml:"folders"`
FileWatcherDelay int `yaml:"-"`
ForceSendOnly bool `yaml:"-"`
GUIAddress string `yaml:"local"`
LocalHome string `yaml:"-"`
RemoteHome string `yaml:"-"`
LogPath string `yaml:"-"`
ListenAddress string `yaml:"-"`
RemoteAddress string `yaml:"-"`
RemoteDeviceID string `yaml:"-"`
RemoteGUIAddress string `yaml:"remote"`
RemoteGUIPort int `yaml:"-"`
RemotePort int `yaml:"-"`
LocalGUIPort int `yaml:"-"`
LocalPort int `yaml:"-"`
Type string `yaml:"-"`
IgnoreDelete bool `yaml:"-"`
PortForwardBackGroundPid int `yaml:"-"`
SyncthingBackGroundPid int `yaml:"-"`
pid int `yaml:"-"`
RescanInterval string `yaml:"-"`
// resolve ignore/sync from pattern
SyncedPattern []string `yaml:"-"`
IgnoredPattern []string `yaml:"-"`
// resolve ignore/sync from gitignore
EnableParseFromGitIgnore bool `yaml:"-"`
}
// IsSubPathFolder checks if a sync folder is a subpath of another sync folder
func IsSubPathFolder(path string, paths []string) (bool, error) {
found := false
for _, sync := range paths {
rel, err := filepath.Rel(sync, path)
if err != nil {
log.Debugf("error making rel '%s' and '%s'", sync, path)
return false, errors.Wrap(err, "")
}
if strings.HasPrefix(rel, "..") {
continue
}
found = true
if rel != "." {
return true, nil
}
}
if found {
return false, nil
}
return false, errors.New("not found")
}
func cleanupDaemon(pid int, wait bool) error {
process, err := ps.FindProcess(pid)
if process == nil && err == nil {
return nil
}
if err != nil {
return errors.Wrap(err, "")
}
if process.Executable() != GetBinaryName() {
log.Infof("%d is not a syncthing process", pid)
return nil
}
if err = terminate.Terminate(pid, wait); err == nil {
log.Debugf("terminated syncthing with pid %d", pid)
}
return err
}
func (s *Syncthing) initConfig() error {
if err := os.MkdirAll(s.LocalHome, 0700); err != nil {
return fmt.Errorf("failed to create %s: %s", s.LocalHome, err)
}
if err := s.UpdateConfig(); err != nil {
return err
}
if err := ioutil.WriteFile(filepath.Join(s.LocalHome, certFile), local.Cert, 0700); err != nil {
return fmt.Errorf("failed to write syncthing certificate: %w", err)
}
if err := ioutil.WriteFile(filepath.Join(s.LocalHome, keyFile), local.Key, 0700); err != nil {
return fmt.Errorf("failed to write syncthing key: %w", err)
}
return nil
}
// UpdateConfig updates the syncthing config file
func (s *Syncthing) UpdateConfig() error {
bs, err := s.GetLocalConfigXML()
if err != nil {
return err
}
if err := ioutil.WriteFile(filepath.Join(s.LocalHome, configFile), bs, 0700); err != nil {
return fmt.Errorf("failed to write syncthing configuration file: %w", err)
}
return nil
}
// Generate s.LocalHome/.nhignore by file sync option
func (s *Syncthing) generateIgnoredFileConfig() (string, error) {
var ignoreFilePath = filepath.Join(s.LocalHome, IgnoredFIle)
var syncedPatternAdaption = make([]string, len(s.SyncedPattern))
var enableParseFromGitIgnore = DisableParseFromGitIgnore
for i, synced := range s.SyncedPattern {
var afterAdapt = synced
// previews version support such this syntax
if synced == "." {
afterAdapt = "**"
}
if strings.Index(synced, "./") == 0 {
afterAdapt = synced[1:]
}
syncedPatternAdaption[i] = "!" + afterAdapt
}
var ignoredPatternAdaption = make([]string, len(s.IgnoredPattern))
for i, ignored := range s.IgnoredPattern {
var afterAdapt = ignored
// previews version support such this syntax
if ignored == "." {
afterAdapt = "**"
}
if strings.Index(ignored, "./") == 0 {
afterAdapt = ignored[1:]
}
ignoredPatternAdaption[i] = afterAdapt
}
if len(syncedPatternAdaption) == 0 {
syncedPatternAdaption = []string{"!**"}
}
ignoredPattern := ""
syncedPattern := ""
if s.EnableParseFromGitIgnore {
log.Infof("\n Enable parsing file's ignore/sync from git ignore\n")
enableParseFromGitIgnore = EnableParseFromGitIgnore
} else {
ignoredPattern = strings.Join(ignoredPatternAdaption, "\n")
syncedPattern = strings.Join(syncedPatternAdaption, "\n")
log.Infof("IgnoredPattern: \n" + ignoredPattern)
log.Infof("SyncedPattern: \n" + syncedPattern)
}
var values = map[string]string{
"enableParseFromGitIgnore": enableParseFromGitIgnore,
"ignoredPattern": ignoredPattern,
"syncedPattern": syncedPattern,
}
buf := new(bytes.Buffer)
if err := ignoredFileTemplate.Execute(buf, values); err != nil {
return "", fmt.Errorf("failed to write .nhignore configuration template: %w", err)
}
if err := ioutil.WriteFile(ignoreFilePath, buf.Bytes(), _const.DefaultNewFilePermission); err != nil {
return "", fmt.Errorf("failed to generate .nhignore configuration: %w", err)
}
return ignoreFilePath, nil
}
// Run local syncthing server
func (s *Syncthing) Run(ctx context.Context) error {
if err := s.initConfig(); err != nil {
log.Warnf("fail to init syncthing: %s", err.Error())
return err
}
ignoreFilePath, err := s.generateIgnoredFileConfig()
if err != nil {
log.Warnf("fail to generate ignore file config: %s", err.Error())
ignoreFilePath = ""
}
pidPath := filepath.Join(s.LocalHome, syncthingPidFile)
cmdArgs := []string{
"serve",
"--home", s.LocalHome,
"--no-browser",
"--verbose",
"--logfile", s.LogPath,
"--log-max-old-files=0",
"--ignore-file-path=" + ignoreFilePath,
}
log.Debug(cmdArgs)
s.cmd = exec.Command(s.BinPath, cmdArgs...) //nolint: gas, gosec
s.cmd.Env = append(os.Environ(), "STNOUPGRADE=1")
if err := s.cmd.Start(); err != nil {
return fmt.Errorf("failed to start syncthing: %w", err)
}
if s.cmd.Process == nil {
return nil
}
if err := ioutil.WriteFile(pidPath, []byte(strconv.Itoa(s.cmd.Process.Pid)), 0600); err != nil {
return fmt.Errorf("failed to write syncthing pid file: %w", err)
}
s.pid = s.cmd.Process.Pid
log.Debugf("local syncthing pid-%d running", s.pid)
return nil
}
// Stop syncthing background process
func Stop(pid int, force bool) error {
if err := cleanupDaemon(pid, force); err != nil {
return err
}
return nil
}
func (s *Syncthing) GetRemoteConfigXML() ([]byte, error) {
configTemplate := template.Must(template.New("syncthingConfig").Parse(local.RemoteSyncConfigXML))
buf := new(bytes.Buffer)
if err := configTemplate.Execute(buf, s); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (s *Syncthing) GetLocalConfigXML() ([]byte, error) {
configTemplate := template.Must(template.New("syncthingConfig").Parse(local.LocalSyncConfigXML))
buf := new(bytes.Buffer)
if err := configTemplate.Execute(buf, s); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func getPID(pidPath string) (int, error) {
if _, err := os.Stat(pidPath); err != nil {
return 0, err
}
content, err := ioutil.ReadFile(pidPath) // nolint: gosec
if err != nil {
return 0, err
}
return strconv.Atoi(string(content))
}