all repos — flounder @ ea2c1241eaa89aa42c07a47059955eed7a9bcbbe

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