abyss/handlers.go

138 lines
3.5 KiB
Go
Raw Normal View History

2024-08-19 12:48:30 -03:00
package main
import (
"crypto/sha256"
"crypto/subtle"
2024-08-19 12:48:30 -03:00
"fmt"
"io"
"net/http"
"os"
2024-08-19 16:11:25 -03:00
"path/filepath"
2024-08-19 12:48:30 -03:00
"time"
)
2024-08-25 23:15:11 -03:00
type Application struct {
auth struct {
username string
password string
}
url string
key string
filesDir string
port string
lastUploadedFile string
}
2024-08-19 15:50:17 -03:00
2024-09-18 09:55:26 -03:00
func (app *Application) treeHandler(w http.ResponseWriter, r *http.Request) {
http.StripPrefix("/tree/", http.FileServer(http.Dir(app.filesDir))).ServeHTTP(w, r)
}
func (app *Application) indexHandler(w http.ResponseWriter, r *http.Request) {
2024-08-25 23:15:11 -03:00
if r.Method == http.MethodPost {
app.uploadHandler(w, r)
return
}
2024-08-19 20:00:07 -03:00
name := filepath.Clean(r.URL.Path)
path := filepath.Join(app.filesDir, name)
2024-08-19 20:00:07 -03:00
if !filepath.IsLocal(path) {
http.Error(w, "Wrong url", http.StatusBadRequest)
return
}
2024-08-19 16:11:25 -03:00
if fileInfo, err := os.Stat(path); err == nil && !fileInfo.IsDir() {
http.ServeFile(w, r, path)
} else {
2024-09-18 09:55:26 -03:00
http.StripPrefix("/", http.FileServer(http.Dir("static"))).ServeHTTP(w, r)
2024-08-19 16:11:25 -03:00
}
}
func (app *Application) lastHandler(w http.ResponseWriter, r *http.Request) {
if app.lastUploadedFile == "" {
http.Error(w, "No new files uploaded yet", http.StatusNotFound)
return
}
http.ServeFile(w, r, app.lastUploadedFile)
}
2024-08-25 23:15:11 -03:00
func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.Error(w, "Method not allowed", http.StatusUnauthorized)
2024-08-19 12:48:30 -03:00
return
}
if !app.checkAuth(r) {
2024-08-19 19:47:32 -03:00
http.Error(w, "You're not authorized.", http.StatusBadRequest)
return
}
2024-08-20 09:00:44 -03:00
file, handler, err := r.FormFile("file")
2024-08-19 12:48:30 -03:00
if err != nil {
http.Error(w, "Error retrieving the file", http.StatusBadRequest)
2024-08-19 12:48:30 -03:00
return
}
defer file.Close()
if _, err := os.Stat(app.filesDir); err != nil {
if err := os.Mkdir(app.filesDir, 0750); err != nil {
2024-08-19 13:54:33 -03:00
http.Error(w, "Error creating storage directory", http.StatusInternalServerError)
2024-08-19 12:48:30 -03:00
}
}
2024-08-19 13:54:33 -03:00
time := int64(float64(time.Now().Unix()) * 2.71828) // euler :)
2024-08-20 09:00:44 -03:00
filename := fmt.Sprintf("%d%s", time, filepath.Ext(handler.Filename))
2024-08-19 12:48:30 -03:00
filepath := fmt.Sprintf("%s/%s", app.filesDir, filename)
2024-08-20 09:00:44 -03:00
dst, err := os.Create(filepath)
2024-08-19 12:48:30 -03:00
if err != nil {
2024-08-19 13:54:33 -03:00
http.Error(w, "Error creating file\n", http.StatusInternalServerError)
2024-08-19 12:48:30 -03:00
}
defer dst.Close()
if _, err := io.Copy(dst, file); err != nil {
http.Error(w, "Error copying the file", http.StatusInternalServerError)
}
app.lastUploadedFile = filepath
if app.url == "" {
fmt.Fprintf(w, "http://localhost%s/%s\n", app.port, filename)
2024-08-19 15:50:17 -03:00
} else {
2024-08-20 09:00:44 -03:00
fmt.Fprintf(w, "http://%s/%s\n", app.url, filename)
2024-08-19 15:50:17 -03:00
}
2024-08-19 12:48:30 -03:00
}
2024-08-19 19:47:32 -03:00
func (app *Application) checkAuth(r *http.Request) bool {
return r.Header.Get("X-Auth") == string(app.key)
2024-08-19 19:47:32 -03:00
}
2024-08-25 23:15:11 -03:00
func (app *Application) basicAuth(next http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
username, password, ok := r.BasicAuth()
if ok {
// hash password received
usernameHash := sha256.Sum256([]byte(username))
passwordHash := sha256.Sum256([]byte(password))
// hash our password
expectedUsernameHash := sha256.Sum256([]byte(app.auth.username))
expectedPasswordHash := sha256.Sum256([]byte(app.auth.password))
// compare hashes
usernameMatch := (subtle.ConstantTimeCompare(usernameHash[:], expectedUsernameHash[:]) == 1)
passwordMatch := (subtle.ConstantTimeCompare(passwordHash[:], expectedPasswordHash[:]) == 1)
if usernameMatch && passwordMatch {
next.ServeHTTP(w, r)
return
}
}
w.Header().Set("WWW-Authenticate", `Basic realm="restricted", charset="UTF-8`)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
})
}