gui.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package main
  16. import (
  17. "crypto/tls"
  18. "encoding/json"
  19. "fmt"
  20. "io/ioutil"
  21. "mime"
  22. "net"
  23. "net/http"
  24. "os"
  25. "path/filepath"
  26. "runtime"
  27. "strconv"
  28. "strings"
  29. "sync"
  30. "time"
  31. "github.com/calmh/logger"
  32. "github.com/syncthing/syncthing/internal/auto"
  33. "github.com/syncthing/syncthing/internal/config"
  34. "github.com/syncthing/syncthing/internal/discover"
  35. "github.com/syncthing/syncthing/internal/events"
  36. "github.com/syncthing/syncthing/internal/files"
  37. "github.com/syncthing/syncthing/internal/model"
  38. "github.com/syncthing/syncthing/internal/osutil"
  39. "github.com/syncthing/syncthing/internal/protocol"
  40. "github.com/syncthing/syncthing/internal/upgrade"
  41. "github.com/vitrun/qart/qr"
  42. "golang.org/x/crypto/bcrypt"
  43. )
  44. type guiError struct {
  45. Time time.Time
  46. Error string
  47. }
  48. var (
  49. configInSync = true
  50. guiErrors = []guiError{}
  51. guiErrorsMut sync.Mutex
  52. modt = time.Now().UTC().Format(http.TimeFormat)
  53. eventSub *events.BufferedSubscription
  54. )
  55. func init() {
  56. l.AddHandler(logger.LevelWarn, showGuiError)
  57. sub := events.Default.Subscribe(events.AllEvents)
  58. eventSub = events.NewBufferedSubscription(sub, 1000)
  59. }
  60. func startGUI(cfg config.GUIConfiguration, assetDir string, m *model.Model) error {
  61. var err error
  62. cert, err := loadCert(confDir, "https-")
  63. if err != nil {
  64. l.Infoln("Loading HTTPS certificate:", err)
  65. l.Infoln("Creating new HTTPS certificate")
  66. // When generating the HTTPS certificate, use the system host name per
  67. // default. If that isn't available, use the "syncthing" default.
  68. var name string
  69. name, err = os.Hostname()
  70. if err != nil {
  71. name = tlsDefaultCommonName
  72. }
  73. newCertificate(confDir, "https-", name)
  74. cert, err = loadCert(confDir, "https-")
  75. }
  76. if err != nil {
  77. return err
  78. }
  79. tlsCfg := &tls.Config{
  80. Certificates: []tls.Certificate{cert},
  81. MinVersion: tls.VersionTLS10, // No SSLv3
  82. CipherSuites: []uint16{
  83. // No RC4
  84. tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  85. tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  86. tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  87. tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
  88. tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  89. tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
  90. tls.TLS_RSA_WITH_AES_128_CBC_SHA,
  91. tls.TLS_RSA_WITH_AES_256_CBC_SHA,
  92. tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
  93. tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA,
  94. },
  95. }
  96. rawListener, err := net.Listen("tcp", cfg.Address)
  97. if err != nil {
  98. return err
  99. }
  100. listener := &DowngradingListener{rawListener, tlsCfg}
  101. // The GET handlers
  102. getRestMux := http.NewServeMux()
  103. getRestMux.HandleFunc("/rest/ping", restPing)
  104. getRestMux.HandleFunc("/rest/completion", withModel(m, restGetCompletion))
  105. getRestMux.HandleFunc("/rest/config", restGetConfig)
  106. getRestMux.HandleFunc("/rest/config/sync", restGetConfigInSync)
  107. getRestMux.HandleFunc("/rest/connections", withModel(m, restGetConnections))
  108. getRestMux.HandleFunc("/rest/autocomplete/directory", restGetAutocompleteDirectory)
  109. getRestMux.HandleFunc("/rest/discovery", restGetDiscovery)
  110. getRestMux.HandleFunc("/rest/errors", restGetErrors)
  111. getRestMux.HandleFunc("/rest/events", restGetEvents)
  112. getRestMux.HandleFunc("/rest/ignores", withModel(m, restGetIgnores))
  113. getRestMux.HandleFunc("/rest/lang", restGetLang)
  114. getRestMux.HandleFunc("/rest/model", withModel(m, restGetModel))
  115. getRestMux.HandleFunc("/rest/need", withModel(m, restGetNeed))
  116. getRestMux.HandleFunc("/rest/deviceid", restGetDeviceID)
  117. getRestMux.HandleFunc("/rest/report", withModel(m, restGetReport))
  118. getRestMux.HandleFunc("/rest/system", restGetSystem)
  119. getRestMux.HandleFunc("/rest/upgrade", restGetUpgrade)
  120. getRestMux.HandleFunc("/rest/version", restGetVersion)
  121. getRestMux.HandleFunc("/rest/stats/device", withModel(m, restGetDeviceStats))
  122. getRestMux.HandleFunc("/rest/stats/folder", withModel(m, restGetFolderStats))
  123. // Debug endpoints, not for general use
  124. getRestMux.HandleFunc("/rest/debug/peerCompletion", withModel(m, restGetPeerCompletion))
  125. // The POST handlers
  126. postRestMux := http.NewServeMux()
  127. postRestMux.HandleFunc("/rest/ping", restPing)
  128. postRestMux.HandleFunc("/rest/config", withModel(m, restPostConfig))
  129. postRestMux.HandleFunc("/rest/discovery/hint", restPostDiscoveryHint)
  130. postRestMux.HandleFunc("/rest/error", restPostError)
  131. postRestMux.HandleFunc("/rest/error/clear", restClearErrors)
  132. postRestMux.HandleFunc("/rest/ignores", withModel(m, restPostIgnores))
  133. postRestMux.HandleFunc("/rest/model/override", withModel(m, restPostOverride))
  134. postRestMux.HandleFunc("/rest/reset", restPostReset)
  135. postRestMux.HandleFunc("/rest/restart", restPostRestart)
  136. postRestMux.HandleFunc("/rest/shutdown", restPostShutdown)
  137. postRestMux.HandleFunc("/rest/upgrade", restPostUpgrade)
  138. postRestMux.HandleFunc("/rest/scan", withModel(m, restPostScan))
  139. postRestMux.HandleFunc("/rest/bump", withModel(m, restPostBump))
  140. // A handler that splits requests between the two above and disables
  141. // caching
  142. restMux := noCacheMiddleware(getPostHandler(getRestMux, postRestMux))
  143. // The main routing handler
  144. mux := http.NewServeMux()
  145. mux.Handle("/rest/", restMux)
  146. mux.HandleFunc("/qr/", getQR)
  147. // Serve compiled in assets unless an asset directory was set (for development)
  148. mux.Handle("/", embeddedStatic(assetDir))
  149. // Wrap everything in CSRF protection. The /rest prefix should be
  150. // protected, other requests will grant cookies.
  151. handler := csrfMiddleware("/rest", cfg.APIKey, mux)
  152. // Add our version as a header to responses
  153. handler = withVersionMiddleware(handler)
  154. // Wrap everything in basic auth, if user/password is set.
  155. if len(cfg.User) > 0 && len(cfg.Password) > 0 {
  156. handler = basicAuthAndSessionMiddleware(cfg, handler)
  157. }
  158. // Redirect to HTTPS if we are supposed to
  159. if cfg.UseTLS {
  160. handler = redirectToHTTPSMiddleware(handler)
  161. }
  162. srv := http.Server{
  163. Handler: handler,
  164. ReadTimeout: 10 * time.Second,
  165. }
  166. go func() {
  167. err := srv.Serve(listener)
  168. if err != nil {
  169. panic(err)
  170. }
  171. }()
  172. return nil
  173. }
  174. func getPostHandler(get, post http.Handler) http.Handler {
  175. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  176. switch r.Method {
  177. case "GET":
  178. get.ServeHTTP(w, r)
  179. case "POST":
  180. post.ServeHTTP(w, r)
  181. default:
  182. http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
  183. }
  184. })
  185. }
  186. func redirectToHTTPSMiddleware(h http.Handler) http.Handler {
  187. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  188. // Add a generous access-control-allow-origin header since we may be
  189. // redirecting REST requests over protocols
  190. w.Header().Add("Access-Control-Allow-Origin", "*")
  191. if r.TLS == nil {
  192. // Redirect HTTP requests to HTTPS
  193. r.URL.Host = r.Host
  194. r.URL.Scheme = "https"
  195. http.Redirect(w, r, r.URL.String(), http.StatusFound)
  196. } else {
  197. h.ServeHTTP(w, r)
  198. }
  199. })
  200. }
  201. func noCacheMiddleware(h http.Handler) http.Handler {
  202. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  203. w.Header().Set("Cache-Control", "no-cache")
  204. h.ServeHTTP(w, r)
  205. })
  206. }
  207. func withVersionMiddleware(h http.Handler) http.Handler {
  208. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  209. w.Header().Set("X-Syncthing-Version", Version)
  210. h.ServeHTTP(w, r)
  211. })
  212. }
  213. func withModel(m *model.Model, h func(m *model.Model, w http.ResponseWriter, r *http.Request)) http.HandlerFunc {
  214. return func(w http.ResponseWriter, r *http.Request) {
  215. h(m, w, r)
  216. }
  217. }
  218. func restPing(w http.ResponseWriter, r *http.Request) {
  219. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  220. json.NewEncoder(w).Encode(map[string]string{
  221. "ping": "pong",
  222. })
  223. }
  224. func restGetVersion(w http.ResponseWriter, r *http.Request) {
  225. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  226. json.NewEncoder(w).Encode(map[string]string{
  227. "version": Version,
  228. "longVersion": LongVersion,
  229. "os": runtime.GOOS,
  230. "arch": runtime.GOARCH,
  231. })
  232. }
  233. func restGetCompletion(m *model.Model, w http.ResponseWriter, r *http.Request) {
  234. var qs = r.URL.Query()
  235. var folder = qs.Get("folder")
  236. var deviceStr = qs.Get("device")
  237. device, err := protocol.DeviceIDFromString(deviceStr)
  238. if err != nil {
  239. http.Error(w, err.Error(), 500)
  240. return
  241. }
  242. res := map[string]float64{
  243. "completion": m.Completion(device, folder),
  244. }
  245. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  246. json.NewEncoder(w).Encode(res)
  247. }
  248. func restGetModel(m *model.Model, w http.ResponseWriter, r *http.Request) {
  249. var qs = r.URL.Query()
  250. var folder = qs.Get("folder")
  251. var res = make(map[string]interface{})
  252. res["invalid"] = cfg.Folders()[folder].Invalid
  253. globalFiles, globalDeleted, globalBytes := m.GlobalSize(folder)
  254. res["globalFiles"], res["globalDeleted"], res["globalBytes"] = globalFiles, globalDeleted, globalBytes
  255. localFiles, localDeleted, localBytes := m.LocalSize(folder)
  256. res["localFiles"], res["localDeleted"], res["localBytes"] = localFiles, localDeleted, localBytes
  257. needFiles, needBytes := m.NeedSize(folder)
  258. res["needFiles"], res["needBytes"] = needFiles, needBytes
  259. res["inSyncFiles"], res["inSyncBytes"] = globalFiles-needFiles, globalBytes-needBytes
  260. res["state"], res["stateChanged"] = m.State(folder)
  261. res["version"] = m.CurrentLocalVersion(folder) + m.RemoteLocalVersion(folder)
  262. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  263. json.NewEncoder(w).Encode(res)
  264. }
  265. func restPostOverride(m *model.Model, w http.ResponseWriter, r *http.Request) {
  266. var qs = r.URL.Query()
  267. var folder = qs.Get("folder")
  268. go m.Override(folder)
  269. }
  270. func restGetNeed(m *model.Model, w http.ResponseWriter, r *http.Request) {
  271. var qs = r.URL.Query()
  272. var folder = qs.Get("folder")
  273. progress, queued, rest := m.NeedFolderFiles(folder, 100)
  274. // Convert the struct to a more loose structure, and inject the size.
  275. output := map[string][]map[string]interface{}{
  276. "progress": toNeedSlice(progress),
  277. "queued": toNeedSlice(queued),
  278. "rest": toNeedSlice(rest),
  279. }
  280. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  281. json.NewEncoder(w).Encode(output)
  282. }
  283. func restGetConnections(m *model.Model, w http.ResponseWriter, r *http.Request) {
  284. var res = m.ConnectionStats()
  285. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  286. json.NewEncoder(w).Encode(res)
  287. }
  288. func restGetDeviceStats(m *model.Model, w http.ResponseWriter, r *http.Request) {
  289. var res = m.DeviceStatistics()
  290. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  291. json.NewEncoder(w).Encode(res)
  292. }
  293. func restGetFolderStats(m *model.Model, w http.ResponseWriter, r *http.Request) {
  294. var res = m.FolderStatistics()
  295. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  296. json.NewEncoder(w).Encode(res)
  297. }
  298. func restGetConfig(w http.ResponseWriter, r *http.Request) {
  299. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  300. json.NewEncoder(w).Encode(cfg.Raw())
  301. }
  302. func restPostConfig(m *model.Model, w http.ResponseWriter, r *http.Request) {
  303. var newCfg config.Configuration
  304. err := json.NewDecoder(r.Body).Decode(&newCfg)
  305. if err != nil {
  306. l.Warnln("decoding posted config:", err)
  307. http.Error(w, err.Error(), 500)
  308. return
  309. }
  310. if newCfg.GUI.Password != cfg.GUI().Password {
  311. if newCfg.GUI.Password != "" {
  312. hash, err := bcrypt.GenerateFromPassword([]byte(newCfg.GUI.Password), 0)
  313. if err != nil {
  314. l.Warnln("bcrypting password:", err)
  315. http.Error(w, err.Error(), 500)
  316. return
  317. }
  318. newCfg.GUI.Password = string(hash)
  319. }
  320. }
  321. // Start or stop usage reporting as appropriate
  322. if curAcc := cfg.Options().URAccepted; newCfg.Options.URAccepted > curAcc {
  323. // UR was enabled
  324. newCfg.Options.URAccepted = usageReportVersion
  325. newCfg.Options.URUniqueID = randomString(8)
  326. err := sendUsageReport(m)
  327. if err != nil {
  328. l.Infoln("Usage report:", err)
  329. }
  330. go usageReportingLoop(m)
  331. } else if newCfg.Options.URAccepted < curAcc {
  332. // UR was disabled
  333. newCfg.Options.URAccepted = -1
  334. newCfg.Options.URUniqueID = ""
  335. stopUsageReporting()
  336. }
  337. // Activate and save
  338. configInSync = !config.ChangeRequiresRestart(cfg.Raw(), newCfg)
  339. cfg.Replace(newCfg)
  340. cfg.Save()
  341. }
  342. func restGetConfigInSync(w http.ResponseWriter, r *http.Request) {
  343. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  344. json.NewEncoder(w).Encode(map[string]bool{"configInSync": configInSync})
  345. }
  346. func restPostRestart(w http.ResponseWriter, r *http.Request) {
  347. flushResponse(`{"ok": "restarting"}`, w)
  348. go restart()
  349. }
  350. func restPostReset(w http.ResponseWriter, r *http.Request) {
  351. flushResponse(`{"ok": "resetting folders"}`, w)
  352. resetFolders()
  353. go restart()
  354. }
  355. func restPostShutdown(w http.ResponseWriter, r *http.Request) {
  356. flushResponse(`{"ok": "shutting down"}`, w)
  357. go shutdown()
  358. }
  359. func flushResponse(s string, w http.ResponseWriter) {
  360. w.Write([]byte(s + "\n"))
  361. f := w.(http.Flusher)
  362. f.Flush()
  363. }
  364. var cpuUsagePercent [10]float64 // The last ten seconds
  365. var cpuUsageLock sync.RWMutex
  366. func restGetSystem(w http.ResponseWriter, r *http.Request) {
  367. var m runtime.MemStats
  368. runtime.ReadMemStats(&m)
  369. tilde, _ := osutil.ExpandTilde("~")
  370. res := make(map[string]interface{})
  371. res["myID"] = myID.String()
  372. res["goroutines"] = runtime.NumGoroutine()
  373. res["alloc"] = m.Alloc
  374. res["sys"] = m.Sys - m.HeapReleased
  375. res["tilde"] = tilde
  376. if cfg.Options().GlobalAnnEnabled && discoverer != nil {
  377. res["extAnnounceOK"] = discoverer.ExtAnnounceOK()
  378. }
  379. cpuUsageLock.RLock()
  380. var cpusum float64
  381. for _, p := range cpuUsagePercent {
  382. cpusum += p
  383. }
  384. cpuUsageLock.RUnlock()
  385. res["cpuPercent"] = cpusum / 10
  386. res["pathSeparator"] = string(filepath.Separator)
  387. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  388. json.NewEncoder(w).Encode(res)
  389. }
  390. func restGetErrors(w http.ResponseWriter, r *http.Request) {
  391. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  392. guiErrorsMut.Lock()
  393. json.NewEncoder(w).Encode(map[string][]guiError{"errors": guiErrors})
  394. guiErrorsMut.Unlock()
  395. }
  396. func restPostError(w http.ResponseWriter, r *http.Request) {
  397. bs, _ := ioutil.ReadAll(r.Body)
  398. r.Body.Close()
  399. showGuiError(0, string(bs))
  400. }
  401. func restClearErrors(w http.ResponseWriter, r *http.Request) {
  402. guiErrorsMut.Lock()
  403. guiErrors = []guiError{}
  404. guiErrorsMut.Unlock()
  405. }
  406. func showGuiError(l logger.LogLevel, err string) {
  407. guiErrorsMut.Lock()
  408. guiErrors = append(guiErrors, guiError{time.Now(), err})
  409. if len(guiErrors) > 5 {
  410. guiErrors = guiErrors[len(guiErrors)-5:]
  411. }
  412. guiErrorsMut.Unlock()
  413. }
  414. func restPostDiscoveryHint(w http.ResponseWriter, r *http.Request) {
  415. var qs = r.URL.Query()
  416. var device = qs.Get("device")
  417. var addr = qs.Get("addr")
  418. if len(device) != 0 && len(addr) != 0 && discoverer != nil {
  419. discoverer.Hint(device, []string{addr})
  420. }
  421. }
  422. func restGetDiscovery(w http.ResponseWriter, r *http.Request) {
  423. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  424. devices := map[string][]discover.CacheEntry{}
  425. if discoverer != nil {
  426. // Device ids can't be marshalled as keys so we need to manually
  427. // rebuild this map using strings. Discoverer may be nil if discovery
  428. // has not started yet.
  429. for device, entries := range discoverer.All() {
  430. devices[device.String()] = entries
  431. }
  432. }
  433. json.NewEncoder(w).Encode(devices)
  434. }
  435. func restGetReport(m *model.Model, w http.ResponseWriter, r *http.Request) {
  436. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  437. json.NewEncoder(w).Encode(reportData(m))
  438. }
  439. func restGetIgnores(m *model.Model, w http.ResponseWriter, r *http.Request) {
  440. qs := r.URL.Query()
  441. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  442. ignores, patterns, err := m.GetIgnores(qs.Get("folder"))
  443. if err != nil {
  444. http.Error(w, err.Error(), 500)
  445. return
  446. }
  447. json.NewEncoder(w).Encode(map[string][]string{
  448. "ignore": ignores,
  449. "patterns": patterns,
  450. })
  451. }
  452. func restPostIgnores(m *model.Model, w http.ResponseWriter, r *http.Request) {
  453. qs := r.URL.Query()
  454. var data map[string][]string
  455. err := json.NewDecoder(r.Body).Decode(&data)
  456. r.Body.Close()
  457. if err != nil {
  458. http.Error(w, err.Error(), 500)
  459. return
  460. }
  461. err = m.SetIgnores(qs.Get("folder"), data["ignore"])
  462. if err != nil {
  463. http.Error(w, err.Error(), 500)
  464. return
  465. }
  466. restGetIgnores(m, w, r)
  467. }
  468. func restGetEvents(w http.ResponseWriter, r *http.Request) {
  469. qs := r.URL.Query()
  470. sinceStr := qs.Get("since")
  471. limitStr := qs.Get("limit")
  472. since, _ := strconv.Atoi(sinceStr)
  473. limit, _ := strconv.Atoi(limitStr)
  474. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  475. // Flush before blocking, to indicate that we've received the request
  476. // and that it should not be retried.
  477. f := w.(http.Flusher)
  478. f.Flush()
  479. evs := eventSub.Since(since, nil)
  480. if 0 < limit && limit < len(evs) {
  481. evs = evs[len(evs)-limit:]
  482. }
  483. json.NewEncoder(w).Encode(evs)
  484. }
  485. func restGetUpgrade(w http.ResponseWriter, r *http.Request) {
  486. if noUpgrade {
  487. http.Error(w, upgrade.ErrUpgradeUnsupported.Error(), 500)
  488. return
  489. }
  490. rel, err := upgrade.LatestRelease(strings.Contains(Version, "-beta"))
  491. if err != nil {
  492. http.Error(w, err.Error(), 500)
  493. return
  494. }
  495. res := make(map[string]interface{})
  496. res["running"] = Version
  497. res["latest"] = rel.Tag
  498. res["newer"] = upgrade.CompareVersions(rel.Tag, Version) == 1
  499. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  500. json.NewEncoder(w).Encode(res)
  501. }
  502. func restGetDeviceID(w http.ResponseWriter, r *http.Request) {
  503. qs := r.URL.Query()
  504. idStr := qs.Get("id")
  505. id, err := protocol.DeviceIDFromString(idStr)
  506. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  507. if err == nil {
  508. json.NewEncoder(w).Encode(map[string]string{
  509. "id": id.String(),
  510. })
  511. } else {
  512. json.NewEncoder(w).Encode(map[string]string{
  513. "error": err.Error(),
  514. })
  515. }
  516. }
  517. func restGetLang(w http.ResponseWriter, r *http.Request) {
  518. lang := r.Header.Get("Accept-Language")
  519. var langs []string
  520. for _, l := range strings.Split(lang, ",") {
  521. parts := strings.SplitN(l, ";", 2)
  522. langs = append(langs, strings.ToLower(strings.TrimSpace(parts[0])))
  523. }
  524. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  525. json.NewEncoder(w).Encode(langs)
  526. }
  527. func restPostUpgrade(w http.ResponseWriter, r *http.Request) {
  528. rel, err := upgrade.LatestRelease(strings.Contains(Version, "-beta"))
  529. if err != nil {
  530. l.Warnln("getting latest release:", err)
  531. http.Error(w, err.Error(), 500)
  532. return
  533. }
  534. if upgrade.CompareVersions(rel.Tag, Version) == 1 {
  535. err = upgrade.To(rel)
  536. if err != nil {
  537. l.Warnln("upgrading:", err)
  538. http.Error(w, err.Error(), 500)
  539. return
  540. }
  541. flushResponse(`{"ok": "restarting"}`, w)
  542. l.Infoln("Upgrading")
  543. stop <- exitUpgrading
  544. }
  545. }
  546. func restPostScan(m *model.Model, w http.ResponseWriter, r *http.Request) {
  547. qs := r.URL.Query()
  548. folder := qs.Get("folder")
  549. sub := qs.Get("sub")
  550. err := m.ScanFolderSub(folder, sub)
  551. if err != nil {
  552. http.Error(w, err.Error(), 500)
  553. }
  554. }
  555. func restPostBump(m *model.Model, w http.ResponseWriter, r *http.Request) {
  556. qs := r.URL.Query()
  557. folder := qs.Get("folder")
  558. file := qs.Get("file")
  559. m.BringToFront(folder, file)
  560. restGetNeed(m, w, r)
  561. }
  562. func getQR(w http.ResponseWriter, r *http.Request) {
  563. var qs = r.URL.Query()
  564. var text = qs.Get("text")
  565. code, err := qr.Encode(text, qr.M)
  566. if err != nil {
  567. http.Error(w, "Invalid", 500)
  568. return
  569. }
  570. w.Header().Set("Content-Type", "image/png")
  571. w.Write(code.PNG())
  572. }
  573. func restGetPeerCompletion(m *model.Model, w http.ResponseWriter, r *http.Request) {
  574. tot := map[string]float64{}
  575. count := map[string]float64{}
  576. for _, folder := range cfg.Folders() {
  577. for _, device := range folder.DeviceIDs() {
  578. deviceStr := device.String()
  579. if m.ConnectedTo(device) {
  580. tot[deviceStr] += m.Completion(device, folder.ID)
  581. } else {
  582. tot[deviceStr] = 0
  583. }
  584. count[deviceStr]++
  585. }
  586. }
  587. comp := map[string]int{}
  588. for device := range tot {
  589. comp[device] = int(tot[device] / count[device])
  590. }
  591. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  592. json.NewEncoder(w).Encode(comp)
  593. }
  594. func restGetAutocompleteDirectory(w http.ResponseWriter, r *http.Request) {
  595. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  596. qs := r.URL.Query()
  597. current := qs.Get("current")
  598. search, _ := osutil.ExpandTilde(current)
  599. pathSeparator := string(os.PathSeparator)
  600. if strings.HasSuffix(current, pathSeparator) && !strings.HasSuffix(search, pathSeparator) {
  601. search = search + pathSeparator
  602. }
  603. subdirectories, _ := filepath.Glob(search + "*")
  604. ret := make([]string, 0, 10)
  605. for _, subdirectory := range subdirectories {
  606. info, err := os.Stat(subdirectory)
  607. if err == nil && info.IsDir() {
  608. ret = append(ret, subdirectory+pathSeparator)
  609. if len(ret) > 9 {
  610. break
  611. }
  612. }
  613. }
  614. json.NewEncoder(w).Encode(ret)
  615. }
  616. func embeddedStatic(assetDir string) http.Handler {
  617. assets := auto.Assets()
  618. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  619. file := r.URL.Path
  620. if file[0] == '/' {
  621. file = file[1:]
  622. }
  623. if len(file) == 0 {
  624. file = "index.html"
  625. }
  626. if assetDir != "" {
  627. p := filepath.Join(assetDir, filepath.FromSlash(file))
  628. _, err := os.Stat(p)
  629. if err == nil {
  630. http.ServeFile(w, r, p)
  631. return
  632. }
  633. }
  634. bs, ok := assets[file]
  635. if !ok {
  636. http.NotFound(w, r)
  637. return
  638. }
  639. mtype := mimeTypeForFile(file)
  640. if len(mtype) != 0 {
  641. w.Header().Set("Content-Type", mtype)
  642. }
  643. w.Header().Set("Content-Length", fmt.Sprintf("%d", len(bs)))
  644. w.Header().Set("Last-Modified", modt)
  645. w.Write(bs)
  646. })
  647. }
  648. func mimeTypeForFile(file string) string {
  649. // We use a built in table of the common types since the system
  650. // TypeByExtension might be unreliable. But if we don't know, we delegate
  651. // to the system.
  652. ext := filepath.Ext(file)
  653. switch ext {
  654. case ".htm", ".html":
  655. return "text/html"
  656. case ".css":
  657. return "text/css"
  658. case ".js":
  659. return "application/javascript"
  660. case ".json":
  661. return "application/json"
  662. case ".png":
  663. return "image/png"
  664. case ".ttf":
  665. return "application/x-font-ttf"
  666. case ".woff":
  667. return "application/x-font-woff"
  668. default:
  669. return mime.TypeByExtension(ext)
  670. }
  671. }
  672. func toNeedSlice(fs []files.FileInfoTruncated) []map[string]interface{} {
  673. output := make([]map[string]interface{}, len(fs))
  674. for i, file := range fs {
  675. output[i] = map[string]interface{}{
  676. "Name": file.Name,
  677. "Flags": file.Flags,
  678. "Modified": file.Modified,
  679. "Version": file.Version,
  680. "LocalVersion": file.LocalVersion,
  681. "NumBlocks": file.NumBlocks,
  682. "Size": files.BlocksToSize(file.NumBlocks),
  683. }
  684. }
  685. return output
  686. }