2014-03-02 23:58:14 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2014-04-19 13:33:51 +02:00
|
|
|
"bytes"
|
|
|
|
|
"encoding/base64"
|
2014-03-02 23:58:14 +01:00
|
|
|
"encoding/json"
|
|
|
|
|
"io/ioutil"
|
2014-04-19 13:33:51 +02:00
|
|
|
"math/rand"
|
2014-03-02 23:58:14 +01:00
|
|
|
"net/http"
|
|
|
|
|
"runtime"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
2014-04-19 13:33:51 +02:00
|
|
|
"code.google.com/p/go.crypto/bcrypt"
|
2014-03-08 23:02:01 +01:00
|
|
|
"github.com/calmh/syncthing/scanner"
|
2014-03-02 23:58:14 +01:00
|
|
|
"github.com/codegangsta/martini"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type guiError struct {
|
|
|
|
|
Time time.Time
|
|
|
|
|
Error string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
configInSync = true
|
|
|
|
|
guiErrors = []guiError{}
|
|
|
|
|
guiErrorsMut sync.Mutex
|
|
|
|
|
)
|
|
|
|
|
|
2014-04-19 13:33:51 +02:00
|
|
|
const (
|
|
|
|
|
unchangedPassword = "--password-unchanged--"
|
|
|
|
|
)
|
|
|
|
|
|
2014-04-08 15:56:12 +02:00
|
|
|
func startGUI(cfg GUIConfiguration, m *Model) {
|
2014-03-02 23:58:14 +01:00
|
|
|
router := martini.NewRouter()
|
|
|
|
|
router.Get("/", getRoot)
|
|
|
|
|
router.Get("/rest/version", restGetVersion)
|
2014-04-23 10:04:07 +02:00
|
|
|
router.Get("/rest/model", restGetModel)
|
2014-03-02 23:58:14 +01: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)
|
|
|
|
|
|
|
|
|
|
router.Post("/rest/config", restPostConfig)
|
|
|
|
|
router.Post("/rest/restart", restPostRestart)
|
2014-04-03 22:10:51 +02:00
|
|
|
router.Post("/rest/reset", restPostReset)
|
2014-03-02 23:58:14 +01:00
|
|
|
router.Post("/rest/error", restPostError)
|
2014-04-16 16:30:49 +02:00
|
|
|
router.Post("/rest/error/clear", restClearErrors)
|
2014-03-02 23:58:14 +01:00
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
mr := martini.New()
|
2014-04-08 15:56:12 +02:00
|
|
|
if len(cfg.User) > 0 && len(cfg.Password) > 0 {
|
2014-04-19 13:33:51 +02:00
|
|
|
mr.Use(basic(cfg.User, cfg.Password))
|
2014-04-08 15:56:12 +02:00
|
|
|
}
|
2014-03-02 23:58:14 +01:00
|
|
|
mr.Use(embeddedStatic())
|
|
|
|
|
mr.Use(martini.Recovery())
|
2014-03-26 20:32:35 +01:00
|
|
|
mr.Use(restMiddleware)
|
2014-03-02 23:58:14 +01:00
|
|
|
mr.Action(router.Handle)
|
|
|
|
|
mr.Map(m)
|
2014-04-08 15:56:12 +02:00
|
|
|
err := http.ListenAndServe(cfg.Address, mr)
|
2014-03-02 23:58:14 +01:00
|
|
|
if err != nil {
|
|
|
|
|
warnln("GUI not possible:", err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getRoot(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
http.Redirect(w, r, "/index.html", 302)
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-26 20:32:35 +01: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 23:58:14 +01:00
|
|
|
func restGetVersion() string {
|
|
|
|
|
return Version
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-23 10:04:07 +02:00
|
|
|
func restGetModel(m *Model, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var qs = r.URL.Query()
|
|
|
|
|
var repo = qs.Get("repo")
|
2014-03-02 23:58:14 +01:00
|
|
|
var res = make(map[string]interface{})
|
|
|
|
|
|
2014-04-27 21:53:27 +02:00
|
|
|
for _, cr := range cfg.Repositories {
|
|
|
|
|
if cr.ID == repo {
|
|
|
|
|
res["invalid"] = cr.Invalid
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-09 22:03:30 +02:00
|
|
|
globalFiles, globalDeleted, globalBytes := m.GlobalSize(repo)
|
2014-03-02 23:58:14 +01:00
|
|
|
res["globalFiles"], res["globalDeleted"], res["globalBytes"] = globalFiles, globalDeleted, globalBytes
|
|
|
|
|
|
2014-04-09 22:03:30 +02:00
|
|
|
localFiles, localDeleted, localBytes := m.LocalSize(repo)
|
2014-03-02 23:58:14 +01:00
|
|
|
res["localFiles"], res["localDeleted"], res["localBytes"] = localFiles, localDeleted, localBytes
|
|
|
|
|
|
2014-04-09 22:03:30 +02:00
|
|
|
needFiles, needBytes := m.NeedSize(repo)
|
|
|
|
|
res["needFiles"], res["needBytes"] = needFiles, needBytes
|
2014-03-02 23:58:14 +01:00
|
|
|
|
2014-04-09 22:03:30 +02:00
|
|
|
res["inSyncFiles"], res["inSyncBytes"] = globalFiles-needFiles, globalBytes-needBytes
|
2014-03-02 23:58:14 +01:00
|
|
|
|
2014-04-14 09:58:17 +02:00
|
|
|
res["state"] = m.State(repo)
|
|
|
|
|
|
2014-03-02 23:58:14 +01:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func restGetConnections(m *Model, w http.ResponseWriter) {
|
|
|
|
|
var res = m.ConnectionStats()
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func restGetConfig(w http.ResponseWriter) {
|
2014-04-19 13:33:51 +02:00
|
|
|
encCfg := cfg
|
2014-04-19 22:36:12 +02:00
|
|
|
if encCfg.GUI.Password != "" {
|
|
|
|
|
encCfg.GUI.Password = unchangedPassword
|
|
|
|
|
}
|
2014-04-19 13:33:51 +02:00
|
|
|
json.NewEncoder(w).Encode(encCfg)
|
2014-03-02 23:58:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func restPostConfig(req *http.Request) {
|
2014-04-19 13:33:51 +02:00
|
|
|
var prevPassHash = cfg.GUI.Password
|
2014-03-02 23:58:14 +01:00
|
|
|
err := json.NewDecoder(req.Body).Decode(&cfg)
|
|
|
|
|
if err != nil {
|
2014-04-19 13:33:51 +02:00
|
|
|
warnln(err)
|
2014-03-02 23:58:14 +01:00
|
|
|
} else {
|
2014-04-19 22:36:12 +02:00
|
|
|
if cfg.GUI.Password == "" {
|
|
|
|
|
// Leave it empty
|
|
|
|
|
} else if cfg.GUI.Password != unchangedPassword {
|
2014-04-19 13:33:51 +02:00
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(cfg.GUI.Password), 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
warnln(err)
|
|
|
|
|
} else {
|
|
|
|
|
cfg.GUI.Password = string(hash)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
cfg.GUI.Password = prevPassHash
|
|
|
|
|
}
|
2014-03-02 23:58:14 +01:00
|
|
|
saveConfig()
|
|
|
|
|
configInSync = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func restGetConfigInSync(w http.ResponseWriter) {
|
|
|
|
|
json.NewEncoder(w).Encode(map[string]bool{"configInSync": configInSync})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func restPostRestart(req *http.Request) {
|
2014-04-03 22:10:51 +02:00
|
|
|
go restart()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func restPostReset(req *http.Request) {
|
|
|
|
|
resetRepositories()
|
|
|
|
|
go restart()
|
2014-03-02 23:58:14 +01:00
|
|
|
}
|
|
|
|
|
|
2014-03-08 23:02:01 +01:00
|
|
|
type guiFile scanner.File
|
2014-03-02 23:58:14 +01:00
|
|
|
|
|
|
|
|
func (f guiFile) MarshalJSON() ([]byte, error) {
|
|
|
|
|
type t struct {
|
2014-03-29 14:58:44 +01:00
|
|
|
Name string
|
|
|
|
|
Size int64
|
|
|
|
|
Modified int64
|
|
|
|
|
Flags uint32
|
2014-03-02 23:58:14 +01:00
|
|
|
}
|
|
|
|
|
return json.Marshal(t{
|
2014-03-29 14:58:44 +01:00
|
|
|
Name: f.Name,
|
|
|
|
|
Size: scanner.File(f).Size,
|
|
|
|
|
Modified: f.Modified,
|
|
|
|
|
Flags: f.Flags,
|
2014-03-02 23:58:14 +01:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-14 12:02:40 +02:00
|
|
|
var cpuUsagePercent [10]float64 // The last ten seconds
|
2014-03-02 23:58:14 +01: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-04-16 17:36:09 +02:00
|
|
|
if discoverer != nil {
|
|
|
|
|
res["extAnnounceOK"] = discoverer.ExtAnnounceOK()
|
|
|
|
|
}
|
2014-03-02 23:58:14 +01:00
|
|
|
cpuUsageLock.RLock()
|
2014-04-14 12:02:40 +02:00
|
|
|
var cpusum float64
|
|
|
|
|
for _, p := range cpuUsagePercent {
|
|
|
|
|
cpusum += p
|
|
|
|
|
}
|
2014-03-02 23:58:14 +01:00
|
|
|
cpuUsageLock.RUnlock()
|
2014-04-14 12:02:40 +02:00
|
|
|
res["cpuPercent"] = cpusum / 10
|
2014-03-02 23:58:14 +01: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()
|
|
|
|
|
showGuiError(string(bs))
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-16 16:30:49 +02:00
|
|
|
func restClearErrors() {
|
|
|
|
|
guiErrorsMut.Lock()
|
|
|
|
|
guiErrors = nil
|
|
|
|
|
guiErrorsMut.Unlock()
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-02 23:58:14 +01:00
|
|
|
func showGuiError(err string) {
|
|
|
|
|
guiErrorsMut.Lock()
|
|
|
|
|
guiErrors = append(guiErrors, guiError{time.Now(), err})
|
|
|
|
|
if len(guiErrors) > 5 {
|
|
|
|
|
guiErrors = guiErrors[len(guiErrors)-5:]
|
|
|
|
|
}
|
|
|
|
|
guiErrorsMut.Unlock()
|
|
|
|
|
}
|
2014-04-19 13:33:51 +02: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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|