This commit is contained in:
vxclutch
2026-05-27 18:52:37 -04:00
parent 6da1bb9fc1
commit 7ee19a2883
3 changed files with 55 additions and 25 deletions

View File

@@ -3,7 +3,7 @@ package handlers
import ( import (
"encoding/json" "encoding/json"
"lash" "lash"
"lash/internal/errx" "strconv"
"net/http" "net/http"
) )
@@ -17,19 +17,23 @@ type ValidateRequest struct {
func (h FileData) APIHandler(w http.ResponseWriter, r *http.Request) { func (h FileData) APIHandler(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body) decoder := json.NewDecoder(r.Body)
var t ValidateRequest var t ValidateRequest
err := decoder.Decode(&t) err := decoder.Decode(&t)
if err != nil { if err != nil {
errx.FatalPerror(err) http.Error(w, err.Error(), http.StatusBadRequest)
return
} }
if t.Token == lash.Token { if t.Token != lash.Token {
errx.Log("Got token") http.Error(w, "Invalid Token", http.StatusUnauthorized)
} else { return
errx.Log("No Token")
} }
}
func (h FileData) DownloadHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Disposition", "attachment; filename=file.bin")
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Length", strconv.Itoa(len(h.Contents)))
w.WriteHeader(http.StatusOK)
w.Write(h.Contents) w.Write(h.Contents)
} }

View File

@@ -3,7 +3,7 @@ package lash
import ( import (
"embed" "embed"
// "github.com/google/uuid" "github.com/google/uuid"
) )
//go:embed templates/*.html //go:embed templates/*.html
@@ -12,5 +12,4 @@ var Templates embed.FS
//go:embed version //go:embed version
var Version string var Version string
// var Token string = uuid.New().String() var Token string = uuid.New().String()
var Token string = "foo"

View File

@@ -1,20 +1,47 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Lash v{{.Version}}</title> <title>Lash v{{.Version}}</title>
</head> </head>
<body> <body>
</body> <h1>Enter Token</h1>
<script> <input
await fetch('/api/receive-token', { type="text"
method: 'POST', id="tokenInput"
headers: { 'Content-Type': 'application/json' }, placeholder="Enter token"
body: JSON.stringify({ token: "foo" }) />
});
</script>
<button id="submitBtn">
Submit
</button>
<p id="status"></p>
<script>
const tokenInput = document.getElementById('tokenInput');
const submitBtn = document.getElementById('submitBtn');
const status = document.getElementById('status');
submitBtn.addEventListener('click', async () => {
const token = tokenInput.value.trim();
if (!token) {
status.textContent = 'Please enter a token.';
return;
}
const response = await fetch('/api/receive-token', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ token })
});
});
</script>
</body>
</html> </html>