2014-06-01 13:50:14 -07:00
|
|
|
// Copyright (C) 2014 Jakob Borg and other contributors. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2014-03-02 15:58:14 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-04-19 04:33:51 -07:00
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
2014-03-02 15:58:14 -07:00
|
|
|
"encoding/json"
|
2014-05-22 07:12:19 -07:00
|
|
|
"fmt"
|
2014-03-02 15:58:14 -07:00
|
|
|
"io/ioutil"
|
2014-04-30 13:42:39 -07:00
|
|
|
"log"
|
2014-04-19 04:33:51 -07:00
|
|
|
"math/rand"
|
2014-05-22 07:12:19 -07:00
|
|
|
"mime"
|
2014-04-30 13:52:38 -07:00
|
|
|
"net"
|
2014-03-02 15:58:14 -07:00
|
|
|
"net/http"
|
2014-05-22 07:12:19 -07:00
|
|
|
"path/filepath"
|
2014-03-02 15:58:14 -07:00
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2014-05-21 05:04:16 -07:00
|
|
|
"crypto/tls"
|
2014-04-19 04:33:51 -07:00
|
|
|
"code.google.com/p/go.crypto/bcrypt"
|
2014-05-22 07:12:19 -07:00
|
|
|
"github.com/calmh/syncthing/auto"
|
2014-05-14 17:18:09 -07:00
|
|
|
"github.com/calmh/syncthing/config"
|
2014-05-14 17:08:56 -07:00
|
|
|
"github.com/calmh/syncthing/logger"
|
2014-05-14 20:26:55 -07:00
|
|
|
"github.com/calmh/syncthing/model"
|
2014-03-02 15:58:14 -07:00
|
|
|
"github.com/codegangsta/martini"
|
2014-05-21 11:06:14 -07:00
|
|
|
"github.com/vitrun/qart/qr"
|
2014-03-02 15:58:14 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type guiError struct {
|
|
|
|
Time time.Time
|
|
|
|
Error string
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
configInSync = true
|
|
|
|
guiErrors = []guiError{}
|
|
|
|
guiErrorsMut sync.Mutex
|
2014-05-22 07:12:19 -07:00
|
|
|
static func(http.ResponseWriter, *http.Request, *log.Logger)
|
2014-03-02 15:58:14 -07:00
|
|
|
)
|
|
|
|
|
2014-04-19 04:33:51 -07:00
|
|
|
const (
|
|
|
|
unchangedPassword = "--password-unchanged--"
|
|
|
|
)
|
|
|
|
|
2014-05-14 17:08:56 -07:00
|
|
|
func init() {
|
|
|
|
l.AddHandler(logger.LevelWarn, showGuiError)
|
|
|
|
}
|
|
|
|
|
2014-05-22 07:12:19 -07:00
|
|
|
func startGUI(cfg config.GUIConfiguration, assetDir string, m *model.Model) error {
|
2014-05-21 05:04:16 -07:00
|
|
|
var listener net.Listener
|
|
|
|
var err error
|
|
|
|
if cfg.UseTLS {
|
|
|
|
cert, err := loadCert(confDir, "https-")
|
|
|
|
if err != nil {
|
|
|
|
newCertificate(confDir, "https-")
|
|
|
|
cert, err = loadCert(confDir, "https-")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tlsCfg := &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{cert},
|
|
|
|
ServerName: "syncthing",
|
|
|
|
}
|
|
|
|
listener, err = tls.Listen("tcp", cfg.Address, tlsCfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
listener, err = net.Listen("tcp", cfg.Address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-30 13:52:38 -07:00
|
|
|
}
|
|
|
|
|
2014-05-22 07:12:19 -07:00
|
|
|
if len(assetDir) > 0 {
|
|
|
|
static = martini.Static(assetDir).(func(http.ResponseWriter, *http.Request, *log.Logger))
|
|
|
|
} else {
|
|
|
|
static = embeddedStatic()
|
|
|
|
}
|
|
|
|
|
2014-03-02 15:58:14 -07:00
|
|
|
router := martini.NewRouter()
|
|
|
|
router.Get("/", getRoot)
|
|
|
|
router.Get("/rest/version", restGetVersion)
|
2014-04-23 01:04:07 -07:00
|
|
|
router.Get("/rest/model", restGetModel)
|
2014-05-19 13:31:28 -07:00
|
|
|
router.Get("/rest/need", restGetNeed)
|
2014-03-02 15:58:14 -07:00
|
|
|
router.Get("/rest/connections", restGetConnections)
|
|
|
|
router.Get("/rest/config", restGetConfig)
|
|
|
|
router.Get("/rest/config/sync", restGetConfigInSync)
|
|
|
|
router.Get("/rest/system", restGetSystem)
|
|
|
|
router.Get("/rest/errors", restGetErrors)
|
2014-05-12 18:08:55 -07:00
|
|
|
router.Get("/rest/discovery", restGetDiscovery)
|
2014-05-21 11:06:14 -07:00
|
|
|
router.Get("/qr/:text", getQR)
|
2014-03-02 15:58:14 -07:00
|
|
|
|
|
|
|
router.Post("/rest/config", restPostConfig)
|
|
|
|
router.Post("/rest/restart", restPostRestart)
|
2014-04-03 13:10:51 -07:00
|
|
|
router.Post("/rest/reset", restPostReset)
|
2014-05-11 16:16:27 -07:00
|
|
|
router.Post("/rest/shutdown", restPostShutdown)
|
2014-03-02 15:58:14 -07:00
|
|
|
router.Post("/rest/error", restPostError)
|
2014-04-16 07:30:49 -07:00
|
|
|
router.Post("/rest/error/clear", restClearErrors)
|
2014-05-12 17:50:54 -07:00
|
|
|
router.Post("/rest/discovery/hint", restPostDiscoveryHint)
|
2014-03-02 15:58:14 -07:00
|
|
|
|
2014-04-30 13:52:38 -07:00
|
|
|
mr := martini.New()
|
|
|
|
if len(cfg.User) > 0 && len(cfg.Password) > 0 {
|
|
|
|
mr.Use(basic(cfg.User, cfg.Password))
|
|
|
|
}
|
|
|
|
mr.Use(static)
|
|
|
|
mr.Use(martini.Recovery())
|
|
|
|
mr.Use(restMiddleware)
|
|
|
|
mr.Action(router.Handle)
|
|
|
|
mr.Map(m)
|
|
|
|
|
2014-05-14 17:08:56 -07:00
|
|
|
go http.Serve(listener, mr)
|
2014-04-30 13:52:38 -07:00
|
|
|
|
|
|
|
return nil
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func getRoot(w http.ResponseWriter, r *http.Request) {
|
2014-04-30 13:10:13 -07:00
|
|
|
r.URL.Path = "/index.html"
|
2014-05-22 07:12:19 -07:00
|
|
|
static(w, r, nil)
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
|
|
|
|
2014-03-26 12:32:35 -07:00
|
|
|
func restMiddleware(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if len(r.URL.Path) >= 6 && r.URL.Path[:6] == "/rest/" {
|
|
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-02 15:58:14 -07:00
|
|
|
func restGetVersion() string {
|
|
|
|
return Version
|
|
|
|
}
|
|
|
|
|
2014-05-14 20:26:55 -07:00
|
|
|
func restGetModel(m *model.Model, w http.ResponseWriter, r *http.Request) {
|
2014-04-23 01:04:07 -07:00
|
|
|
var qs = r.URL.Query()
|
|
|
|
var repo = qs.Get("repo")
|
2014-03-02 15:58:14 -07:00
|
|
|
var res = make(map[string]interface{})
|
|
|
|
|
2014-04-27 12:53:27 -07:00
|
|
|
for _, cr := range cfg.Repositories {
|
|
|
|
if cr.ID == repo {
|
|
|
|
res["invalid"] = cr.Invalid
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-09 13:03:30 -07:00
|
|
|
globalFiles, globalDeleted, globalBytes := m.GlobalSize(repo)
|
2014-03-02 15:58:14 -07:00
|
|
|
res["globalFiles"], res["globalDeleted"], res["globalBytes"] = globalFiles, globalDeleted, globalBytes
|
|
|
|
|
2014-04-09 13:03:30 -07:00
|
|
|
localFiles, localDeleted, localBytes := m.LocalSize(repo)
|
2014-03-02 15:58:14 -07:00
|
|
|
res["localFiles"], res["localDeleted"], res["localBytes"] = localFiles, localDeleted, localBytes
|
|
|
|
|
2014-04-09 13:03:30 -07:00
|
|
|
needFiles, needBytes := m.NeedSize(repo)
|
|
|
|
res["needFiles"], res["needBytes"] = needFiles, needBytes
|
2014-03-02 15:58:14 -07:00
|
|
|
|
2014-04-09 13:03:30 -07:00
|
|
|
res["inSyncFiles"], res["inSyncBytes"] = globalFiles-needFiles, globalBytes-needBytes
|
2014-03-02 15:58:14 -07:00
|
|
|
|
2014-04-14 00:58:17 -07:00
|
|
|
res["state"] = m.State(repo)
|
|
|
|
|
2014-03-02 15:58:14 -07:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
|
}
|
|
|
|
|
2014-05-19 13:31:28 -07:00
|
|
|
func restGetNeed(m *model.Model, w http.ResponseWriter, r *http.Request) {
|
|
|
|
var qs = r.URL.Query()
|
|
|
|
var repo = qs.Get("repo")
|
|
|
|
|
|
|
|
files := m.NeedFilesRepo(repo)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(files)
|
|
|
|
}
|
|
|
|
|
2014-05-14 20:26:55 -07:00
|
|
|
func restGetConnections(m *model.Model, w http.ResponseWriter) {
|
2014-03-02 15:58:14 -07:00
|
|
|
var res = m.ConnectionStats()
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
func restGetConfig(w http.ResponseWriter) {
|
2014-04-19 04:33:51 -07:00
|
|
|
encCfg := cfg
|
2014-04-19 13:36:12 -07:00
|
|
|
if encCfg.GUI.Password != "" {
|
|
|
|
encCfg.GUI.Password = unchangedPassword
|
|
|
|
}
|
2014-04-19 04:33:51 -07:00
|
|
|
json.NewEncoder(w).Encode(encCfg)
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func restPostConfig(req *http.Request) {
|
2014-05-24 12:00:47 -07:00
|
|
|
var newCfg config.Configuration
|
|
|
|
err := json.NewDecoder(req.Body).Decode(&newCfg)
|
2014-03-02 15:58:14 -07:00
|
|
|
if err != nil {
|
2014-05-14 17:08:56 -07:00
|
|
|
l.Warnln(err)
|
2014-03-02 15:58:14 -07:00
|
|
|
} else {
|
2014-05-24 12:00:47 -07:00
|
|
|
if newCfg.GUI.Password == "" {
|
2014-04-19 13:36:12 -07:00
|
|
|
// Leave it empty
|
2014-05-24 12:00:47 -07:00
|
|
|
} else if newCfg.GUI.Password == unchangedPassword {
|
|
|
|
newCfg.GUI.Password = cfg.GUI.Password
|
|
|
|
} else {
|
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(newCfg.GUI.Password), 0)
|
2014-04-19 04:33:51 -07:00
|
|
|
if err != nil {
|
2014-05-14 17:08:56 -07:00
|
|
|
l.Warnln(err)
|
2014-04-19 04:33:51 -07:00
|
|
|
} else {
|
2014-05-24 12:00:47 -07:00
|
|
|
newCfg.GUI.Password = string(hash)
|
2014-04-19 04:33:51 -07:00
|
|
|
}
|
|
|
|
}
|
2014-05-24 12:00:47 -07:00
|
|
|
cfg = newCfg
|
2014-03-02 15:58:14 -07:00
|
|
|
saveConfig()
|
|
|
|
configInSync = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func restGetConfigInSync(w http.ResponseWriter) {
|
|
|
|
json.NewEncoder(w).Encode(map[string]bool{"configInSync": configInSync})
|
|
|
|
}
|
|
|
|
|
2014-05-12 17:15:18 -07:00
|
|
|
func restPostRestart(w http.ResponseWriter) {
|
|
|
|
flushResponse(`{"ok": "restarting"}`, w)
|
2014-04-03 13:10:51 -07:00
|
|
|
go restart()
|
|
|
|
}
|
|
|
|
|
2014-05-12 17:15:18 -07:00
|
|
|
func restPostReset(w http.ResponseWriter) {
|
|
|
|
flushResponse(`{"ok": "resetting repos"}`, w)
|
2014-04-03 13:10:51 -07:00
|
|
|
resetRepositories()
|
|
|
|
go restart()
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
|
|
|
|
2014-05-12 17:15:18 -07:00
|
|
|
func restPostShutdown(w http.ResponseWriter) {
|
|
|
|
flushResponse(`{"ok": "shutting down"}`, w)
|
2014-05-11 16:16:27 -07:00
|
|
|
go shutdown()
|
|
|
|
}
|
|
|
|
|
2014-05-12 17:15:18 -07:00
|
|
|
func flushResponse(s string, w http.ResponseWriter) {
|
|
|
|
w.Write([]byte(s + "\n"))
|
|
|
|
f := w.(http.Flusher)
|
|
|
|
f.Flush()
|
|
|
|
}
|
|
|
|
|
2014-04-14 03:02:40 -07:00
|
|
|
var cpuUsagePercent [10]float64 // The last ten seconds
|
2014-03-02 15:58:14 -07:00
|
|
|
var cpuUsageLock sync.RWMutex
|
|
|
|
|
|
|
|
func restGetSystem(w http.ResponseWriter) {
|
|
|
|
var m runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&m)
|
|
|
|
|
|
|
|
res := make(map[string]interface{})
|
|
|
|
res["myID"] = myID
|
|
|
|
res["goroutines"] = runtime.NumGoroutine()
|
|
|
|
res["alloc"] = m.Alloc
|
|
|
|
res["sys"] = m.Sys
|
2014-05-24 04:22:09 -07:00
|
|
|
res["tilde"] = expandTilde("~")
|
2014-04-30 13:17:43 -07:00
|
|
|
if cfg.Options.GlobalAnnEnabled && discoverer != nil {
|
2014-04-16 08:36:09 -07:00
|
|
|
res["extAnnounceOK"] = discoverer.ExtAnnounceOK()
|
|
|
|
}
|
2014-03-02 15:58:14 -07:00
|
|
|
cpuUsageLock.RLock()
|
2014-04-14 03:02:40 -07:00
|
|
|
var cpusum float64
|
|
|
|
for _, p := range cpuUsagePercent {
|
|
|
|
cpusum += p
|
|
|
|
}
|
2014-03-02 15:58:14 -07:00
|
|
|
cpuUsageLock.RUnlock()
|
2014-04-14 03:02:40 -07:00
|
|
|
res["cpuPercent"] = cpusum / 10
|
2014-03-02 15:58:14 -07:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
func restGetErrors(w http.ResponseWriter) {
|
|
|
|
guiErrorsMut.Lock()
|
|
|
|
json.NewEncoder(w).Encode(guiErrors)
|
|
|
|
guiErrorsMut.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func restPostError(req *http.Request) {
|
|
|
|
bs, _ := ioutil.ReadAll(req.Body)
|
|
|
|
req.Body.Close()
|
2014-05-14 17:08:56 -07:00
|
|
|
showGuiError(0, string(bs))
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
|
|
|
|
2014-04-16 07:30:49 -07:00
|
|
|
func restClearErrors() {
|
|
|
|
guiErrorsMut.Lock()
|
2014-05-17 04:54:11 -07:00
|
|
|
guiErrors = []guiError{}
|
2014-04-16 07:30:49 -07:00
|
|
|
guiErrorsMut.Unlock()
|
|
|
|
}
|
|
|
|
|
2014-05-14 17:08:56 -07:00
|
|
|
func showGuiError(l logger.LogLevel, err string) {
|
2014-03-02 15:58:14 -07:00
|
|
|
guiErrorsMut.Lock()
|
|
|
|
guiErrors = append(guiErrors, guiError{time.Now(), err})
|
|
|
|
if len(guiErrors) > 5 {
|
|
|
|
guiErrors = guiErrors[len(guiErrors)-5:]
|
|
|
|
}
|
|
|
|
guiErrorsMut.Unlock()
|
|
|
|
}
|
2014-04-19 04:33:51 -07:00
|
|
|
|
2014-05-12 17:50:54 -07:00
|
|
|
func restPostDiscoveryHint(r *http.Request) {
|
|
|
|
var qs = r.URL.Query()
|
|
|
|
var node = qs.Get("node")
|
|
|
|
var addr = qs.Get("addr")
|
|
|
|
if len(node) != 0 && len(addr) != 0 && discoverer != nil {
|
|
|
|
discoverer.Hint(node, []string{addr})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-12 18:08:55 -07:00
|
|
|
func restGetDiscovery(w http.ResponseWriter) {
|
|
|
|
json.NewEncoder(w).Encode(discoverer.All())
|
|
|
|
}
|
|
|
|
|
2014-05-21 11:06:14 -07:00
|
|
|
func getQR(w http.ResponseWriter, params martini.Params) {
|
|
|
|
code, err := qr.Encode(params["text"], qr.M)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Invalid", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "image/png")
|
|
|
|
w.Write(code.PNG())
|
|
|
|
}
|
|
|
|
|
2014-04-19 04:33:51 -07:00
|
|
|
func basic(username string, passhash string) http.HandlerFunc {
|
|
|
|
return func(res http.ResponseWriter, req *http.Request) {
|
|
|
|
error := func() {
|
|
|
|
time.Sleep(time.Duration(rand.Intn(100)+100) * time.Millisecond)
|
|
|
|
res.Header().Set("WWW-Authenticate", "Basic realm=\"Authorization Required\"")
|
|
|
|
http.Error(res, "Not Authorized", http.StatusUnauthorized)
|
|
|
|
}
|
|
|
|
|
|
|
|
hdr := req.Header.Get("Authorization")
|
|
|
|
if len(hdr) < len("Basic ") || hdr[:6] != "Basic " {
|
|
|
|
error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hdr = hdr[6:]
|
|
|
|
bs, err := base64.StdEncoding.DecodeString(hdr)
|
|
|
|
if err != nil {
|
|
|
|
error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fields := bytes.SplitN(bs, []byte(":"), 2)
|
|
|
|
if len(fields) != 2 {
|
|
|
|
error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(fields[0]) != username {
|
|
|
|
error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bcrypt.CompareHashAndPassword([]byte(passhash), fields[1]); err != nil {
|
|
|
|
error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-22 07:12:19 -07:00
|
|
|
|
|
|
|
func embeddedStatic() func(http.ResponseWriter, *http.Request, *log.Logger) {
|
|
|
|
var modt = time.Now().UTC().Format(http.TimeFormat)
|
|
|
|
|
|
|
|
return func(res http.ResponseWriter, req *http.Request, log *log.Logger) {
|
|
|
|
file := req.URL.Path
|
|
|
|
|
|
|
|
if file[0] == '/' {
|
|
|
|
file = file[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
bs, ok := auto.Assets[file]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mtype := mime.TypeByExtension(filepath.Ext(req.URL.Path))
|
|
|
|
if len(mtype) != 0 {
|
|
|
|
res.Header().Set("Content-Type", mtype)
|
|
|
|
}
|
|
|
|
res.Header().Set("Content-Length", fmt.Sprintf("%d", len(bs)))
|
|
|
|
res.Header().Set("Last-Modified", modt)
|
|
|
|
|
|
|
|
res.Write(bs)
|
|
|
|
}
|
|
|
|
}
|