forked from jpillora/webproc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent_http.go
105 lines (97 loc) · 2.08 KB
/
agent_http.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
package agent
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"time"
"github.com/jpillora/velox"
)
func (a *agent) ServeHTTP(w http.ResponseWriter, r *http.Request) {
a.root.ServeHTTP(w, r)
}
func (a *agent) router(w http.ResponseWriter, r *http.Request) {
switch filepath.Base(r.URL.Path) {
case "velox.js":
velox.JS.ServeHTTP(w, r)
case "sync":
a.sync.ServeHTTP(w, r)
case "restart":
a.serveRestart(w, r)
case "refresh":
a.serveRefresh(w, r)
case "save":
a.serveSave(w, r)
default:
//fallback to static files
a.fs.ServeHTTP(w, r)
}
}
func (a *agent) serveRestart(w http.ResponseWriter, r *http.Request) {
a.restart() //user restart
w.WriteHeader(200)
}
func (a *agent) serveRefresh(w http.ResponseWriter, r *http.Request) {
a.readFiles() //user refresh config files
w.WriteHeader(200)
}
func (a *agent) serveSave(w http.ResponseWriter, r *http.Request) {
files := map[string]string{}
if err := json.NewDecoder(r.Body).Decode(&files); err != nil {
http.Error(w, "json error", 400)
return
}
if len(files) == 0 {
http.Error(w, "no files", 400)
return
}
//ensure in file whitelist
for f := range files {
allowed := false
for _, configFile := range a.data.Config.ConfigurationFiles {
if f == configFile {
allowed = true
break
}
}
if !allowed {
http.Error(w, "invalid file", 400)
return
}
}
for f, contents := range files {
perms := os.FileMode(600)
//use existing perms if able
exists := false
if info, err := os.Stat(f); err == nil {
perms = info.Mode().Perm()
exists = true
}
var newb = []byte(contents)
if exists {
b, err := ioutil.ReadFile(f)
if err != nil {
http.Error(w, "failed to read file", 500)
return
}
if bytes.Equal(b, newb) {
http.Error(w, "no change", 400)
return
}
}
if err := ioutil.WriteFile(f, newb, perms); err != nil {
http.Error(w, "failed to write changes", 500)
return
}
}
a.readFiles()
time.Sleep(100 * time.Millisecond)
switch a.data.Config.OnSave {
case OnSaveRestart:
a.restart()
}
w.WriteHeader(200)
return
}