gui.go 21 KB

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