2024-08-19 12:48:30 -03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-09-18 13:48:04 -03:00
|
|
|
"crypto/md5"
|
2024-08-20 08:38:56 -03:00
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/subtle"
|
2024-09-18 13:48:04 -03:00
|
|
|
"encoding/hex"
|
2024-08-19 12:48:30 -03:00
|
|
|
"fmt"
|
2024-09-19 15:38:43 -03:00
|
|
|
"html/template"
|
2024-08-19 12:48:30 -03:00
|
|
|
"io"
|
2024-09-19 10:28:23 -03:00
|
|
|
"log/slog"
|
2024-08-19 12:48:30 -03:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2024-08-19 16:11:25 -03:00
|
|
|
"path/filepath"
|
2024-08-19 12:48:30 -03:00
|
|
|
)
|
|
|
|
|
2024-08-25 23:15:11 -03:00
|
|
|
type Application struct {
|
2024-08-20 08:38:56 -03:00
|
|
|
auth struct {
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
}
|
2024-09-18 10:03:53 -03:00
|
|
|
url string
|
|
|
|
key string
|
|
|
|
filesDir string
|
|
|
|
port string
|
|
|
|
lastUploadedFile string
|
2024-08-20 08:38:56 -03:00
|
|
|
}
|
2024-08-19 15:50:17 -03:00
|
|
|
|
2024-09-19 10:28:23 -03:00
|
|
|
type FileInfo struct {
|
|
|
|
Name string
|
|
|
|
Path string
|
|
|
|
Size int64
|
|
|
|
FormattedSize string
|
|
|
|
}
|
|
|
|
|
|
|
|
type TemplateData struct {
|
|
|
|
Files []FileInfo
|
|
|
|
URL string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *Application) fileListingHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
dir := app.filesDir + r.URL.Path
|
|
|
|
|
|
|
|
files, err := os.ReadDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var fileInfos []FileInfo
|
|
|
|
for _, file := range files {
|
|
|
|
filePath := filepath.Join(dir, file.Name())
|
|
|
|
info, err := os.Stat(filePath)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fileInfos = append(fileInfos, FileInfo{
|
|
|
|
Name: file.Name(),
|
|
|
|
Path: filepath.Join(r.URL.Path, file.Name()),
|
|
|
|
Size: info.Size(),
|
2024-10-03 21:19:14 -03:00
|
|
|
FormattedSize: FormatFileSize(info.Size()),
|
2024-09-19 10:28:23 -03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl := template.Must(template.ParseFiles("templates/dirlist.html"))
|
|
|
|
templateData := TemplateData{
|
|
|
|
Files: fileInfos,
|
|
|
|
URL: app.url,
|
|
|
|
}
|
|
|
|
if err := tmpl.Execute(w, templateData); err != nil {
|
|
|
|
slog.Warn(error.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-18 09:55:26 -03:00
|
|
|
func (app *Application) indexHandler(w http.ResponseWriter, r *http.Request) {
|
2024-08-25 23:15:11 -03:00
|
|
|
if r.Method == http.MethodPost {
|
2024-10-03 21:19:14 -03:00
|
|
|
app.uploadCurlHandler(w, r)
|
2024-08-25 23:15:11 -03:00
|
|
|
return
|
2024-08-20 08:38:56 -03:00
|
|
|
}
|
|
|
|
|
2024-08-19 20:00:07 -03:00
|
|
|
name := filepath.Clean(r.URL.Path)
|
2024-09-06 16:34:20 -03:00
|
|
|
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() {
|
2024-09-18 13:58:05 -03:00
|
|
|
ext := filepath.Ext(path)
|
|
|
|
|
|
|
|
textExtensions := map[string]bool{
|
|
|
|
".sh": true, ".bash": true, ".zsh": true,
|
|
|
|
".bat": true, ".cmd": true, ".ps1": true,
|
|
|
|
".ini": true, ".cfg": true, ".conf": true,
|
|
|
|
".toml": true, ".yml": true, ".yaml": true,
|
|
|
|
".c": true, ".cpp": true, ".h": true,
|
|
|
|
".go": true, ".py": true, ".js": true,
|
|
|
|
".ts": true, ".html": true, ".htm": true,
|
|
|
|
".xml": true, ".css": true, ".java": true,
|
|
|
|
".rs": true, ".rb": true, ".php": true,
|
|
|
|
".pl": true, ".sql": true, ".md": true,
|
|
|
|
".log": true, ".txt": true, ".csv": true,
|
|
|
|
".json": true, ".env": true, ".sum": true,
|
|
|
|
".gitignore": true, ".dockerfile": true, ".Makefile": true,
|
|
|
|
".rst": true,
|
|
|
|
}
|
|
|
|
|
2024-09-19 15:47:24 -03:00
|
|
|
videoExtensions := map[string]bool{
|
|
|
|
".mkv": true,
|
|
|
|
}
|
|
|
|
|
2024-09-18 13:58:05 -03:00
|
|
|
if textExtensions[ext] {
|
|
|
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
}
|
|
|
|
|
2024-09-19 15:47:24 -03:00
|
|
|
if videoExtensions[ext] {
|
|
|
|
w.Header().Set("Content-Type", "video/mp4")
|
|
|
|
}
|
|
|
|
|
2024-08-19 16:11:25 -03:00
|
|
|
http.ServeFile(w, r, path)
|
2024-09-18 13:58:05 -03:00
|
|
|
return
|
2024-08-19 16:11:25 -03:00
|
|
|
}
|
2024-09-18 13:58:05 -03:00
|
|
|
|
|
|
|
http.StripPrefix("/", http.FileServer(http.Dir("./static"))).ServeHTTP(w, r)
|
2024-08-19 16:11:25 -03:00
|
|
|
}
|
|
|
|
|
2024-09-18 10:03:53 -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-10-03 21:19:14 -03:00
|
|
|
func (app *Application) uploadCurlHandler(w http.ResponseWriter, r *http.Request) {
|
2024-08-25 23:15:11 -03:00
|
|
|
if r.URL.Path != "/" {
|
|
|
|
http.Error(w, "Method not allowed", http.StatusUnauthorized)
|
2024-08-19 12:48:30 -03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-03 21:19:14 -03:00
|
|
|
if !CheckAuth(r, app.key) {
|
2024-08-19 19:47:32 -03:00
|
|
|
http.Error(w, "You're not authorized.", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-03 21:19:14 -03:00
|
|
|
app.uploadHandler(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) {
|
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 {
|
2024-08-20 08:38:56 -03:00
|
|
|
http.Error(w, "Error retrieving the file", http.StatusBadRequest)
|
2024-08-19 12:48:30 -03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2024-09-06 16:34:20 -03:00
|
|
|
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-09-18 13:48:04 -03:00
|
|
|
hasher := md5.New()
|
|
|
|
if _, err := io.Copy(hasher, file); err != nil {
|
|
|
|
http.Error(w, "Error hashing file content", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sha1Hash := hex.EncodeToString(hasher.Sum(nil))[:8]
|
2024-08-19 13:54:33 -03:00
|
|
|
|
2024-09-18 13:48:04 -03:00
|
|
|
filename := fmt.Sprintf("%s%s", sha1Hash, filepath.Ext(handler.Filename))
|
2024-08-19 12:48:30 -03:00
|
|
|
|
2024-09-18 13:48:04 -03:00
|
|
|
filepath := filepath.Join(app.filesDir, filename)
|
|
|
|
|
|
|
|
// reopen the file for copying, as the hash process consumed the file reader
|
|
|
|
file, _, err = r.FormFile("file")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Error retrieving the file", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
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)
|
|
|
|
}
|
|
|
|
|
2024-09-18 10:03:53 -03:00
|
|
|
app.lastUploadedFile = filepath
|
|
|
|
|
2024-09-19 10:28:23 -03:00
|
|
|
fmt.Fprintf(w, "http://%s/%s\n", app.url, filename)
|
2024-08-19 12:48:30 -03:00
|
|
|
}
|
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 {
|
2024-08-20 08:38:56 -03:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|