all repos — flounder @ f516ef62ea08f7a54e40aabba9931f65d87dde05

A small site builder for the Gemini protocol

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	impers, ok := session.Values["impersonating_user"].(string)
370	if ok {
371		session.Values["auth_user"] = impers
372		session.Values["impersonating_user"] = nil // TODO expire this automatically
373		// session.Values["admin"] = nil // TODO fix admin
374	} else {
375		session.Options.MaxAge = -1
376	}
377	session.Save(r, w)
378	http.Redirect(w, r, "/", http.StatusSeeOther)
379}
380
381const ok = "-0123456789abcdefghijklmnopqrstuvwxyz"
382
383func isOkUsername(s string) error {
384	if len(s) < 1 {
385		return fmt.Errorf("Username is too short")
386	}
387	if len(s) > 32 {
388		return fmt.Errorf("Username is too long. 32 char max.")
389	}
390	for _, char := range s {
391		if !strings.Contains(ok, strings.ToLower(string(char))) {
392			return fmt.Errorf("Username contains invalid characters. Valid characters include lowercase letters, numbers, and hyphens.")
393		}
394	}
395	return nil
396}
397func registerHandler(w http.ResponseWriter, r *http.Request) {
398	if r.Method == "GET" {
399		data := struct {
400			Host      string
401			Errors    []string
402			PageTitle string
403		}{c.Host, nil, "Register"}
404		err := t.ExecuteTemplate(w, "register.html", data)
405		if err != nil {
406			panic(err)
407		}
408	} else if r.Method == "POST" {
409		r.ParseForm()
410		email := r.Form.Get("email")
411		password := r.Form.Get("password")
412		errors := []string{}
413		if r.Form.Get("password") != r.Form.Get("password2") {
414			errors = append(errors, "Passwords don't match")
415		}
416		if len(password) < 6 {
417			errors = append(errors, "Password is too short")
418		}
419		username := strings.ToLower(r.Form.Get("username"))
420		err := isOkUsername(username)
421		if err != nil {
422			errors = append(errors, err.Error())
423		}
424		hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), 8) // TODO handle error
425		reference := r.Form.Get("reference")
426		if len(errors) == 0 {
427			_, err = DB.Exec("insert into user (username, email, password_hash, reference) values ($1, $2, $3, $4)", username, email, string(hashedPassword), reference)
428			if err != nil {
429				errors = append(errors, "Username or email is already used")
430			}
431		}
432		if len(errors) > 0 {
433			data := struct {
434				Host      string
435				Errors    []string
436				PageTitle string
437			}{c.Host, errors, "Register"}
438			t.ExecuteTemplate(w, "register.html", data)
439		} else {
440			data := struct {
441				Host      string
442				Message   string
443				PageTitle string
444			}{c.Host, "Registration complete! The server admin will approve your request before you can log in.", "Registration Complete"}
445			t.ExecuteTemplate(w, "message.html", data)
446		}
447	}
448}
449
450func deleteFileHandler(w http.ResponseWriter, r *http.Request) {
451	user := newGetAuthUser(r)
452	if !user.LoggedIn {
453		renderDefaultError(w, http.StatusForbidden)
454		return
455	}
456	filePath := safeGetFilePath(user.Username, r.URL.Path[len("/delete/"):])
457	if r.Method == "POST" {
458		os.Remove(filePath) // TODO handle error
459	}
460	http.Redirect(w, r, "/my_site", http.StatusSeeOther)
461}
462
463func adminHandler(w http.ResponseWriter, r *http.Request) {
464	user := newGetAuthUser(r)
465	if !user.IsAdmin {
466		renderDefaultError(w, http.StatusForbidden)
467		return
468	}
469	allUsers, err := getUsers()
470	if err != nil {
471		log.Println(err)
472		renderDefaultError(w, http.StatusInternalServerError)
473		return
474	}
475	data := struct {
476		Users     []User
477		AuthUser  AuthUser
478		PageTitle string
479		Host      string
480	}{allUsers, user, "Admin", c.Host}
481	err = t.ExecuteTemplate(w, "admin.html", data)
482	if err != nil {
483		panic(err)
484	}
485}
486
487func getFavicon(user string) string {
488	faviconPath := path.Join(c.FilesDirectory, filepath.Clean(user), "favicon.txt")
489	content, err := ioutil.ReadFile(faviconPath)
490	if err != nil {
491		return ""
492	}
493	strcontent := []rune(string(content))
494	if len(strcontent) > 0 {
495		return string(strcontent[0])
496	}
497	return ""
498}
499
500// Server a user's file
501func userFile(w http.ResponseWriter, r *http.Request) {
502	userName := filepath.Clean(strings.Split(r.Host, ".")[0]) // Clean probably unnecessary
503	p := filepath.Clean(r.URL.Path)
504	var isDir bool
505	fileName := path.Join(c.FilesDirectory, userName, p)
506	stat, err := os.Stat(fileName)
507	if stat != nil {
508		isDir = stat.IsDir()
509	}
510	if p == "/" || isDir {
511		fileName = path.Join(fileName, "index.gmi")
512	}
513
514	if strings.HasPrefix(p, "/.hidden") {
515		renderDefaultError(w, http.StatusForbidden)
516		return
517	}
518	if r.URL.Path == "/style.css" {
519		http.ServeFile(w, r, path.Join(c.TemplatesDirectory, "static/style.css"))
520		return
521	}
522
523	_, err = os.Stat(fileName)
524	if os.IsNotExist(err) {
525		renderDefaultError(w, http.StatusNotFound)
526		return
527	}
528
529	// Dumb content negotiation
530	extension := path.Ext(fileName)
531	_, raw := r.URL.Query()["raw"]
532	acceptsGemini := strings.Contains(r.Header.Get("Accept"), "text/gemini")
533	if !raw && !acceptsGemini && (extension == ".gmi" || extension == ".gemini") {
534		file, _ := os.Open(fileName)
535		htmlString := textToHTML(gmi.ParseText(file))
536		favicon := getFavicon(userName)
537		data := struct {
538			SiteBody  template.HTML
539			Favicon   string
540			PageTitle string
541		}{template.HTML(htmlString), favicon, userName + p}
542		t.ExecuteTemplate(w, "user_page.html", data)
543	} else {
544		http.ServeFile(w, r, fileName)
545	}
546}
547
548func deleteAccountHandler(w http.ResponseWriter, r *http.Request) {
549	user := newGetAuthUser(r)
550	if r.Method == "POST" {
551		err := deleteUser(user.Username)
552		if err != nil {
553			log.Println(err)
554			renderDefaultError(w, http.StatusInternalServerError)
555			return
556		}
557		logoutHandler(w, r)
558	}
559}
560
561func resetPasswordHandler(w http.ResponseWriter, r *http.Request) {
562}
563
564func adminUserHandler(w http.ResponseWriter, r *http.Request) {
565	user := newGetAuthUser(r)
566	if r.Method == "POST" {
567		if !user.IsAdmin {
568			renderDefaultError(w, http.StatusForbidden)
569			return
570		}
571		components := strings.Split(r.URL.Path, "/")
572		if len(components) < 5 {
573			renderError(w, "Invalid action", http.StatusBadRequest)
574			return
575		}
576		userName := components[3]
577		action := components[4]
578		var err error
579		if action == "activate" {
580			err = activateUser(userName)
581		} else if action == "impersonate" {
582			session, _ := SessionStore.Get(r, "cookie-session")
583			session.Values["auth_user"] = userName
584			session.Values["impersonating_user"] = user.Username
585			session.Save(r, w)
586			http.Redirect(w, r, "/", http.StatusSeeOther)
587			return
588		}
589		if err != nil {
590			log.Println(err)
591			renderDefaultError(w, http.StatusInternalServerError)
592			return
593		}
594		http.Redirect(w, r, "/admin", http.StatusSeeOther)
595	}
596}
597
598func runHTTPServer() {
599	log.Printf("Running http server with hostname %s on port %d. TLS enabled: %t", c.Host, c.HttpPort, c.HttpsEnabled)
600	var err error
601	t, err = template.ParseGlob(path.Join(c.TemplatesDirectory, "*.html"))
602	if err != nil {
603		log.Fatal(err)
604	}
605	serveMux := http.NewServeMux()
606
607	s := strings.SplitN(c.Host, ":", 2)
608	hostname := s[0]
609	port := c.HttpPort
610
611	serveMux.HandleFunc(hostname+"/", rootHandler)
612	serveMux.HandleFunc(hostname+"/my_site", mySiteHandler)
613	serveMux.HandleFunc(hostname+"/me", myAccountHandler)
614	serveMux.HandleFunc(hostname+"/my_site/flounder-archive.zip", archiveHandler)
615	serveMux.HandleFunc(hostname+"/admin", adminHandler)
616	serveMux.HandleFunc(hostname+"/edit/", editFileHandler)
617	serveMux.HandleFunc(hostname+"/upload", uploadFilesHandler)
618	serveMux.Handle(hostname+"/login", limit(http.HandlerFunc(loginHandler)))
619	serveMux.Handle(hostname+"/register", limit(http.HandlerFunc(registerHandler)))
620	serveMux.HandleFunc(hostname+"/logout", logoutHandler)
621	serveMux.HandleFunc(hostname+"/delete/", deleteFileHandler)
622	serveMux.HandleFunc(hostname+"/delete-account", deleteAccountHandler)
623	serveMux.HandleFunc(hostname+"/reset-password", resetPasswordHandler)
624
625	// admin commands
626	serveMux.HandleFunc(hostname+"/admin/user/", adminUserHandler)
627
628	// TODO rate limit login https://github.com/ulule/limiter
629
630	wrapped := (handlers.LoggingHandler(log.Writer(), handlers.RecoveryHandler()(serveMux)))
631
632	// handle user files based on subdomain
633	serveMux.HandleFunc("/", userFile)
634	// login+register functions
635	srv := &http.Server{
636		ReadTimeout:  5 * time.Second,
637		WriteTimeout: 10 * time.Second,
638		IdleTimeout:  120 * time.Second,
639		Addr:         fmt.Sprintf(":%d", port),
640		// TLSConfig:    tlsConfig,
641		Handler: wrapped,
642	}
643	if c.HttpsEnabled {
644		log.Fatal(srv.ListenAndServeTLS(c.TLSCertFile, c.TLSKeyFile))
645	} else {
646		log.Fatal(srv.ListenAndServe())
647	}
648}