gui.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  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. package main
  7. import (
  8. "bytes"
  9. "compress/gzip"
  10. "crypto/tls"
  11. "encoding/json"
  12. "fmt"
  13. "io/ioutil"
  14. "mime"
  15. "net"
  16. "net/http"
  17. "os"
  18. "path/filepath"
  19. "reflect"
  20. "runtime"
  21. "strconv"
  22. "strings"
  23. "time"
  24. "github.com/calmh/logger"
  25. "github.com/syncthing/protocol"
  26. "github.com/syncthing/syncthing/internal/auto"
  27. "github.com/syncthing/syncthing/internal/config"
  28. "github.com/syncthing/syncthing/internal/db"
  29. "github.com/syncthing/syncthing/internal/discover"
  30. "github.com/syncthing/syncthing/internal/events"
  31. "github.com/syncthing/syncthing/internal/model"
  32. "github.com/syncthing/syncthing/internal/osutil"
  33. "github.com/syncthing/syncthing/internal/sync"
  34. "github.com/syncthing/syncthing/internal/upgrade"
  35. "github.com/vitrun/qart/qr"
  36. "golang.org/x/crypto/bcrypt"
  37. )
  38. type guiError struct {
  39. Time time.Time `json:"time"`
  40. Error string `json:"error"`
  41. }
  42. var (
  43. configInSync = true
  44. guiErrors = []guiError{}
  45. guiErrorsMut = sync.NewMutex()
  46. startTime = time.Now()
  47. eventSub *events.BufferedSubscription
  48. )
  49. type apiSvc struct {
  50. cfg config.GUIConfiguration
  51. assetDir string
  52. model *model.Model
  53. fss *folderSummarySvc
  54. listener net.Listener
  55. }
  56. func newAPISvc(cfg config.GUIConfiguration, assetDir string, m *model.Model) (*apiSvc, error) {
  57. svc := &apiSvc{
  58. cfg: cfg,
  59. assetDir: assetDir,
  60. model: m,
  61. fss: newFolderSummarySvc(m),
  62. }
  63. var err error
  64. svc.listener, err = svc.getListener()
  65. return svc, err
  66. }
  67. func (s *apiSvc) getListener() (net.Listener, error) {
  68. cert, err := tls.LoadX509KeyPair(locations[locHTTPSCertFile], locations[locHTTPSKeyFile])
  69. if err != nil {
  70. l.Infoln("Loading HTTPS certificate:", err)
  71. l.Infoln("Creating new HTTPS certificate")
  72. // When generating the HTTPS certificate, use the system host name per
  73. // default. If that isn't available, use the "syncthing" default.
  74. var name string
  75. name, err = os.Hostname()
  76. if err != nil {
  77. name = tlsDefaultCommonName
  78. }
  79. cert, err = newCertificate(locations[locHTTPSCertFile], locations[locHTTPSKeyFile], name)
  80. }
  81. if err != nil {
  82. return nil, err
  83. }
  84. tlsCfg := &tls.Config{
  85. Certificates: []tls.Certificate{cert},
  86. MinVersion: tls.VersionTLS10, // No SSLv3
  87. CipherSuites: []uint16{
  88. // No RC4
  89. tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  90. tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  91. tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
  92. tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
  93. tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
  94. tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
  95. tls.TLS_RSA_WITH_AES_128_CBC_SHA,
  96. tls.TLS_RSA_WITH_AES_256_CBC_SHA,
  97. tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
  98. tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA,
  99. },
  100. }
  101. rawListener, err := net.Listen("tcp", s.cfg.Address)
  102. if err != nil {
  103. return nil, err
  104. }
  105. listener := &DowngradingListener{rawListener, tlsCfg}
  106. return listener, nil
  107. }
  108. func (s *apiSvc) Serve() {
  109. l.AddHandler(logger.LevelWarn, s.showGuiError)
  110. sub := events.Default.Subscribe(events.AllEvents)
  111. eventSub = events.NewBufferedSubscription(sub, 1000)
  112. defer events.Default.Unsubscribe(sub)
  113. // The GET handlers
  114. getRestMux := http.NewServeMux()
  115. getRestMux.HandleFunc("/rest/db/completion", s.getDBCompletion) // device folder
  116. getRestMux.HandleFunc("/rest/db/file", s.getDBFile) // folder file
  117. getRestMux.HandleFunc("/rest/db/ignores", s.getDBIgnores) // folder
  118. getRestMux.HandleFunc("/rest/db/need", s.getDBNeed) // folder [perpage] [page]
  119. getRestMux.HandleFunc("/rest/db/status", s.getDBStatus) // folder
  120. getRestMux.HandleFunc("/rest/db/browse", s.getDBBrowse) // folder [prefix] [dirsonly] [levels]
  121. getRestMux.HandleFunc("/rest/events", s.getEvents) // since [limit]
  122. getRestMux.HandleFunc("/rest/stats/device", s.getDeviceStats) // -
  123. getRestMux.HandleFunc("/rest/stats/folder", s.getFolderStats) // -
  124. getRestMux.HandleFunc("/rest/svc/deviceid", s.getDeviceID) // id
  125. getRestMux.HandleFunc("/rest/svc/lang", s.getLang) // -
  126. getRestMux.HandleFunc("/rest/svc/report", s.getReport) // -
  127. getRestMux.HandleFunc("/rest/system/browse", s.getSystemBrowse) // current
  128. getRestMux.HandleFunc("/rest/system/config", s.getSystemConfig) // -
  129. getRestMux.HandleFunc("/rest/system/config/insync", s.getSystemConfigInsync) // -
  130. getRestMux.HandleFunc("/rest/system/connections", s.getSystemConnections) // -
  131. getRestMux.HandleFunc("/rest/system/discovery", s.getSystemDiscovery) // -
  132. getRestMux.HandleFunc("/rest/system/error", s.getSystemError) // -
  133. getRestMux.HandleFunc("/rest/system/ping", s.restPing) // -
  134. getRestMux.HandleFunc("/rest/system/status", s.getSystemStatus) // -
  135. getRestMux.HandleFunc("/rest/system/upgrade", s.getSystemUpgrade) // -
  136. getRestMux.HandleFunc("/rest/system/version", s.getSystemVersion) // -
  137. // The POST handlers
  138. postRestMux := http.NewServeMux()
  139. postRestMux.HandleFunc("/rest/db/prio", s.postDBPrio) // folder file [perpage] [page]
  140. postRestMux.HandleFunc("/rest/db/ignores", s.postDBIgnores) // folder
  141. postRestMux.HandleFunc("/rest/db/override", s.postDBOverride) // folder
  142. postRestMux.HandleFunc("/rest/db/scan", s.postDBScan) // folder [sub...] [delay]
  143. postRestMux.HandleFunc("/rest/system/config", s.postSystemConfig) // <body>
  144. postRestMux.HandleFunc("/rest/system/discovery", s.postSystemDiscovery) // device addr
  145. postRestMux.HandleFunc("/rest/system/error", s.postSystemError) // <body>
  146. postRestMux.HandleFunc("/rest/system/error/clear", s.postSystemErrorClear) // -
  147. postRestMux.HandleFunc("/rest/system/ping", s.restPing) // -
  148. postRestMux.HandleFunc("/rest/system/reset", s.postSystemReset) // [folder]
  149. postRestMux.HandleFunc("/rest/system/restart", s.postSystemRestart) // -
  150. postRestMux.HandleFunc("/rest/system/shutdown", s.postSystemShutdown) // -
  151. postRestMux.HandleFunc("/rest/system/upgrade", s.postSystemUpgrade) // -
  152. // Debug endpoints, not for general use
  153. getRestMux.HandleFunc("/rest/debug/peerCompletion", s.getPeerCompletion)
  154. // A handler that splits requests between the two above and disables
  155. // caching
  156. restMux := noCacheMiddleware(getPostHandler(getRestMux, postRestMux))
  157. // The main routing handler
  158. mux := http.NewServeMux()
  159. mux.Handle("/rest/", restMux)
  160. mux.HandleFunc("/qr/", s.getQR)
  161. // Serve compiled in assets unless an asset directory was set (for development)
  162. mux.Handle("/", embeddedStatic{
  163. assetDir: s.assetDir,
  164. assets: auto.Assets(),
  165. })
  166. // Wrap everything in CSRF protection. The /rest prefix should be
  167. // protected, other requests will grant cookies.
  168. handler := csrfMiddleware("/rest", s.cfg.APIKey, mux)
  169. // Add our version as a header to responses
  170. handler = withVersionMiddleware(handler)
  171. // Wrap everything in basic auth, if user/password is set.
  172. if len(s.cfg.User) > 0 && len(s.cfg.Password) > 0 {
  173. handler = basicAuthAndSessionMiddleware(s.cfg, handler)
  174. }
  175. // Redirect to HTTPS if we are supposed to
  176. if s.cfg.UseTLS {
  177. handler = redirectToHTTPSMiddleware(handler)
  178. }
  179. if debugHTTP {
  180. handler = debugMiddleware(handler)
  181. }
  182. srv := http.Server{
  183. Handler: handler,
  184. ReadTimeout: 10 * time.Second,
  185. }
  186. s.fss.ServeBackground()
  187. err := srv.Serve(s.listener)
  188. l.Warnln("API:", err)
  189. }
  190. func (s *apiSvc) Stop() {
  191. s.listener.Close()
  192. s.fss.Stop()
  193. }
  194. func getPostHandler(get, post http.Handler) http.Handler {
  195. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  196. switch r.Method {
  197. case "GET":
  198. get.ServeHTTP(w, r)
  199. case "POST":
  200. post.ServeHTTP(w, r)
  201. default:
  202. http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
  203. }
  204. })
  205. }
  206. func debugMiddleware(h http.Handler) http.Handler {
  207. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  208. t0 := time.Now()
  209. h.ServeHTTP(w, r)
  210. ms := 1000 * time.Since(t0).Seconds()
  211. // The variable `w` is most likely a *http.response, which we can't do
  212. // much with since it's a non exported type. We can however peek into
  213. // it with reflection to get at the status code and number of bytes
  214. // written.
  215. var status, written int64
  216. if rw := reflect.Indirect(reflect.ValueOf(w)); rw.IsValid() && rw.Kind() == reflect.Struct {
  217. if rf := rw.FieldByName("status"); rf.IsValid() && rf.Kind() == reflect.Int {
  218. status = rf.Int()
  219. }
  220. if rf := rw.FieldByName("written"); rf.IsValid() && rf.Kind() == reflect.Int64 {
  221. written = rf.Int()
  222. }
  223. }
  224. l.Debugf("http: %s %q: status %d, %d bytes in %.02f ms", r.Method, r.URL.String(), status, written, ms)
  225. })
  226. }
  227. func redirectToHTTPSMiddleware(h http.Handler) http.Handler {
  228. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  229. // Add a generous access-control-allow-origin header since we may be
  230. // redirecting REST requests over protocols
  231. w.Header().Add("Access-Control-Allow-Origin", "*")
  232. if r.TLS == nil {
  233. // Redirect HTTP requests to HTTPS
  234. r.URL.Host = r.Host
  235. r.URL.Scheme = "https"
  236. http.Redirect(w, r, r.URL.String(), http.StatusFound)
  237. } else {
  238. h.ServeHTTP(w, r)
  239. }
  240. })
  241. }
  242. func noCacheMiddleware(h http.Handler) http.Handler {
  243. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  244. w.Header().Set("Cache-Control", "max-age=0, no-cache, no-store")
  245. w.Header().Set("Expires", time.Now().UTC().Format(http.TimeFormat))
  246. w.Header().Set("Pragma", "no-cache")
  247. h.ServeHTTP(w, r)
  248. })
  249. }
  250. func withVersionMiddleware(h http.Handler) http.Handler {
  251. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  252. w.Header().Set("X-Syncthing-Version", Version)
  253. h.ServeHTTP(w, r)
  254. })
  255. }
  256. func (s *apiSvc) restPing(w http.ResponseWriter, r *http.Request) {
  257. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  258. json.NewEncoder(w).Encode(map[string]string{
  259. "ping": "pong",
  260. })
  261. }
  262. func (s *apiSvc) getSystemVersion(w http.ResponseWriter, r *http.Request) {
  263. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  264. json.NewEncoder(w).Encode(map[string]string{
  265. "version": Version,
  266. "longVersion": LongVersion,
  267. "os": runtime.GOOS,
  268. "arch": runtime.GOARCH,
  269. })
  270. }
  271. func (s *apiSvc) getDBBrowse(w http.ResponseWriter, r *http.Request) {
  272. qs := r.URL.Query()
  273. folder := qs.Get("folder")
  274. prefix := qs.Get("prefix")
  275. dirsonly := qs.Get("dirsonly") != ""
  276. levels, err := strconv.Atoi(qs.Get("levels"))
  277. if err != nil {
  278. levels = -1
  279. }
  280. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  281. tree := s.model.GlobalDirectoryTree(folder, prefix, levels, dirsonly)
  282. json.NewEncoder(w).Encode(tree)
  283. }
  284. func (s *apiSvc) getDBCompletion(w http.ResponseWriter, r *http.Request) {
  285. var qs = r.URL.Query()
  286. var folder = qs.Get("folder")
  287. var deviceStr = qs.Get("device")
  288. device, err := protocol.DeviceIDFromString(deviceStr)
  289. if err != nil {
  290. http.Error(w, err.Error(), 500)
  291. return
  292. }
  293. res := map[string]float64{
  294. "completion": s.model.Completion(device, folder),
  295. }
  296. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  297. json.NewEncoder(w).Encode(res)
  298. }
  299. func (s *apiSvc) getDBStatus(w http.ResponseWriter, r *http.Request) {
  300. qs := r.URL.Query()
  301. folder := qs.Get("folder")
  302. res := folderSummary(s.model, folder)
  303. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  304. json.NewEncoder(w).Encode(res)
  305. }
  306. func folderSummary(m *model.Model, folder string) map[string]interface{} {
  307. var res = make(map[string]interface{})
  308. res["invalid"] = cfg.Folders()[folder].Invalid
  309. globalFiles, globalDeleted, globalBytes := m.GlobalSize(folder)
  310. res["globalFiles"], res["globalDeleted"], res["globalBytes"] = globalFiles, globalDeleted, globalBytes
  311. localFiles, localDeleted, localBytes := m.LocalSize(folder)
  312. res["localFiles"], res["localDeleted"], res["localBytes"] = localFiles, localDeleted, localBytes
  313. needFiles, needBytes := m.NeedSize(folder)
  314. res["needFiles"], res["needBytes"] = needFiles, needBytes
  315. res["inSyncFiles"], res["inSyncBytes"] = globalFiles-needFiles, globalBytes-needBytes
  316. var err error
  317. res["state"], res["stateChanged"], err = m.State(folder)
  318. if err != nil {
  319. res["error"] = err.Error()
  320. }
  321. res["version"] = m.CurrentLocalVersion(folder) + m.RemoteLocalVersion(folder)
  322. ignorePatterns, _, _ := m.GetIgnores(folder)
  323. res["ignorePatterns"] = false
  324. for _, line := range ignorePatterns {
  325. if len(line) > 0 && !strings.HasPrefix(line, "//") {
  326. res["ignorePatterns"] = true
  327. break
  328. }
  329. }
  330. return res
  331. }
  332. func (s *apiSvc) postDBOverride(w http.ResponseWriter, r *http.Request) {
  333. var qs = r.URL.Query()
  334. var folder = qs.Get("folder")
  335. go s.model.Override(folder)
  336. }
  337. func (s *apiSvc) getDBNeed(w http.ResponseWriter, r *http.Request) {
  338. qs := r.URL.Query()
  339. folder := qs.Get("folder")
  340. page, err := strconv.Atoi(qs.Get("page"))
  341. if err != nil || page < 1 {
  342. page = 1
  343. }
  344. perpage, err := strconv.Atoi(qs.Get("perpage"))
  345. if err != nil || perpage < 1 {
  346. perpage = 1 << 16
  347. }
  348. progress, queued, rest, total := s.model.NeedFolderFiles(folder, page, perpage)
  349. // Convert the struct to a more loose structure, and inject the size.
  350. output := map[string]interface{}{
  351. "progress": s.toNeedSlice(progress),
  352. "queued": s.toNeedSlice(queued),
  353. "rest": s.toNeedSlice(rest),
  354. "total": total,
  355. "page": page,
  356. "perpage": perpage,
  357. }
  358. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  359. json.NewEncoder(w).Encode(output)
  360. }
  361. func (s *apiSvc) getSystemConnections(w http.ResponseWriter, r *http.Request) {
  362. var res = s.model.ConnectionStats()
  363. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  364. json.NewEncoder(w).Encode(res)
  365. }
  366. func (s *apiSvc) getDeviceStats(w http.ResponseWriter, r *http.Request) {
  367. var res = s.model.DeviceStatistics()
  368. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  369. json.NewEncoder(w).Encode(res)
  370. }
  371. func (s *apiSvc) getFolderStats(w http.ResponseWriter, r *http.Request) {
  372. var res = s.model.FolderStatistics()
  373. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  374. json.NewEncoder(w).Encode(res)
  375. }
  376. func (s *apiSvc) getDBFile(w http.ResponseWriter, r *http.Request) {
  377. qs := r.URL.Query()
  378. folder := qs.Get("folder")
  379. file := qs.Get("file")
  380. gf, _ := s.model.CurrentGlobalFile(folder, file)
  381. lf, _ := s.model.CurrentFolderFile(folder, file)
  382. av := s.model.Availability(folder, file)
  383. json.NewEncoder(w).Encode(map[string]interface{}{
  384. "global": jsonFileInfo(gf),
  385. "local": jsonFileInfo(lf),
  386. "availability": av,
  387. })
  388. }
  389. func (s *apiSvc) getSystemConfig(w http.ResponseWriter, r *http.Request) {
  390. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  391. json.NewEncoder(w).Encode(cfg.Raw())
  392. }
  393. func (s *apiSvc) postSystemConfig(w http.ResponseWriter, r *http.Request) {
  394. var newCfg config.Configuration
  395. err := json.NewDecoder(r.Body).Decode(&newCfg)
  396. if err != nil {
  397. l.Warnln("decoding posted config:", err)
  398. http.Error(w, err.Error(), 500)
  399. return
  400. }
  401. if newCfg.GUI.Password != cfg.GUI().Password {
  402. if newCfg.GUI.Password != "" {
  403. hash, err := bcrypt.GenerateFromPassword([]byte(newCfg.GUI.Password), 0)
  404. if err != nil {
  405. l.Warnln("bcrypting password:", err)
  406. http.Error(w, err.Error(), 500)
  407. return
  408. }
  409. newCfg.GUI.Password = string(hash)
  410. }
  411. }
  412. // Start or stop usage reporting as appropriate
  413. if curAcc := cfg.Options().URAccepted; newCfg.Options.URAccepted > curAcc {
  414. // UR was enabled
  415. newCfg.Options.URAccepted = usageReportVersion
  416. newCfg.Options.URUniqueID = randomString(8)
  417. err := sendUsageReport(s.model)
  418. if err != nil {
  419. l.Infoln("Usage report:", err)
  420. }
  421. go usageReportingLoop(s.model)
  422. } else if newCfg.Options.URAccepted < curAcc {
  423. // UR was disabled
  424. newCfg.Options.URAccepted = -1
  425. newCfg.Options.URUniqueID = ""
  426. stopUsageReporting()
  427. }
  428. // Activate and save
  429. configInSync = !config.ChangeRequiresRestart(cfg.Raw(), newCfg)
  430. cfg.Replace(newCfg)
  431. cfg.Save()
  432. }
  433. func (s *apiSvc) getSystemConfigInsync(w http.ResponseWriter, r *http.Request) {
  434. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  435. json.NewEncoder(w).Encode(map[string]bool{"configInSync": configInSync})
  436. }
  437. func (s *apiSvc) postSystemRestart(w http.ResponseWriter, r *http.Request) {
  438. s.flushResponse(`{"ok": "restarting"}`, w)
  439. go restart()
  440. }
  441. func (s *apiSvc) postSystemReset(w http.ResponseWriter, r *http.Request) {
  442. var qs = r.URL.Query()
  443. folder := qs.Get("folder")
  444. var err error
  445. if len(folder) == 0 {
  446. err = resetDB()
  447. } else {
  448. err = s.model.ResetFolder(folder)
  449. }
  450. if err != nil {
  451. http.Error(w, err.Error(), 500)
  452. return
  453. }
  454. if len(folder) == 0 {
  455. s.flushResponse(`{"ok": "resetting database"}`, w)
  456. } else {
  457. s.flushResponse(`{"ok": "resetting folder " + folder}`, w)
  458. }
  459. go restart()
  460. }
  461. func (s *apiSvc) postSystemShutdown(w http.ResponseWriter, r *http.Request) {
  462. s.flushResponse(`{"ok": "shutting down"}`, w)
  463. go shutdown()
  464. }
  465. func (s *apiSvc) flushResponse(resp string, w http.ResponseWriter) {
  466. w.Write([]byte(resp + "\n"))
  467. f := w.(http.Flusher)
  468. f.Flush()
  469. }
  470. var cpuUsagePercent [10]float64 // The last ten seconds
  471. var cpuUsageLock = sync.NewRWMutex()
  472. func (s *apiSvc) getSystemStatus(w http.ResponseWriter, r *http.Request) {
  473. var m runtime.MemStats
  474. runtime.ReadMemStats(&m)
  475. tilde, _ := osutil.ExpandTilde("~")
  476. res := make(map[string]interface{})
  477. res["myID"] = myID.String()
  478. res["goroutines"] = runtime.NumGoroutine()
  479. res["alloc"] = m.Alloc
  480. res["sys"] = m.Sys - m.HeapReleased
  481. res["tilde"] = tilde
  482. if cfg.Options().GlobalAnnEnabled && discoverer != nil {
  483. res["extAnnounceOK"] = discoverer.ExtAnnounceOK()
  484. }
  485. cpuUsageLock.RLock()
  486. var cpusum float64
  487. for _, p := range cpuUsagePercent {
  488. cpusum += p
  489. }
  490. cpuUsageLock.RUnlock()
  491. res["cpuPercent"] = cpusum / float64(len(cpuUsagePercent)) / float64(runtime.NumCPU())
  492. res["pathSeparator"] = string(filepath.Separator)
  493. res["uptime"] = int(time.Since(startTime).Seconds())
  494. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  495. json.NewEncoder(w).Encode(res)
  496. }
  497. func (s *apiSvc) getSystemError(w http.ResponseWriter, r *http.Request) {
  498. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  499. guiErrorsMut.Lock()
  500. json.NewEncoder(w).Encode(map[string][]guiError{"errors": guiErrors})
  501. guiErrorsMut.Unlock()
  502. }
  503. func (s *apiSvc) postSystemError(w http.ResponseWriter, r *http.Request) {
  504. bs, _ := ioutil.ReadAll(r.Body)
  505. r.Body.Close()
  506. s.showGuiError(0, string(bs))
  507. }
  508. func (s *apiSvc) postSystemErrorClear(w http.ResponseWriter, r *http.Request) {
  509. guiErrorsMut.Lock()
  510. guiErrors = []guiError{}
  511. guiErrorsMut.Unlock()
  512. }
  513. func (s *apiSvc) showGuiError(l logger.LogLevel, err string) {
  514. guiErrorsMut.Lock()
  515. guiErrors = append(guiErrors, guiError{time.Now(), err})
  516. if len(guiErrors) > 5 {
  517. guiErrors = guiErrors[len(guiErrors)-5:]
  518. }
  519. guiErrorsMut.Unlock()
  520. }
  521. func (s *apiSvc) postSystemDiscovery(w http.ResponseWriter, r *http.Request) {
  522. var qs = r.URL.Query()
  523. var device = qs.Get("device")
  524. var addr = qs.Get("addr")
  525. if len(device) != 0 && len(addr) != 0 && discoverer != nil {
  526. discoverer.Hint(device, []string{addr})
  527. }
  528. }
  529. func (s *apiSvc) getSystemDiscovery(w http.ResponseWriter, r *http.Request) {
  530. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  531. devices := map[string][]discover.CacheEntry{}
  532. if discoverer != nil {
  533. // Device ids can't be marshalled as keys so we need to manually
  534. // rebuild this map using strings. Discoverer may be nil if discovery
  535. // has not started yet.
  536. for device, entries := range discoverer.All() {
  537. devices[device.String()] = entries
  538. }
  539. }
  540. json.NewEncoder(w).Encode(devices)
  541. }
  542. func (s *apiSvc) getReport(w http.ResponseWriter, r *http.Request) {
  543. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  544. json.NewEncoder(w).Encode(reportData(s.model))
  545. }
  546. func (s *apiSvc) getDBIgnores(w http.ResponseWriter, r *http.Request) {
  547. qs := r.URL.Query()
  548. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  549. ignores, patterns, err := s.model.GetIgnores(qs.Get("folder"))
  550. if err != nil {
  551. http.Error(w, err.Error(), 500)
  552. return
  553. }
  554. json.NewEncoder(w).Encode(map[string][]string{
  555. "ignore": ignores,
  556. "patterns": patterns,
  557. })
  558. }
  559. func (s *apiSvc) postDBIgnores(w http.ResponseWriter, r *http.Request) {
  560. qs := r.URL.Query()
  561. var data map[string][]string
  562. err := json.NewDecoder(r.Body).Decode(&data)
  563. r.Body.Close()
  564. if err != nil {
  565. http.Error(w, err.Error(), 500)
  566. return
  567. }
  568. err = s.model.SetIgnores(qs.Get("folder"), data["ignore"])
  569. if err != nil {
  570. http.Error(w, err.Error(), 500)
  571. return
  572. }
  573. s.getDBIgnores(w, r)
  574. }
  575. func (s *apiSvc) getEvents(w http.ResponseWriter, r *http.Request) {
  576. qs := r.URL.Query()
  577. sinceStr := qs.Get("since")
  578. limitStr := qs.Get("limit")
  579. since, _ := strconv.Atoi(sinceStr)
  580. limit, _ := strconv.Atoi(limitStr)
  581. s.fss.gotEventRequest()
  582. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  583. // Flush before blocking, to indicate that we've received the request
  584. // and that it should not be retried.
  585. f := w.(http.Flusher)
  586. f.Flush()
  587. evs := eventSub.Since(since, nil)
  588. if 0 < limit && limit < len(evs) {
  589. evs = evs[len(evs)-limit:]
  590. }
  591. json.NewEncoder(w).Encode(evs)
  592. }
  593. func (s *apiSvc) getSystemUpgrade(w http.ResponseWriter, r *http.Request) {
  594. if noUpgrade {
  595. http.Error(w, upgrade.ErrUpgradeUnsupported.Error(), 500)
  596. return
  597. }
  598. rel, err := upgrade.LatestRelease(Version)
  599. if err != nil {
  600. http.Error(w, err.Error(), 500)
  601. return
  602. }
  603. res := make(map[string]interface{})
  604. res["running"] = Version
  605. res["latest"] = rel.Tag
  606. res["newer"] = upgrade.CompareVersions(rel.Tag, Version) == upgrade.Newer
  607. res["majorNewer"] = upgrade.CompareVersions(rel.Tag, Version) == upgrade.MajorNewer
  608. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  609. json.NewEncoder(w).Encode(res)
  610. }
  611. func (s *apiSvc) getDeviceID(w http.ResponseWriter, r *http.Request) {
  612. qs := r.URL.Query()
  613. idStr := qs.Get("id")
  614. id, err := protocol.DeviceIDFromString(idStr)
  615. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  616. if err == nil {
  617. json.NewEncoder(w).Encode(map[string]string{
  618. "id": id.String(),
  619. })
  620. } else {
  621. json.NewEncoder(w).Encode(map[string]string{
  622. "error": err.Error(),
  623. })
  624. }
  625. }
  626. func (s *apiSvc) getLang(w http.ResponseWriter, r *http.Request) {
  627. lang := r.Header.Get("Accept-Language")
  628. var langs []string
  629. for _, l := range strings.Split(lang, ",") {
  630. parts := strings.SplitN(l, ";", 2)
  631. langs = append(langs, strings.ToLower(strings.TrimSpace(parts[0])))
  632. }
  633. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  634. json.NewEncoder(w).Encode(langs)
  635. }
  636. func (s *apiSvc) postSystemUpgrade(w http.ResponseWriter, r *http.Request) {
  637. rel, err := upgrade.LatestRelease(Version)
  638. if err != nil {
  639. l.Warnln("getting latest release:", err)
  640. http.Error(w, err.Error(), 500)
  641. return
  642. }
  643. if upgrade.CompareVersions(rel.Tag, Version) > upgrade.Equal {
  644. err = upgrade.To(rel)
  645. if err != nil {
  646. l.Warnln("upgrading:", err)
  647. http.Error(w, err.Error(), 500)
  648. return
  649. }
  650. s.flushResponse(`{"ok": "restarting"}`, w)
  651. l.Infoln("Upgrading")
  652. stop <- exitUpgrading
  653. }
  654. }
  655. func (s *apiSvc) postDBScan(w http.ResponseWriter, r *http.Request) {
  656. qs := r.URL.Query()
  657. folder := qs.Get("folder")
  658. if folder != "" {
  659. subs := qs["sub"]
  660. err := s.model.ScanFolderSubs(folder, subs)
  661. if err != nil {
  662. http.Error(w, err.Error(), 500)
  663. return
  664. }
  665. } else {
  666. errors := s.model.ScanFolders()
  667. if len(errors) > 0 {
  668. http.Error(w, "Error scanning folders", 500)
  669. json.NewEncoder(w).Encode(errors)
  670. return
  671. }
  672. }
  673. nextStr := qs.Get("next")
  674. next, err := strconv.Atoi(nextStr)
  675. if err == nil {
  676. s.model.DelayScan(folder, time.Duration(next)*time.Second)
  677. }
  678. }
  679. func (s *apiSvc) postDBPrio(w http.ResponseWriter, r *http.Request) {
  680. qs := r.URL.Query()
  681. folder := qs.Get("folder")
  682. file := qs.Get("file")
  683. s.model.BringToFront(folder, file)
  684. s.getDBNeed(w, r)
  685. }
  686. func (s *apiSvc) getQR(w http.ResponseWriter, r *http.Request) {
  687. var qs = r.URL.Query()
  688. var text = qs.Get("text")
  689. code, err := qr.Encode(text, qr.M)
  690. if err != nil {
  691. http.Error(w, "Invalid", 500)
  692. return
  693. }
  694. w.Header().Set("Content-Type", "image/png")
  695. w.Write(code.PNG())
  696. }
  697. func (s *apiSvc) getPeerCompletion(w http.ResponseWriter, r *http.Request) {
  698. tot := map[string]float64{}
  699. count := map[string]float64{}
  700. for _, folder := range cfg.Folders() {
  701. for _, device := range folder.DeviceIDs() {
  702. deviceStr := device.String()
  703. if s.model.ConnectedTo(device) {
  704. tot[deviceStr] += s.model.Completion(device, folder.ID)
  705. } else {
  706. tot[deviceStr] = 0
  707. }
  708. count[deviceStr]++
  709. }
  710. }
  711. comp := map[string]int{}
  712. for device := range tot {
  713. comp[device] = int(tot[device] / count[device])
  714. }
  715. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  716. json.NewEncoder(w).Encode(comp)
  717. }
  718. func (s *apiSvc) getSystemBrowse(w http.ResponseWriter, r *http.Request) {
  719. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  720. qs := r.URL.Query()
  721. current := qs.Get("current")
  722. search, _ := osutil.ExpandTilde(current)
  723. pathSeparator := string(os.PathSeparator)
  724. if strings.HasSuffix(current, pathSeparator) && !strings.HasSuffix(search, pathSeparator) {
  725. search = search + pathSeparator
  726. }
  727. subdirectories, _ := osutil.Glob(search + "*")
  728. ret := make([]string, 0, 10)
  729. for _, subdirectory := range subdirectories {
  730. info, err := os.Stat(subdirectory)
  731. if err == nil && info.IsDir() {
  732. ret = append(ret, subdirectory+pathSeparator)
  733. if len(ret) > 9 {
  734. break
  735. }
  736. }
  737. }
  738. json.NewEncoder(w).Encode(ret)
  739. }
  740. type embeddedStatic struct {
  741. assetDir string
  742. assets map[string][]byte
  743. }
  744. func (s embeddedStatic) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  745. file := r.URL.Path
  746. if file[0] == '/' {
  747. file = file[1:]
  748. }
  749. if len(file) == 0 {
  750. file = "index.html"
  751. }
  752. if s.assetDir != "" {
  753. p := filepath.Join(s.assetDir, filepath.FromSlash(file))
  754. _, err := os.Stat(p)
  755. if err == nil {
  756. http.ServeFile(w, r, p)
  757. return
  758. }
  759. }
  760. bs, ok := s.assets[file]
  761. if !ok {
  762. http.NotFound(w, r)
  763. return
  764. }
  765. mtype := s.mimeTypeForFile(file)
  766. if len(mtype) != 0 {
  767. w.Header().Set("Content-Type", mtype)
  768. }
  769. if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
  770. w.Header().Set("Content-Encoding", "gzip")
  771. } else {
  772. // ungzip if browser not send gzip accepted header
  773. var gr *gzip.Reader
  774. gr, _ = gzip.NewReader(bytes.NewReader(bs))
  775. bs, _ = ioutil.ReadAll(gr)
  776. gr.Close()
  777. }
  778. w.Header().Set("Content-Length", fmt.Sprintf("%d", len(bs)))
  779. w.Header().Set("Last-Modified", auto.AssetsBuildDate)
  780. w.Write(bs)
  781. }
  782. func (s embeddedStatic) mimeTypeForFile(file string) string {
  783. // We use a built in table of the common types since the system
  784. // TypeByExtension might be unreliable. But if we don't know, we delegate
  785. // to the system.
  786. ext := filepath.Ext(file)
  787. switch ext {
  788. case ".htm", ".html":
  789. return "text/html"
  790. case ".css":
  791. return "text/css"
  792. case ".js":
  793. return "application/javascript"
  794. case ".json":
  795. return "application/json"
  796. case ".png":
  797. return "image/png"
  798. case ".ttf":
  799. return "application/x-font-ttf"
  800. case ".woff":
  801. return "application/x-font-woff"
  802. case ".svg":
  803. return "image/svg+xml"
  804. default:
  805. return mime.TypeByExtension(ext)
  806. }
  807. }
  808. func (s *apiSvc) toNeedSlice(fs []db.FileInfoTruncated) []jsonDBFileInfo {
  809. res := make([]jsonDBFileInfo, len(fs))
  810. for i, f := range fs {
  811. res[i] = jsonDBFileInfo(f)
  812. }
  813. return res
  814. }
  815. // Type wrappers for nice JSON serialization
  816. type jsonFileInfo protocol.FileInfo
  817. func (f jsonFileInfo) MarshalJSON() ([]byte, error) {
  818. return json.Marshal(map[string]interface{}{
  819. "name": f.Name,
  820. "size": protocol.FileInfo(f).Size(),
  821. "flags": fmt.Sprintf("%#o", f.Flags),
  822. "modified": time.Unix(f.Modified, 0),
  823. "localVersion": f.LocalVersion,
  824. "numBlocks": len(f.Blocks),
  825. "version": jsonVersionVector(f.Version),
  826. })
  827. }
  828. type jsonDBFileInfo db.FileInfoTruncated
  829. func (f jsonDBFileInfo) MarshalJSON() ([]byte, error) {
  830. return json.Marshal(map[string]interface{}{
  831. "name": f.Name,
  832. "size": db.FileInfoTruncated(f).Size(),
  833. "flags": fmt.Sprintf("%#o", f.Flags),
  834. "modified": time.Unix(f.Modified, 0),
  835. "localVersion": f.LocalVersion,
  836. "version": jsonVersionVector(f.Version),
  837. })
  838. }
  839. type jsonVersionVector protocol.Vector
  840. func (v jsonVersionVector) MarshalJSON() ([]byte, error) {
  841. res := make([]string, len(v))
  842. for i, c := range v {
  843. res[i] = fmt.Sprintf("%d:%d", c.ID, c.Value)
  844. }
  845. return json.Marshal(res)
  846. }