feat: kill server after n downloads

This commit is contained in:
vxclutch
2026-05-29 08:05:00 -04:00
parent 09e147f2f1
commit 230e58c286
7 changed files with 61 additions and 25 deletions

1
TODO
View File

@@ -5,5 +5,4 @@ feat: improve flags
feat: replace uuid dep with custom id generator feat: replace uuid dep with custom id generator
feat: multiple files feat: multiple files
fix: remove `_` prefix fix: remove `_` prefix
fix: kill server after `n` downloads
feat: use zeroconf for mDNS feat: use zeroconf for mDNS

View File

@@ -14,6 +14,7 @@ import (
var versionFlag = flag.Bool("version", false, "Print out version and exit.") var versionFlag = flag.Bool("version", false, "Print out version and exit.")
var port = flag.Int("p", 1337, "Set the port for LASH exchanges.") var port = flag.Int("p", 1337, "Set the port for LASH exchanges.")
var numberOfShares = flag.Int("n", -1, "Number of shares to allow before killing the server.")
func main() { func main() {
flag.Parse() flag.Parse()
@@ -23,14 +24,22 @@ func main() {
return return
} }
srv := app.New() ctx := lash.LashContext{
N: *numberOfShares,
}
srv := app.New(&ctx)
// TODO(vxc): Make this more portable // TODO(vxc): Make this more portable
errx.Log("Your share link is %s", share.GenerateShareLink(*port)) errx.Log("Your share link is %s", share.GenerateShareLink(*port))
errx.Log("Your token is \033[1;92m%s\033[0m", lash.Token) errx.Log("Your token is \033[1;92m%s\033[0m", lash.Token)
errx.Log("starting server at http://0.0.0.0:%d", *port) errx.Log("starting server at http://0.0.0.0:%d", *port)
if err := http.ListenAndServe(fmt.Sprintf(":%d", *port), srv); err != nil { server := http.Server{
Addr: fmt.Sprintf(":%d", *port),
Handler: srv,
}
if err := server.ListenAndServe(); err != nil {
errx.FatalPerror(err) errx.FatalPerror(err)
} }
} }

View File

@@ -8,7 +8,7 @@ import (
"os" "os"
) )
func New() http.Handler { func New(ctx *lash.LashContext) http.Handler {
mux := http.NewServeMux() mux := http.NewServeMux()
fp, err := GetFilePath() fp, err := GetFilePath()
@@ -25,9 +25,12 @@ func New() http.Handler {
Version: lash.Version, Version: lash.Version,
} }
file := handlers.FileData{ file := handlers.FileHandler{
Ctx: ctx,
FileData: handlers.FileData{
Contents: contents, Contents: contents,
FileName: fp, FileName: fp,
},
} }
mux.HandleFunc("/", share.Handler) mux.HandleFunc("/", share.Handler)

View File

@@ -5,9 +5,15 @@ import (
"fmt" "fmt"
"lash" "lash"
"net/http" "net/http"
"os"
"strconv" "strconv"
) )
type FileHandler struct {
Ctx *lash.LashContext
FileData FileData
}
type FileData struct { type FileData struct {
Contents []byte Contents []byte
FileName string FileName string
@@ -17,7 +23,14 @@ type ValidateRequest struct {
Token string Token string
} }
func (h FileData) APIHandler(w http.ResponseWriter, r *http.Request) { var sent int = 0
func (h FileHandler) APIHandler(w http.ResponseWriter, r *http.Request) {
if sent >= h.Ctx.N && h.Ctx.N != -1 {
w.WriteHeader(http.StatusTooManyRequests)
os.Exit(0)
return
}
decoder := json.NewDecoder(r.Body) decoder := json.NewDecoder(r.Body)
var t ValidateRequest var t ValidateRequest
@@ -32,21 +45,27 @@ func (h FileData) APIHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
// Send the file over as a stream of bytes w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", h.FileData.FileName))
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", h.FileName))
w.Header().Set("Content-Type", "application/octet-stream") w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Length", strconv.Itoa(len(h.Contents))) w.Header().Set("Content-Length", strconv.Itoa(len(h.FileData.Contents)))
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
w.Write(h.Contents) w.Write(h.FileData.Contents)
sent++
} }
func (h FileData) FileHandler(w http.ResponseWriter, r *http.Request) { func (h FileHandler) FileHandler(w http.ResponseWriter, r *http.Request) {
// Send the file over as a stream of bytes if sent >= h.Ctx.N && h.Ctx.N != -1 {
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", h.FileName)) w.WriteHeader(http.StatusTooManyRequests)
os.Exit(0)
return
}
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", h.FileData.FileName))
w.Header().Set("Content-Type", "application/octet-stream") w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Length", strconv.Itoa(len(h.Contents))) w.Header().Set("Content-Length", strconv.Itoa(len(h.FileData.Contents)))
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
w.Write(h.Contents) w.Write(h.FileData.Contents)
sent++
} }

View File

@@ -2,10 +2,16 @@ package lash
import ( import (
"embed" "embed"
"net/http"
"github.com/google/uuid" "github.com/google/uuid"
) )
type LashContext struct {
N int
Server *http.Server
}
//go:embed templates/*.html //go:embed templates/*.html
var Templates embed.FS var Templates embed.FS

View File

@@ -1 +1 @@
1.0.0 3