-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
184 lines (165 loc) · 5.21 KB
/
main.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
// This program 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.
//
// This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
// widdly is a self-hosted web application which can serve as a personal TiddlyWiki.
package main
import (
"bytes"
"compress/flate"
"crypto/subtle"
"flag"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"golang.org/x/crypto/bcrypt"
"github.com/daaku/go.zipexe"
"gitlab.com/opennota/widdly/api"
"gitlab.com/opennota/widdly/store"
)
var (
addr = flag.String("http", "127.0.0.1:8080", "HTTP service address")
password = flag.String("p", "", "Optional password to protect the wiki (the username is widdly)")
)
func main() {
flag.Parse()
// Open the data store and tell HTTP handlers to use it.
api.Store = store.MustOpen(*dataSource)
// Maybe read index.html from a zip archive appended to the current executable.
wikiData := tryReadWikiFromExecutable()
// Override api.ServeIndex to allow serving embedded index.html.
wiki := pathToWiki()
api.ServeIndex = func(w http.ResponseWriter, r *http.Request) {
if fi, err := os.Stat(wiki); err == nil && isRegular(fi) { // Prefer the real file, if it exists.
http.ServeFile(w, r, wiki)
} else if fi, err := os.Stat("index.html"); err == nil && isRegular(fi) {
http.ServeFile(w, r, "index.html")
} else if len(wikiData) > 0 { // ...or use an embedded one.
w.Header().Add("Content-Type", "text/html")
w.Header().Add("Content-Encoding", "deflate")
w.Header().Add("Content-Length", strconv.Itoa(len(wikiData)))
w.Write(wikiData)
} else {
http.NotFound(w, r)
}
}
// Optionally protect by a password.
if *password != "" {
// Select an appropriate bcrypt cost.
bcryptCost := bcrypt.DefaultCost
for cost := bcrypt.MinCost + 1; cost <= bcrypt.MaxCost; cost++ {
start := time.Now()
if _, err := bcrypt.GenerateFromPassword([]byte("qwerty"), cost); err != nil {
log.Fatal(err)
}
if time.Since(start) > time.Second {
bcryptCost = cost - 1
break
}
}
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(*password), bcryptCost)
if err != nil {
log.Fatal(err)
}
// Set api.Authenticate and provide a login handler for simple password authentication.
api.Authenticate = func(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth()
if !ok || bcrypt.CompareHashAndPassword(hashedPassword, []byte(pass)) != nil ||
subtle.ConstantTimeCompare([]byte(user), []byte("widdly")) != 1 { // DON'T use subtle.ConstantTimeCompare like this!
w.Header().Add("Www-Authenticate", `Basic realm="Who are you?"`)
w.WriteHeader(http.StatusUnauthorized)
}
}
}
log.Fatal(http.ListenAndServe(*addr, nil))
}
// pathToWiki returns a path that should be checked for index.html.
// If there is index.html, it should be put next to the executable.
// If for some reason pathToWiki fails to find the path to the current executable,
// it falls back to searching in the current directory.
func pathToWiki() string {
dir := ""
path, err := os.Executable()
if err == nil {
dir = filepath.Dir(path)
} else if wd, err := os.Getwd(); err == nil {
dir = wd
}
return filepath.Join(dir, "index.html")
}
// isRegular returns true iff the file described by fi is a regular file.
func isRegular(fi os.FileInfo) bool {
return fi.Mode()&os.ModeType == 0
}
// deflate compresses data and returns the compressed slice.
func deflate(data []byte) ([]byte, error) {
var buf bytes.Buffer
w, _ := flate.NewWriter(&buf, flate.BestCompression)
_, err := w.Write(data)
if err != nil {
return nil, err
}
err = w.Close()
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// tryReadWikiFromExecutable tries to read index.html from a zip archive appended to the current executable.
// If it succeeds, it returns deflate-compressed index.html. If it fails, it returns nil.
func tryReadWikiFromExecutable() []byte {
path, err := os.Executable()
if err != nil {
return nil
}
f, err := os.Open(path)
if err != nil {
return nil
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return nil
}
r, err := zipexe.NewReader(f, fi.Size())
if err != nil {
return nil
}
for _, zf := range r.File {
// Get the first .html file.
if !strings.HasSuffix(zf.Name, ".html") {
continue
}
// Read the archived index.html. Could use zf.DataOffset() and zf.CompressedSize64
// and avoid the unnecessary decompression and compression steps,
// but zipexe does not provide zip file offset relative to the executable.
rc, err := zf.Open()
if err != nil {
return nil
}
defer rc.Close()
data, err := ioutil.ReadAll(rc)
if err != nil {
return nil
}
compressed, err := deflate(data)
if err != nil {
return nil
}
return compressed
}
return nil
}