gui.go 30 KB

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