http.go (view raw)
1package main
2
3import (
4 "bytes"
5 "database/sql"
6 "fmt"
7 gmi "git.sr.ht/~adnano/go-gemini"
8 "github.com/gorilla/handlers"
9 "github.com/gorilla/sessions"
10 _ "github.com/mattn/go-sqlite3"
11 "golang.org/x/crypto/bcrypt"
12 "html/template"
13 "io"
14 "io/ioutil"
15 "log"
16 "mime"
17 "net/http"
18 "os"
19 "path"
20 "path/filepath"
21 "strings"
22 "time"
23)
24
25var t *template.Template
26var DB *sql.DB
27var SessionStore *sessions.CookieStore
28
29func renderDefaultError(w http.ResponseWriter, statusCode int) {
30 errorMsg := http.StatusText(statusCode)
31 renderError(w, errorMsg, statusCode)
32}
33
34func renderError(w http.ResponseWriter, errorMsg string, statusCode int) {
35 data := struct {
36 PageTitle string
37 StatusCode int
38 ErrorMsg string
39 }{"Error!", statusCode, errorMsg}
40 err := t.ExecuteTemplate(w, "error.html", data)
41 if err != nil { // Shouldn't happen probably
42 http.Error(w, errorMsg, statusCode)
43 }
44}
45
46func rootHandler(w http.ResponseWriter, r *http.Request) {
47 // serve everything inside static directory
48 if r.URL.Path != "/" {
49 fileName := path.Join(c.TemplatesDirectory, "static", filepath.Clean(r.URL.Path))
50 _, err := os.Stat(fileName)
51 if err != nil {
52 renderDefaultError(w, http.StatusNotFound)
53 return
54 }
55 http.ServeFile(w, r, fileName) // TODO better error handling
56 return
57 }
58
59 user := newGetAuthUser(r)
60 indexFiles, err := getIndexFiles(user.IsAdmin)
61 if err != nil {
62 panic(err)
63 }
64 allUsers, err := getActiveUserNames()
65 if err != nil {
66 panic(err)
67 }
68 data := struct {
69 Host string
70 PageTitle string
71 Files []*File
72 Users []string
73 AuthUser AuthUser
74 }{c.Host, c.SiteTitle, indexFiles, allUsers, user}
75 err = t.ExecuteTemplate(w, "index.html", data)
76 if err != nil {
77 panic(err)
78 }
79}
80
81func editFileHandler(w http.ResponseWriter, r *http.Request) {
82 user := newGetAuthUser(r)
83 if !user.LoggedIn {
84 renderDefaultError(w, http.StatusForbidden)
85 return
86 }
87 fileName := filepath.Clean(r.URL.Path[len("/edit/"):])
88 isText := strings.HasPrefix(mime.TypeByExtension(path.Ext(fileName)), "text")
89 filePath := path.Join(c.FilesDirectory, user.Username, fileName)
90
91 if r.Method == "GET" {
92 err := checkIfValidFile(filePath, nil)
93 if err != nil {
94 log.Println(err)
95 renderError(w, err.Error(), http.StatusBadRequest)
96 return
97 }
98 // Create directories if dne
99 f, err := os.OpenFile(filePath, os.O_RDONLY, 0644)
100 var fileBytes []byte
101 if os.IsNotExist(err) || !isText {
102 fileBytes = []byte{}
103 err = nil
104 } else {
105 defer f.Close()
106 fileBytes, err = ioutil.ReadAll(f)
107 }
108 if err != nil {
109 panic(err)
110 }
111 data := struct {
112 FileName string
113 FileText string
114 PageTitle string
115 AuthUser AuthUser
116 Host string
117 IsText bool
118 }{fileName, string(fileBytes), c.SiteTitle, user, c.Host, isText}
119 err = t.ExecuteTemplate(w, "edit_file.html", data)
120 if err != nil {
121 panic(err)
122 }
123 } else if r.Method == "POST" {
124 // get post body
125 r.ParseForm()
126 fileText := r.Form.Get("file_text")
127 // Web form by default gives us CR LF newlines.
128 // Unix files use just LF
129 fileText = strings.ReplaceAll(fileText, "\r\n", "\n")
130 fileBytes := []byte(fileText)
131 err := checkIfValidFile(filePath, fileBytes)
132 if err != nil {
133 log.Println(err)
134 renderError(w, err.Error(), http.StatusBadRequest)
135 return
136 }
137 // create directories if dne
138 os.MkdirAll(path.Dir(filePath), os.ModePerm)
139 if userHasSpace(user.Username, len(fileBytes)) {
140 if isText { // Cant edit binary files here
141 err = ioutil.WriteFile(filePath, fileBytes, 0644)
142 }
143 } else {
144 renderError(w, fmt.Sprintf("Bad Request: Out of file space. Max space: %d.", c.MaxUserBytes), http.StatusBadRequest)
145 return
146 }
147 if err != nil {
148 panic(err)
149 }
150 newName := filepath.Clean(r.Form.Get("rename"))
151 err = checkIfValidFile(newName, fileBytes)
152 if err != nil {
153 log.Println(err)
154 renderError(w, err.Error(), http.StatusBadRequest)
155 return
156 }
157 if newName != fileName {
158 newPath := path.Join(c.FilesDirectory, user.Username, newName)
159 os.MkdirAll(path.Dir(newPath), os.ModePerm)
160 os.Rename(filePath, newPath)
161 fileName = newName
162 }
163 http.Redirect(w, r, path.Join("/edit", fileName), http.StatusSeeOther)
164 }
165}
166
167func uploadFilesHandler(w http.ResponseWriter, r *http.Request) {
168 if r.Method == "POST" {
169 user := newGetAuthUser(r)
170 if !user.LoggedIn {
171 renderDefaultError(w, http.StatusForbidden)
172 return
173 }
174 r.ParseMultipartForm(10 << 6) // why does this not work
175 file, fileHeader, err := r.FormFile("file")
176 fileName := filepath.Clean(fileHeader.Filename)
177 defer file.Close()
178 if err != nil {
179 log.Println(err)
180 renderError(w, err.Error(), http.StatusBadRequest)
181 return
182 }
183 dest, _ := ioutil.ReadAll(file)
184 err = checkIfValidFile(fileName, dest)
185 if err != nil {
186 log.Println(err)
187 renderError(w, err.Error(), http.StatusBadRequest)
188 return
189 }
190 destPath := path.Join(c.FilesDirectory, user.Username, fileName)
191
192 f, err := os.OpenFile(destPath, os.O_WRONLY|os.O_CREATE, 0644)
193 if err != nil {
194 panic(err)
195 }
196 defer f.Close()
197 if userHasSpace(user.Username, c.MaxFileBytes) { // Not quite right
198 io.Copy(f, bytes.NewReader(dest))
199 } else {
200 renderError(w, fmt.Sprintf("Bad Request: Out of file space. Max space: %d.", c.MaxUserBytes), http.StatusBadRequest)
201 return
202 }
203 }
204 http.Redirect(w, r, "/my_site", http.StatusSeeOther)
205}
206
207// TODO use this
208type AuthUser struct {
209 LoggedIn bool
210 Username string
211 IsAdmin bool
212 ImpersonatingUser string // used if impersonating
213}
214
215func newGetAuthUser(r *http.Request) AuthUser {
216 session, _ := SessionStore.Get(r, "cookie-session")
217 user, ok := session.Values["auth_user"].(string)
218 impers, _ := session.Values["impersonating_user"].(string)
219 isAdmin, _ := session.Values["admin"].(bool)
220 return AuthUser{
221 LoggedIn: ok,
222 Username: user,
223 IsAdmin: isAdmin,
224 ImpersonatingUser: impers,
225 }
226}
227
228func mySiteHandler(w http.ResponseWriter, r *http.Request) {
229 user := newGetAuthUser(r)
230 if !user.LoggedIn {
231 renderDefaultError(w, http.StatusForbidden)
232 return
233 }
234 // check auth
235 userFolder := getUserDirectory(user.Username)
236 files, _ := getMyFilesRecursive(userFolder, user.Username)
237 data := struct {
238 Host string
239 PageTitle string
240 Files []*File
241 AuthUser AuthUser
242 }{c.Host, c.SiteTitle, files, user}
243 _ = t.ExecuteTemplate(w, "my_site.html", data)
244}
245
246func myAccountHandler(w http.ResponseWriter, r *http.Request) {
247 user := newGetAuthUser(r)
248 authUser := user.Username
249 if !user.LoggedIn {
250 renderDefaultError(w, http.StatusForbidden)
251 return
252 }
253 me, _ := getUserByName(user.Username)
254 type pageData struct {
255 PageTitle string
256 AuthUser AuthUser
257 Email string
258 Errors []string
259 }
260 data := pageData{"My Account", user, me.Email, nil}
261
262 if r.Method == "GET" {
263 err := t.ExecuteTemplate(w, "me.html", data)
264 if err != nil {
265 panic(err)
266 }
267 } else if r.Method == "POST" {
268 r.ParseForm()
269 newUsername := r.Form.Get("username")
270 errors := []string{}
271 newEmail := r.Form.Get("email")
272 newUsername = strings.ToLower(newUsername)
273 var err error
274 if newEmail != me.Email {
275 _, err = DB.Exec("update user set email = ? where username = ?", newEmail, me.Email)
276 if err != nil {
277 // TODO better error not sql
278 errors = append(errors, err.Error())
279 } else {
280 log.Printf("Changed email for %s from %s to %s", authUser, me.Email, newEmail)
281 }
282 }
283 if newUsername != authUser {
284 // Rename User
285 err = renameUser(authUser, newUsername)
286 if err != nil {
287 errors = append(errors, err.Error())
288 } else {
289 session, _ := SessionStore.Get(r, "cookie-session")
290 session.Values["auth_user"] = newUsername
291 session.Save(r, w)
292 }
293 }
294 // reset auth
295 user = newGetAuthUser(r)
296 data.Errors = errors
297 data.AuthUser = user
298 data.Email = newEmail
299 _ = t.ExecuteTemplate(w, "me.html", data)
300 }
301}
302
303func archiveHandler(w http.ResponseWriter, r *http.Request) {
304 authUser := newGetAuthUser(r)
305 if !authUser.LoggedIn {
306 renderDefaultError(w, http.StatusForbidden)
307 return
308 }
309 if r.Method == "GET" {
310 userFolder := getUserDirectory(authUser.Username)
311 err := zipit(userFolder, w)
312 if err != nil {
313 panic(err)
314 }
315
316 }
317}
318func loginHandler(w http.ResponseWriter, r *http.Request) {
319 if r.Method == "GET" {
320 // show page
321 data := struct {
322 Error string
323 PageTitle string
324 }{"", "Login"}
325 err := t.ExecuteTemplate(w, "login.html", data)
326 if err != nil {
327 panic(err)
328 }
329 } else if r.Method == "POST" {
330 r.ParseForm()
331 name := r.Form.Get("username")
332 password := r.Form.Get("password")
333 row := DB.QueryRow("SELECT username, password_hash, active, admin FROM user where username = $1 OR email = $1", name)
334 var db_password []byte
335 var username string
336 var active bool
337 var isAdmin bool
338 _ = row.Scan(&username, &db_password, &active, &isAdmin)
339 if db_password != nil && !active {
340 data := struct {
341 Error string
342 PageTitle string
343 }{"Your account is not active yet. Pending admin approval", c.SiteTitle}
344 t.ExecuteTemplate(w, "login.html", data)
345 return
346 }
347 if bcrypt.CompareHashAndPassword(db_password, []byte(password)) == nil {
348 log.Println("logged in")
349 session, _ := SessionStore.Get(r, "cookie-session")
350 session.Values["auth_user"] = username
351 session.Values["admin"] = isAdmin
352 session.Save(r, w)
353 http.Redirect(w, r, "/my_site", http.StatusSeeOther)
354 } else {
355 data := struct {
356 Error string
357 PageTitle string
358 }{"Invalid login or password", c.SiteTitle}
359 err := t.ExecuteTemplate(w, "login.html", data)
360 if err != nil {
361 panic(err)
362 }
363 }
364 }
365}
366
367func logoutHandler(w http.ResponseWriter, r *http.Request) {
368 session, _ := SessionStore.Get(r, "cookie-session")
369 session.Options.MaxAge = -1
370 session.Save(r, w)
371 http.Redirect(w, r, "/", http.StatusSeeOther)
372}
373
374const ok = "-0123456789abcdefghijklmnopqrstuvwxyz"
375
376func isOkUsername(s string) error {
377 if len(s) < 1 {
378 return fmt.Errorf("Username is too short")
379 }
380 if len(s) > 32 {
381 return fmt.Errorf("Username is too long. 32 char max.")
382 }
383 for _, char := range s {
384 if !strings.Contains(ok, strings.ToLower(string(char))) {
385 return fmt.Errorf("Username contains invalid characters. Valid characters include lowercase letters, numbers, and hyphens.")
386 }
387 }
388 return nil
389}
390func registerHandler(w http.ResponseWriter, r *http.Request) {
391 if r.Method == "GET" {
392 data := struct {
393 Host string
394 Errors []string
395 PageTitle string
396 }{c.Host, nil, "Register"}
397 err := t.ExecuteTemplate(w, "register.html", data)
398 if err != nil {
399 panic(err)
400 }
401 } else if r.Method == "POST" {
402 r.ParseForm()
403 email := r.Form.Get("email")
404 password := r.Form.Get("password")
405 errors := []string{}
406 if r.Form.Get("password") != r.Form.Get("password2") {
407 errors = append(errors, "Passwords don't match")
408 }
409 if len(password) < 6 {
410 errors = append(errors, "Password is too short")
411 }
412 username := strings.ToLower(r.Form.Get("username"))
413 err := isOkUsername(username)
414 if err != nil {
415 errors = append(errors, err.Error())
416 }
417 hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), 8) // TODO handle error
418 reference := r.Form.Get("reference")
419 if len(errors) == 0 {
420 _, err = DB.Exec("insert into user (username, email, password_hash, reference) values ($1, $2, $3, $4)", username, email, string(hashedPassword), reference)
421 if err != nil {
422 errors = append(errors, "Username or email is already used")
423 }
424 }
425 if len(errors) > 0 {
426 data := struct {
427 Host string
428 Errors []string
429 PageTitle string
430 }{c.Host, errors, "Register"}
431 t.ExecuteTemplate(w, "register.html", data)
432 } else {
433 data := struct {
434 Host string
435 Message string
436 PageTitle string
437 }{c.Host, "Registration complete! The server admin will approve your request before you can log in.", "Registration Complete"}
438 t.ExecuteTemplate(w, "message.html", data)
439 }
440 }
441}
442
443func deleteFileHandler(w http.ResponseWriter, r *http.Request) {
444 user := newGetAuthUser(r)
445 if !user.LoggedIn {
446 renderDefaultError(w, http.StatusForbidden)
447 return
448 }
449 filePath := safeGetFilePath(user.Username, r.URL.Path[len("/delete/"):])
450 if r.Method == "POST" {
451 os.Remove(filePath) // TODO handle error
452 }
453 http.Redirect(w, r, "/my_site", http.StatusSeeOther)
454}
455
456func adminHandler(w http.ResponseWriter, r *http.Request) {
457 user := newGetAuthUser(r)
458 if !user.IsAdmin {
459 renderDefaultError(w, http.StatusForbidden)
460 return
461 }
462 allUsers, err := getUsers()
463 if err != nil {
464 log.Println(err)
465 renderDefaultError(w, http.StatusInternalServerError)
466 return
467 }
468 data := struct {
469 Users []User
470 LoggedIn bool
471 IsAdmin bool
472 PageTitle string
473 Host string
474 }{allUsers, true, true, "Admin", c.Host}
475 err = t.ExecuteTemplate(w, "admin.html", data)
476 if err != nil {
477 panic(err)
478 }
479}
480
481func getFavicon(user string) string {
482 faviconPath := path.Join(c.FilesDirectory, filepath.Clean(user), "favicon.txt")
483 content, err := ioutil.ReadFile(faviconPath)
484 if err != nil {
485 return ""
486 }
487 strcontent := []rune(string(content))
488 if len(strcontent) > 0 {
489 return string(strcontent[0])
490 }
491 return ""
492}
493
494// Server a user's file
495func userFile(w http.ResponseWriter, r *http.Request) {
496 userName := filepath.Clean(strings.Split(r.Host, ".")[0]) // Clean probably unnecessary
497 p := filepath.Clean(r.URL.Path)
498 var isDir bool
499 fileName := path.Join(c.FilesDirectory, userName, p)
500 stat, err := os.Stat(fileName)
501 if stat != nil {
502 isDir = stat.IsDir()
503 }
504 if p == "/" || isDir {
505 fileName = path.Join(fileName, "index.gmi")
506 }
507
508 if strings.HasPrefix(p, "/.hidden") {
509 renderDefaultError(w, http.StatusForbidden)
510 return
511 }
512 if r.URL.Path == "/style.css" {
513 http.ServeFile(w, r, path.Join(c.TemplatesDirectory, "static/style.css"))
514 return
515 }
516
517 _, err = os.Stat(fileName)
518 if os.IsNotExist(err) {
519 renderDefaultError(w, http.StatusNotFound)
520 return
521 }
522
523 // Dumb content negotiation
524 extension := path.Ext(fileName)
525 _, raw := r.URL.Query()["raw"]
526 acceptsGemini := strings.Contains(r.Header.Get("Accept"), "text/gemini")
527 if !raw && !acceptsGemini && (extension == ".gmi" || extension == ".gemini") {
528 file, _ := os.Open(fileName)
529 htmlString := textToHTML(gmi.ParseText(file))
530 favicon := getFavicon(userName)
531 data := struct {
532 SiteBody template.HTML
533 Favicon string
534 PageTitle string
535 }{template.HTML(htmlString), favicon, userName + p}
536 t.ExecuteTemplate(w, "user_page.html", data)
537 } else {
538 http.ServeFile(w, r, fileName)
539 }
540}
541
542func deleteAccountHandler(w http.ResponseWriter, r *http.Request) {
543 user := newGetAuthUser(r)
544 if r.Method == "POST" {
545 err := deleteUser(user.Username)
546 if err != nil {
547 log.Println(err)
548 renderDefaultError(w, http.StatusInternalServerError)
549 return
550 }
551 logoutHandler(w, r)
552 }
553}
554
555func resetPasswordHandler(w http.ResponseWriter, r *http.Request) {
556}
557
558func adminUserHandler(w http.ResponseWriter, r *http.Request) {
559 user := newGetAuthUser(r)
560 if r.Method == "POST" {
561 if !user.IsAdmin {
562 renderDefaultError(w, http.StatusForbidden)
563 return
564 }
565 components := strings.Split(r.URL.Path, "/")
566 if len(components) < 5 {
567 renderError(w, "Invalid action", http.StatusBadRequest)
568 return
569 }
570 userName := components[3]
571 action := components[4]
572 var err error
573 if action == "activate" {
574 err = activateUser(userName)
575 } else if action == "delete" {
576 err = deleteUser(userName)
577 }
578 if err != nil {
579 log.Println(err)
580 renderDefaultError(w, http.StatusInternalServerError)
581 return
582 }
583 http.Redirect(w, r, "/admin", http.StatusSeeOther)
584 }
585}
586
587func runHTTPServer() {
588 log.Printf("Running http server with hostname %s on port %d. TLS enabled: %t", c.Host, c.HttpPort, c.HttpsEnabled)
589 var err error
590 t, err = template.ParseGlob(path.Join(c.TemplatesDirectory, "*.html"))
591 if err != nil {
592 log.Fatal(err)
593 }
594 serveMux := http.NewServeMux()
595
596 s := strings.SplitN(c.Host, ":", 2)
597 hostname := s[0]
598 port := c.HttpPort
599
600 serveMux.HandleFunc(hostname+"/", rootHandler)
601 serveMux.HandleFunc(hostname+"/my_site", mySiteHandler)
602 serveMux.HandleFunc(hostname+"/me", myAccountHandler)
603 serveMux.HandleFunc(hostname+"/my_site/flounder-archive.zip", archiveHandler)
604 serveMux.HandleFunc(hostname+"/admin", adminHandler)
605 serveMux.HandleFunc(hostname+"/edit/", editFileHandler)
606 serveMux.HandleFunc(hostname+"/upload", uploadFilesHandler)
607 serveMux.Handle(hostname+"/login", limit(http.HandlerFunc(loginHandler)))
608 serveMux.Handle(hostname+"/register", limit(http.HandlerFunc(registerHandler)))
609 serveMux.HandleFunc(hostname+"/logout", logoutHandler)
610 serveMux.HandleFunc(hostname+"/delete/", deleteFileHandler)
611 serveMux.HandleFunc(hostname+"/delete-account", deleteAccountHandler)
612 serveMux.HandleFunc(hostname+"/reset-password", resetPasswordHandler)
613
614 // admin commands
615 serveMux.HandleFunc(hostname+"/admin/user/", adminUserHandler)
616
617 // TODO rate limit login https://github.com/ulule/limiter
618
619 wrapped := (handlers.LoggingHandler(log.Writer(), handlers.RecoveryHandler()(serveMux)))
620
621 // handle user files based on subdomain
622 serveMux.HandleFunc("/", userFile)
623 // login+register functions
624 srv := &http.Server{
625 ReadTimeout: 5 * time.Second,
626 WriteTimeout: 10 * time.Second,
627 IdleTimeout: 120 * time.Second,
628 Addr: fmt.Sprintf(":%d", port),
629 // TLSConfig: tlsConfig,
630 Handler: wrapped,
631 }
632 if c.HttpsEnabled {
633 log.Fatal(srv.ListenAndServeTLS(c.TLSCertFile, c.TLSKeyFile))
634 } else {
635 log.Fatal(srv.ListenAndServe())
636 }
637}