2014-07-12 15:45:33 -07:00
|
|
|
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
|
|
|
|
// All rights reserved. Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-06-01 13:50:14 -07:00
|
|
|
|
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-07-22 11:11:36 -07:00
|
|
|
"os"
|
2014-05-22 07:12:19 -07:00
|
|
|
"path/filepath"
|
2014-06-06 19:00:46 -07:00
|
|
|
"reflect"
|
2014-03-02 15:58:14 -07:00
|
|
|
"runtime"
|
2014-07-13 12:07:24 -07:00
|
|
|
"strconv"
|
2014-07-05 12:40:29 -07:00
|
|
|
"strings"
|
2014-03-02 15:58:14 -07:00
|
|
|
"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-07-13 12:07:24 -07:00
|
|
|
"github.com/calmh/syncthing/events"
|
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-07-18 01:00:02 -07:00
|
|
|
"github.com/calmh/syncthing/protocol"
|
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-06-05 00:16:12 -07:00
|
|
|
apiKey string
|
2014-07-05 12:40:29 -07:00
|
|
|
modt = time.Now().UTC().Format(http.TimeFormat)
|
2014-07-13 12:07:24 -07:00
|
|
|
eventSub = events.NewBufferedSubscription(events.Default.Subscribe(events.AllEvents), 1000)
|
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 {
|
2014-06-10 06:40:21 -07:00
|
|
|
l.Infoln("Loading HTTPS certificate:", err)
|
2014-06-12 16:49:30 -07:00
|
|
|
l.Infoln("Creating new HTTPS certificate")
|
2014-05-21 05:04:16 -07:00
|
|
|
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-07-05 12:40:29 -07:00
|
|
|
apiKey = cfg.APIKey
|
|
|
|
loadCsrfTokens()
|
|
|
|
|
|
|
|
// The GET handlers
|
|
|
|
getRestMux := http.NewServeMux()
|
|
|
|
getRestMux.HandleFunc("/rest/version", restGetVersion)
|
|
|
|
getRestMux.HandleFunc("/rest/model", withModel(m, restGetModel))
|
|
|
|
getRestMux.HandleFunc("/rest/model/version", withModel(m, restGetModelVersion))
|
|
|
|
getRestMux.HandleFunc("/rest/need", withModel(m, restGetNeed))
|
|
|
|
getRestMux.HandleFunc("/rest/connections", withModel(m, restGetConnections))
|
|
|
|
getRestMux.HandleFunc("/rest/config", restGetConfig)
|
|
|
|
getRestMux.HandleFunc("/rest/config/sync", restGetConfigInSync)
|
|
|
|
getRestMux.HandleFunc("/rest/system", restGetSystem)
|
|
|
|
getRestMux.HandleFunc("/rest/errors", restGetErrors)
|
|
|
|
getRestMux.HandleFunc("/rest/discovery", restGetDiscovery)
|
|
|
|
getRestMux.HandleFunc("/rest/report", withModel(m, restGetReport))
|
2014-07-13 12:07:24 -07:00
|
|
|
getRestMux.HandleFunc("/rest/events", restGetEvents)
|
2014-07-14 01:45:29 -07:00
|
|
|
getRestMux.HandleFunc("/rest/upgrade", restGetUpgrade)
|
2014-07-18 01:00:02 -07:00
|
|
|
getRestMux.HandleFunc("/rest/nodeid", restGetNodeID)
|
2014-07-26 13:30:29 -07:00
|
|
|
getRestMux.HandleFunc("/rest/lang", restGetLang)
|
2014-07-05 12:40:29 -07:00
|
|
|
|
|
|
|
// The POST handlers
|
|
|
|
postRestMux := http.NewServeMux()
|
|
|
|
postRestMux.HandleFunc("/rest/config", withModel(m, restPostConfig))
|
|
|
|
postRestMux.HandleFunc("/rest/restart", restPostRestart)
|
|
|
|
postRestMux.HandleFunc("/rest/reset", restPostReset)
|
|
|
|
postRestMux.HandleFunc("/rest/shutdown", restPostShutdown)
|
|
|
|
postRestMux.HandleFunc("/rest/error", restPostError)
|
|
|
|
postRestMux.HandleFunc("/rest/error/clear", restClearErrors)
|
|
|
|
postRestMux.HandleFunc("/rest/discovery/hint", restPostDiscoveryHint)
|
|
|
|
postRestMux.HandleFunc("/rest/model/override", withModel(m, restPostOverride))
|
2014-07-14 01:45:29 -07:00
|
|
|
postRestMux.HandleFunc("/rest/upgrade", restPostUpgrade)
|
2014-07-05 12:40:29 -07:00
|
|
|
|
|
|
|
// A handler that splits requests between the two above and disables
|
|
|
|
// caching
|
|
|
|
restMux := noCacheMiddleware(getPostHandler(getRestMux, postRestMux))
|
|
|
|
|
|
|
|
// The main routing handler
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.Handle("/rest/", restMux)
|
|
|
|
mux.HandleFunc("/qr/", getQR)
|
|
|
|
|
|
|
|
// Serve compiled in assets unless an asset directory was set (for development)
|
2014-07-22 11:11:36 -07:00
|
|
|
mux.Handle("/", embeddedStatic(assetDir))
|
2014-05-22 07:12:19 -07:00
|
|
|
|
2014-07-06 06:00:44 -07:00
|
|
|
// Wrap everything in CSRF protection. The /rest prefix should be
|
|
|
|
// protected, other requests will grant cookies.
|
|
|
|
handler := csrfMiddleware("/rest", mux)
|
2014-06-04 12:20:07 -07:00
|
|
|
|
2014-07-05 12:40:29 -07:00
|
|
|
// Wrap everything in basic auth, if user/password is set.
|
|
|
|
if len(cfg.User) > 0 {
|
|
|
|
handler = basicAuthMiddleware(cfg.User, cfg.Password, handler)
|
|
|
|
}
|
2014-04-30 13:52:38 -07:00
|
|
|
|
2014-07-05 12:40:29 -07:00
|
|
|
go http.Serve(listener, handler)
|
2014-04-30 13:52:38 -07:00
|
|
|
return nil
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
|
|
|
|
2014-07-05 12:40:29 -07:00
|
|
|
func getPostHandler(get, post http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.Method {
|
|
|
|
case "GET":
|
|
|
|
get.ServeHTTP(w, r)
|
|
|
|
case "POST":
|
|
|
|
post.ServeHTTP(w, r)
|
|
|
|
default:
|
|
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
}
|
|
|
|
})
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
|
|
|
|
2014-07-05 12:40:29 -07:00
|
|
|
func noCacheMiddleware(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2014-03-26 12:32:35 -07:00
|
|
|
w.Header().Set("Cache-Control", "no-cache")
|
2014-07-05 12:40:29 -07:00
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func withModel(m *model.Model, h func(m *model.Model, w http.ResponseWriter, r *http.Request)) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
h(m, w, r)
|
2014-03-26 12:32:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-05 12:40:29 -07:00
|
|
|
func restGetVersion(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write([]byte(Version))
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
|
|
|
|
2014-06-19 15:27:54 -07:00
|
|
|
func restGetModelVersion(m *model.Model, w http.ResponseWriter, r *http.Request) {
|
|
|
|
var qs = r.URL.Query()
|
|
|
|
var repo = qs.Get("repo")
|
|
|
|
var res = make(map[string]interface{})
|
|
|
|
|
2014-07-15 04:04:37 -07:00
|
|
|
res["version"] = m.LocalVersion(repo)
|
2014-06-19 15:27:54 -07:00
|
|
|
|
2014-06-22 08:26:31 -07:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2014-06-19 15:27:54 -07:00
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
|
}
|
|
|
|
|
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-07-17 04:38:36 -07:00
|
|
|
res["state"], res["stateChanged"] = m.State(repo)
|
2014-07-15 04:04:37 -07:00
|
|
|
res["version"] = m.LocalVersion(repo)
|
2014-04-14 00:58:17 -07:00
|
|
|
|
2014-06-22 08:26:31 -07:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2014-03-02 15:58:14 -07:00
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
|
}
|
|
|
|
|
2014-07-05 12:40:29 -07:00
|
|
|
func restPostOverride(m *model.Model, w http.ResponseWriter, r *http.Request) {
|
2014-06-16 01:47:02 -07:00
|
|
|
var qs = r.URL.Query()
|
|
|
|
var repo = qs.Get("repo")
|
|
|
|
m.Override(repo)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
2014-06-22 08:26:31 -07:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2014-05-19 13:31:28 -07:00
|
|
|
json.NewEncoder(w).Encode(files)
|
|
|
|
}
|
|
|
|
|
2014-07-05 12:40:29 -07:00
|
|
|
func restGetConnections(m *model.Model, w http.ResponseWriter, r *http.Request) {
|
2014-03-02 15:58:14 -07:00
|
|
|
var res = m.ConnectionStats()
|
2014-06-22 08:26:31 -07:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2014-03-02 15:58:14 -07:00
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
|
}
|
|
|
|
|
2014-07-05 12:40:29 -07:00
|
|
|
func restGetConfig(w http.ResponseWriter, r *http.Request) {
|
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-06-22 08:26:31 -07:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2014-04-19 04:33:51 -07:00
|
|
|
json.NewEncoder(w).Encode(encCfg)
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
|
|
|
|
2014-07-05 12:40:29 -07:00
|
|
|
func restPostConfig(m *model.Model, w http.ResponseWriter, r *http.Request) {
|
2014-05-24 12:00:47 -07:00
|
|
|
var newCfg config.Configuration
|
2014-07-05 12:40:29 -07:00
|
|
|
err := json.NewDecoder(r.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-06-06 19:00:46 -07:00
|
|
|
|
|
|
|
// Figure out if any changes require a restart
|
|
|
|
|
|
|
|
if len(cfg.Repositories) != len(newCfg.Repositories) {
|
|
|
|
configInSync = false
|
|
|
|
} else {
|
|
|
|
om := cfg.RepoMap()
|
|
|
|
nm := newCfg.RepoMap()
|
|
|
|
for id := range om {
|
|
|
|
if !reflect.DeepEqual(om[id], nm[id]) {
|
|
|
|
configInSync = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cfg.Nodes) != len(newCfg.Nodes) {
|
|
|
|
configInSync = false
|
|
|
|
} else {
|
|
|
|
om := cfg.NodeMap()
|
|
|
|
nm := newCfg.NodeMap()
|
|
|
|
for k := range om {
|
|
|
|
if _, ok := nm[k]; !ok {
|
2014-07-28 03:44:46 -07:00
|
|
|
// A node was removed and another added
|
2014-06-06 19:00:46 -07:00
|
|
|
configInSync = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-17 14:22:19 -07:00
|
|
|
if newCfg.Options.URAccepted > cfg.Options.URAccepted {
|
2014-06-11 11:04:23 -07:00
|
|
|
// UR was enabled
|
|
|
|
newCfg.Options.URAccepted = usageReportVersion
|
2014-06-11 16:05:00 -07:00
|
|
|
err := sendUsageReport(m)
|
|
|
|
if err != nil {
|
|
|
|
l.Infoln("Usage report:", err)
|
|
|
|
}
|
2014-06-11 11:04:23 -07:00
|
|
|
go usageReportingLoop(m)
|
2014-06-17 14:22:19 -07:00
|
|
|
} else if newCfg.Options.URAccepted < cfg.Options.URAccepted {
|
2014-06-11 11:04:23 -07:00
|
|
|
// UR was disabled
|
2014-06-17 14:22:19 -07:00
|
|
|
newCfg.Options.URAccepted = -1
|
2014-06-11 11:04:23 -07:00
|
|
|
stopUsageReporting()
|
|
|
|
}
|
|
|
|
|
2014-06-13 11:25:10 -07:00
|
|
|
if !reflect.DeepEqual(cfg.Options, newCfg.Options) || !reflect.DeepEqual(cfg.GUI, newCfg.GUI) {
|
2014-06-06 19:00:46 -07:00
|
|
|
configInSync = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Activate and save
|
|
|
|
|
2014-05-24 12:00:47 -07:00
|
|
|
cfg = newCfg
|
2014-03-02 15:58:14 -07:00
|
|
|
saveConfig()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-05 12:40:29 -07:00
|
|
|
func restGetConfigInSync(w http.ResponseWriter, r *http.Request) {
|
2014-06-22 08:26:31 -07:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2014-03-02 15:58:14 -07:00
|
|
|
json.NewEncoder(w).Encode(map[string]bool{"configInSync": configInSync})
|
|
|
|
}
|
|
|
|
|
2014-07-05 12:40:29 -07:00
|
|
|
func restPostRestart(w http.ResponseWriter, r *http.Request) {
|
2014-05-12 17:15:18 -07:00
|
|
|
flushResponse(`{"ok": "restarting"}`, w)
|
2014-04-03 13:10:51 -07:00
|
|
|
go restart()
|
|
|
|
}
|
|
|
|
|
2014-07-05 12:40:29 -07:00
|
|
|
func restPostReset(w http.ResponseWriter, r *http.Request) {
|
2014-05-12 17:15:18 -07:00
|
|
|
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-07-05 12:40:29 -07:00
|
|
|
func restPostShutdown(w http.ResponseWriter, r *http.Request) {
|
2014-05-12 17:15:18 -07:00
|
|
|
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
|
|
|
|
|
2014-07-05 12:40:29 -07:00
|
|
|
func restGetSystem(w http.ResponseWriter, r *http.Request) {
|
2014-03-02 15:58:14 -07:00
|
|
|
var m runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&m)
|
|
|
|
|
|
|
|
res := make(map[string]interface{})
|
2014-06-29 16:42:03 -07:00
|
|
|
res["myID"] = myID.String()
|
2014-03-02 15:58:14 -07:00
|
|
|
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
|
|
|
|
2014-06-22 08:26:31 -07:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2014-03-02 15:58:14 -07:00
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
|
}
|
|
|
|
|
2014-07-05 12:40:29 -07:00
|
|
|
func restGetErrors(w http.ResponseWriter, r *http.Request) {
|
2014-06-22 08:26:31 -07:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2014-03-02 15:58:14 -07:00
|
|
|
guiErrorsMut.Lock()
|
|
|
|
json.NewEncoder(w).Encode(guiErrors)
|
|
|
|
guiErrorsMut.Unlock()
|
|
|
|
}
|
|
|
|
|
2014-07-05 12:40:29 -07:00
|
|
|
func restPostError(w http.ResponseWriter, r *http.Request) {
|
|
|
|
bs, _ := ioutil.ReadAll(r.Body)
|
|
|
|
r.Body.Close()
|
2014-05-14 17:08:56 -07:00
|
|
|
showGuiError(0, string(bs))
|
2014-03-02 15:58:14 -07:00
|
|
|
}
|
|
|
|
|
2014-07-05 12:40:29 -07:00
|
|
|
func restClearErrors(w http.ResponseWriter, r *http.Request) {
|
2014-04-16 07:30:49 -07:00
|
|
|
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-07-05 12:40:29 -07:00
|
|
|
func restPostDiscoveryHint(w http.ResponseWriter, r *http.Request) {
|
2014-05-12 17:50:54 -07:00
|
|
|
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-07-05 12:40:29 -07:00
|
|
|
func restGetDiscovery(w http.ResponseWriter, r *http.Request) {
|
2014-05-12 18:08:55 -07:00
|
|
|
json.NewEncoder(w).Encode(discoverer.All())
|
|
|
|
}
|
|
|
|
|
2014-07-05 12:40:29 -07:00
|
|
|
func restGetReport(m *model.Model, w http.ResponseWriter, r *http.Request) {
|
2014-06-22 08:26:31 -07:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2014-06-11 11:04:23 -07:00
|
|
|
json.NewEncoder(w).Encode(reportData(m))
|
|
|
|
}
|
|
|
|
|
2014-07-13 12:07:24 -07:00
|
|
|
func restGetEvents(w http.ResponseWriter, r *http.Request) {
|
|
|
|
qs := r.URL.Query()
|
|
|
|
ts := qs.Get("since")
|
|
|
|
since, _ := strconv.Atoi(ts)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
json.NewEncoder(w).Encode(eventSub.Since(since, nil))
|
|
|
|
}
|
|
|
|
|
2014-07-14 01:45:29 -07:00
|
|
|
func restGetUpgrade(w http.ResponseWriter, r *http.Request) {
|
|
|
|
rel, err := currentRelease()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res := make(map[string]interface{})
|
|
|
|
res["running"] = Version
|
|
|
|
res["latest"] = rel.Tag
|
|
|
|
res["newer"] = compareVersions(rel.Tag, Version) == 1
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
|
}
|
|
|
|
|
2014-07-18 01:00:02 -07:00
|
|
|
func restGetNodeID(w http.ResponseWriter, r *http.Request) {
|
|
|
|
qs := r.URL.Query()
|
|
|
|
idStr := qs.Get("id")
|
|
|
|
id, err := protocol.NodeIDFromString(idStr)
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
if err == nil {
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{
|
|
|
|
"id": id.String(),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{
|
|
|
|
"error": err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-26 13:30:29 -07:00
|
|
|
func restGetLang(w http.ResponseWriter, r *http.Request) {
|
|
|
|
lang := r.Header.Get("Accept-Language")
|
|
|
|
var langs []string
|
|
|
|
for _, l := range strings.Split(lang, ",") {
|
|
|
|
if len(l) >= 2 {
|
|
|
|
langs = append(langs, l[:2])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
json.NewEncoder(w).Encode(langs)
|
|
|
|
}
|
|
|
|
|
2014-07-14 01:45:29 -07:00
|
|
|
func restPostUpgrade(w http.ResponseWriter, r *http.Request) {
|
|
|
|
err := upgrade()
|
|
|
|
if err != nil {
|
|
|
|
l.Warnln(err)
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
restPostRestart(w, r)
|
|
|
|
}
|
|
|
|
|
2014-07-05 12:40:29 -07:00
|
|
|
func getQR(w http.ResponseWriter, r *http.Request) {
|
|
|
|
r.ParseForm()
|
|
|
|
text := r.FormValue("text")
|
|
|
|
code, err := qr.Encode(text, qr.M)
|
2014-05-21 11:06:14 -07:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Invalid", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "image/png")
|
|
|
|
w.Write(code.PNG())
|
|
|
|
}
|
|
|
|
|
2014-07-05 12:40:29 -07:00
|
|
|
func basicAuthMiddleware(username string, passhash string, next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if validAPIKey(r.Header.Get("X-API-Key")) {
|
|
|
|
next.ServeHTTP(w, r)
|
2014-06-04 13:00:55 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-04-19 04:33:51 -07:00
|
|
|
error := func() {
|
|
|
|
time.Sleep(time.Duration(rand.Intn(100)+100) * time.Millisecond)
|
2014-07-05 12:40:29 -07:00
|
|
|
w.Header().Set("WWW-Authenticate", "Basic realm=\"Authorization Required\"")
|
|
|
|
http.Error(w, "Not Authorized", http.StatusUnauthorized)
|
2014-04-19 04:33:51 -07:00
|
|
|
}
|
|
|
|
|
2014-07-05 12:40:29 -07:00
|
|
|
hdr := r.Header.Get("Authorization")
|
|
|
|
if !strings.HasPrefix(hdr, "Basic ") {
|
2014-04-19 04:33:51 -07:00
|
|
|
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-07-05 12:40:29 -07:00
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
2014-04-19 04:33:51 -07:00
|
|
|
}
|
2014-05-22 07:12:19 -07:00
|
|
|
|
2014-06-04 13:00:55 -07:00
|
|
|
func validAPIKey(k string) bool {
|
2014-06-05 00:16:12 -07:00
|
|
|
return len(apiKey) > 0 && k == apiKey
|
2014-06-04 13:00:55 -07:00
|
|
|
}
|
|
|
|
|
2014-07-22 11:11:36 -07:00
|
|
|
func embeddedStatic(assetDir string) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
file := r.URL.Path
|
2014-05-22 07:12:19 -07:00
|
|
|
|
2014-07-22 11:11:36 -07:00
|
|
|
if file[0] == '/' {
|
|
|
|
file = file[1:]
|
|
|
|
}
|
2014-05-22 07:12:19 -07:00
|
|
|
|
2014-07-22 11:11:36 -07:00
|
|
|
if len(file) == 0 {
|
|
|
|
file = "index.html"
|
|
|
|
}
|
2014-05-22 07:12:19 -07:00
|
|
|
|
2014-07-22 11:11:36 -07:00
|
|
|
if assetDir != "" {
|
|
|
|
p := filepath.Join(assetDir, filepath.FromSlash(file))
|
|
|
|
_, err := os.Stat(p)
|
|
|
|
if err == nil {
|
|
|
|
http.ServeFile(w, r, p)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2014-05-22 07:12:19 -07:00
|
|
|
|
2014-07-22 11:11:36 -07:00
|
|
|
bs, ok := auto.Assets[file]
|
|
|
|
if !ok {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mtype := mime.TypeByExtension(filepath.Ext(r.URL.Path))
|
|
|
|
if len(mtype) != 0 {
|
|
|
|
w.Header().Set("Content-Type", mtype)
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(bs)))
|
|
|
|
w.Header().Set("Last-Modified", modt)
|
2014-07-05 12:40:29 -07:00
|
|
|
|
2014-07-22 11:11:36 -07:00
|
|
|
w.Write(bs)
|
|
|
|
})
|
2014-05-22 07:12:19 -07:00
|
|
|
}
|