mirror of
https://github.com/skidoodle/safebin.git
synced 2026-04-28 19:27:41 +02:00
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
FROM --platform=$BUILDPLATFORM golang:1.25.5 AS builder
|
FROM --platform=$BUILDPLATFORM golang:1.25.6 AS builder
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
module github.com/skidoodle/safebin
|
module github.com/skidoodle/safebin
|
||||||
|
|
||||||
go 1.25.5
|
go 1.25.6
|
||||||
|
|||||||
+39
-21
@@ -21,36 +21,54 @@ type App struct {
|
|||||||
Logger *slog.Logger
|
Logger *slog.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadConfig() Config {
|
const (
|
||||||
h := getEnv("SAFEBIN_HOST", "0.0.0.0")
|
defaultHost = "0.0.0.0"
|
||||||
p := getEnvInt("SAFEBIN_PORT", 8080)
|
defaultPort = 8080
|
||||||
s := getEnv("SAFEBIN_STORAGE", "./storage")
|
defaultStorage = "./storage"
|
||||||
mDefault := int64(getEnvInt("SAFEBIN_MAX_MB", 512))
|
defaultMaxMB = 512
|
||||||
|
)
|
||||||
|
|
||||||
var m int64
|
func LoadConfig() Config {
|
||||||
flag.StringVar(&h, "h", h, "Bind address")
|
hostEnv := getEnv("SAFEBIN_HOST", defaultHost)
|
||||||
flag.IntVar(&p, "p", p, "Port")
|
portEnv := getEnvInt("SAFEBIN_PORT", defaultPort)
|
||||||
flag.StringVar(&s, "s", s, "Storage directory")
|
storageEnv := getEnv("SAFEBIN_STORAGE", defaultStorage)
|
||||||
flag.Int64Var(&m, "m", mDefault, "Max file size in MB")
|
maxMBEnv := int64(getEnvInt("SAFEBIN_MAX_MB", defaultMaxMB))
|
||||||
|
|
||||||
|
var host string
|
||||||
|
var port int
|
||||||
|
var storage string
|
||||||
|
var maxMB int64
|
||||||
|
|
||||||
|
flag.StringVar(&host, "h", hostEnv, "Bind address")
|
||||||
|
flag.IntVar(&port, "p", portEnv, "Port")
|
||||||
|
flag.StringVar(&storage, "s", storageEnv, "Storage directory")
|
||||||
|
flag.Int64Var(&maxMB, "m", maxMBEnv, "Max file size in MB")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
return Config{Addr: fmt.Sprintf("%s:%d", h, p), StorageDir: s, MaxMB: m}
|
return Config{
|
||||||
}
|
Addr: fmt.Sprintf("%s:%d", host, port),
|
||||||
|
StorageDir: storage,
|
||||||
func getEnv(k, f string) string {
|
MaxMB: maxMB,
|
||||||
if v, ok := os.LookupEnv(k); ok {
|
|
||||||
return v
|
|
||||||
}
|
}
|
||||||
return f
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getEnvInt(k string, f int) int {
|
func getEnv(key, fallback string) string {
|
||||||
if v, ok := os.LookupEnv(k); ok {
|
if value, ok := os.LookupEnv(key); ok {
|
||||||
if i, err := strconv.Atoi(v); err == nil {
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
return fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
func getEnvInt(key string, fallback int) int {
|
||||||
|
if value, ok := os.LookupEnv(key); ok {
|
||||||
|
i, err := strconv.Atoi(value)
|
||||||
|
if err == nil {
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return f
|
|
||||||
|
return fallback
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseTemplates() *template.Template {
|
func ParseTemplates() *template.Template {
|
||||||
|
|||||||
+249
-139
@@ -2,6 +2,7 @@ package app
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"mime"
|
"mime"
|
||||||
@@ -14,187 +15,268 @@ import (
|
|||||||
"github.com/skidoodle/safebin/internal/crypto"
|
"github.com/skidoodle/safebin/internal/crypto"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
uploadChunkSize = 8 << 20
|
||||||
|
maxRequestOverhead = 10 << 20
|
||||||
|
permUserRWX = 0o700
|
||||||
|
slugLength = 22
|
||||||
|
keyLength = 16
|
||||||
|
megaByte = 1 << 20
|
||||||
|
chunkSafetyMargin = 2
|
||||||
|
)
|
||||||
|
|
||||||
var reUploadID = regexp.MustCompile(`^[a-zA-Z0-9]{10,50}$`)
|
var reUploadID = regexp.MustCompile(`^[a-zA-Z0-9]{10,50}$`)
|
||||||
|
|
||||||
func (app *App) HandleHome(w http.ResponseWriter, r *http.Request) {
|
func (app *App) HandleHome(writer http.ResponseWriter, request *http.Request) {
|
||||||
err := app.Tmpl.ExecuteTemplate(w, "base", map[string]any{
|
err := app.Tmpl.ExecuteTemplate(writer, "base", map[string]any{
|
||||||
"MaxMB": app.Conf.MaxMB,
|
"MaxMB": app.Conf.MaxMB,
|
||||||
"Host": r.Host,
|
"Host": request.Host,
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.Logger.Error("Template error", "err", err)
|
app.Logger.Error("Template error", "err", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) HandleUpload(w http.ResponseWriter, r *http.Request) {
|
func (app *App) HandleUpload(writer http.ResponseWriter, request *http.Request) {
|
||||||
limit := (app.Conf.MaxMB << 20) + (1 << 20)
|
limit := (app.Conf.MaxMB * megaByte) + megaByte
|
||||||
r.Body = http.MaxBytesReader(w, r.Body, limit)
|
request.Body = http.MaxBytesReader(writer, request.Body, limit)
|
||||||
|
|
||||||
|
file, header, err := request.FormFile("file")
|
||||||
|
|
||||||
file, header, err := r.FormFile("file")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err.Error() == "http: request body too large" {
|
if err.Error() == "http: request body too large" {
|
||||||
app.SendError(w, r, http.StatusRequestEntityTooLarge)
|
app.SendError(writer, request, http.StatusRequestEntityTooLarge)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
app.SendError(w, r, http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
tmpPath := filepath.Join(app.Conf.StorageDir, "tmp", fmt.Sprintf("up_%d", os.Getpid()))
|
app.SendError(writer, request, http.StatusBadRequest)
|
||||||
tmp, err := os.Create(tmpPath)
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if closeErr := file.Close(); closeErr != nil {
|
||||||
|
app.Logger.Error("Failed to close upload file", "err", closeErr)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
tmp, err := os.CreateTemp(filepath.Join(app.Conf.StorageDir, "tmp"), "up_*")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.Logger.Error("Failed to create temp file", "err", err)
|
app.Logger.Error("Failed to create temp file", "err", err)
|
||||||
app.SendError(w, r, http.StatusInternalServerError)
|
app.SendError(writer, request, http.StatusInternalServerError)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer os.Remove(tmpPath)
|
|
||||||
defer tmp.Close()
|
tmpPath := tmp.Name()
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if removeErr := os.Remove(tmpPath); removeErr != nil && !os.IsNotExist(removeErr) {
|
||||||
|
app.Logger.Error("Failed to remove temp file", "err", removeErr)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if closeErr := tmp.Close(); closeErr != nil {
|
||||||
|
app.Logger.Error("Failed to close temp file", "err", closeErr)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
if _, err := io.Copy(tmp, file); err != nil {
|
if _, err := io.Copy(tmp, file); err != nil {
|
||||||
app.Logger.Error("Failed to write temp file", "err", err)
|
app.Logger.Error("Failed to write temp file", "err", err)
|
||||||
app.SendError(w, r, http.StatusRequestEntityTooLarge)
|
app.SendError(writer, request, http.StatusRequestEntityTooLarge)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
app.FinalizeFile(w, r, tmp, header.Filename)
|
app.FinalizeFile(writer, request, tmp, header.Filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) HandleChunk(w http.ResponseWriter, r *http.Request) {
|
func (app *App) HandleChunk(writer http.ResponseWriter, request *http.Request) {
|
||||||
r.Body = http.MaxBytesReader(w, r.Body, 10<<20)
|
request.Body = http.MaxBytesReader(writer, request.Body, maxRequestOverhead)
|
||||||
|
|
||||||
uid := r.FormValue("upload_id")
|
uid := request.FormValue("upload_id")
|
||||||
idx, err := strconv.Atoi(r.FormValue("index"))
|
|
||||||
|
idx, err := strconv.Atoi(request.FormValue("index"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.SendError(w, r, http.StatusBadRequest)
|
app.SendError(writer, request, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const chunkSize = 8 << 20
|
maxChunks := int((app.Conf.MaxMB*megaByte)/uploadChunkSize) + chunkSafetyMargin
|
||||||
maxChunks := int((app.Conf.MaxMB<<20)/chunkSize) + 2
|
|
||||||
|
|
||||||
if !reUploadID.MatchString(uid) || idx > maxChunks || idx < 0 {
|
if !reUploadID.MatchString(uid) || idx > maxChunks || idx < 0 {
|
||||||
app.SendError(w, r, http.StatusBadRequest)
|
app.SendError(writer, request, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
file, _, err := r.FormFile("chunk")
|
file, _, err := request.FormFile("chunk")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err.Error() == "http: request body too large" {
|
if err.Error() == "http: request body too large" {
|
||||||
app.SendError(w, r, http.StatusRequestEntityTooLarge)
|
app.SendError(writer, request, http.StatusRequestEntityTooLarge)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
app.SendError(w, r, http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
dir := filepath.Join(app.Conf.StorageDir, "tmp", uid)
|
app.SendError(writer, request, http.StatusBadRequest)
|
||||||
if err := os.MkdirAll(dir, 0700); err != nil {
|
|
||||||
app.Logger.Error("Failed to create chunk dir", "err", err)
|
|
||||||
app.SendError(w, r, http.StatusInternalServerError)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if closeErr := file.Close(); closeErr != nil {
|
||||||
|
app.Logger.Error("Failed to close chunk file", "err", closeErr)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
if err := app.saveChunk(uid, idx, file); err != nil {
|
||||||
|
app.Logger.Error("Failed to save chunk", "err", err)
|
||||||
|
app.SendError(writer, request, http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *App) saveChunk(uid string, idx int, src io.Reader) error {
|
||||||
|
dir := filepath.Join(app.Conf.StorageDir, "tmp", uid)
|
||||||
|
|
||||||
|
if err := os.MkdirAll(dir, permUserRWX); err != nil {
|
||||||
|
return fmt.Errorf("create chunk dir: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
dest, err := os.Create(filepath.Join(dir, strconv.Itoa(idx)))
|
dest, err := os.Create(filepath.Join(dir, strconv.Itoa(idx)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.Logger.Error("Failed to create chunk file", "err", err)
|
return fmt.Errorf("create chunk file: %w", err)
|
||||||
app.SendError(w, r, http.StatusInternalServerError)
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if closeErr := dest.Close(); closeErr != nil {
|
||||||
|
app.Logger.Error("Failed to close chunk dest", "err", closeErr)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
if _, err := io.Copy(dest, src); err != nil {
|
||||||
|
return fmt.Errorf("copy chunk: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *App) HandleFinish(writer http.ResponseWriter, request *http.Request) {
|
||||||
|
uid := request.FormValue("upload_id")
|
||||||
|
|
||||||
|
total, err := strconv.Atoi(request.FormValue("total"))
|
||||||
|
if err != nil {
|
||||||
|
app.SendError(writer, request, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer dest.Close()
|
|
||||||
|
|
||||||
if _, err := io.Copy(dest, file); err != nil {
|
maxChunks := int((app.Conf.MaxMB*megaByte)/uploadChunkSize) + chunkSafetyMargin
|
||||||
app.Logger.Error("Failed to save chunk", "err", err)
|
|
||||||
app.SendError(w, r, http.StatusInternalServerError)
|
if !reUploadID.MatchString(uid) || total > maxChunks || total <= 0 {
|
||||||
|
app.SendError(writer, request, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mergedPath, err := app.mergeChunks(uid, total)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
app.Logger.Error("Merge failed", "err", err)
|
||||||
|
|
||||||
|
if errors.Is(err, io.ErrShortWrite) {
|
||||||
|
app.SendError(writer, request, http.StatusRequestEntityTooLarge)
|
||||||
|
} else {
|
||||||
|
app.SendError(writer, request, http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if removeErr := os.Remove(mergedPath); removeErr != nil && !os.IsNotExist(removeErr) {
|
||||||
|
app.Logger.Error("Failed to remove merged file", "err", removeErr)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
mergedRead, err := os.Open(mergedPath)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
app.Logger.Error("Failed to open merged file", "err", err)
|
||||||
|
app.SendError(writer, request, http.StatusInternalServerError)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if closeErr := mergedRead.Close(); closeErr != nil {
|
||||||
|
app.Logger.Error("Failed to close merged reader", "err", closeErr)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
app.FinalizeFile(writer, request, mergedRead, request.FormValue("filename"))
|
||||||
|
|
||||||
|
if err := os.RemoveAll(filepath.Join(app.Conf.StorageDir, "tmp", uid)); err != nil {
|
||||||
|
app.Logger.Error("Failed to remove chunk dir", "err", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) HandleFinish(w http.ResponseWriter, r *http.Request) {
|
func (app *App) mergeChunks(uid string, total int) (string, error) {
|
||||||
uid := r.FormValue("upload_id")
|
|
||||||
total, err := strconv.Atoi(r.FormValue("total"))
|
|
||||||
if err != nil {
|
|
||||||
app.SendError(w, r, http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const chunkSize = 8 << 20
|
|
||||||
maxChunks := int((app.Conf.MaxMB<<20)/chunkSize) + 2
|
|
||||||
|
|
||||||
if !reUploadID.MatchString(uid) || total > maxChunks || total <= 0 {
|
|
||||||
app.SendError(w, r, http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
tmpPath := filepath.Join(app.Conf.StorageDir, "tmp", "m_"+uid)
|
tmpPath := filepath.Join(app.Conf.StorageDir, "tmp", "m_"+uid)
|
||||||
|
|
||||||
merged, err := os.Create(tmpPath)
|
merged, err := os.Create(tmpPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.Logger.Error("Failed to create merge file", "err", err)
|
return "", fmt.Errorf("create merge file: %w", err)
|
||||||
app.SendError(w, r, http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
defer os.Remove(tmpPath)
|
|
||||||
defer merged.Close()
|
|
||||||
|
|
||||||
limit := app.Conf.MaxMB << 20
|
defer func() {
|
||||||
|
if closeErr := merged.Close(); closeErr != nil {
|
||||||
|
app.Logger.Error("Failed to close merged file", "err", closeErr)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
limit := app.Conf.MaxMB * megaByte
|
||||||
var written int64
|
var written int64
|
||||||
|
|
||||||
for i := range total {
|
for i := range total {
|
||||||
partPath := filepath.Join(app.Conf.StorageDir, "tmp", uid, strconv.Itoa(i))
|
partPath := filepath.Join(app.Conf.StorageDir, "tmp", uid, strconv.Itoa(i))
|
||||||
|
|
||||||
part, err := os.Open(partPath)
|
part, err := os.Open(partPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.Logger.Error("Missing chunk during merge", "uid", uid, "index", i, "err", err)
|
return "", fmt.Errorf("open chunk %d: %w", i, err)
|
||||||
app.SendError(w, r, http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
n, err := io.Copy(merged, part)
|
n, err := io.Copy(merged, part)
|
||||||
part.Close()
|
|
||||||
|
if closeErr := part.Close(); closeErr != nil {
|
||||||
|
app.Logger.Error("Failed to close chunk part", "err", closeErr)
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.Logger.Error("Failed to append chunk", "err", err)
|
return "", fmt.Errorf("append chunk %d: %w", i, err)
|
||||||
app.SendError(w, r, http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
written += n
|
written += n
|
||||||
if written > limit {
|
if written > limit {
|
||||||
app.SendError(w, r, http.StatusRequestEntityTooLarge)
|
return "", io.ErrShortWrite
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := merged.Close(); err != nil {
|
return tmpPath, nil
|
||||||
app.Logger.Error("Failed to close merged file", "err", err)
|
|
||||||
app.SendError(w, r, http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
mergedRead, err := os.Open(tmpPath)
|
|
||||||
if err != nil {
|
|
||||||
app.Logger.Error("Failed to open merged file", "err", err)
|
|
||||||
app.SendError(w, r, http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer mergedRead.Close()
|
|
||||||
|
|
||||||
app.FinalizeFile(w, r, mergedRead, r.FormValue("filename"))
|
|
||||||
os.RemoveAll(filepath.Join(app.Conf.StorageDir, "tmp", uid))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) HandleGetFile(w http.ResponseWriter, r *http.Request) {
|
func (app *App) HandleGetFile(writer http.ResponseWriter, request *http.Request) {
|
||||||
slug := r.PathValue("slug")
|
slug := request.PathValue("slug")
|
||||||
if len(slug) < 22 {
|
if len(slug) < slugLength {
|
||||||
app.SendError(w, r, http.StatusBadRequest)
|
app.SendError(writer, request, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
keyBase64 := slug[:22]
|
keyBase64 := slug[:slugLength]
|
||||||
ext := slug[22:]
|
ext := slug[slugLength:]
|
||||||
|
|
||||||
key, err := base64.RawURLEncoding.DecodeString(keyBase64)
|
key, err := base64.RawURLEncoding.DecodeString(keyBase64)
|
||||||
if err != nil || len(key) != 16 {
|
if err != nil || len(key) != keyLength {
|
||||||
app.SendError(w, r, http.StatusUnauthorized)
|
app.SendError(writer, request, http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,105 +285,133 @@ func (app *App) HandleGetFile(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
info, err := os.Stat(path)
|
info, err := os.Stat(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.SendError(w, r, http.StatusNotFound)
|
app.SendError(writer, request, http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
f, err := os.Open(path)
|
file, err := os.Open(path)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.Logger.Error("Failed to open file", "path", path, "err", err)
|
app.Logger.Error("Failed to open file", "path", path, "err", err)
|
||||||
app.SendError(w, r, http.StatusInternalServerError)
|
app.SendError(writer, request, http.StatusInternalServerError)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer f.Close()
|
|
||||||
|
defer func() {
|
||||||
|
if closeErr := file.Close(); closeErr != nil {
|
||||||
|
app.Logger.Error("Failed to close file", "err", closeErr)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
streamer, err := crypto.NewGCMStreamer(key)
|
streamer, err := crypto.NewGCMStreamer(key)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.Logger.Error("Failed to create crypto streamer", "err", err)
|
app.Logger.Error("Failed to create crypto streamer", "err", err)
|
||||||
app.SendError(w, r, http.StatusInternalServerError)
|
app.SendError(writer, request, http.StatusInternalServerError)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
decryptor := crypto.NewDecryptor(f, streamer.AEAD, info.Size())
|
decryptor := crypto.NewDecryptor(file, streamer.AEAD, info.Size())
|
||||||
|
|
||||||
contentType := mime.TypeByExtension(ext)
|
contentType := mime.TypeByExtension(ext)
|
||||||
if contentType == "" {
|
if contentType == "" {
|
||||||
contentType = "application/octet-stream"
|
contentType = "application/octet-stream"
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", contentType)
|
csp := "default-src 'none'; img-src 'self' data:; media-src 'self' data:; " +
|
||||||
w.Header().Set("Content-Security-Policy", "default-src 'none'; img-src 'self' data:; media-src 'self' data:; style-src 'unsafe-inline'; sandbox allow-forms allow-scripts allow-downloads allow-same-origin")
|
"style-src 'unsafe-inline'; sandbox allow-forms allow-scripts allow-downloads allow-same-origin"
|
||||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
||||||
w.Header().Set("Content-Disposition", fmt.Sprintf("inline; filename=%q", slug))
|
|
||||||
|
|
||||||
http.ServeContent(w, r, slug, info.ModTime(), decryptor)
|
writer.Header().Set("Content-Type", contentType)
|
||||||
|
writer.Header().Set("Content-Security-Policy", csp)
|
||||||
|
writer.Header().Set("X-Content-Type-Options", "nosniff")
|
||||||
|
writer.Header().Set("Content-Disposition", fmt.Sprintf("inline; filename=%q", slug))
|
||||||
|
|
||||||
|
http.ServeContent(writer, request, slug, info.ModTime(), decryptor)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) FinalizeFile(w http.ResponseWriter, r *http.Request, src *os.File, filename string) {
|
func (app *App) FinalizeFile(writer http.ResponseWriter, request *http.Request, src *os.File, filename string) {
|
||||||
if _, err := src.Seek(0, 0); err != nil {
|
if _, err := src.Seek(0, 0); err != nil {
|
||||||
app.Logger.Error("Seek failed", "err", err)
|
app.Logger.Error("Seek failed", "err", err)
|
||||||
app.SendError(w, r, http.StatusInternalServerError)
|
app.SendError(writer, request, http.StatusInternalServerError)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
key, err := crypto.DeriveKey(src)
|
key, err := crypto.DeriveKey(src)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.Logger.Error("Key derivation failed", "err", err)
|
app.Logger.Error("Key derivation failed", "err", err)
|
||||||
app.SendError(w, r, http.StatusInternalServerError)
|
app.SendError(writer, request, http.StatusInternalServerError)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ext := filepath.Ext(filename)
|
ext := filepath.Ext(filename)
|
||||||
id := crypto.GetID(key, ext)
|
id := crypto.GetID(key, ext)
|
||||||
|
|
||||||
if _, err := src.Seek(0, 0); err != nil {
|
|
||||||
app.Logger.Error("Seek failed", "err", err)
|
|
||||||
app.SendError(w, r, http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
finalPath := filepath.Join(app.Conf.StorageDir, id)
|
finalPath := filepath.Join(app.Conf.StorageDir, id)
|
||||||
|
|
||||||
if _, err := os.Stat(finalPath); err == nil {
|
if _, err := os.Stat(finalPath); err == nil {
|
||||||
app.RespondWithLink(w, r, key, filename)
|
app.RespondWithLink(writer, request, key, filename)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
out, err := os.Create(finalPath + ".tmp")
|
if _, err := src.Seek(0, 0); err != nil {
|
||||||
if err != nil {
|
app.Logger.Error("Seek failed", "err", err)
|
||||||
app.Logger.Error("Failed to create final file", "err", err)
|
app.SendError(writer, request, http.StatusInternalServerError)
|
||||||
app.SendError(w, r, http.StatusInternalServerError)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := app.encryptAndSave(src, key, finalPath); err != nil {
|
||||||
|
app.Logger.Error("Encryption failed", "err", err)
|
||||||
|
app.SendError(writer, request, http.StatusInternalServerError)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
app.RespondWithLink(writer, request, key, filename)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (app *App) encryptAndSave(src io.Reader, key []byte, finalPath string) error {
|
||||||
|
out, err := os.Create(finalPath + ".tmp")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("create final file: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var closed bool
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
out.Close()
|
if !closed {
|
||||||
os.Remove(finalPath + ".tmp")
|
if closeErr := out.Close(); closeErr != nil {
|
||||||
|
app.Logger.Error("Failed to close final file", "err", closeErr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if removeErr := os.Remove(finalPath + ".tmp"); removeErr != nil && !os.IsNotExist(removeErr) {
|
||||||
|
app.Logger.Error("Failed to remove temp final file", "err", removeErr)
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
streamer, err := crypto.NewGCMStreamer(key)
|
streamer, err := crypto.NewGCMStreamer(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.Logger.Error("Failed to create streamer", "err", err)
|
return fmt.Errorf("create streamer: %w", err)
|
||||||
app.SendError(w, r, http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := streamer.EncryptStream(out, src); err != nil {
|
if err := streamer.EncryptStream(out, src); err != nil {
|
||||||
app.Logger.Error("Encryption failed", "err", err)
|
return fmt.Errorf("encrypt stream: %w", err)
|
||||||
app.SendError(w, r, http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := out.Close(); err != nil {
|
if err := out.Close(); err != nil {
|
||||||
app.Logger.Error("Failed to close final file", "err", err)
|
return fmt.Errorf("close final file: %w", err)
|
||||||
app.SendError(w, r, http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
closed = true
|
||||||
|
|
||||||
if err := os.Rename(finalPath+".tmp", finalPath); err != nil {
|
if err := os.Rename(finalPath+".tmp", finalPath); err != nil {
|
||||||
app.Logger.Error("Failed to rename final file", "err", err)
|
return fmt.Errorf("rename final file: %w", err)
|
||||||
app.SendError(w, r, http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
app.RespondWithLink(w, r, key, filename)
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
+35
-15
@@ -9,22 +9,25 @@ import (
|
|||||||
|
|
||||||
func (app *App) Routes() *http.ServeMux {
|
func (app *App) Routes() *http.ServeMux {
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
fs := http.FileServer(http.Dir("./web/static"))
|
fileServer := http.FileServer(http.Dir("./web/static"))
|
||||||
mux.Handle("GET /static/", http.StripPrefix("/static/", fs))
|
|
||||||
|
mux.Handle("GET /static/", http.StripPrefix("/static/", fileServer))
|
||||||
mux.HandleFunc("GET /{$}", app.HandleHome)
|
mux.HandleFunc("GET /{$}", app.HandleHome)
|
||||||
mux.HandleFunc("POST /{$}", app.HandleUpload)
|
mux.HandleFunc("POST /{$}", app.HandleUpload)
|
||||||
mux.HandleFunc("POST /upload/chunk", app.HandleChunk)
|
mux.HandleFunc("POST /upload/chunk", app.HandleChunk)
|
||||||
mux.HandleFunc("POST /upload/finish", app.HandleFinish)
|
mux.HandleFunc("POST /upload/finish", app.HandleFinish)
|
||||||
mux.HandleFunc("GET /{slug}", app.HandleGetFile)
|
mux.HandleFunc("GET /{slug}", app.HandleGetFile)
|
||||||
|
|
||||||
return mux
|
return mux
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) RespondWithLink(w http.ResponseWriter, r *http.Request, key []byte, originalName string) {
|
func (app *App) RespondWithLink(writer http.ResponseWriter, request *http.Request, key []byte, originalName string) {
|
||||||
keySlug := base64.RawURLEncoding.EncodeToString(key)
|
keySlug := base64.RawURLEncoding.EncodeToString(key)
|
||||||
ext := filepath.Ext(originalName)
|
ext := filepath.Ext(originalName)
|
||||||
link := fmt.Sprintf("%s/%s%s", r.Host, keySlug, ext)
|
link := fmt.Sprintf("%s/%s%s", request.Host, keySlug, ext)
|
||||||
if r.Header.Get("X-Requested-With") == "XMLHttpRequest" {
|
|
||||||
fmt.Fprintf(w, `
|
if request.Header.Get("X-Requested-With") == "XMLHttpRequest" {
|
||||||
|
html := `
|
||||||
<div class="result-container">
|
<div class="result-container">
|
||||||
<div class="dim result-label">Upload Complete:</div>
|
<div class="dim result-label">Upload Complete:</div>
|
||||||
<div class="copy-box">
|
<div class="copy-box">
|
||||||
@@ -34,27 +37,44 @@ func (app *App) RespondWithLink(w http.ResponseWriter, r *http.Request, key []by
|
|||||||
<div class="reset-wrapper">
|
<div class="reset-wrapper">
|
||||||
<button class="reset-btn" onclick="resetUI()">Upload another</button>
|
<button class="reset-btn" onclick="resetUI()">Upload another</button>
|
||||||
</div>
|
</div>
|
||||||
</div>`, link)
|
</div>`
|
||||||
|
|
||||||
|
if _, err := fmt.Fprintf(writer, html, link); err != nil {
|
||||||
|
app.Logger.Error("Failed to write response", "err", err)
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
scheme := "https"
|
scheme := "https"
|
||||||
if r.TLS == nil {
|
|
||||||
|
if request.TLS == nil {
|
||||||
scheme = "http"
|
scheme = "http"
|
||||||
}
|
}
|
||||||
fmt.Fprintf(w, "%s://%s\n", scheme, link)
|
|
||||||
|
if _, err := fmt.Fprintf(writer, "%s://%s\n", scheme, link); err != nil {
|
||||||
|
app.Logger.Error("Failed to write response", "err", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) SendError(w http.ResponseWriter, r *http.Request, code int) {
|
func (app *App) SendError(writer http.ResponseWriter, request *http.Request, code int) {
|
||||||
if r.Header.Get("X-Requested-With") == "XMLHttpRequest" {
|
if request.Header.Get("X-Requested-With") == "XMLHttpRequest" {
|
||||||
w.WriteHeader(code)
|
writer.WriteHeader(code)
|
||||||
fmt.Fprintf(w, `
|
|
||||||
|
html := `
|
||||||
<div class="result-container">
|
<div class="result-container">
|
||||||
<div class="error-text">Error %d</div>
|
<div class="error-text">Error %d</div>
|
||||||
<div class="reset-wrapper">
|
<div class="reset-wrapper">
|
||||||
<button class="reset-btn" onclick="resetUI()">Try again</button>
|
<button class="reset-btn" onclick="resetUI()">Try again</button>
|
||||||
</div>
|
</div>
|
||||||
</div>`, code)
|
</div>`
|
||||||
|
|
||||||
|
if _, err := fmt.Fprintf(writer, html, code); err != nil {
|
||||||
|
app.Logger.Error("Failed to write error response", "err", err)
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
http.Error(w, http.StatusText(code), code)
|
|
||||||
|
http.Error(writer, http.StatusText(code), code)
|
||||||
}
|
}
|
||||||
|
|||||||
+59
-20
@@ -8,43 +8,82 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
cleanupInterval = 1 * time.Hour
|
||||||
|
tempExpiry = 4 * time.Hour
|
||||||
|
minRetention = 24 * time.Hour
|
||||||
|
maxRetention = 365 * 24 * time.Hour
|
||||||
|
bytesInMB = 1 << 20
|
||||||
|
)
|
||||||
|
|
||||||
func (app *App) StartCleanupTask(ctx context.Context) {
|
func (app *App) StartCleanupTask(ctx context.Context) {
|
||||||
ticker := time.NewTicker(1 * time.Hour)
|
ticker := time.NewTicker(cleanupInterval)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
|
ticker.Stop()
|
||||||
return
|
return
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
app.CleanDir(app.Conf.StorageDir, false)
|
app.CleanStorage(app.Conf.StorageDir)
|
||||||
app.CleanDir(filepath.Join(app.Conf.StorageDir, "tmp"), true)
|
app.CleanTemp(filepath.Join(app.Conf.StorageDir, "tmp"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *App) CleanDir(path string, isTmp bool) {
|
func (app *App) CleanStorage(path string) {
|
||||||
entries, _ := os.ReadDir(path)
|
entries, err := os.ReadDir(path)
|
||||||
for _, entry := range entries {
|
if err != nil {
|
||||||
info, _ := entry.Info()
|
app.Logger.Error("Failed to read storage dir", "err", err)
|
||||||
expiry := 4 * time.Hour
|
return
|
||||||
if !isTmp {
|
|
||||||
expiry = CalculateRetention(info.Size(), app.Conf.MaxMB)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, entry := range entries {
|
||||||
|
info, err := entry.Info()
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
expiry := CalculateRetention(info.Size(), app.Conf.MaxMB)
|
||||||
|
|
||||||
if time.Since(info.ModTime()) > expiry {
|
if time.Since(info.ModTime()) > expiry {
|
||||||
os.RemoveAll(filepath.Join(path, entry.Name()))
|
if err := os.RemoveAll(filepath.Join(path, entry.Name())); err != nil {
|
||||||
|
app.Logger.Error("Failed to remove expired file", "path", entry.Name(), "err", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CalculateRetention(fileSize int64, maxMB int64) time.Duration {
|
func (app *App) CleanTemp(path string) {
|
||||||
const (
|
entries, err := os.ReadDir(path)
|
||||||
minAge = 24 * time.Hour
|
if err != nil {
|
||||||
maxAge = 365 * 24 * time.Hour
|
app.Logger.Error("Failed to read temp dir", "err", err)
|
||||||
)
|
return
|
||||||
ratio := math.Max(0, math.Min(1, float64(fileSize)/float64(maxMB<<20)))
|
|
||||||
retention := float64(maxAge) * math.Pow(1.0-ratio, 3)
|
|
||||||
if retention < float64(minAge) {
|
|
||||||
return minAge
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, entry := range entries {
|
||||||
|
info, err := entry.Info()
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if time.Since(info.ModTime()) > tempExpiry {
|
||||||
|
if err := os.RemoveAll(filepath.Join(path, entry.Name())); err != nil {
|
||||||
|
app.Logger.Error("Failed to remove expired temp file", "path", entry.Name(), "err", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func CalculateRetention(fileSize, maxMB int64) time.Duration {
|
||||||
|
ratio := math.Max(0, math.Min(1, float64(fileSize)/float64(maxMB*bytesInMB)))
|
||||||
|
|
||||||
|
invRatio := 1.0 - ratio
|
||||||
|
retention := float64(maxRetention) * (invRatio * invRatio * invRatio)
|
||||||
|
|
||||||
|
if retention < float64(minRetention) {
|
||||||
|
return minRetention
|
||||||
|
}
|
||||||
|
|
||||||
return time.Duration(retention)
|
return time.Duration(retention)
|
||||||
}
|
}
|
||||||
|
|||||||
+37
-21
@@ -6,27 +6,34 @@ import (
|
|||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
GCMChunkSize = 64 * 1024
|
GCMChunkSize = 64 * 1024
|
||||||
NonceSize = 12
|
NonceSize = 12
|
||||||
|
KeySize = 16
|
||||||
|
IDSize = 9
|
||||||
)
|
)
|
||||||
|
|
||||||
func DeriveKey(r io.Reader) ([]byte, error) {
|
func DeriveKey(reader io.Reader) ([]byte, error) {
|
||||||
h := sha256.New()
|
hasher := sha256.New()
|
||||||
if _, err := io.Copy(h, r); err != nil {
|
|
||||||
return nil, err
|
if _, err := io.Copy(hasher, reader); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to copy to hasher: %w", err)
|
||||||
}
|
}
|
||||||
return h.Sum(nil)[:16], nil
|
|
||||||
|
return hasher.Sum(nil)[:KeySize], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetID(key []byte, ext string) string {
|
func GetID(key []byte, ext string) string {
|
||||||
h := sha256.New()
|
hasher := sha256.New()
|
||||||
h.Write(key)
|
hasher.Write(key)
|
||||||
h.Write([]byte(ext))
|
hasher.Write([]byte(ext))
|
||||||
return base64.RawURLEncoding.EncodeToString(h.Sum(nil)[:9])
|
|
||||||
|
return base64.RawURLEncoding.EncodeToString(hasher.Sum(nil)[:IDSize])
|
||||||
}
|
}
|
||||||
|
|
||||||
type GCMStreamer struct {
|
type GCMStreamer struct {
|
||||||
@@ -34,37 +41,46 @@ type GCMStreamer struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewGCMStreamer(key []byte) (*GCMStreamer, error) {
|
func NewGCMStreamer(key []byte) (*GCMStreamer, error) {
|
||||||
b, err := aes.NewCipher(key)
|
block, err := aes.NewCipher(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("failed to create cipher: %w", err)
|
||||||
}
|
}
|
||||||
g, err := cipher.NewGCM(b)
|
|
||||||
|
gcm, err := cipher.NewGCM(block)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("failed to create GCM: %w", err)
|
||||||
}
|
}
|
||||||
return &GCMStreamer{AEAD: g}, nil
|
|
||||||
|
return &GCMStreamer{AEAD: gcm}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GCMStreamer) EncryptStream(dst io.Writer, src io.Reader) error {
|
func (g *GCMStreamer) EncryptStream(dst io.Writer, src io.Reader) error {
|
||||||
buf := make([]byte, GCMChunkSize)
|
buf := make([]byte, GCMChunkSize)
|
||||||
var chunkIdx uint64 = 0
|
var chunkIdx uint64
|
||||||
|
|
||||||
for {
|
for {
|
||||||
n, err := io.ReadFull(src, buf)
|
bytesRead, err := io.ReadFull(src, buf)
|
||||||
if n > 0 {
|
if bytesRead > 0 {
|
||||||
nonce := make([]byte, NonceSize)
|
nonce := make([]byte, NonceSize)
|
||||||
binary.BigEndian.PutUint64(nonce[4:], chunkIdx)
|
binary.BigEndian.PutUint64(nonce[4:], chunkIdx)
|
||||||
ciphertext := g.AEAD.Seal(nil, nonce, buf[:n], nil)
|
|
||||||
|
ciphertext := g.AEAD.Seal(nil, nonce, buf[:bytesRead], nil)
|
||||||
|
|
||||||
if _, werr := dst.Write(ciphertext); werr != nil {
|
if _, werr := dst.Write(ciphertext); werr != nil {
|
||||||
return werr
|
return fmt.Errorf("failed to write ciphertext: %w", werr)
|
||||||
}
|
}
|
||||||
|
|
||||||
chunkIdx++
|
chunkIdx++
|
||||||
}
|
}
|
||||||
if err == io.EOF || err == io.ErrUnexpectedEOF {
|
|
||||||
|
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("failed to read source: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
+33
-18
@@ -4,34 +4,41 @@ import (
|
|||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ErrInvalidWhence = errors.New("invalid whence")
|
||||||
|
var ErrNegativeBias = errors.New("negative bias")
|
||||||
|
|
||||||
type Decryptor struct {
|
type Decryptor struct {
|
||||||
rs io.ReadSeeker
|
readSeeker io.ReadSeeker
|
||||||
aead cipher.AEAD
|
aead cipher.AEAD
|
||||||
size int64
|
size int64
|
||||||
offset int64
|
offset int64
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDecryptor(rs io.ReadSeeker, aead cipher.AEAD, encryptedSize int64) *Decryptor {
|
func NewDecryptor(readSeeker io.ReadSeeker, aead cipher.AEAD, encryptedSize int64) *Decryptor {
|
||||||
overhead := int64(aead.Overhead())
|
overhead := int64(aead.Overhead())
|
||||||
fullBlocks := encryptedSize / (GCMChunkSize + overhead)
|
chunkWithOverhead := int64(GCMChunkSize) + overhead
|
||||||
remainder := encryptedSize % (GCMChunkSize + overhead)
|
|
||||||
|
|
||||||
plainSize := (fullBlocks * GCMChunkSize)
|
fullBlocks := encryptedSize / chunkWithOverhead
|
||||||
|
remainder := encryptedSize % chunkWithOverhead
|
||||||
|
|
||||||
|
plainSize := fullBlocks * GCMChunkSize
|
||||||
if remainder > overhead {
|
if remainder > overhead {
|
||||||
plainSize += (remainder - overhead)
|
plainSize += (remainder - overhead)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Decryptor{
|
return &Decryptor{
|
||||||
rs: rs,
|
readSeeker: readSeeker,
|
||||||
aead: aead,
|
aead: aead,
|
||||||
size: plainSize,
|
size: plainSize,
|
||||||
|
offset: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decryptor) Read(p []byte) (int, error) {
|
func (d *Decryptor) Read(buf []byte) (int, error) {
|
||||||
if d.offset >= d.size {
|
if d.offset >= d.size {
|
||||||
return 0, io.EOF
|
return 0, io.EOF
|
||||||
}
|
}
|
||||||
@@ -40,25 +47,29 @@ func (d *Decryptor) Read(p []byte) (int, error) {
|
|||||||
overhang := d.offset % GCMChunkSize
|
overhang := d.offset % GCMChunkSize
|
||||||
|
|
||||||
overhead := int64(d.aead.Overhead())
|
overhead := int64(d.aead.Overhead())
|
||||||
actualChunkSize := int64(GCMChunkSize + overhead)
|
actualChunkSize := int64(GCMChunkSize) + overhead
|
||||||
|
|
||||||
_, err := d.rs.Seek(chunkIdx*actualChunkSize, io.SeekStart)
|
_, err := d.readSeeker.Seek(chunkIdx*actualChunkSize, io.SeekStart)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, fmt.Errorf("failed to seek: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
encrypted := make([]byte, actualChunkSize)
|
encrypted := make([]byte, actualChunkSize)
|
||||||
n, err := io.ReadFull(d.rs, encrypted)
|
|
||||||
if err != nil && err != io.ErrUnexpectedEOF {
|
bytesRead, err := io.ReadFull(d.readSeeker, encrypted)
|
||||||
return 0, err
|
if err != nil && !errors.Is(err, io.ErrUnexpectedEOF) {
|
||||||
|
return 0, fmt.Errorf("failed to read encrypted data: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
nonce := make([]byte, NonceSize)
|
nonce := make([]byte, NonceSize)
|
||||||
|
if chunkIdx < 0 {
|
||||||
|
return 0, fmt.Errorf("invalid chunk index")
|
||||||
|
}
|
||||||
binary.BigEndian.PutUint64(nonce[4:], uint64(chunkIdx))
|
binary.BigEndian.PutUint64(nonce[4:], uint64(chunkIdx))
|
||||||
|
|
||||||
plaintext, err := d.aead.Open(nil, nonce, encrypted[:n], nil)
|
plaintext, err := d.aead.Open(nil, nonce, encrypted[:bytesRead], nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, fmt.Errorf("failed to decrypt: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if overhang >= int64(len(plaintext)) {
|
if overhang >= int64(len(plaintext)) {
|
||||||
@@ -66,7 +77,7 @@ func (d *Decryptor) Read(p []byte) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
available := plaintext[overhang:]
|
available := plaintext[overhang:]
|
||||||
nCopied := copy(p, available)
|
nCopied := copy(buf, available)
|
||||||
d.offset += int64(nCopied)
|
d.offset += int64(nCopied)
|
||||||
|
|
||||||
return nCopied, nil
|
return nCopied, nil
|
||||||
@@ -74,6 +85,7 @@ func (d *Decryptor) Read(p []byte) (int, error) {
|
|||||||
|
|
||||||
func (d *Decryptor) Seek(offset int64, whence int) (int64, error) {
|
func (d *Decryptor) Seek(offset int64, whence int) (int64, error) {
|
||||||
var abs int64
|
var abs int64
|
||||||
|
|
||||||
switch whence {
|
switch whence {
|
||||||
case io.SeekStart:
|
case io.SeekStart:
|
||||||
abs = offset
|
abs = offset
|
||||||
@@ -82,11 +94,14 @@ func (d *Decryptor) Seek(offset int64, whence int) (int64, error) {
|
|||||||
case io.SeekEnd:
|
case io.SeekEnd:
|
||||||
abs = d.size + offset
|
abs = d.size + offset
|
||||||
default:
|
default:
|
||||||
return 0, errors.New("invalid whence")
|
return 0, ErrInvalidWhence
|
||||||
}
|
}
|
||||||
|
|
||||||
if abs < 0 {
|
if abs < 0 {
|
||||||
return 0, errors.New("negative bias")
|
return 0, ErrNegativeBias
|
||||||
}
|
}
|
||||||
|
|
||||||
d.offset = abs
|
d.offset = abs
|
||||||
|
|
||||||
return abs, nil
|
return abs, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,27 +2,39 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"path/filepath"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/skidoodle/safebin/internal/app"
|
"github.com/skidoodle/safebin/internal/app"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
permUserRWX = 0o700
|
||||||
|
serverTimeout = 10 * time.Minute
|
||||||
|
shutdownTimeout = 10 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cfg := app.LoadConfig()
|
cfg := app.LoadConfig()
|
||||||
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug}))
|
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
|
||||||
|
Level: slog.LevelDebug,
|
||||||
|
AddSource: true,
|
||||||
|
}))
|
||||||
|
|
||||||
logger.Info("Initializing Safebin Server",
|
logger.Info("Initializing Safebin Server",
|
||||||
"storage_dir", cfg.StorageDir,
|
"storage_dir", cfg.StorageDir,
|
||||||
"max_file_size", fmt.Sprintf("%dMB", cfg.MaxMB),
|
"max_file_size", fmt.Sprintf("%dMB", cfg.MaxMB),
|
||||||
)
|
)
|
||||||
|
|
||||||
if err := os.MkdirAll(fmt.Sprintf("%s/tmp", cfg.StorageDir), 0700); err != nil {
|
tmpDir := filepath.Join(cfg.StorageDir, "tmp")
|
||||||
|
if err := os.MkdirAll(tmpDir, permUserRWX); err != nil {
|
||||||
logger.Error("Failed to initialize storage directory", "err", err)
|
logger.Error("Failed to initialize storage directory", "err", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
@@ -41,13 +53,15 @@ func main() {
|
|||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Addr: cfg.Addr,
|
Addr: cfg.Addr,
|
||||||
Handler: application.Routes(),
|
Handler: application.Routes(),
|
||||||
ReadTimeout: 10 * time.Minute,
|
ReadTimeout: serverTimeout,
|
||||||
WriteTimeout: 10 * time.Minute,
|
WriteTimeout: serverTimeout,
|
||||||
|
IdleTimeout: serverTimeout,
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
application.Logger.Info("Server is ready and listening", "addr", cfg.Addr)
|
application.Logger.Info("Server is ready and listening", "addr", cfg.Addr)
|
||||||
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
||||||
|
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||||
application.Logger.Error("Server failed to start", "err", err)
|
application.Logger.Error("Server failed to start", "err", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
@@ -56,10 +70,12 @@ func main() {
|
|||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
application.Logger.Info("Shutting down gracefully...")
|
application.Logger.Info("Shutting down gracefully...")
|
||||||
|
|
||||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
if err := srv.Shutdown(shutdownCtx); err != nil {
|
if err := srv.Shutdown(shutdownCtx); err != nil {
|
||||||
application.Logger.Error("Forced shutdown", "err", err)
|
application.Logger.Error("Forced shutdown", "err", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
application.Logger.Info("Server stopped")
|
application.Logger.Info("Server stopped")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user