├── .gitignore
├── .gobuilder.yml
├── LICENSE
├── README.md
├── cmd
└── syncthingfuse
│ ├── addresslister.go
│ ├── config.go
│ ├── fuse.go
│ ├── gui.go
│ ├── gui_test.go
│ ├── locations.go
│ ├── main.go
│ ├── random.go
│ ├── tls.go
│ └── usage.go
├── gui
├── css
│ └── icon-addon.css
├── index.html
├── js
│ ├── app.js
│ ├── core
│ │ ├── core.js
│ │ └── module.js
│ ├── device
│ │ ├── editDeviceModalDirective.js
│ │ ├── editDeviceModalView.html
│ │ ├── editSettingsModalDirective.js
│ │ ├── editSettingsModalView.html
│ │ └── module.js
│ ├── folder
│ │ ├── editFolderModalDirective.js
│ │ ├── editFolderModalView.html
│ │ └── module.js
│ └── pins
│ │ ├── editPinsModalDirective.js
│ │ ├── editPinsModalView.html
│ │ └── module.js
└── vendor
│ ├── angular-1.4.7
│ └── angular.min.js
│ └── jquery-2.1.4.min.js
├── lib
├── autogenerated
│ └── gui.files.go
├── config
│ ├── config.go
│ ├── converter.go
│ ├── debug.go
│ └── wrapper.go
├── fileblockcache
│ ├── debug.go
│ ├── fileblockcache.go
│ └── fileblockcache_test.go
├── filetreecache
│ ├── debug.go
│ └── filetreecache.go
└── model
│ ├── debug.go
│ ├── model.go
│ └── model_test.go
├── old-status.md
├── pre-commit-hook
└── scripts
└── packassets.go
/.gitignore:
--------------------------------------------------------------------------------
1 | private.md
2 |
3 | # Compiled Object files, Static and Dynamic libs (Shared Objects)
4 | *.o
5 | *.a
6 | *.so
7 |
8 | # Folders
9 | _obj
10 | _test
11 |
12 | # Architecture specific extensions/prefixes
13 | *.[568vq]
14 | [568vq].out
15 |
16 | *.cgo1.go
17 | *.cgo2.c
18 | _cgo_defun.c
19 | _cgo_gotypes.go
20 | _cgo_export.*
21 |
22 | _testmain.go
23 |
24 | *.exe
25 | *.test
26 | *.prof
27 |
28 | syncthingfuse
29 |
--------------------------------------------------------------------------------
/.gobuilder.yml:
--------------------------------------------------------------------------------
1 | build_matrix:
2 | osx/amd64:
3 | osx/386:
4 | linux/amd64:
5 | linux/386:
6 | readme_file: README.md
7 |
8 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Mozilla Public License, version 2.0
2 |
3 | 1. Definitions
4 |
5 | 1.1. "Contributor"
6 |
7 | means each individual or legal entity that creates, contributes to the
8 | creation of, or owns Covered Software.
9 |
10 | 1.2. "Contributor Version"
11 |
12 | means the combination of the Contributions of others (if any) used by a
13 | Contributor and that particular Contributor's Contribution.
14 |
15 | 1.3. "Contribution"
16 |
17 | means Covered Software of a particular Contributor.
18 |
19 | 1.4. "Covered Software"
20 |
21 | means Source Code Form to which the initial Contributor has attached the
22 | notice in Exhibit A, the Executable Form of such Source Code Form, and
23 | Modifications of such Source Code Form, in each case including portions
24 | thereof.
25 |
26 | 1.5. "Incompatible With Secondary Licenses"
27 | means
28 |
29 | a. that the initial Contributor has attached the notice described in
30 | Exhibit B to the Covered Software; or
31 |
32 | b. that the Covered Software was made available under the terms of
33 | version 1.1 or earlier of the License, but not also under the terms of
34 | a Secondary License.
35 |
36 | 1.6. "Executable Form"
37 |
38 | means any form of the work other than Source Code Form.
39 |
40 | 1.7. "Larger Work"
41 |
42 | means a work that combines Covered Software with other material, in a
43 | separate file or files, that is not Covered Software.
44 |
45 | 1.8. "License"
46 |
47 | means this document.
48 |
49 | 1.9. "Licensable"
50 |
51 | means having the right to grant, to the maximum extent possible, whether
52 | at the time of the initial grant or subsequently, any and all of the
53 | rights conveyed by this License.
54 |
55 | 1.10. "Modifications"
56 |
57 | means any of the following:
58 |
59 | a. any file in Source Code Form that results from an addition to,
60 | deletion from, or modification of the contents of Covered Software; or
61 |
62 | b. any new file in Source Code Form that contains any Covered Software.
63 |
64 | 1.11. "Patent Claims" of a Contributor
65 |
66 | means any patent claim(s), including without limitation, method,
67 | process, and apparatus claims, in any patent Licensable by such
68 | Contributor that would be infringed, but for the grant of the License,
69 | by the making, using, selling, offering for sale, having made, import,
70 | or transfer of either its Contributions or its Contributor Version.
71 |
72 | 1.12. "Secondary License"
73 |
74 | means either the GNU General Public License, Version 2.0, the GNU Lesser
75 | General Public License, Version 2.1, the GNU Affero General Public
76 | License, Version 3.0, or any later versions of those licenses.
77 |
78 | 1.13. "Source Code Form"
79 |
80 | means the form of the work preferred for making modifications.
81 |
82 | 1.14. "You" (or "Your")
83 |
84 | means an individual or a legal entity exercising rights under this
85 | License. For legal entities, "You" includes any entity that controls, is
86 | controlled by, or is under common control with You. For purposes of this
87 | definition, "control" means (a) the power, direct or indirect, to cause
88 | the direction or management of such entity, whether by contract or
89 | otherwise, or (b) ownership of more than fifty percent (50%) of the
90 | outstanding shares or beneficial ownership of such entity.
91 |
92 |
93 | 2. License Grants and Conditions
94 |
95 | 2.1. Grants
96 |
97 | Each Contributor hereby grants You a world-wide, royalty-free,
98 | non-exclusive license:
99 |
100 | a. under intellectual property rights (other than patent or trademark)
101 | Licensable by such Contributor to use, reproduce, make available,
102 | modify, display, perform, distribute, and otherwise exploit its
103 | Contributions, either on an unmodified basis, with Modifications, or
104 | as part of a Larger Work; and
105 |
106 | b. under Patent Claims of such Contributor to make, use, sell, offer for
107 | sale, have made, import, and otherwise transfer either its
108 | Contributions or its Contributor Version.
109 |
110 | 2.2. Effective Date
111 |
112 | The licenses granted in Section 2.1 with respect to any Contribution
113 | become effective for each Contribution on the date the Contributor first
114 | distributes such Contribution.
115 |
116 | 2.3. Limitations on Grant Scope
117 |
118 | The licenses granted in this Section 2 are the only rights granted under
119 | this License. No additional rights or licenses will be implied from the
120 | distribution or licensing of Covered Software under this License.
121 | Notwithstanding Section 2.1(b) above, no patent license is granted by a
122 | Contributor:
123 |
124 | a. for any code that a Contributor has removed from Covered Software; or
125 |
126 | b. for infringements caused by: (i) Your and any other third party's
127 | modifications of Covered Software, or (ii) the combination of its
128 | Contributions with other software (except as part of its Contributor
129 | Version); or
130 |
131 | c. under Patent Claims infringed by Covered Software in the absence of
132 | its Contributions.
133 |
134 | This License does not grant any rights in the trademarks, service marks,
135 | or logos of any Contributor (except as may be necessary to comply with
136 | the notice requirements in Section 3.4).
137 |
138 | 2.4. Subsequent Licenses
139 |
140 | No Contributor makes additional grants as a result of Your choice to
141 | distribute the Covered Software under a subsequent version of this
142 | License (see Section 10.2) or under the terms of a Secondary License (if
143 | permitted under the terms of Section 3.3).
144 |
145 | 2.5. Representation
146 |
147 | Each Contributor represents that the Contributor believes its
148 | Contributions are its original creation(s) or it has sufficient rights to
149 | grant the rights to its Contributions conveyed by this License.
150 |
151 | 2.6. Fair Use
152 |
153 | This License is not intended to limit any rights You have under
154 | applicable copyright doctrines of fair use, fair dealing, or other
155 | equivalents.
156 |
157 | 2.7. Conditions
158 |
159 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in
160 | Section 2.1.
161 |
162 |
163 | 3. Responsibilities
164 |
165 | 3.1. Distribution of Source Form
166 |
167 | All distribution of Covered Software in Source Code Form, including any
168 | Modifications that You create or to which You contribute, must be under
169 | the terms of this License. You must inform recipients that the Source
170 | Code Form of the Covered Software is governed by the terms of this
171 | License, and how they can obtain a copy of this License. You may not
172 | attempt to alter or restrict the recipients' rights in the Source Code
173 | Form.
174 |
175 | 3.2. Distribution of Executable Form
176 |
177 | If You distribute Covered Software in Executable Form then:
178 |
179 | a. such Covered Software must also be made available in Source Code Form,
180 | as described in Section 3.1, and You must inform recipients of the
181 | Executable Form how they can obtain a copy of such Source Code Form by
182 | reasonable means in a timely manner, at a charge no more than the cost
183 | of distribution to the recipient; and
184 |
185 | b. You may distribute such Executable Form under the terms of this
186 | License, or sublicense it under different terms, provided that the
187 | license for the Executable Form does not attempt to limit or alter the
188 | recipients' rights in the Source Code Form under this License.
189 |
190 | 3.3. Distribution of a Larger Work
191 |
192 | You may create and distribute a Larger Work under terms of Your choice,
193 | provided that You also comply with the requirements of this License for
194 | the Covered Software. If the Larger Work is a combination of Covered
195 | Software with a work governed by one or more Secondary Licenses, and the
196 | Covered Software is not Incompatible With Secondary Licenses, this
197 | License permits You to additionally distribute such Covered Software
198 | under the terms of such Secondary License(s), so that the recipient of
199 | the Larger Work may, at their option, further distribute the Covered
200 | Software under the terms of either this License or such Secondary
201 | License(s).
202 |
203 | 3.4. Notices
204 |
205 | You may not remove or alter the substance of any license notices
206 | (including copyright notices, patent notices, disclaimers of warranty, or
207 | limitations of liability) contained within the Source Code Form of the
208 | Covered Software, except that You may alter any license notices to the
209 | extent required to remedy known factual inaccuracies.
210 |
211 | 3.5. Application of Additional Terms
212 |
213 | You may choose to offer, and to charge a fee for, warranty, support,
214 | indemnity or liability obligations to one or more recipients of Covered
215 | Software. However, You may do so only on Your own behalf, and not on
216 | behalf of any Contributor. You must make it absolutely clear that any
217 | such warranty, support, indemnity, or liability obligation is offered by
218 | You alone, and You hereby agree to indemnify every Contributor for any
219 | liability incurred by such Contributor as a result of warranty, support,
220 | indemnity or liability terms You offer. You may include additional
221 | disclaimers of warranty and limitations of liability specific to any
222 | jurisdiction.
223 |
224 | 4. Inability to Comply Due to Statute or Regulation
225 |
226 | If it is impossible for You to comply with any of the terms of this License
227 | with respect to some or all of the Covered Software due to statute,
228 | judicial order, or regulation then You must: (a) comply with the terms of
229 | this License to the maximum extent possible; and (b) describe the
230 | limitations and the code they affect. Such description must be placed in a
231 | text file included with all distributions of the Covered Software under
232 | this License. Except to the extent prohibited by statute or regulation,
233 | such description must be sufficiently detailed for a recipient of ordinary
234 | skill to be able to understand it.
235 |
236 | 5. Termination
237 |
238 | 5.1. The rights granted under this License will terminate automatically if You
239 | fail to comply with any of its terms. However, if You become compliant,
240 | then the rights granted under this License from a particular Contributor
241 | are reinstated (a) provisionally, unless and until such Contributor
242 | explicitly and finally terminates Your grants, and (b) on an ongoing
243 | basis, if such Contributor fails to notify You of the non-compliance by
244 | some reasonable means prior to 60 days after You have come back into
245 | compliance. Moreover, Your grants from a particular Contributor are
246 | reinstated on an ongoing basis if such Contributor notifies You of the
247 | non-compliance by some reasonable means, this is the first time You have
248 | received notice of non-compliance with this License from such
249 | Contributor, and You become compliant prior to 30 days after Your receipt
250 | of the notice.
251 |
252 | 5.2. If You initiate litigation against any entity by asserting a patent
253 | infringement claim (excluding declaratory judgment actions,
254 | counter-claims, and cross-claims) alleging that a Contributor Version
255 | directly or indirectly infringes any patent, then the rights granted to
256 | You by any and all Contributors for the Covered Software under Section
257 | 2.1 of this License shall terminate.
258 |
259 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user
260 | license agreements (excluding distributors and resellers) which have been
261 | validly granted by You or Your distributors under this License prior to
262 | termination shall survive termination.
263 |
264 | 6. Disclaimer of Warranty
265 |
266 | Covered Software is provided under this License on an "as is" basis,
267 | without warranty of any kind, either expressed, implied, or statutory,
268 | including, without limitation, warranties that the Covered Software is free
269 | of defects, merchantable, fit for a particular purpose or non-infringing.
270 | The entire risk as to the quality and performance of the Covered Software
271 | is with You. Should any Covered Software prove defective in any respect,
272 | You (not any Contributor) assume the cost of any necessary servicing,
273 | repair, or correction. This disclaimer of warranty constitutes an essential
274 | part of this License. No use of any Covered Software is authorized under
275 | this License except under this disclaimer.
276 |
277 | 7. Limitation of Liability
278 |
279 | Under no circumstances and under no legal theory, whether tort (including
280 | negligence), contract, or otherwise, shall any Contributor, or anyone who
281 | distributes Covered Software as permitted above, be liable to You for any
282 | direct, indirect, special, incidental, or consequential damages of any
283 | character including, without limitation, damages for lost profits, loss of
284 | goodwill, work stoppage, computer failure or malfunction, or any and all
285 | other commercial damages or losses, even if such party shall have been
286 | informed of the possibility of such damages. This limitation of liability
287 | shall not apply to liability for death or personal injury resulting from
288 | such party's negligence to the extent applicable law prohibits such
289 | limitation. Some jurisdictions do not allow the exclusion or limitation of
290 | incidental or consequential damages, so this exclusion and limitation may
291 | not apply to You.
292 |
293 | 8. Litigation
294 |
295 | Any litigation relating to this License may be brought only in the courts
296 | of a jurisdiction where the defendant maintains its principal place of
297 | business and such litigation shall be governed by laws of that
298 | jurisdiction, without reference to its conflict-of-law provisions. Nothing
299 | in this Section shall prevent a party's ability to bring cross-claims or
300 | counter-claims.
301 |
302 | 9. Miscellaneous
303 |
304 | This License represents the complete agreement concerning the subject
305 | matter hereof. If any provision of this License is held to be
306 | unenforceable, such provision shall be reformed only to the extent
307 | necessary to make it enforceable. Any law or regulation which provides that
308 | the language of a contract shall be construed against the drafter shall not
309 | be used to construe this License against a Contributor.
310 |
311 |
312 | 10. Versions of the License
313 |
314 | 10.1. New Versions
315 |
316 | Mozilla Foundation is the license steward. Except as provided in Section
317 | 10.3, no one other than the license steward has the right to modify or
318 | publish new versions of this License. Each version will be given a
319 | distinguishing version number.
320 |
321 | 10.2. Effect of New Versions
322 |
323 | You may distribute the Covered Software under the terms of the version
324 | of the License under which You originally received the Covered Software,
325 | or under the terms of any subsequent version published by the license
326 | steward.
327 |
328 | 10.3. Modified Versions
329 |
330 | If you create software not governed by this License, and you want to
331 | create a new license for such software, you may create and use a
332 | modified version of this License if you rename the license and remove
333 | any references to the name of the license steward (except to note that
334 | such modified license differs from this License).
335 |
336 | 10.4. Distributing Source Code Form that is Incompatible With Secondary
337 | Licenses If You choose to distribute Source Code Form that is
338 | Incompatible With Secondary Licenses under the terms of this version of
339 | the License, the notice described in Exhibit B of this License must be
340 | attached.
341 |
342 | Exhibit A - Source Code Form License Notice
343 |
344 | This Source Code Form is subject to the
345 | terms of the Mozilla Public License, v.
346 | 2.0. If a copy of the MPL was not
347 | distributed with this file, You can
348 | obtain one at
349 | http://mozilla.org/MPL/2.0/.
350 |
351 | If it is not possible or desirable to put the notice in a particular file,
352 | then You may include the notice in a location (such as a LICENSE file in a
353 | relevant directory) where a recipient would be likely to look for such a
354 | notice.
355 |
356 | You may add additional accurate notices of copyright ownership.
357 |
358 | Exhibit B - "Incompatible With Secondary Licenses" Notice
359 |
360 | This Source Code Form is "Incompatible
361 | With Secondary Licenses", as defined by
362 | the Mozilla Public License, v. 2.0.
363 |
364 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | SyncthingFUSE
2 | =============
3 |
4 | SyncthingFUSE allows you to see all of the files in shared [Syncthing](https://syncthing.net) folders, but only stores a fixed amount of data locally.
5 |
6 | When you open a file, the contents are served from a local cache, if possible. If the contents are not in the cache, then SyncthingFUSE asks peers for the contents and adds them to the cache. If no peers are currently available for the file, then opening the file will fail.
7 |
8 | This is particularly useful if you have a Syncthing device with a lot of data that you want access to, but don't have room for all of on a device. For example, you may have a large collection of photos on a desktop running Syncthing at home. Your laptop's hard drive may not be large enough to hold all of the photos. Running SyncthingFUSE on the laptop, you will see all of your photos. As you view the photos on your laptop, they'll be read from the local cache or pulled from home. The local cache will not grow larger than a fixed size, though.
9 |
10 | SyncthingFUSE is available on OS X and Linux.
11 |
12 | SynthingFUSE is currently read-only. You can browse and view files but cannot write or modify them. (Supporting writes appears possible, but no one has put in the development effort, yet.)
13 |
14 | _SyncthingFUSE is currently an early release. Since it's currently read-only, it poses a low threat to damaging your computer or Syncthing folders. There is some risk, however, and you assume all of that yourself._
15 |
16 | Getting Started
17 | ===============
18 |
19 | SyncthingFUSE follows many patterns of Syncthing, so you should be familiar with it before starting. Additionally, SyncthingFUSE requires at least one device running Syncthing.
20 |
21 | To get started, grab a [release](https://github.com/burkemw3/syncthingfuse/releases) for your operating system, unzip it, and run it. When you start the `syncthingfuse` binary, it will set itself up with some defaults and start. ([OSXFUSE](https://osxfuse.github.io/) may be required on OS X, if you don't have it already.)
22 |
23 | By default, a configuration UI is available in a browser at `http://127.0.0.1:8385` (If the default port is taken, check the output of the startup for the line `API listening on`). Upon visiting, you will see a UI similar (albeit uglier) to Syncthing. On the left are folders that are configured, and on the right are devices.
24 |
25 | Add devices and folders through the UI and restart SyncthingFUSE for the changes to take effect. Folders have a default cache size of 512 MiB, configurable through the UI. You'll also need to add the SyncthingFUSE device to your Syncthing devices.
26 |
27 | By default, a mount point called "SyncthingFUSE" will be created in your home directory. After SyncthingFUSE connects to other Syncthing devices, you will be able to browse folder contents through this mount point.
28 |
29 | SyncthingFUSE will appear as "Syncing (0%)" when connected in Syncthing devices. This looks strange but is expected.
30 |
31 | Syncthing Compatibility
32 | =======================
33 |
34 | Supports:
35 |
36 | - connecting with Syncthing instances, including:
37 | - local and global discovery
38 | - relays
39 |
40 | Does not support:
41 |
42 | - accurate reporting of status: SyncthingFUSE will always appear as 0% synced on Syncthing devices
43 | - symlink files
44 | - UPnP
45 | - introducers: additional peers will not be added automatically
46 | - responding to read requests from other peers
47 |
--------------------------------------------------------------------------------
/cmd/syncthingfuse/addresslister.go:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2015 The Syncthing Authors.
2 | //
3 | // This Source Code Form is subject to the terms of the Mozilla Public
4 | // License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 | // You can obtain one at http://mozilla.org/MPL/2.0/.
6 |
7 | package main
8 |
9 | import (
10 | "net"
11 | "net/url"
12 |
13 | "github.com/syncthing/syncthing/lib/config"
14 | )
15 |
16 | type addressLister struct {
17 | cfg *config.Wrapper
18 | }
19 |
20 | func newAddressLister(cfg *config.Wrapper) *addressLister {
21 | return &addressLister{
22 | cfg: cfg,
23 | }
24 | }
25 |
26 | // ExternalAddresses returns a list of addresses that are our best guess for
27 | // where we are reachable from the outside. As a special case, we may return
28 | // one or more addresses with an empty IP address (0.0.0.0 or ::) and just
29 | // port number - this means that the outside address of a NAT gateway should
30 | // be substituted.
31 | func (e *addressLister) ExternalAddresses() []string {
32 | return e.addresses(false)
33 | }
34 |
35 | // AllAddresses returns a list of addresses that are our best guess for where
36 | // we are reachable from the local network. Same conditions as
37 | // ExternalAddresses, but private IPv4 addresses are included.
38 | func (e *addressLister) AllAddresses() []string {
39 | return e.addresses(true)
40 | }
41 |
42 | func (e *addressLister) addresses(includePrivateIPV4 bool) []string {
43 | var addrs []string
44 |
45 | // Grab our listen addresses from the config. Unspecified ones are passed
46 | // on verbatim (to be interpreted by a global discovery server or local
47 | // discovery peer). Public addresses are passed on verbatim. Private
48 | // addresses are filtered.
49 | for _, addrStr := range e.cfg.Options().ListenAddresses {
50 | addrURL, err := url.Parse(addrStr)
51 | if err != nil {
52 | l.Infoln("Listen address", addrStr, "is invalid:", err)
53 | continue
54 | }
55 | addr, err := net.ResolveTCPAddr("tcp", addrURL.Host)
56 | if err != nil {
57 | l.Infoln("Listen address", addrStr, "is invalid:", err)
58 | continue
59 | }
60 |
61 | if addr.IP == nil || addr.IP.IsUnspecified() {
62 | // Address like 0.0.0.0:22000 or [::]:22000 or :22000; include as is.
63 | addrs = append(addrs, tcpAddr(addr.String()))
64 | } else if isPublicIPv4(addr.IP) || isPublicIPv6(addr.IP) {
65 | // A public address; include as is.
66 | addrs = append(addrs, tcpAddr(addr.String()))
67 | } else if includePrivateIPV4 && addr.IP.To4().IsGlobalUnicast() {
68 | // A private IPv4 address.
69 | addrs = append(addrs, tcpAddr(addr.String()))
70 | }
71 | }
72 |
73 | return addrs
74 | }
75 |
76 | func isPublicIPv4(ip net.IP) bool {
77 | ip = ip.To4()
78 | if ip == nil {
79 | // Not an IPv4 address (IPv6)
80 | return false
81 | }
82 |
83 | // IsGlobalUnicast below only checks that it's not link local or
84 | // multicast, and we want to exclude private (NAT:ed) addresses as well.
85 | rfc1918 := []net.IPNet{
86 | {IP: net.IP{10, 0, 0, 0}, Mask: net.IPMask{255, 0, 0, 0}},
87 | {IP: net.IP{172, 16, 0, 0}, Mask: net.IPMask{255, 240, 0, 0}},
88 | {IP: net.IP{192, 168, 0, 0}, Mask: net.IPMask{255, 255, 0, 0}},
89 | }
90 | for _, n := range rfc1918 {
91 | if n.Contains(ip) {
92 | return false
93 | }
94 | }
95 |
96 | return ip.IsGlobalUnicast()
97 | }
98 |
99 | func isPublicIPv6(ip net.IP) bool {
100 | if ip.To4() != nil {
101 | // Not an IPv6 address (IPv4)
102 | // (To16() returns a v6 mapped v4 address so can't be used to check
103 | // that it's an actual v6 address)
104 | return false
105 | }
106 |
107 | return ip.IsGlobalUnicast()
108 | }
109 |
110 | func tcpAddr(host string) string {
111 | u := url.URL{
112 | Scheme: "tcp",
113 | Host: host,
114 | }
115 | return u.String()
116 | }
117 |
--------------------------------------------------------------------------------
/cmd/syncthingfuse/config.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "fmt"
5 | "net"
6 | "os"
7 |
8 | "github.com/burkemw3/syncthingfuse/lib/config"
9 | "github.com/syncthing/syncthing/lib/osutil"
10 | )
11 |
12 | func getConfiguration() *config.Wrapper {
13 | cfgFile := locations[locConfigFile]
14 |
15 | // Load the configuration file, if it exists. If it does not, create a template.
16 | if info, err := os.Stat(cfgFile); err == nil {
17 | if !info.Mode().IsRegular() {
18 | l.Fatalln("Config file is not a file?")
19 | }
20 | cfg, err = config.Load(cfgFile, myID)
21 | if err != nil {
22 | l.Fatalln("Configuration:", err)
23 | }
24 | } else {
25 | l.Infoln("No config file; starting with empty defaults")
26 | myName, _ := os.Hostname()
27 | newCfg := defaultConfig(myName)
28 | cfg = config.Wrap(cfgFile, newCfg)
29 | cfg.Save()
30 | l.Infof("Edit %s to taste or use the GUI\n", cfgFile)
31 | }
32 |
33 | return cfg
34 | }
35 |
36 | func ensureDir(dir string, mode int) {
37 | fi, err := os.Stat(dir)
38 | if os.IsNotExist(err) {
39 | err := osutil.MkdirAll(dir, 0700)
40 | if err != nil {
41 | l.Fatalln(err)
42 | }
43 | } else if mode >= 0 && err == nil && int(fi.Mode()&0777) != mode {
44 | err := os.Chmod(dir, os.FileMode(mode))
45 | // This can fail on crappy filesystems, nothing we can do about it.
46 | if err != nil {
47 | l.Warnln(err)
48 | }
49 | }
50 | }
51 |
52 | func defaultConfig(myName string) config.Configuration {
53 | newCfg := config.New(myID, myName)
54 |
55 | port, err := getFreePort("0.0.0.0", 22000)
56 | if err != nil {
57 | l.Fatalln("get free port (BEP):", err)
58 | }
59 | newCfg.Options.ListenAddress = []string{fmt.Sprintf("tcp://0.0.0.0:%d", port)}
60 |
61 | return newCfg
62 | }
63 |
64 | // getFreePort returns a free TCP port fort listening on. The ports given are
65 | // tried in succession and the first to succeed is returned. If none succeed,
66 | // a random high port is returned.
67 | func getFreePort(host string, ports ...int) (int, error) {
68 | for _, port := range ports {
69 | c, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port))
70 | if err == nil {
71 | c.Close()
72 | return port, nil
73 | }
74 | }
75 |
76 | c, err := net.Listen("tcp", host+":0")
77 | if err != nil {
78 | return 0, err
79 | }
80 | addr := c.Addr().(*net.TCPAddr)
81 | c.Close()
82 | return addr.Port, nil
83 | }
84 |
--------------------------------------------------------------------------------
/cmd/syncthingfuse/fuse.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "errors"
5 | "flag"
6 | "fmt"
7 | "os"
8 | "os/exec"
9 | "os/signal"
10 | "path"
11 | "path/filepath"
12 | "runtime"
13 | "strings"
14 | "syscall"
15 | "time"
16 |
17 | "bazil.org/fuse"
18 | "bazil.org/fuse/fs"
19 | "github.com/burkemw3/syncthingfuse/lib/model"
20 | "github.com/thejerf/suture"
21 | "golang.org/x/net/context"
22 | )
23 |
24 | var Usage = func() {
25 | fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
26 | fmt.Fprintf(os.Stderr, " %s MOUNTPOINT\n", os.Args[0])
27 | flag.PrintDefaults()
28 | }
29 |
30 | func MountFuse(mountpoint string, m *model.Model, mainSvc suture.Service) {
31 | c, err := fuse.Mount(
32 | mountpoint,
33 | fuse.FSName("syncthingfuse"),
34 | fuse.Subtype("syncthingfuse"),
35 | fuse.LocalVolume(),
36 | fuse.VolumeName("Syncthing FUSE"),
37 | )
38 | if err != nil {
39 | l.Warnln(err)
40 | }
41 |
42 | sigc := make(chan os.Signal, 1)
43 | signal.Notify(sigc, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
44 |
45 | doneServe := make(chan error, 1)
46 | go func() {
47 | doneServe <- fs.Serve(c, FS{m: m})
48 | }()
49 |
50 | select {
51 | case err := <-doneServe:
52 | l.Infoln("conn.Serve returned", err)
53 |
54 | // check if the mount process has an error to report
55 | <-c.Ready
56 | if err := c.MountError; err != nil {
57 | l.Warnln("conn.MountError:", err)
58 | }
59 | case sig := <-sigc:
60 | l.Infoln("Signal", sig, "received, shutting down.")
61 | }
62 |
63 | mainSvc.Stop()
64 |
65 | l.Infoln("Unmounting...")
66 | err = Unmount(mountpoint)
67 | if err == nil {
68 | l.Infoln("Unmounted")
69 | } else {
70 | l.Infoln("Unmount failed:", err)
71 | }
72 | }
73 |
74 | var (
75 | debugFuse = strings.Contains(os.Getenv("STTRACE"), "fuse") || os.Getenv("STTRACE") == "all"
76 | )
77 |
78 | type FS struct {
79 | m *model.Model
80 | }
81 |
82 | func (fs FS) Root() (fs.Node, error) {
83 | if debugFuse {
84 | l.Debugln("Root")
85 | }
86 | return STFolder{m: fs.m}, nil
87 | }
88 |
89 | type STFolder struct {
90 | m *model.Model
91 | }
92 |
93 | func (stf STFolder) Attr(ctx context.Context, a *fuse.Attr) error {
94 | if debugFuse {
95 | l.Debugln("stf Attr")
96 | }
97 | a.Mode = os.ModeDir | 0555
98 | return nil
99 | }
100 |
101 | func (stf STFolder) Lookup(ctx context.Context, folderName string) (fs.Node, error) {
102 | if debugFuse {
103 | l.Debugln("STF Lookup folder", folderName)
104 | }
105 |
106 | if stf.m.HasFolder(folderName) {
107 | return Dir{
108 | folder: folderName,
109 | m: stf.m,
110 | }, nil
111 | }
112 |
113 | return Dir{}, fuse.ENOENT
114 | }
115 |
116 | func (stf STFolder) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
117 | if debugFuse {
118 | l.Debugln("ReadDirAll stf")
119 | }
120 |
121 | entries := stf.m.GetFolders()
122 | result := make([]fuse.Dirent, len(entries))
123 | for i, entry := range entries {
124 | result[i] = fuse.Dirent{
125 | Name: entry,
126 | Type: fuse.DT_Dir,
127 | }
128 | }
129 |
130 | return result, nil
131 | }
132 |
133 | // Dir implements both Node and Handle for the root directory.
134 | type Dir struct {
135 | path string
136 | folder string
137 | m *model.Model
138 | }
139 |
140 | func (d Dir) Attr(ctx context.Context, a *fuse.Attr) error {
141 | if debugFuse {
142 | l.Debugln("Dir Attr folder", d.folder, "path", d.path)
143 | }
144 |
145 | entry, _ := d.m.GetEntry(d.folder, d.path)
146 |
147 | // TODO assert directory?
148 |
149 | a.Mode = os.ModeDir | 0555
150 | a.Mtime = time.Unix(entry.ModifiedS, 0)
151 | return nil
152 | }
153 |
154 | func (d Dir) Lookup(ctx context.Context, name string) (fs.Node, error) {
155 | if debugFuse {
156 | l.Debugln("Dir Lookup folder", d.folder, "path", d.path, "for", name)
157 | }
158 | entry, found := d.m.GetEntry(d.folder, filepath.Join(d.path, name))
159 |
160 | if false == found {
161 | return nil, fuse.ENOENT
162 | }
163 |
164 | var node fs.Node
165 | if entry.IsDirectory() {
166 | node = Dir{
167 | path: entry.Name,
168 | folder: d.folder,
169 | m: d.m,
170 | }
171 | } else {
172 | node = File{
173 | path: entry.Name,
174 | folder: d.folder,
175 | m: d.m,
176 | }
177 | }
178 |
179 | return node, nil
180 | }
181 |
182 | func (d Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
183 | if debugFuse {
184 | l.Debugln("ReadDirAll", d.path)
185 | }
186 |
187 | p := path.Clean(d.path)
188 |
189 | entries := d.m.GetChildren(d.folder, p)
190 | result := make([]fuse.Dirent, len(entries))
191 | for i, entry := range entries {
192 | eType := fuse.DT_File
193 | if entry.IsDirectory() {
194 | eType = fuse.DT_Dir
195 | }
196 | result[i] = fuse.Dirent{
197 | Name: path.Base(entry.Name),
198 | Type: eType,
199 | }
200 | }
201 |
202 | return result, nil
203 | }
204 |
205 | // File implements both Node and Handle for the hello file.
206 | type File struct {
207 | path string
208 | folder string
209 | m *model.Model
210 | }
211 |
212 | func (f File) Attr(ctx context.Context, a *fuse.Attr) error {
213 | entry, found := f.m.GetEntry(f.folder, f.path)
214 |
215 | // TODO assert file?
216 |
217 | if false == found {
218 | return fuse.ENOENT
219 | }
220 |
221 | a.Mode = 0444
222 | a.Mtime = time.Unix(entry.ModifiedS, 0)
223 | a.Size = uint64(entry.Size)
224 | return nil
225 | }
226 |
227 | func (f File) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
228 | data, err := f.m.GetFileData(f.folder, f.path, req.Offset, req.Size)
229 |
230 | if err != nil {
231 | return err
232 | }
233 |
234 | resp.Data = data
235 |
236 | return err
237 | }
238 |
239 | // Unmount attempts to unmount the provided FUSE mount point, forcibly
240 | // if necessary.
241 | func Unmount(point string) error {
242 | var cmd *exec.Cmd
243 | switch runtime.GOOS {
244 | case "darwin":
245 | cmd = exec.Command("/usr/sbin/diskutil", "umount", "force", point)
246 | case "linux":
247 | cmd = exec.Command("fusermount", "-u", point)
248 | default:
249 | return errors.New("unmount: unimplemented")
250 | }
251 |
252 | errc := make(chan error, 1)
253 | go func() {
254 | if err := exec.Command("umount", point).Run(); err == nil {
255 | errc <- err
256 | }
257 | // retry to unmount with the fallback cmd
258 | errc <- cmd.Run()
259 | }()
260 | select {
261 | case <-time.After(10 * time.Second):
262 | return errors.New("umount timeout")
263 | case err := <-errc:
264 | return err
265 | }
266 | }
267 |
--------------------------------------------------------------------------------
/cmd/syncthingfuse/gui.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "bytes"
5 | "compress/gzip"
6 | "crypto/tls"
7 | "encoding/json"
8 | "fmt"
9 | "io/ioutil"
10 | "mime"
11 | "net"
12 | "net/http"
13 | "os"
14 | "path/filepath"
15 | "strings"
16 | "time"
17 |
18 | "github.com/burkemw3/syncthingfuse/lib/autogenerated"
19 | "github.com/burkemw3/syncthingfuse/lib/config"
20 | "github.com/burkemw3/syncthingfuse/lib/model"
21 | human "github.com/dustin/go-humanize"
22 | "github.com/syncthing/syncthing/lib/protocol"
23 | "github.com/syncthing/syncthing/lib/sync"
24 | "github.com/syncthing/syncthing/lib/tlsutil"
25 | )
26 |
27 | var (
28 | guiAssets = os.Getenv("STGUIASSETS")
29 | )
30 |
31 | type apiSvc struct {
32 | id protocol.DeviceID
33 | cfg *config.Wrapper
34 | model *model.Model
35 | assetDir string
36 | listener net.Listener
37 | stop chan struct{}
38 | configInSync bool
39 | systemConfigMut sync.Mutex
40 | }
41 |
42 | func newAPISvc(id protocol.DeviceID, cfg *config.Wrapper, model *model.Model) (*apiSvc, error) {
43 | if guiAssets == "" {
44 | guiAssets = locations[locGUIAssets]
45 | }
46 |
47 | svc := &apiSvc{
48 | id: id,
49 | cfg: cfg,
50 | model: model,
51 | assetDir: guiAssets,
52 | systemConfigMut: sync.NewMutex(),
53 | configInSync: true,
54 | }
55 |
56 | var err error
57 | svc.listener, err = svc.getListener()
58 | return svc, err
59 | }
60 |
61 | func (s *apiSvc) getListener() (net.Listener, error) {
62 | cert, err := tls.LoadX509KeyPair(locations[locHTTPSCertFile], locations[locHTTPSKeyFile])
63 | if err != nil {
64 | l.Infoln("Loading HTTPS certificate:", err)
65 | l.Infoln("Creating new HTTPS certificate")
66 |
67 | // When generating the HTTPS certificate, use the system host name per
68 | // default. If that isn't available, use the "syncthing" default.
69 | var name string
70 | name, err = os.Hostname()
71 | if err != nil {
72 | name = tlsDefaultCommonName
73 | }
74 |
75 | cert, err = tlsutil.NewCertificate(locations[locHTTPSCertFile], locations[locHTTPSKeyFile], name, tlsRSABits)
76 | }
77 | if err != nil {
78 | return nil, err
79 | }
80 | tlsCfg := &tls.Config{
81 | Certificates: []tls.Certificate{cert},
82 | MinVersion: tls.VersionTLS10, // No SSLv3
83 | CipherSuites: []uint16{
84 | // No RC4
85 | tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
86 | tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
87 | tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
88 | tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
89 | tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
90 | tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
91 | tls.TLS_RSA_WITH_AES_128_CBC_SHA,
92 | tls.TLS_RSA_WITH_AES_256_CBC_SHA,
93 | tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
94 | tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA,
95 | },
96 | }
97 |
98 | rawListener, err := net.Listen("tcp", s.cfg.Raw().GUI.RawAddress)
99 | if err != nil {
100 | return nil, err
101 | }
102 |
103 | listener := &tlsutil.DowngradingListener{rawListener, tlsCfg}
104 | return listener, nil
105 | }
106 |
107 | func (s *apiSvc) getMux() *http.ServeMux {
108 | mux := http.NewServeMux()
109 |
110 | getApiMux := http.NewServeMux()
111 | getApiMux.HandleFunc("/api/system/config", s.getSystemConfig)
112 | getApiMux.HandleFunc("/api/system/config/insync", s.getSystemConfigInSync)
113 | getApiMux.HandleFunc("/api/system/connections", s.getSystemConnections)
114 | getApiMux.HandleFunc("/api/system/pins/status", s.getPinStatus)
115 | getApiMux.HandleFunc("/api/verify/deviceid", s.getDeviceID) // id
116 | getApiMux.HandleFunc("/api/db/browse", s.getDBBrowse) // folderID pathPrefix
117 |
118 | postApiMux := http.NewServeMux()
119 | postApiMux.HandleFunc("/api/system/config", s.postSystemConfig) //
120 | postApiMux.HandleFunc("/api/verify/humansize", s.postVerifyHumanSize) //
121 |
122 | apiMux := getMethodHandler(getApiMux, postApiMux)
123 | mux.Handle("/api/", apiMux)
124 |
125 | // Serve compiled in assets unless an asset directory was set (for development)
126 | mux.Handle("/", embeddedStatic{
127 | assetDir: s.assetDir,
128 | assets: autogenerated.Assets(),
129 | })
130 |
131 | return mux
132 | }
133 |
134 | func (s *apiSvc) Serve() {
135 | s.stop = make(chan struct{})
136 |
137 | srv := http.Server{
138 | Handler: s.getMux(),
139 | ReadTimeout: 10 * time.Second,
140 | }
141 |
142 | l.Infoln("API listening on", s.listener.Addr())
143 | err := srv.Serve(s.listener)
144 |
145 | // The return could be due to an intentional close. Wait for the stop
146 | // signal before returning. IF there is no stop signal within a second, we
147 | // assume it was unintentional and log the error before retrying.
148 | select {
149 | case <-s.stop:
150 | case <-time.After(time.Second):
151 | l.Warnln("API:", err)
152 | }
153 | }
154 |
155 | func getMethodHandler(get, post http.Handler) http.Handler {
156 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
157 | switch r.Method {
158 | case "GET":
159 | get.ServeHTTP(w, r)
160 | case "POST":
161 | post.ServeHTTP(w, r)
162 | default:
163 | http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
164 | }
165 | })
166 | }
167 |
168 | func (s *apiSvc) Stop() {
169 | close(s.stop)
170 | s.listener.Close()
171 | }
172 |
173 | func (s *apiSvc) String() string {
174 | return fmt.Sprintf("apiSvc@%p", s)
175 | }
176 |
177 | func (s *apiSvc) getSystemConfig(w http.ResponseWriter, r *http.Request) {
178 | w.Header().Set("Content-Type", "application/json; charset=utf-8")
179 | json.NewEncoder(w).Encode(s.cfg.Raw())
180 | }
181 |
182 | func (s *apiSvc) getSystemConfigInSync(w http.ResponseWriter, r *http.Request) {
183 | w.Header().Set("Content-Type", "application/json; charset=utf-8")
184 | json.NewEncoder(w).Encode(s.configInSync)
185 | }
186 |
187 | func (s *apiSvc) getSystemConnections(w http.ResponseWriter, r *http.Request) {
188 | w.Header().Set("Content-Type", "application/json; charset=utf-8")
189 | json.NewEncoder(w).Encode(s.model.GetConnections())
190 | }
191 |
192 | func (s *apiSvc) getPinStatus(w http.ResponseWriter, r *http.Request) {
193 | w.Header().Set("Content-Type", "application/json; charset=utf-8")
194 | json.NewEncoder(w).Encode(s.model.GetPinsStatusByFolder())
195 | }
196 |
197 | func (s *apiSvc) getDeviceID(w http.ResponseWriter, r *http.Request) {
198 | qs := r.URL.Query()
199 | idStr := qs.Get("id")
200 | id, err := protocol.DeviceIDFromString(idStr)
201 | w.Header().Set("Content-Type", "application/json; charset=utf-8")
202 | if err == nil {
203 | json.NewEncoder(w).Encode(map[string]string{
204 | "id": id.String(),
205 | })
206 | } else {
207 | json.NewEncoder(w).Encode(map[string]string{
208 | "error": err.Error(),
209 | })
210 | }
211 | }
212 |
213 | func (s *apiSvc) getDBBrowse(w http.ResponseWriter, r *http.Request) {
214 | qs := r.URL.Query()
215 | folderID := qs.Get("folderID")
216 | pathPrefix := qs.Get("pathPrefix")
217 |
218 | paths := s.model.GetPathsMatchingPrefix(folderID, pathPrefix)
219 |
220 | json.NewEncoder(w).Encode(paths)
221 | }
222 |
223 | func (s *apiSvc) postSystemConfig(w http.ResponseWriter, r *http.Request) {
224 | s.systemConfigMut.Lock()
225 | defer s.systemConfigMut.Unlock()
226 |
227 | // deserialize
228 | var to config.Configuration
229 | err := json.NewDecoder(r.Body).Decode(&to)
230 | if err != nil {
231 | l.Warnln("decoding posted config:", err)
232 | http.Error(w, err.Error(), 500)
233 | return
234 | }
235 |
236 | // Activate and save
237 | err = s.cfg.Replace(to)
238 | s.configInSync = false
239 | if err != nil {
240 | http.Error(w, err.Error(), 400)
241 | return
242 | }
243 | s.cfg.Save()
244 | }
245 |
246 | func (s *apiSvc) postVerifyHumanSize(w http.ResponseWriter, r *http.Request) {
247 | b, err := ioutil.ReadAll(r.Body)
248 | if err != nil {
249 | http.Error(w, "Error reading body"+err.Error(), 500)
250 | return
251 | }
252 |
253 | _, err = human.ParseBytes(string(b))
254 | if err != nil {
255 | http.Error(w, "Cannot parse size"+err.Error(), 500)
256 | return
257 | }
258 | return
259 | }
260 |
261 | type embeddedStatic struct {
262 | assetDir string
263 | assets map[string][]byte
264 | }
265 |
266 | func (s embeddedStatic) ServeHTTP(w http.ResponseWriter, r *http.Request) {
267 | file := r.URL.Path
268 |
269 | if file[0] == '/' {
270 | file = file[1:]
271 | }
272 |
273 | if len(file) == 0 {
274 | file = "index.html"
275 | }
276 |
277 | if s.assetDir != "" {
278 | p := filepath.Join(s.assetDir, filepath.FromSlash(file))
279 | _, err := os.Stat(p)
280 | if err == nil {
281 | http.ServeFile(w, r, p)
282 | return
283 | }
284 | }
285 |
286 | bs, ok := s.assets[file]
287 | if !ok {
288 | http.NotFound(w, r)
289 | return
290 | }
291 |
292 | if r.Header.Get("If-Modified-Since") == autogenerated.AssetsBuildDate {
293 | w.WriteHeader(http.StatusNotModified)
294 | return
295 | }
296 |
297 | mtype := s.mimeTypeForFile(file)
298 | if len(mtype) != 0 {
299 | w.Header().Set("Content-Type", mtype)
300 | }
301 | if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
302 | w.Header().Set("Content-Encoding", "gzip")
303 | } else {
304 | // ungzip if browser not send gzip accepted header
305 | var gr *gzip.Reader
306 | gr, _ = gzip.NewReader(bytes.NewReader(bs))
307 | bs, _ = ioutil.ReadAll(gr)
308 | gr.Close()
309 | }
310 | w.Header().Set("Content-Length", fmt.Sprintf("%d", len(bs)))
311 | w.Header().Set("Last-Modified", autogenerated.AssetsBuildDate)
312 | w.Header().Set("Cache-Control", "public")
313 |
314 | w.Write(bs)
315 | }
316 |
317 | func (s embeddedStatic) mimeTypeForFile(file string) string {
318 | // We use a built in table of the common types since the system
319 | // TypeByExtension might be unreliable. But if we don't know, we delegate
320 | // to the system.
321 | ext := filepath.Ext(file)
322 | switch ext {
323 | case ".htm", ".html":
324 | return "text/html"
325 | case ".css":
326 | return "text/css"
327 | case ".js":
328 | return "application/javascript"
329 | case ".json":
330 | return "application/json"
331 | case ".png":
332 | return "image/png"
333 | case ".ttf":
334 | return "application/x-font-ttf"
335 | case ".woff":
336 | return "application/x-font-woff"
337 | case ".svg":
338 | return "image/svg+xml"
339 | default:
340 | return mime.TypeByExtension(ext)
341 | }
342 | }
343 |
--------------------------------------------------------------------------------
/cmd/syncthingfuse/gui_test.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "net/http"
5 | "net/http/httptest"
6 | "strings"
7 | "testing"
8 | )
9 |
10 | func TestHumanSizeVerifications(t *testing.T) {
11 | // Arrange
12 | var api apiSvc
13 | mux := api.getMux()
14 | server := httptest.NewServer(mux)
15 | defer server.Close()
16 |
17 | // Act
18 | assertHumanSizeVerification(t, server, "512 MiB", true)
19 | assertHumanSizeVerification(t, server, "102 MiB", true)
20 | assertHumanSizeVerification(t, server, "102", true)
21 |
22 | assertHumanSizeVerification(t, server, "MiB", false)
23 | assertHumanSizeVerification(t, server, "foobar", false)
24 | assertHumanSizeVerification(t, server, "512m MB", false)
25 | }
26 |
27 | func assertHumanSizeVerification(t *testing.T, server *httptest.Server, input string, success bool) {
28 | // Act
29 | resp, err := http.Post(server.URL+"/api/verify/humansize", "application/x-www-form-urlencoded", strings.NewReader(input))
30 |
31 | // Assert
32 | if success {
33 | if err != nil {
34 | t.Error(input, err)
35 | }
36 | if resp.StatusCode != 200 {
37 | t.Errorf(input+"Received non-200 response: %d\n", resp.StatusCode)
38 | }
39 | } else {
40 | if resp.StatusCode != 500 {
41 | t.Errorf(input+" Received non-500 response: %d\n", resp.StatusCode)
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/cmd/syncthingfuse/locations.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "os"
5 | "path/filepath"
6 | "runtime"
7 | "strings"
8 |
9 | "github.com/syncthing/syncthing/lib/osutil"
10 | )
11 |
12 | type locationEnum string
13 |
14 | // Use strings as keys to make printout and serialization of the locations map
15 | // more meaningful.
16 | const (
17 | locConfigFile locationEnum = "config"
18 | locCertFile = "certFile"
19 | locKeyFile = "keyFile"
20 | locHTTPSCertFile = "httpsCertFile"
21 | locHTTPSKeyFile = "httpsKeyFile"
22 | locDatabase = "database"
23 | locLogFile = "logFile"
24 | locCsrfTokens = "csrfTokens"
25 | locPanicLog = "panicLog"
26 | locAuditLog = "auditLog"
27 | locGUIAssets = "GUIAssets"
28 | locDefFolder = "defFolder"
29 | )
30 |
31 | // Platform dependent directories
32 | var baseDirs = map[string]string{
33 | "config": defaultConfigDir(), // Overridden by -home flag
34 | "home": homeDir(), // User's home directory, *not* -home flag
35 | }
36 |
37 | // Use the variables from baseDirs here
38 | var locations = map[locationEnum]string{
39 | locConfigFile: "${config}/config.xml",
40 | locCertFile: "${config}/cert.pem",
41 | locKeyFile: "${config}/key.pem",
42 | locHTTPSCertFile: "${config}/https-cert.pem",
43 | locHTTPSKeyFile: "${config}/https-key.pem",
44 | locDatabase: "${config}/index-v0.11.0.db",
45 | locLogFile: "${config}/syncthing.log", // -logfile on Windows
46 | locCsrfTokens: "${config}/csrftokens.txt",
47 | locPanicLog: "${config}/panic-${timestamp}.log",
48 | locAuditLog: "${config}/audit-${timestamp}.log",
49 | locGUIAssets: "${config}/gui",
50 | locDefFolder: "${home}/Sync",
51 | }
52 |
53 | // expandLocations replaces the variables in the location map with actual
54 | // directory locations.
55 | func expandLocations() error {
56 | for key, dir := range locations {
57 | for varName, value := range baseDirs {
58 | dir = strings.Replace(dir, "${"+varName+"}", value, -1)
59 | }
60 | var err error
61 | dir, err = osutil.ExpandTilde(dir)
62 | if err != nil {
63 | return err
64 | }
65 | locations[key] = dir
66 | }
67 | return nil
68 | }
69 |
70 | // defaultConfigDir returns the default configuration directory, as figured
71 | // out by various the environment variables present on each platform, or dies
72 | // trying.
73 | func defaultConfigDir() string {
74 | switch runtime.GOOS {
75 | case "darwin":
76 | dir, err := osutil.ExpandTilde("~/Library/Application Support/SyncthingFUSE")
77 | if err != nil {
78 | l.Fatalln(err)
79 | }
80 | return dir
81 | case "linux":
82 | if xdgCfg := os.Getenv("XDG_CONFIG_HOME"); xdgCfg != "" {
83 | return filepath.Join(xdgCfg, "syncthing")
84 | }
85 | dir, err := osutil.ExpandTilde("~/.config/syncthingfuse")
86 | if err != nil {
87 | l.Fatalln(err)
88 | }
89 | return dir
90 |
91 | default:
92 | l.Fatalln("Only OS X and Linux supported right now!")
93 | }
94 |
95 | return "nil"
96 | }
97 |
98 | // homeDir returns the user's home directory, or dies trying.
99 | func homeDir() string {
100 | home, err := osutil.ExpandTilde("~")
101 | if err != nil {
102 | l.Fatalln(err)
103 | }
104 | return home
105 | }
106 |
--------------------------------------------------------------------------------
/cmd/syncthingfuse/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "crypto/tls"
5 | "flag"
6 | "fmt"
7 | "net"
8 | "os"
9 | "path"
10 | "time"
11 |
12 | "github.com/boltdb/bolt"
13 | "github.com/burkemw3/syncthingfuse/lib/config"
14 | "github.com/burkemw3/syncthingfuse/lib/model"
15 | "github.com/calmh/logger"
16 | "github.com/syncthing/syncthing/lib/connections"
17 | "github.com/syncthing/syncthing/lib/discover"
18 | "github.com/syncthing/syncthing/lib/osutil"
19 | "github.com/syncthing/syncthing/lib/protocol"
20 | "github.com/thejerf/suture"
21 | )
22 |
23 | var (
24 | Version = "unknown-dev"
25 | LongVersion = Version
26 | )
27 |
28 | var (
29 | cfg *config.Wrapper
30 | myID protocol.DeviceID
31 | confDir string
32 | stop = make(chan int)
33 | cert tls.Certificate
34 | lans []*net.IPNet
35 | m *model.Model
36 | )
37 |
38 | const (
39 | bepProtocolName = "bep/1.0"
40 | )
41 |
42 | var l = logger.DefaultLogger
43 |
44 | // Command line and environment options
45 | var (
46 | showVersion bool
47 | )
48 |
49 | const (
50 | usage = "syncthingfuse [options]"
51 | extraUsage = `
52 | The default configuration directory is:
53 |
54 | %s
55 |
56 | `
57 | )
58 |
59 | // The discovery results are sorted by their source priority.
60 | const (
61 | ipv6LocalDiscoveryPriority = iota
62 | ipv4LocalDiscoveryPriority
63 | globalDiscoveryPriority
64 | )
65 |
66 | func main() {
67 | flag.BoolVar(&showVersion, "version", false, "Show version")
68 |
69 | flag.Usage = usageFor(flag.CommandLine, usage, fmt.Sprintf(extraUsage, baseDirs["config"]))
70 | flag.Parse()
71 |
72 | if showVersion {
73 | fmt.Println(Version)
74 | return
75 | }
76 |
77 | if err := expandLocations(); err != nil {
78 | l.Fatalln(err)
79 | }
80 |
81 | // Ensure that our home directory exists.
82 | ensureDir(baseDirs["config"], 0700)
83 |
84 | // Ensure that that we have a certificate and key.
85 | tlsCfg, cert := getTlsConfig()
86 |
87 | // We reinitialize the predictable RNG with our device ID, to get a
88 | // sequence that is always the same but unique to this syncthing instance.
89 | predictableRandom.Seed(seedFromBytes(cert.Certificate[0]))
90 |
91 | myID = protocol.NewDeviceID(cert.Certificate[0])
92 | l.SetPrefix(fmt.Sprintf("[%s] ", myID.String()[:5]))
93 |
94 | l.Infoln("Started syncthingfuse v.", LongVersion)
95 | l.Infoln("My ID:", myID)
96 |
97 | cfg := getConfiguration()
98 |
99 | if info, err := os.Stat(cfg.Raw().MountPoint); err == nil {
100 | if !info.Mode().IsDir() {
101 | l.Fatalln("Mount point (", cfg.Raw().MountPoint, ") must be a directory, but isn't")
102 | os.Exit(1)
103 | }
104 | } else {
105 | l.Infoln("Mount point (", cfg.Raw().MountPoint, ") does not exist, creating it")
106 | err = os.MkdirAll(cfg.Raw().MountPoint, 0700)
107 | if err != nil {
108 | l.Warnln("Error creating mount point", cfg.Raw().MountPoint, err)
109 | l.Warnln("Sometimes, SyncthingFUSE doesn't shut down and unmount cleanly,")
110 | l.Warnln("If you don't know of any other file systems you have mounted at")
111 | l.Warnln("the mount point, try running the command below to unmount, then")
112 | l.Warnln("start SyncthingFUSE again.")
113 | l.Warnln(" umount", cfg.Raw().MountPoint)
114 | l.Fatalln("Cannot create missing mount point")
115 | os.Exit(1)
116 | }
117 | }
118 |
119 | mainSvc := suture.New("main", suture.Spec{
120 | Log: func(line string) {
121 | l.Debugln(line)
122 | },
123 | })
124 | mainSvc.ServeBackground()
125 |
126 | database := openDatabase(cfg)
127 |
128 | m = model.NewModel(cfg, database)
129 |
130 | lans, _ := osutil.GetLans()
131 |
132 | // Start discovery
133 | cachedDiscovery := discover.NewCachingMux()
134 | mainSvc.Add(cachedDiscovery)
135 |
136 | // Start connection management
137 | connectionsService := connections.NewService(cfg.AsStCfg(myID), myID, m, tlsCfg, cachedDiscovery, bepProtocolName, tlsDefaultCommonName, lans)
138 | mainSvc.Add(connectionsService)
139 |
140 | if cfg.Raw().Options.GlobalAnnounceEnabled {
141 | for _, srv := range cfg.Raw().Options.GlobalAnnounceServers {
142 | l.Infoln("Using discovery server", srv)
143 | gd, err := discover.NewGlobal(srv, cert, connectionsService)
144 | if err != nil {
145 | l.Warnln("Global discovery:", err)
146 | continue
147 | }
148 |
149 | // Each global discovery server gets its results cached for five
150 | // minutes, and is not asked again for a minute when it's returned
151 | // unsuccessfully.
152 | cachedDiscovery.Add(gd, 5*time.Minute, time.Minute, globalDiscoveryPriority)
153 | }
154 | }
155 |
156 | if cfg.Raw().Options.LocalAnnounceEnabled {
157 | // v4 broadcasts
158 | bcd, err := discover.NewLocal(myID, fmt.Sprintf(":%d", cfg.Raw().Options.LocalAnnouncePort), connectionsService)
159 | if err != nil {
160 | l.Warnln("IPv4 local discovery:", err)
161 | } else {
162 | cachedDiscovery.Add(bcd, 0, 0, ipv4LocalDiscoveryPriority)
163 | }
164 | // v6 multicasts
165 | mcd, err := discover.NewLocal(myID, cfg.Raw().Options.LocalAnnounceMCAddr, connectionsService)
166 | if err != nil {
167 | l.Warnln("IPv6 local discovery:", err)
168 | } else {
169 | cachedDiscovery.Add(mcd, 0, 0, ipv6LocalDiscoveryPriority)
170 | }
171 | }
172 |
173 | if cfg.Raw().GUI.Enabled {
174 | api, err := newAPISvc(myID, cfg, m)
175 | if err != nil {
176 | l.Fatalln("Cannot start GUI:", err)
177 | }
178 | mainSvc.Add(api)
179 | }
180 |
181 | l.Infoln("Started ...")
182 |
183 | MountFuse(cfg.Raw().MountPoint, m, mainSvc) // TODO handle fight between FUSE and Syncthing Service
184 |
185 | l.Okln("Exiting")
186 |
187 | return
188 | }
189 |
190 | func openDatabase(cfg *config.Wrapper) *bolt.DB {
191 | databasePath := path.Join(path.Dir(cfg.ConfigPath()), "boltdb")
192 | database, _ := bolt.Open(databasePath, 0600, nil) // TODO check error
193 | return database
194 | }
195 |
--------------------------------------------------------------------------------
/cmd/syncthingfuse/random.go:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 The Syncthing Authors.
2 | //
3 | // This Source Code Form is subject to the terms of the Mozilla Public
4 | // License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 | // You can obtain one at http://mozilla.org/MPL/2.0/.
6 |
7 | package main
8 |
9 | import (
10 | "crypto/md5"
11 | cryptoRand "crypto/rand"
12 | "encoding/binary"
13 | "io"
14 | mathRand "math/rand"
15 | )
16 |
17 | // randomCharset contains the characters that can make up a randomString().
18 | const randomCharset = "01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-"
19 |
20 | // predictableRandom is an RNG that will always have the same sequence. It
21 | // will be seeded with the device ID during startup, so that the sequence is
22 | // predictable but varies between instances.
23 | var predictableRandom = mathRand.New(mathRand.NewSource(42))
24 |
25 | func init() {
26 | // The default RNG should be seeded with something good.
27 | mathRand.Seed(randomInt64())
28 | }
29 |
30 | // randomString returns a string of random characters (taken from
31 | // randomCharset) of the specified length.
32 | func randomString(l int) string {
33 | bs := make([]byte, l)
34 | for i := range bs {
35 | bs[i] = randomCharset[mathRand.Intn(len(randomCharset))]
36 | }
37 | return string(bs)
38 | }
39 |
40 | // randomInt64 returns a strongly random int64, slowly
41 | func randomInt64() int64 {
42 | var bs [8]byte
43 | _, err := io.ReadFull(cryptoRand.Reader, bs[:])
44 | if err != nil {
45 | panic("randomness failure: " + err.Error())
46 | }
47 | return seedFromBytes(bs[:])
48 | }
49 |
50 | // seedFromBytes calculates a weak 64 bit hash from the given byte slice,
51 | // suitable for use a predictable random seed.
52 | func seedFromBytes(bs []byte) int64 {
53 | h := md5.New()
54 | h.Write(bs)
55 | s := h.Sum(nil)
56 | // The MD5 hash of the byte slice is 16 bytes long. We interpret it as two
57 | // uint64s and XOR them together.
58 | return int64(binary.BigEndian.Uint64(s[0:]) ^ binary.BigEndian.Uint64(s[8:]))
59 | }
60 |
--------------------------------------------------------------------------------
/cmd/syncthingfuse/tls.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "crypto/rand"
5 | "crypto/rsa"
6 | "crypto/tls"
7 | "crypto/x509"
8 | "crypto/x509/pkix"
9 | "encoding/pem"
10 | "math/big"
11 | mr "math/rand"
12 | "os"
13 | "time"
14 | )
15 |
16 | const (
17 | tlsRSABits = 3072
18 | tlsDefaultCommonName = "syncthing"
19 | )
20 |
21 | func getTlsConfig() (*tls.Config, tls.Certificate) {
22 | cert, err := tls.LoadX509KeyPair(locations[locCertFile], locations[locKeyFile])
23 | if err != nil {
24 | cert, err = newCertificate(locations[locCertFile], locations[locKeyFile], tlsDefaultCommonName)
25 | if err != nil {
26 | l.Fatalln("load cert:", err)
27 | }
28 | }
29 |
30 | // The TLS configuration is used for both the listening socket and outgoing
31 | // connections.
32 | tlsCfg := &tls.Config{
33 | Certificates: []tls.Certificate{cert},
34 | NextProtos: []string{bepProtocolName},
35 | ClientAuth: tls.RequestClientCert,
36 | SessionTicketsDisabled: true,
37 | InsecureSkipVerify: true,
38 | MinVersion: tls.VersionTLS12,
39 | CipherSuites: []uint16{
40 | tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
41 | tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
42 | tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
43 | tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
44 | tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
45 | tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
46 | },
47 | }
48 |
49 | return tlsCfg, cert
50 | }
51 |
52 | func newCertificate(certFile, keyFile, name string) (tls.Certificate, error) {
53 | l.Infof("Generating RSA key and certificate for %s...", name)
54 |
55 | priv, err := rsa.GenerateKey(rand.Reader, tlsRSABits)
56 | if err != nil {
57 | l.Fatalln("generate key:", err)
58 | }
59 |
60 | notBefore := time.Now()
61 | notAfter := time.Date(2049, 12, 31, 23, 59, 59, 0, time.UTC)
62 |
63 | template := x509.Certificate{
64 | SerialNumber: new(big.Int).SetInt64(mr.Int63()),
65 | Subject: pkix.Name{
66 | CommonName: name,
67 | },
68 | NotBefore: notBefore,
69 | NotAfter: notAfter,
70 |
71 | KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
72 | ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
73 | BasicConstraintsValid: true,
74 | }
75 |
76 | derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
77 | if err != nil {
78 | l.Fatalln("create cert:", err)
79 | }
80 |
81 | certOut, err := os.Create(certFile)
82 | if err != nil {
83 | l.Fatalln("save cert:", err)
84 | }
85 | err = pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
86 | if err != nil {
87 | l.Fatalln("save cert:", err)
88 | }
89 | err = certOut.Close()
90 | if err != nil {
91 | l.Fatalln("save cert:", err)
92 | }
93 |
94 | keyOut, err := os.OpenFile(keyFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
95 | if err != nil {
96 | l.Fatalln("save key:", err)
97 | }
98 | err = pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)})
99 | if err != nil {
100 | l.Fatalln("save key:", err)
101 | }
102 | err = keyOut.Close()
103 | if err != nil {
104 | l.Fatalln("save key:", err)
105 | }
106 |
107 | return tls.LoadX509KeyPair(certFile, keyFile)
108 | }
109 |
--------------------------------------------------------------------------------
/cmd/syncthingfuse/usage.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "bytes"
5 | "flag"
6 | "fmt"
7 | "io"
8 | "text/tabwriter"
9 | )
10 |
11 | func usageFor(fs *flag.FlagSet, usage string, extra string) func() {
12 | return func() {
13 | var b bytes.Buffer
14 | b.WriteString("Usage:\n " + usage + "\n")
15 |
16 | var options [][]string
17 | fs.VisitAll(func(f *flag.Flag) {
18 | var opt = " -" + f.Name
19 |
20 | if f.DefValue != "false" {
21 | opt += "=" + fmt.Sprintf(`"%s"`, f.DefValue)
22 | }
23 | options = append(options, []string{opt, f.Usage})
24 | })
25 |
26 | if len(options) > 0 {
27 | b.WriteString("\nOptions:\n")
28 | optionTable(&b, options)
29 | }
30 |
31 | fmt.Println(b.String())
32 |
33 | if len(extra) > 0 {
34 | fmt.Println(extra)
35 | }
36 | }
37 | }
38 |
39 | func optionTable(w io.Writer, rows [][]string) {
40 | tw := tabwriter.NewWriter(w, 2, 4, 2, ' ', 0)
41 | for _, row := range rows {
42 | for i, cell := range row {
43 | if i > 0 {
44 | tw.Write([]byte("\t"))
45 | }
46 | tw.Write([]byte(cell))
47 | }
48 | tw.Write([]byte("\n"))
49 | }
50 | tw.Flush()
51 | }
52 |
--------------------------------------------------------------------------------
/gui/css/icon-addon.css:
--------------------------------------------------------------------------------
1 | /*
2 | from http://bootsnipp.com/snippets/featured/support-glyph-and-fa-icon-inside-input
3 |
4 | tweaked to put icons on right, not left
5 | */
6 |
7 | .icon-addon {
8 | position: relative;
9 | color: #555;
10 | display: block;
11 | }
12 |
13 | .icon-addon:after,
14 | .icon-addon:before {
15 | display: table;
16 | content: " ";
17 | }
18 |
19 | .icon-addon:after {
20 | clear: both;
21 | }
22 |
23 | .icon-addon.addon-md .glyphicon,
24 | .icon-addon .glyphicon,
25 | .icon-addon.addon-md .fa,
26 | .icon-addon .fa {
27 | position: absolute;
28 | z-index: 2;
29 | right: 10px;
30 | font-size: 14px;
31 | width: 20px;
32 | margin-left: -2.5px;
33 | text-align: center;
34 | padding: 10px 0;
35 | top: 1px
36 | }
37 |
38 | .icon-addon.addon-lg .form-control {
39 | line-height: 1.33;
40 | height: 46px;
41 | font-size: 18px;
42 | padding: 10px 16px 10px 40px;
43 | }
44 |
45 | .icon-addon.addon-sm .form-control {
46 | height: 30px;
47 | padding: 5px 10px 5px 28px;
48 | font-size: 12px;
49 | line-height: 1.5;
50 | }
51 |
52 | .icon-addon.addon-lg .fa,
53 | .icon-addon.addon-lg .glyphicon {
54 | font-size: 18px;
55 | margin-left: 0;
56 | right: 11px;
57 | top: 4px;
58 | }
59 |
60 | .icon-addon.addon-md .form-control,
61 | .icon-addon .form-control {
62 | padding-left: 30px;
63 | float: left;
64 | font-weight: normal;
65 | }
66 |
67 | .icon-addon.addon-sm .fa,
68 | .icon-addon.addon-sm .glyphicon {
69 | margin-left: 0;
70 | font-size: 12px;
71 | right: 5px;
72 | top: -1px
73 | }
74 |
75 | .icon-addon .form-control:focus + .glyphicon,
76 | .icon-addon:hover .glyphicon,
77 | .icon-addon .form-control:focus + .fa,
78 | .icon-addon:hover .fa {
79 | color: #2580db;
80 | }
--------------------------------------------------------------------------------
/gui/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | SyncthingFUSE
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
SyncthingFUSE
12 |
13 |
14 |
15 |
16 |
17 |
Restart Needed
18 |
19 |
The configuration has been saved but not activated. You must restart SyncthingFUSE to activate the new configuration.