-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
418 lines (355 loc) · 8.66 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package main
import (
"crypto/tls"
"embed"
"encoding/csv"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"sync"
"text/template"
"time"
"golang.org/x/crypto/bcrypt"
"golang.org/x/net/webdav"
"golang.org/x/term"
"suah.dev/protect"
)
// Landing will be used to fill our landing template
type Landing struct {
User string
URL string
}
const landingPage = `
<h1>Hello{{if .User}} {{.User}}{{end}}! Welcome to widdler!</h1>
<p>To create a new TiddlyWiki html file, simply append an html file name to the URL in the address bar!</p>
<h3>For example:</h3>
<a href="{{.URL}}">{{.URL}}</a>
<p>This will create a new wiki called "<b>wiki.html</b>"</p>
<p>After creating a wiki, this message will be replaced by a list of your wiki files.</p>
`
var (
twFile = "empty-5.2.1.html"
//go:embed empty-5.2.1.html
tiddly embed.FS
templ *template.Template
)
type userHandler struct {
mu sync.Mutex
dav *webdav.Handler
fs http.Handler
name string
}
type userHandlers struct {
list []userHandler
mu sync.RWMutex
}
func (u *userHandlers) find(name string) *userHandler {
for i := range u.list {
if u.list[i].name == name {
return &u.list[i]
}
}
return nil
}
var (
auth string
davDir string
fullListen string
genHtpass bool
handlers userHandlers
listen string
passPath string
tlsCert string
tlsKey string
users map[string]string
version bool
build string
)
var pledges = "stdio wpath rpath cpath tty inet dns unveil"
func init() {
users = make(map[string]string)
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatalln(err)
}
flag.StringVar(&davDir, "wikis", dir, "Directory of TiddlyWikis to serve over WebDAV.")
flag.StringVar(&listen, "http", "localhost:8080", "Listen on")
flag.StringVar(&tlsCert, "tlscert", "", "TLS certificate.")
flag.StringVar(&tlsKey, "tlskey", "", "TLS key.")
flag.StringVar(&passPath, "htpass", fmt.Sprintf("%s/.htpasswd", dir), "Path to .htpasswd file..")
flag.StringVar(&auth, "auth", "basic", "Enable HTTP Basic Authentication.")
flag.BoolVar(&genHtpass, "gen", false, "Generate a .htpasswd file or add a new entry to an existing file.")
flag.BoolVar(&version, "v", false, "Show version and exit.")
flag.Parse()
// These are OpenBSD specific protections used to prevent unnecessary file access.
_ = protect.Unveil(passPath, "rwc")
_ = protect.Unveil(davDir, "rwc")
_ = protect.Unveil("/etc/ssl/cert.pem", "r")
_ = protect.Unveil("/etc/resolv.conf", "r")
_ = protect.Pledge(pledges)
templ, err = template.New("landing").Parse(landingPage)
if err != nil {
log.Fatalln(err)
}
}
func authenticate(user string, pass string) bool {
htpass, exists := users[user]
if !exists {
return false
}
err := bcrypt.CompareHashAndPassword([]byte(htpass), []byte(pass))
return err == nil
}
func logger(f http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
n := time.Now()
fmt.Printf("%s (%s) [%s] \"%s %s\" %03d\n",
r.RemoteAddr,
n.Format(time.RFC822Z),
r.Method,
r.URL.Path,
r.Proto,
r.ContentLength,
)
f(w, r)
}
}
func createEmpty(path string) error {
_, fErr := os.Stat(path)
if os.IsNotExist(fErr) {
log.Printf("creating %q\n", path)
twData, _ := tiddly.ReadFile(twFile)
wErr := ioutil.WriteFile(path, twData, 0600)
if wErr != nil {
return wErr
}
}
return nil
}
func prompt(prompt string, secure bool) (string, error) {
var input string
fmt.Print(prompt)
if secure {
b, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "", err
}
input = string(b)
} else {
_, err := fmt.Scanln(&input)
if err != nil {
return "", err
}
}
return input, nil
}
func addHandler(u, uPath string) {
handlers.list = append(handlers.list, userHandler{
name: u,
dav: &webdav.Handler{
LockSystem: webdav.NewMemLS(),
FileSystem: webdav.Dir(uPath),
Logger: func(r *http.Request, err error) {
if err != nil {
log.Print(err)
}
},
},
fs: http.FileServer(http.Dir(uPath)),
})
}
func main() {
if version {
fmt.Println(build)
os.Exit(0)
}
if genHtpass {
user, err := prompt("Username: ", false)
if err != nil {
log.Fatalln(err)
}
pass, err := prompt("Password: ", true)
if err != nil {
log.Fatalln(err)
}
hash, err := bcrypt.GenerateFromPassword([]byte(pass), 11)
if err != nil {
log.Fatalln(err)
}
f, err := os.OpenFile(filepath.Clean(passPath), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
log.Fatalln(err)
}
if _, err := f.WriteString(fmt.Sprintf("%s:%s\n", user, hash)); err != nil {
log.Fatalln(err)
}
err = f.Close()
if err != nil {
log.Fatalln(err)
}
fmt.Printf("Added %q to %q\n", user, passPath)
os.Exit(0)
}
pledges, _ = protect.ReducePledges(pledges, "tty")
// drop to only read on passPath
_ = protect.Unveil(passPath, "r")
pledges, _ = protect.ReducePledges(pledges, "unveil")
_, fErr := os.Stat(passPath)
if os.IsNotExist(fErr) {
if auth == "basic" || auth == "header" {
fmt.Println("No .htpasswd file found!")
os.Exit(1)
}
} else {
p, err := os.Open(filepath.Clean(passPath))
if err != nil {
log.Fatal(err)
}
ht := csv.NewReader(p)
ht.Comma = ':'
ht.Comment = '#'
ht.TrimLeadingSpace = true
entries, err := ht.ReadAll()
if err != nil {
log.Fatal(err)
}
err = p.Close()
if err != nil {
log.Fatal(err)
}
for _, parts := range entries {
users[parts[0]] = parts[1]
}
}
if auth == "basic" || auth == "header" {
for u := range users {
uPath := path.Join(davDir, u)
addHandler(u, uPath)
}
} else {
addHandler("", davDir)
}
mux := http.NewServeMux()
mux.HandleFunc("/", logger(func(w http.ResponseWriter, r *http.Request) {
user, pass := "", ""
var ok bool
if strings.Contains(r.URL.Path, ".htpasswd") {
http.NotFound(w, r)
return
}
// Prevent directory traversal
if strings.Contains(r.URL.Path, "..") {
http.NotFound(w, r)
return
}
if auth == "basic" {
user, pass, ok = r.BasicAuth()
if !(ok && authenticate(user, pass)) {
w.Header().Set("WWW-Authenticate", `Basic realm="widdler"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
} else if auth == "header" {
var prefix = "Auth"
for name, values := range r.Header {
if strings.HasPrefix(name, prefix) {
user = strings.TrimLeft(name, prefix)
pass = values[0]
ok = true
break
}
}
if !(ok && authenticate(user, pass)) {
w.Header().Set("WWW-Authenticate", `Basic realm="widdler"`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
}
handlers.mu.RLock()
handler := handlers.find(user)
handlers.mu.RUnlock()
if handler == nil {
http.NotFound(w, r)
return
}
handler.mu.Lock()
defer handler.mu.Unlock()
userPath := path.Join(davDir, user)
fullPath := path.Join(davDir, user, r.URL.Path)
_, dErr := os.Stat(userPath)
if os.IsNotExist(dErr) {
mErr := os.Mkdir(userPath, 0700)
if mErr != nil {
http.Error(w, mErr.Error(), http.StatusInternalServerError)
return
}
}
isHTML, err := regexp.Match(`\.html$`, []byte(r.URL.Path))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if isHTML {
// HTML files will be created or sent back
err := createEmpty(fullPath)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
handler.dav.ServeHTTP(w, r)
} else {
// Everything else is browsable
entries, err := os.ReadDir(userPath)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if len(entries) > 0 {
handler.fs.ServeHTTP(w, r)
} else {
l := Landing{
URL: fmt.Sprintf("%s/wiki.html", fullListen),
}
if user != "" {
l.User = user
}
err = templ.ExecuteTemplate(w, "landing", l)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
}
}))
s := http.Server{
Handler: mux,
}
lis, err := net.Listen("tcp", listen)
if err != nil {
log.Fatalln(err)
}
if tlsCert != "" && tlsKey != "" {
fullListen = fmt.Sprintf("https://%s", listen)
s.TLSConfig = &tls.Config{
MinVersion: tls.VersionTLS12,
CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
PreferServerCipherSuites: true,
}
log.Printf("Listening for HTTPS on 'https://%s'", listen)
log.Fatalln(s.ServeTLS(lis, tlsCert, tlsKey))
} else {
fullListen = fmt.Sprintf("http://%s", listen)
log.Printf("Listening for HTTP on 'http://%s'", listen)
log.Fatalln(s.Serve(lis))
}
}