Compare commits
5 Commits
ec19585e9b
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
645fdc37d8 | ||
|
|
35d908cd73 | ||
|
|
f188264f39 | ||
|
|
1436b8cf2f | ||
|
|
459fe380db |
20
README.md
20
README.md
@@ -1 +1,21 @@
|
|||||||
# LASH (Local Area Share HTTP)
|
# LASH (Local Area Share HTTP)
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
Using lash is pretty simple. You just provide any amount of command line
|
||||||
|
arguments and it will generate a share link, and a code. You either go-to
|
||||||
|
http://<your-local-ip>:port or goto http://<your-share-link> to access the
|
||||||
|
files.
|
||||||
|
|
||||||
|
## Building from Source
|
||||||
|
Lash only has one dependency and that it is Go >= 1.26.3. To build it you just
|
||||||
|
run either of the two following commands.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ make
|
||||||
|
```
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ go build -o lash ./bin/main.go
|
||||||
|
```
|
||||||
|
|||||||
7
TODO
7
TODO
@@ -1,6 +1 @@
|
|||||||
maint: document more
|
feat: sumbit code with press of enter key
|
||||||
feat: improve flags
|
|
||||||
feat: replace uuid dep with custom id generator
|
|
||||||
feat: multiple files
|
|
||||||
fix: remove `_` prefix
|
|
||||||
fix: make mDNS only build on windows
|
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ func main() {
|
|||||||
|
|
||||||
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://%s:%d", share.GetLocalIP(), *port)
|
||||||
if err := server.ListenAndServe(); err != nil {
|
if err := server.ListenAndServe(); err != nil {
|
||||||
errx.FatalPerror(err)
|
errx.FatalPerror(err)
|
||||||
}
|
}
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -1,5 +1,3 @@
|
|||||||
module lash
|
module lash
|
||||||
|
|
||||||
go 1.26.3
|
go 1.26.3
|
||||||
|
|
||||||
require github.com/google/uuid v1.6.0
|
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -1,2 +0,0 @@
|
|||||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
|
||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
|
||||||
|
|||||||
@@ -34,6 +34,10 @@ var keys []string = []string{
|
|||||||
"wash",
|
"wash",
|
||||||
"vote",
|
"vote",
|
||||||
"onyx",
|
"onyx",
|
||||||
|
"snow",
|
||||||
|
"john",
|
||||||
|
"json",
|
||||||
|
"fang",
|
||||||
}
|
}
|
||||||
|
|
||||||
func Generate(n int) (code string) {
|
func Generate(n int) (code string) {
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"lash"
|
"lash"
|
||||||
"lash/internal/errx"
|
"lash/internal/errx"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -25,21 +24,9 @@ type ValidateRequest struct {
|
|||||||
Token string
|
Token string
|
||||||
}
|
}
|
||||||
|
|
||||||
type headers map[string]string
|
|
||||||
|
|
||||||
var zipHeaders headers = headers{
|
|
||||||
"Content-Disposition": "attachment; filename=lash.zip",
|
|
||||||
"Content-Type": "application/octet-stream",
|
|
||||||
}
|
|
||||||
|
|
||||||
var sent int = 0
|
var sent int = 0
|
||||||
|
|
||||||
func (h FileHandler) APIHandler(w http.ResponseWriter, r *http.Request) {
|
func (h FileHandler) APIHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if h.hasHitMax() {
|
|
||||||
w.WriteHeader(http.StatusTooManyRequests)
|
|
||||||
os.Exit(0)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
decoder := json.NewDecoder(r.Body)
|
decoder := json.NewDecoder(r.Body)
|
||||||
|
|
||||||
var t ValidateRequest
|
var t ValidateRequest
|
||||||
@@ -54,71 +41,43 @@ func (h FileHandler) APIHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
http.Redirect(w, r, fmt.Sprintf("/%s", lash.ShareLinkToken), http.StatusTemporaryRedirect)
|
||||||
if len(h.FileData) == 1 {
|
|
||||||
headers := headers{
|
|
||||||
"Content-Disposition": fmt.Sprintf("attachment; filename=%s", h.FileData[0].FileName),
|
|
||||||
"Content-Type": "application/octet-stream",
|
|
||||||
"Content-Length": strconv.Itoa(len(h.FileData[0].FileName)),
|
|
||||||
}
|
|
||||||
headers.set(w)
|
|
||||||
|
|
||||||
w.Write(h.FileData[0].Contents)
|
|
||||||
} else {
|
|
||||||
zipHeaders.set(w)
|
|
||||||
h.writeZip(w)
|
|
||||||
}
|
|
||||||
sent++
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h FileHandler) FileHandler(w http.ResponseWriter, r *http.Request) {
|
func (h FileHandler) FileHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if h.hasHitMax() {
|
if sent >= h.Ctx.N && h.Ctx.N != -1 {
|
||||||
w.WriteHeader(http.StatusTooManyRequests)
|
http.Error(w, "Too many requests", http.StatusTooManyRequests)
|
||||||
os.Exit(0)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
filename := h.FileData[0].FileName
|
||||||
|
if len(h.FileData) > 1 {
|
||||||
|
filename = "lash.zip"
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
|
||||||
|
w.Header().Set("Content-Type", "application/octet-stream")
|
||||||
|
|
||||||
if len(h.FileData) == 1 {
|
if len(h.FileData) == 1 {
|
||||||
headers := headers{
|
w.Header().Set("Content-Length", strconv.Itoa(len(h.FileData[0].Contents)))
|
||||||
"Content-Disposition": fmt.Sprintf("attachment; filename=%s", h.FileData[0].FileName),
|
w.WriteHeader(http.StatusOK)
|
||||||
"Content-Type": "application/octet-stream",
|
_, _ = w.Write(h.FileData[0].Contents)
|
||||||
"Content-Length": strconv.Itoa(len(h.FileData[0].FileName)),
|
|
||||||
}
|
|
||||||
headers.set(w)
|
|
||||||
|
|
||||||
w.Write(h.FileData[0].Contents)
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
zipHeaders.set(w)
|
w.WriteHeader(http.StatusOK)
|
||||||
h.writeZip(w)
|
zw := zip.NewWriter(w)
|
||||||
}
|
|
||||||
sent++
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h FileHandler) writeZip(hw http.ResponseWriter) {
|
|
||||||
zw := zip.NewWriter(hw)
|
|
||||||
defer zw.Close()
|
defer zw.Close()
|
||||||
|
|
||||||
for _, f := range h.FileData {
|
for _, f := range h.FileData {
|
||||||
w, err := zw.Create(f.FileName)
|
fw, err := zw.Create(f.FileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errx.FatalPerror(err)
|
errx.FatalPerror(err)
|
||||||
}
|
}
|
||||||
|
_, err = fw.Write(f.Contents)
|
||||||
_, err = w.Write(f.Contents)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errx.FatalPerror(err)
|
errx.FatalPerror(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h headers) set(w http.ResponseWriter) {
|
sent++
|
||||||
for k, v := range h {
|
|
||||||
w.Header().Set(k, v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h FileHandler) hasHitMax() bool {
|
|
||||||
return sent >= h.Ctx.N && h.Ctx.N != -1
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,10 +24,11 @@
|
|||||||
<script>
|
<script>
|
||||||
const tokenInput = document.getElementById('tokenInput');
|
const tokenInput = document.getElementById('tokenInput');
|
||||||
const submitBtn = document.getElementById('submitBtn');
|
const submitBtn = document.getElementById('submitBtn');
|
||||||
|
|
||||||
const status = document.getElementById('status');
|
const status = document.getElementById('status');
|
||||||
|
|
||||||
submitBtn.addEventListener('click', async () => {
|
submitBtn.addEventListener('click', async () => {
|
||||||
const token = tokenInput.value.trim();
|
const token = tokenInput.value.trim();
|
||||||
|
|
||||||
if (!token) {
|
if (!token) {
|
||||||
status.textContent = 'Please enter a token.';
|
status.textContent = 'Please enter a token.';
|
||||||
return;
|
return;
|
||||||
@@ -41,6 +42,7 @@
|
|||||||
},
|
},
|
||||||
body: JSON.stringify({ token })
|
body: JSON.stringify({ token })
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
status.textContent = 'Download failed.';
|
status.textContent = 'Download failed.';
|
||||||
return;
|
return;
|
||||||
@@ -48,19 +50,19 @@
|
|||||||
|
|
||||||
const blob = await response.blob();
|
const blob = await response.blob();
|
||||||
const url = window.URL.createObjectURL(blob);
|
const url = window.URL.createObjectURL(blob);
|
||||||
a.href = url;
|
|
||||||
|
|
||||||
const a = document.createElement('a');
|
const a = document.createElement('a');
|
||||||
|
a.href = url;
|
||||||
const disposition = response.headers.get('Content-Disposition');
|
const disposition = response.headers.get('Content-Disposition');
|
||||||
|
|
||||||
let filename = 'download.bin';
|
let filename = 'download.bin';
|
||||||
|
|
||||||
if (disposition && disposition.includes('filename=')) {
|
if (disposition && disposition.includes('filename=')) {
|
||||||
filename = disposition
|
filename = disposition
|
||||||
.split('filename=')[1]
|
.split('filename=')[1]
|
||||||
.replace(/"/g, '');
|
.replace(/"/g, '');
|
||||||
}
|
}
|
||||||
a.download = filename;
|
|
||||||
|
|
||||||
|
a.download = filename;
|
||||||
document.body.appendChild(a);
|
document.body.appendChild(a);
|
||||||
a.click();
|
a.click();
|
||||||
a.remove();
|
a.remove();
|
||||||
|
|||||||
Reference in New Issue
Block a user