Compare commits
25 Commits
060200c998
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
645fdc37d8 | ||
|
|
35d908cd73 | ||
|
|
f188264f39 | ||
|
|
1436b8cf2f | ||
|
|
459fe380db | ||
|
|
ec19585e9b | ||
|
|
9ced6600e3 | ||
|
|
b78cc02d5c | ||
|
|
a93fa2f61f | ||
|
|
230e58c286 | ||
|
|
09e147f2f1 | ||
|
|
28e88cc833 | ||
|
|
d06a7cf09d | ||
|
|
d18838dfee | ||
|
|
363c3b6c96 | ||
|
|
b436415980 | ||
|
|
d0ff3003b2 | ||
|
|
bea0213387 | ||
|
|
4d284b7c5a | ||
|
|
1234288e03 | ||
|
|
7ee19a2883 | ||
|
|
6da1bb9fc1 | ||
|
|
fb7d0ee80d | ||
|
|
3c0f1a046c | ||
|
|
71ffad466b |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
lash
|
||||
12
Makefile
Normal file
12
Makefile
Normal file
@@ -0,0 +1,12 @@
|
||||
GO?= go
|
||||
|
||||
.PHONY: build lint
|
||||
|
||||
all: build
|
||||
|
||||
build:
|
||||
$(GO) build -o lash ./bin/main.go
|
||||
|
||||
lint:
|
||||
$(GO) fmt ./...
|
||||
$(GO) vet ./...
|
||||
20
README.md
20
README.md
@@ -1 +1,21 @@
|
||||
# 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
|
||||
```
|
||||
|
||||
44
bin/main.go
Normal file
44
bin/main.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"flag"
|
||||
"fmt"
|
||||
"lash"
|
||||
"lash/internal/app"
|
||||
"lash/internal/errx"
|
||||
share "lash/internal/shareLink"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var versionFlag = flag.Bool("version", false, "Print out version and exit.")
|
||||
|
||||
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() {
|
||||
flag.Parse()
|
||||
|
||||
if *versionFlag {
|
||||
fmt.Print("lash/vxc v", lash.Version)
|
||||
return
|
||||
}
|
||||
|
||||
ctx := lash.LashContext{
|
||||
N: *numberOfShares,
|
||||
}
|
||||
|
||||
srv := app.New(&ctx)
|
||||
|
||||
server := http.Server{
|
||||
Addr: fmt.Sprintf(":%d", *port),
|
||||
Handler: srv,
|
||||
}
|
||||
|
||||
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("starting server at http://%s:%d", share.GetLocalIP(), *port)
|
||||
if err := server.ListenAndServe(); err != nil {
|
||||
errx.FatalPerror(err)
|
||||
}
|
||||
}
|
||||
2
go.mod
2
go.mod
@@ -1,5 +1,3 @@
|
||||
module lash
|
||||
|
||||
go 1.26.3
|
||||
|
||||
require github.com/google/uuid v1.6.0 // indirect
|
||||
|
||||
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=
|
||||
|
||||
45
internal/app/routes.go
Normal file
45
internal/app/routes.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"lash"
|
||||
"lash/internal/errx"
|
||||
"lash/internal/handlers"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func New(ctx *lash.LashContext) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
share := handlers.ShareData{
|
||||
Version: lash.Version,
|
||||
}
|
||||
|
||||
file := handlers.FileHandler{
|
||||
Ctx: ctx,
|
||||
}
|
||||
|
||||
fps := flag.Args()
|
||||
if len(fps) < 1 {
|
||||
errx.FatalPerror(errx.ErrNotEnoughArgs)
|
||||
}
|
||||
|
||||
for _, fp := range fps {
|
||||
contents, err := os.ReadFile(fp)
|
||||
if err != nil {
|
||||
errx.FatalPerror(err)
|
||||
}
|
||||
|
||||
file.FileData = append(file.FileData, handlers.FileData{
|
||||
Contents: contents,
|
||||
FileName: fp,
|
||||
})
|
||||
}
|
||||
|
||||
mux.HandleFunc("/", share.Handler)
|
||||
mux.HandleFunc("/api/receive-token", file.APIHandler)
|
||||
mux.HandleFunc("/"+lash.ShareLinkToken, file.FileHandler)
|
||||
|
||||
return mux
|
||||
}
|
||||
5
internal/errx/define.go
Normal file
5
internal/errx/define.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package errx
|
||||
|
||||
import "errors"
|
||||
|
||||
var ErrNotEnoughArgs error = errors.New("not enough arguments")
|
||||
7
internal/errx/error.go
Normal file
7
internal/errx/error.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package errx
|
||||
|
||||
import "fmt"
|
||||
|
||||
func Error(err string) {
|
||||
fmt.Println("error:", "lash:", err)
|
||||
}
|
||||
15
internal/errx/perror.go
Normal file
15
internal/errx/perror.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package errx
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func Perror(err error) {
|
||||
fmt.Println("error:", "lash:", err)
|
||||
}
|
||||
|
||||
func FatalPerror(err error) {
|
||||
Perror(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
7
internal/errx/print.go
Normal file
7
internal/errx/print.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package errx
|
||||
|
||||
import "fmt"
|
||||
|
||||
func Log(msg string, k ...any) {
|
||||
fmt.Printf("lash: "+msg+"\n", k...)
|
||||
}
|
||||
55
internal/generator/generator.go
Normal file
55
internal/generator/generator.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package generator
|
||||
|
||||
import (
|
||||
"math/rand/v2"
|
||||
)
|
||||
|
||||
var keys []string = []string{
|
||||
"octo",
|
||||
"cool",
|
||||
"whip",
|
||||
"keys",
|
||||
"cats",
|
||||
"part",
|
||||
"rate",
|
||||
"face",
|
||||
"lard",
|
||||
"larp",
|
||||
"dogs",
|
||||
"cash",
|
||||
"city",
|
||||
"cold",
|
||||
"desk",
|
||||
"down",
|
||||
"dirt",
|
||||
"long",
|
||||
"mean",
|
||||
"news",
|
||||
"only",
|
||||
"open",
|
||||
"year",
|
||||
"wood",
|
||||
"wing",
|
||||
"work",
|
||||
"wash",
|
||||
"vote",
|
||||
"onyx",
|
||||
"snow",
|
||||
"john",
|
||||
"json",
|
||||
"fang",
|
||||
}
|
||||
|
||||
func Generate(n int) (code string) {
|
||||
codeLen := n
|
||||
|
||||
start := rand.IntN(len(keys))
|
||||
|
||||
for i := range codeLen {
|
||||
code += keys[(start+i)%len(keys)]
|
||||
if i != codeLen-1 {
|
||||
code += "-"
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
83
internal/handlers/file.go
Normal file
83
internal/handlers/file.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"lash"
|
||||
"lash/internal/errx"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type FileHandler struct {
|
||||
Ctx *lash.LashContext
|
||||
FileData []FileData
|
||||
}
|
||||
|
||||
type FileData struct {
|
||||
Contents []byte
|
||||
FileName string
|
||||
}
|
||||
|
||||
type ValidateRequest struct {
|
||||
Token string
|
||||
}
|
||||
|
||||
var sent int = 0
|
||||
|
||||
func (h FileHandler) APIHandler(w http.ResponseWriter, r *http.Request) {
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
|
||||
var t ValidateRequest
|
||||
err := decoder.Decode(&t)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if t.Token != lash.Token {
|
||||
http.Error(w, "Invalid Token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, fmt.Sprintf("/%s", lash.ShareLinkToken), http.StatusTemporaryRedirect)
|
||||
}
|
||||
|
||||
func (h FileHandler) FileHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if sent >= h.Ctx.N && h.Ctx.N != -1 {
|
||||
http.Error(w, "Too many requests", http.StatusTooManyRequests)
|
||||
return
|
||||
}
|
||||
|
||||
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 {
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(h.FileData[0].Contents)))
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write(h.FileData[0].Contents)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
zw := zip.NewWriter(w)
|
||||
defer zw.Close()
|
||||
|
||||
for _, f := range h.FileData {
|
||||
fw, err := zw.Create(f.FileName)
|
||||
if err != nil {
|
||||
errx.FatalPerror(err)
|
||||
}
|
||||
_, err = fw.Write(f.Contents)
|
||||
if err != nil {
|
||||
errx.FatalPerror(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sent++
|
||||
}
|
||||
21
internal/handlers/share.go
Normal file
21
internal/handlers/share.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
|
||||
"lash"
|
||||
)
|
||||
|
||||
type ShareData struct {
|
||||
Version string
|
||||
FileName string
|
||||
}
|
||||
|
||||
func (h *ShareData) Handler(w http.ResponseWriter, r *http.Request) {
|
||||
tmpl := template.Must(template.ParseFS(lash.Templates, "templates/share.html"))
|
||||
|
||||
if err := tmpl.ExecuteTemplate(w, "share.html", h); err != nil {
|
||||
http.Error(w, "template render error: "+err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
29
internal/shareLink/shareLink.go
Normal file
29
internal/shareLink/shareLink.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package sharelink
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"lash"
|
||||
"lash/internal/errx"
|
||||
"net"
|
||||
)
|
||||
|
||||
func GenerateShareLink(port int) string {
|
||||
link := "http://"
|
||||
link += GetLocalIP()
|
||||
link += fmt.Sprintf(":%d", port)
|
||||
link += "/"
|
||||
link += lash.ShareLinkToken
|
||||
return link
|
||||
}
|
||||
|
||||
func GetLocalIP() string {
|
||||
conn, err := net.Dial("udp", "8.8.8.8:80")
|
||||
if err != nil {
|
||||
errx.FatalPerror(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
localAddress := conn.LocalAddr().(*net.UDPAddr)
|
||||
|
||||
return localAddress.IP.String()
|
||||
}
|
||||
22
lash.go
Normal file
22
lash.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package lash
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"lash/internal/generator"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type LashContext struct {
|
||||
N int
|
||||
Server *http.Server
|
||||
}
|
||||
|
||||
//go:embed templates/*.html
|
||||
var Templates embed.FS
|
||||
|
||||
//go:embed version
|
||||
var Version string
|
||||
|
||||
var Token string = generator.Generate(5)
|
||||
|
||||
var ShareLinkToken string = generator.Generate(8)
|
||||
65
main.go
65
main.go
@@ -1,65 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
//go:embed version
|
||||
var version string
|
||||
|
||||
var fp string
|
||||
var read []byte
|
||||
var id string = uuid.New().String()
|
||||
|
||||
type Data struct {
|
||||
Version string
|
||||
FileName string
|
||||
Contents []byte
|
||||
UUID string
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Println("error: lash: not enough arguments")
|
||||
return
|
||||
}
|
||||
fp = os.Args[1]
|
||||
var err error
|
||||
read, err = os.ReadFile(fp)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
fmt.Printf("error: lash: file `%s` does not exist", fp)
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
http.HandleFunc("/", shareHandler)
|
||||
http.HandleFunc(fmt.Sprintf("/%s", id), fileHandler)
|
||||
fmt.Println("lash: starting server at http://127.0.0.1:1337")
|
||||
log.Fatal(http.ListenAndServe("127.0.0.1:1337", nil))
|
||||
}
|
||||
|
||||
func shareHandler(w http.ResponseWriter, r *http.Request) {
|
||||
tmpl := template.Must(template.ParseFiles("templates/share.html"))
|
||||
|
||||
data := Data{
|
||||
Version: version,
|
||||
FileName: fp,
|
||||
UUID: id,
|
||||
}
|
||||
|
||||
tmpl.ExecuteTemplate(w, "share.html", data)
|
||||
}
|
||||
|
||||
func fileHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write(read)
|
||||
}
|
||||
@@ -1,12 +1,78 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Lash v{{.Version}}</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1><a href="/{{.UUID}}" download>{{.FileName}}</a></h1>
|
||||
<h1>Enter Token</h1>
|
||||
|
||||
<input
|
||||
type="text"
|
||||
id="tokenInput"
|
||||
placeholder="Enter token"
|
||||
/>
|
||||
|
||||
<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;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/receive-token', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ token })
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
status.textContent = 'Download failed.';
|
||||
return;
|
||||
}
|
||||
|
||||
const blob = await response.blob();
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
const disposition = response.headers.get('Content-Disposition');
|
||||
|
||||
let filename = 'download.bin';
|
||||
|
||||
if (disposition && disposition.includes('filename=')) {
|
||||
filename = disposition
|
||||
.split('filename=')[1]
|
||||
.replace(/"/g, '');
|
||||
}
|
||||
|
||||
a.download = filename;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
window.URL.revokeObjectURL(url);
|
||||
status.textContent = 'Download started.';
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
status.textContent = 'An error occurred.';
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user