forked from rouben/zfswatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webserver.go
117 lines (103 loc) · 3.26 KB
/
webserver.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
//
// webserver.go
//
// Copyright © 2012-2013 Damicon Kraa Oy
//
// This file is part of zfswatcher.
//
// Zfswatcher is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Zfswatcher is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with zfswatcher. If not, see <http://www.gnu.org/licenses/>.
//
package main
import (
"fmt"
auth "github.com/abbot/go-http-auth"
"zfswatcher/notifier"
"html/template"
"math/rand"
"net/http"
"strings"
"time"
)
func getUserSecret(username, realm string) string {
if username == "" {
return ""
}
user, ok := cfg.Wwwuser[username]
if !ok {
return ""
}
if user.Enable {
return user.Password
}
return ""
}
func noDirListing(h http.Handler) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/") {
http.NotFound(w, r)
return
}
h.ServeHTTP(w, r)
})
}
func webServer() {
var err error
templates = template.New("zfswatcher").Funcs(template.FuncMap{
"nicenumber": niceNumber,
})
templates, err = templates.ParseGlob(cfg.Www.Templatedir + "/*.html")
if err != nil {
notify.Printf(notifier.ERR, "error parsing templates: %s", err)
}
authenticator := auth.NewBasicAuthenticator("zfswatcher", getUserSecret)
http.Handle(cfg.Www.Rootdir + "/resources/",
http.StripPrefix(cfg.Www.Rootdir + "/resources",
noDirListing(
http.FileServer(
http.Dir(cfg.Www.Resourcedir)))))
http.HandleFunc(cfg.Www.Rootdir + "/", authenticator.Wrap(dashboardHandler))
http.HandleFunc(cfg.Www.Rootdir + "/status/", authenticator.Wrap(statusHandler))
http.HandleFunc(cfg.Www.Rootdir + "/usage/", authenticator.Wrap(usageHandler))
http.HandleFunc(cfg.Www.Rootdir + "/statistics/", authenticator.Wrap(statisticsHandler))
http.HandleFunc(cfg.Www.Rootdir + "/logs/", authenticator.Wrap(logsHandler))
http.HandleFunc(cfg.Www.Rootdir + "/about/", authenticator.Wrap(aboutHandler))
http.HandleFunc(cfg.Www.Rootdir + "/locate/", authenticator.Wrap(locateHandler))
http.HandleFunc(cfg.Www.Rootdir + "/enclosure/", authenticator.Wrap(enclosureHandler))
if cfg.Www.Certfile != "" && cfg.Www.Keyfile != "" {
err = http.ListenAndServeTLS(cfg.Www.Bind, cfg.Www.Certfile, cfg.Www.Keyfile, nil)
} else {
err = http.ListenAndServe(cfg.Www.Bind, nil)
}
if err != nil {
notify.Printf(notifier.ERR, "error starting web server: %s", err)
}
}
func wwwHashPassword() {
fmt.Printf("Password (will echo): ")
var password string
_, err := fmt.Scanln(&password)
if err != nil {
fmt.Println("Error:", err)
return
}
rand.Seed(time.Now().UnixNano())
base64alpha := "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
var salt []byte
for i := 0; i < 8; i++ {
salt = append(salt, base64alpha[rand.Intn(len(base64alpha))])
}
hash := auth.MD5Crypt([]byte(password), salt, []byte("$1$"))
fmt.Println("Hash:", string(hash))
}
// eof