forked from perkeep/perkeep
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wizard.go
287 lines (257 loc) · 6.99 KB
/
wizard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
Copyright 2012 The Camlistore Authors.
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 (
"crypto/rand"
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"os"
"reflect"
"strconv"
"strings"
"camlistore.org/pkg/auth"
"camlistore.org/pkg/blobserver"
"camlistore.org/pkg/httputil"
"camlistore.org/pkg/osutil"
"go4.org/jsonconfig"
"code.google.com/p/xsrftoken"
)
var ignoredFields = map[string]bool{
"gallery": true,
"blog": true,
"replicateTo": true,
}
// 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
}
func printWizard(i interface{}) (s string) {
switch ei := i.(type) {
case []string:
for _, v := range ei {
s += printWizard(v) + ","
}
s = strings.TrimRight(s, ",")
case []interface{}:
for _, v := range ei {
s += printWizard(v) + ","
}
s = strings.TrimRight(s, ",")
default:
return fmt.Sprintf("%v", i)
}
return s
}
// TODO(mpl): probably not needed anymore. check later and remove.
// Flatten all published entities as lists and move them at the root
// 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 == "" {
return fmt.Errorf("missing \"template\" key or \"rootPermanode\" key in %s", k)
}
obj := []string{k, rootPermanode, style}
config[template] = obj
}
delete(config, "publish")
return nil
}
var serverKey = func() string {
var b [20]byte
rand.Read(b[:])
return string(b[:])
}()
func sendWizard(rw http.ResponseWriter, req *http.Request, hasChanged bool) {
config, err := jsonconfig.ReadFile(osutil.UserServerConfigPath())
if err != nil {
httputil.ServeError(rw, req, err)
return
}
err = flattenPublish(config)
if err != nil {
httputil.ServeError(rw, req, err)
return
}
funcMap := template.FuncMap{
"printWizard": printWizard,
"showField": func(inputName string) bool {
if _, ok := ignoredFields[inputName]; ok {
return false
}
return true
},
"genXSRF": func() string {
return xsrftoken.Generate(serverKey, "user", "wizardSave")
},
}
body := `
<form id="WizardForm" method="POST" enctype="multipart/form-data">
<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>
<input type="hidden" name="token" value="{{genXSRF}}">
<input type="submit" form="WizardForm" value="Save"> (Will restart server.)</form>`
if hasChanged {
body += `<p> Configuration succesfully rewritten </p>`
}
tmpl, err := template.New("wizard").Funcs(funcMap).Parse(topWizard + body + bottomWizard)
if err != nil {
httputil.ServeError(rw, req, err)
return
}
err = tmpl.Execute(rw, config)
if err != nil {
httputil.ServeError(rw, req, err)
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
}
func handleSetupChange(rw http.ResponseWriter, req *http.Request) {
hilevelConf, err := jsonconfig.ReadFile(osutil.UserServerConfigPath())
if err != nil {
httputil.ServeError(rw, req, err)
return
}
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
}
hasChanged := false
var el interface{}
publish := jsonconfig.Obj{}
for k, v := range req.Form {
if _, ok := hilevelConf[k]; !ok {
if k != "gallery" && k != "blog" {
continue
}
}
switch k {
case "https", "shareHandler":
b, err := strconv.ParseBool(v[0])
if err != nil {
httputil.ServeError(rw, req, fmt.Errorf("%v field expects a boolean value", k))
}
el = b
default:
el = v[0]
}
if reflect.DeepEqual(hilevelConf[k], el) {
continue
}
hasChanged = true
hilevelConf[k] = el
}
// "publish" wasn't checked yet
if !reflect.DeepEqual(hilevelConf["publish"], publish) {
hilevelConf["publish"] = publish
hasChanged = true
}
if hasChanged {
err = rewriteConfig(&hilevelConf, osutil.UserServerConfigPath())
if err != nil {
httputil.ServeError(rw, req, err)
return
}
err = osutil.RestartProcess()
if err != nil {
log.Fatal("Failed to restart: " + err.Error())
http.Error(rw, "Failed to restart process", 500)
return
}
}
sendWizard(rw, req, hasChanged)
}
func (sh *SetupHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if !auth.IsLocalhost(req) {
fmt.Fprintf(rw,
"<html><body>Setup only allowed from localhost"+
"<p><a href='/'>Back</a></p>"+
"</body></html>\n")
return
}
http.Redirect(rw, req, "https://camlistore.org/doc/server-config", http.StatusMovedPermanently)
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.
if req.Method == "POST" {
err := req.ParseMultipartForm(10e6)
if err != nil {
httputil.ServeError(rw, req, err)
return
}
if len(req.Form) > 0 {
handleSetupChange(rw, req)
}
return
}
sendWizard(rw, req, false)
}