2012-04-03 21:39:49 +00:00
|
|
|
/*
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
Copyright 2012 The Perkeep Authors.
|
2012-04-03 21:39:49 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2013-06-11 15:16:21 +00:00
|
|
|
"crypto/rand"
|
2012-04-03 21:39:49 +00:00
|
|
|
"encoding/json"
|
2012-04-15 18:09:51 +00:00
|
|
|
"fmt"
|
2012-04-03 21:39:49 +00:00
|
|
|
"html/template"
|
2012-05-15 08:44:03 +00:00
|
|
|
"log"
|
2012-04-03 21:39:49 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
2012-04-21 20:41:40 +00:00
|
|
|
"strings"
|
2012-04-03 21:39:49 +00:00
|
|
|
|
2015-12-01 16:19:49 +00:00
|
|
|
"go4.org/jsonconfig"
|
2018-01-03 05:03:30 +00:00
|
|
|
"perkeep.org/internal/httputil"
|
|
|
|
"perkeep.org/internal/osutil"
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
"perkeep.org/pkg/auth"
|
|
|
|
"perkeep.org/pkg/blobserver"
|
2013-06-11 15:16:21 +00:00
|
|
|
|
2017-11-19 20:45:48 +00:00
|
|
|
"golang.org/x/net/xsrftoken"
|
2012-04-03 21:39:49 +00:00
|
|
|
)
|
|
|
|
|
2013-06-11 14:30:17 +00:00
|
|
|
var ignoredFields = map[string]bool{
|
|
|
|
"gallery": true,
|
|
|
|
"blog": true,
|
|
|
|
"replicateTo": true,
|
|
|
|
}
|
|
|
|
|
2012-04-03 21:39:49 +00:00
|
|
|
// SetupHandler handles serving the wizard setup page.
|
|
|
|
type SetupHandler struct {
|
|
|
|
config jsonconfig.Obj
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
blobserver.RegisterHandlerConstructor("setup", newSetupFromConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSetupFromConfig(ld blobserver.Loader, conf jsonconfig.Obj) (h http.Handler, err error) {
|
|
|
|
wizard := &SetupHandler{config: conf}
|
|
|
|
return wizard, nil
|
|
|
|
}
|
|
|
|
|
2012-04-22 15:33:22 +00:00
|
|
|
func printWizard(i interface{}) (s string) {
|
2012-04-21 20:41:40 +00:00
|
|
|
switch ei := i.(type) {
|
2012-04-22 15:33:22 +00:00
|
|
|
case []string:
|
|
|
|
for _, v := range ei {
|
|
|
|
s += printWizard(v) + ","
|
|
|
|
}
|
|
|
|
s = strings.TrimRight(s, ",")
|
2012-04-21 20:41:40 +00:00
|
|
|
case []interface{}:
|
|
|
|
for _, v := range ei {
|
2012-04-22 15:33:22 +00:00
|
|
|
s += printWizard(v) + ","
|
2012-04-21 20:41:40 +00:00
|
|
|
}
|
|
|
|
s = strings.TrimRight(s, ",")
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("%v", i)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2013-06-11 14:30:17 +00:00
|
|
|
// TODO(mpl): probably not needed anymore. check later and remove.
|
|
|
|
// Flatten all published entities as lists and move them at the root
|
2012-04-22 15:33:22 +00:00
|
|
|
// of the conf, to have them displayed individually by the template
|
|
|
|
func flattenPublish(config jsonconfig.Obj) error {
|
|
|
|
gallery := []string{}
|
|
|
|
blog := []string{}
|
|
|
|
config["gallery"] = gallery
|
|
|
|
config["blog"] = blog
|
|
|
|
published, ok := config["publish"]
|
|
|
|
if !ok {
|
|
|
|
delete(config, "publish")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
pubObj, ok := published.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Was expecting a map[string]interface{} for \"publish\", got %T", published)
|
|
|
|
}
|
|
|
|
for k, v := range pubObj {
|
|
|
|
pub, ok := v.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Was expecting a map[string]interface{} for %s, got %T", k, pub)
|
|
|
|
}
|
|
|
|
template, rootPermanode, style := "", "", ""
|
|
|
|
for pk, pv := range pub {
|
|
|
|
val, ok := pv.(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Was expecting a string for %s, got %T", pk, pv)
|
|
|
|
}
|
|
|
|
switch pk {
|
|
|
|
case "template":
|
|
|
|
template = val
|
|
|
|
case "rootPermanode":
|
|
|
|
rootPermanode = val
|
|
|
|
case "style":
|
|
|
|
style = val
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unknown key %q in %s", pk, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if template == "" || rootPermanode == "" {
|
2012-04-27 00:04:20 +00:00
|
|
|
return fmt.Errorf("missing \"template\" key or \"rootPermanode\" key in %s", k)
|
2012-04-22 15:33:22 +00:00
|
|
|
}
|
|
|
|
obj := []string{k, rootPermanode, style}
|
|
|
|
config[template] = obj
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(config, "publish")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2013-06-11 15:16:21 +00:00
|
|
|
var serverKey = func() string {
|
|
|
|
var b [20]byte
|
|
|
|
rand.Read(b[:])
|
|
|
|
return string(b[:])
|
|
|
|
}()
|
|
|
|
|
2012-10-15 15:28:07 +00:00
|
|
|
func sendWizard(rw http.ResponseWriter, req *http.Request, hasChanged bool) {
|
2012-04-03 21:39:49 +00:00
|
|
|
config, err := jsonconfig.ReadFile(osutil.UserServerConfigPath())
|
|
|
|
if err != nil {
|
2013-02-12 04:33:53 +00:00
|
|
|
httputil.ServeError(rw, req, err)
|
2012-04-03 21:39:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-04-22 15:33:22 +00:00
|
|
|
err = flattenPublish(config)
|
|
|
|
if err != nil {
|
2013-02-12 04:33:53 +00:00
|
|
|
httputil.ServeError(rw, req, err)
|
2012-04-22 15:33:22 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-04-21 20:41:40 +00:00
|
|
|
funcMap := template.FuncMap{
|
2013-06-11 14:30:17 +00:00
|
|
|
"printWizard": printWizard,
|
|
|
|
"showField": func(inputName string) bool {
|
|
|
|
if _, ok := ignoredFields[inputName]; ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
},
|
2013-06-11 15:16:21 +00:00
|
|
|
"genXSRF": func() string {
|
|
|
|
return xsrftoken.Generate(serverKey, "user", "wizardSave")
|
|
|
|
},
|
2012-04-21 20:41:40 +00:00
|
|
|
}
|
|
|
|
|
2013-06-11 14:30:17 +00:00
|
|
|
body := `
|
2013-06-11 15:16:21 +00:00
|
|
|
<form id="WizardForm" method="POST" enctype="multipart/form-data">
|
2013-06-11 14:30:17 +00:00
|
|
|
<table>
|
|
|
|
{{range $k,$v := .}}{{if showField $k}}<tr><td>{{printf "%v" $k}}</td><td><input type="text" size="30" name ="{{printf "%v" $k}}" value="{{printWizard $v}}" ></td></tr>{{end}}{{end}}
|
|
|
|
</table>
|
2013-06-11 15:16:21 +00:00
|
|
|
<input type="hidden" name="token" value="{{genXSRF}}">
|
2013-06-11 14:30:17 +00:00
|
|
|
<input type="submit" form="WizardForm" value="Save"> (Will restart server.)</form>`
|
2012-04-03 21:39:49 +00:00
|
|
|
|
|
|
|
if hasChanged {
|
2017-09-10 13:28:34 +00:00
|
|
|
body += `<p> Configuration successfully rewritten </p>`
|
2012-04-03 21:39:49 +00:00
|
|
|
}
|
|
|
|
|
2012-04-21 20:41:40 +00:00
|
|
|
tmpl, err := template.New("wizard").Funcs(funcMap).Parse(topWizard + body + bottomWizard)
|
2012-04-03 21:39:49 +00:00
|
|
|
if err != nil {
|
2013-02-12 04:33:53 +00:00
|
|
|
httputil.ServeError(rw, req, err)
|
2012-04-03 21:39:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = tmpl.Execute(rw, config)
|
|
|
|
if err != nil {
|
2013-02-12 04:33:53 +00:00
|
|
|
httputil.ServeError(rw, req, err)
|
2012-04-03 21:39:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func rewriteConfig(config *jsonconfig.Obj, configfile string) error {
|
|
|
|
b, err := json.MarshalIndent(*config, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s := string(b)
|
|
|
|
f, err := os.Create(configfile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
_, err = f.WriteString(s)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2012-10-15 15:28:07 +00:00
|
|
|
func handleSetupChange(rw http.ResponseWriter, req *http.Request) {
|
2012-04-03 21:39:49 +00:00
|
|
|
hilevelConf, err := jsonconfig.ReadFile(osutil.UserServerConfigPath())
|
|
|
|
if err != nil {
|
2013-02-12 04:33:53 +00:00
|
|
|
httputil.ServeError(rw, req, err)
|
2012-04-03 21:39:49 +00:00
|
|
|
return
|
|
|
|
}
|
2013-06-11 15:16:21 +00:00
|
|
|
if !xsrftoken.Valid(req.FormValue("token"), serverKey, "user", "wizardSave") {
|
|
|
|
http.Error(rw, "Form expired. Press back and reload form.", http.StatusBadRequest)
|
|
|
|
log.Printf("invalid xsrf token=%q", req.FormValue("token"))
|
|
|
|
return
|
|
|
|
}
|
2012-04-03 21:39:49 +00:00
|
|
|
|
|
|
|
hasChanged := false
|
2012-04-21 20:41:40 +00:00
|
|
|
var el interface{}
|
2012-04-22 15:33:22 +00:00
|
|
|
publish := jsonconfig.Obj{}
|
2012-04-03 21:39:49 +00:00
|
|
|
for k, v := range req.Form {
|
|
|
|
if _, ok := hilevelConf[k]; !ok {
|
2012-04-22 15:33:22 +00:00
|
|
|
if k != "gallery" && k != "blog" {
|
|
|
|
continue
|
|
|
|
}
|
2012-04-03 21:39:49 +00:00
|
|
|
}
|
|
|
|
|
2012-04-21 20:41:40 +00:00
|
|
|
switch k {
|
2013-06-11 14:30:17 +00:00
|
|
|
case "https", "shareHandler":
|
2012-04-21 20:41:40 +00:00
|
|
|
b, err := strconv.ParseBool(v[0])
|
|
|
|
if err != nil {
|
2013-04-01 15:04:09 +00:00
|
|
|
httputil.ServeError(rw, req, fmt.Errorf("%v field expects a boolean value", k))
|
2012-04-21 20:41:40 +00:00
|
|
|
}
|
2012-04-03 21:39:49 +00:00
|
|
|
el = b
|
2012-04-21 20:41:40 +00:00
|
|
|
default:
|
|
|
|
el = v[0]
|
2012-04-03 21:39:49 +00:00
|
|
|
}
|
|
|
|
if reflect.DeepEqual(hilevelConf[k], el) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
hasChanged = true
|
|
|
|
hilevelConf[k] = el
|
|
|
|
}
|
2012-04-22 15:33:22 +00:00
|
|
|
// "publish" wasn't checked yet
|
|
|
|
if !reflect.DeepEqual(hilevelConf["publish"], publish) {
|
|
|
|
hilevelConf["publish"] = publish
|
|
|
|
hasChanged = true
|
|
|
|
}
|
2012-04-03 21:39:49 +00:00
|
|
|
|
|
|
|
if hasChanged {
|
|
|
|
err = rewriteConfig(&hilevelConf, osutil.UserServerConfigPath())
|
|
|
|
if err != nil {
|
2013-02-12 04:33:53 +00:00
|
|
|
httputil.ServeError(rw, req, err)
|
2012-04-03 21:39:49 +00:00
|
|
|
return
|
|
|
|
}
|
2013-06-11 15:16:21 +00:00
|
|
|
err = osutil.RestartProcess()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to restart: " + err.Error())
|
|
|
|
http.Error(rw, "Failed to restart process", 500)
|
|
|
|
return
|
|
|
|
}
|
2012-04-03 21:39:49 +00:00
|
|
|
}
|
2012-10-15 15:28:07 +00:00
|
|
|
sendWizard(rw, req, hasChanged)
|
2012-04-03 21:39:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *SetupHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
2013-01-02 22:52:35 +00:00
|
|
|
if !auth.IsLocalhost(req) {
|
2012-04-15 18:09:51 +00:00
|
|
|
fmt.Fprintf(rw,
|
|
|
|
"<html><body>Setup only allowed from localhost"+
|
|
|
|
"<p><a href='/'>Back</a></p>"+
|
|
|
|
"</body></html>\n")
|
|
|
|
return
|
|
|
|
}
|
2016-04-27 15:48:50 +00:00
|
|
|
http.Redirect(rw, req, "https://camlistore.org/doc/server-config", http.StatusMovedPermanently)
|
2014-10-19 16:18:49 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
// TODO: this file and the code in wizard-html.go is outdated. Anyone interested enough
|
|
|
|
// can take care of updating it as something nicer which would fit better with the
|
|
|
|
// react UI. But in the meantime we don't link to it anymore.
|
|
|
|
|
2012-04-03 21:39:49 +00:00
|
|
|
if req.Method == "POST" {
|
2012-05-15 08:44:03 +00:00
|
|
|
err := req.ParseMultipartForm(10e6)
|
|
|
|
if err != nil {
|
2013-02-12 04:33:53 +00:00
|
|
|
httputil.ServeError(rw, req, err)
|
2012-05-15 08:44:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(req.Form) > 0 {
|
2012-10-15 15:28:07 +00:00
|
|
|
handleSetupChange(rw, req)
|
2012-05-15 08:44:03 +00:00
|
|
|
}
|
2013-06-11 15:16:21 +00:00
|
|
|
return
|
2012-04-03 21:39:49 +00:00
|
|
|
}
|
|
|
|
|
2012-10-15 15:28:07 +00:00
|
|
|
sendWizard(rw, req, false)
|
2012-04-03 21:39:49 +00:00
|
|
|
}
|