Compare commits
7 Commits
b78cc02d5c
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
645fdc37d8 | ||
|
|
35d908cd73 | ||
|
|
f188264f39 | ||
|
|
1436b8cf2f | ||
|
|
459fe380db | ||
|
|
ec19585e9b | ||
|
|
9ced6600e3 |
2
Makefile
2
Makefile
@@ -5,7 +5,7 @@ GO?= go
|
|||||||
all: build
|
all: build
|
||||||
|
|
||||||
build:
|
build:
|
||||||
$(GO) build -o lash ./cmd/lash/
|
$(GO) build -o lash ./bin/main.go
|
||||||
|
|
||||||
lint:
|
lint:
|
||||||
$(GO) fmt ./...
|
$(GO) fmt ./...
|
||||||
|
|||||||
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
|
||||||
|
```
|
||||||
|
|||||||
9
TODO
9
TODO
@@ -1,8 +1 @@
|
|||||||
maint: clean up source code
|
feat: sumbit code with press of enter key
|
||||||
maint: document more
|
|
||||||
maint: make the colors more cross platform
|
|
||||||
feat: improve flags
|
|
||||||
feat: replace uuid dep with custom id generator
|
|
||||||
feat: multiple files
|
|
||||||
fix: remove `_` prefix
|
|
||||||
fix: make mDNS only build on windows
|
|
||||||
|
|||||||
@@ -9,9 +9,6 @@ import (
|
|||||||
"lash/internal/errx"
|
"lash/internal/errx"
|
||||||
share "lash/internal/shareLink"
|
share "lash/internal/shareLink"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/grandcat/zeroconf"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var versionFlag = flag.Bool("version", false, "Print out version and exit.")
|
var versionFlag = flag.Bool("version", false, "Print out version and exit.")
|
||||||
@@ -38,30 +35,9 @@ func main() {
|
|||||||
Handler: srv,
|
Handler: srv,
|
||||||
}
|
}
|
||||||
|
|
||||||
host, err := os.Hostname()
|
|
||||||
if err != nil {
|
|
||||||
errx.FatalPerror(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
zeroServer, err := zeroconf.Register(
|
|
||||||
host,
|
|
||||||
"_http._tcp",
|
|
||||||
"local.",
|
|
||||||
*port,
|
|
||||||
[]string{"path=/"},
|
|
||||||
nil,
|
|
||||||
)
|
|
||||||
defer zeroServer.Shutdown()
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
errx.FatalPerror(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO(vxc): Make this more portable
|
|
||||||
errx.Log("Your share link is http://%s.local:%d", host, *port)
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
11
go.mod
11
go.mod
@@ -1,14 +1,3 @@
|
|||||||
module lash
|
module lash
|
||||||
|
|
||||||
go 1.26.3
|
go 1.26.3
|
||||||
|
|
||||||
require github.com/google/uuid v1.6.0
|
|
||||||
|
|
||||||
require (
|
|
||||||
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
|
|
||||||
github.com/grandcat/zeroconf v1.0.0 // indirect
|
|
||||||
github.com/miekg/dns v1.1.27 // indirect
|
|
||||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
|
|
||||||
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa // indirect
|
|
||||||
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe // indirect
|
|
||||||
)
|
|
||||||
|
|||||||
26
go.sum
26
go.sum
@@ -1,26 +0,0 @@
|
|||||||
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
|
|
||||||
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
|
|
||||||
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=
|
|
||||||
github.com/grandcat/zeroconf v1.0.0 h1:uHhahLBKqwWBV6WZUDAT71044vwOTL+McW0mBJvo6kE=
|
|
||||||
github.com/grandcat/zeroconf v1.0.0/go.mod h1:lTKmG1zh86XyCoUeIHSA4FJMBwCJiQmGfcP2PdzytEs=
|
|
||||||
github.com/miekg/dns v1.1.27 h1:aEH/kqUzUxGJ/UHcEKdJY+ugH6WEzsEBBSPa8zuy1aM=
|
|
||||||
github.com/miekg/dns v1.1.27/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM=
|
|
||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
|
||||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=
|
|
||||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
|
||||||
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
|
|
||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
|
||||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
|
||||||
golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
|
||||||
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa h1:F+8P+gmewFQYRk6JoLQLwjBCTu3mcIURZfNkVweuRKA=
|
|
||||||
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
|
||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
|
||||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe h1:6fAMxZRR6sl1Uq8U61gxU+kPTs2tR8uOySCbBP7BN/M=
|
|
||||||
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
|
||||||
golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
|
|
||||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
package app
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"flag"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func GetFilePath() (string, error) {
|
|
||||||
fp := ""
|
|
||||||
|
|
||||||
for _, v := range flag.Args() {
|
|
||||||
if !strings.HasPrefix(v, "-") {
|
|
||||||
fp = v
|
|
||||||
return fp, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "", errors.New("not enough arguments")
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"lash"
|
"lash"
|
||||||
"lash/internal/errx"
|
"lash/internal/errx"
|
||||||
"lash/internal/handlers"
|
"lash/internal/handlers"
|
||||||
@@ -11,26 +12,29 @@ import (
|
|||||||
func New(ctx *lash.LashContext) http.Handler {
|
func New(ctx *lash.LashContext) http.Handler {
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
fp, err := GetFilePath()
|
|
||||||
if err != nil {
|
|
||||||
errx.FatalPerror(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
contents, err := os.ReadFile(fp)
|
|
||||||
if err != nil {
|
|
||||||
errx.FatalPerror(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
share := handlers.ShareData{
|
share := handlers.ShareData{
|
||||||
Version: lash.Version,
|
Version: lash.Version,
|
||||||
}
|
}
|
||||||
|
|
||||||
file := handlers.FileHandler{
|
file := handlers.FileHandler{
|
||||||
Ctx: ctx,
|
Ctx: ctx,
|
||||||
FileData: handlers.FileData{
|
}
|
||||||
|
|
||||||
|
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,
|
Contents: contents,
|
||||||
FileName: fp,
|
FileName: fp,
|
||||||
},
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
mux.HandleFunc("/", share.Handler)
|
mux.HandleFunc("/", share.Handler)
|
||||||
|
|||||||
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")
|
||||||
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
|
||||||
|
}
|
||||||
@@ -1,17 +1,18 @@
|
|||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"archive/zip"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"lash"
|
"lash"
|
||||||
|
"lash/internal/errx"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FileHandler struct {
|
type FileHandler struct {
|
||||||
Ctx *lash.LashContext
|
Ctx *lash.LashContext
|
||||||
FileData FileData
|
FileData []FileData
|
||||||
}
|
}
|
||||||
|
|
||||||
type FileData struct {
|
type FileData struct {
|
||||||
@@ -26,11 +27,6 @@ type ValidateRequest struct {
|
|||||||
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 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
|
||||||
@@ -45,27 +41,43 @@ func (h FileHandler) APIHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", h.FileData.FileName))
|
http.Redirect(w, r, fmt.Sprintf("/%s", lash.ShareLinkToken), http.StatusTemporaryRedirect)
|
||||||
w.Header().Set("Content-Type", "application/octet-stream")
|
|
||||||
w.Header().Set("Content-Length", strconv.Itoa(len(h.FileData.Contents)))
|
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
w.Write(h.FileData.Contents)
|
|
||||||
sent++
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h FileHandler) FileHandler(w http.ResponseWriter, r *http.Request) {
|
func (h FileHandler) FileHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if sent >= h.Ctx.N && h.Ctx.N != -1 {
|
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.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", h.FileData.FileName))
|
filename := h.FileData[0].FileName
|
||||||
w.Header().Set("Content-Type", "application/octet-stream")
|
if len(h.FileData) > 1 {
|
||||||
w.Header().Set("Content-Length", strconv.Itoa(len(h.FileData.Contents)))
|
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.WriteHeader(http.StatusOK)
|
||||||
w.Write(h.FileData.Contents)
|
_, _ = 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++
|
sent++
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ type ShareData struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *ShareData) Handler(w http.ResponseWriter, r *http.Request) {
|
func (h *ShareData) Handler(w http.ResponseWriter, r *http.Request) {
|
||||||
// Although `Must` can fail since `Templates` is embeded these files will always exist.
|
|
||||||
tmpl := template.Must(template.ParseFS(lash.Templates, "templates/share.html"))
|
tmpl := template.Must(template.ParseFS(lash.Templates, "templates/share.html"))
|
||||||
|
|
||||||
if err := tmpl.ExecuteTemplate(w, "share.html", h); err != nil {
|
if err := tmpl.ExecuteTemplate(w, "share.html", h); err != nil {
|
||||||
|
|||||||
8
lash.go
8
lash.go
@@ -2,9 +2,8 @@ package lash
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
|
"lash/internal/generator"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type LashContext struct {
|
type LashContext struct {
|
||||||
@@ -18,7 +17,6 @@ var Templates embed.FS
|
|||||||
//go:embed version
|
//go:embed version
|
||||||
var Version string
|
var Version string
|
||||||
|
|
||||||
// TODO(vxc): Replace this with custom token generator
|
var Token string = generator.Generate(5)
|
||||||
var Token string = uuid.New().String()
|
|
||||||
|
|
||||||
var ShareLinkToken string = uuid.New().String()
|
var ShareLinkToken string = generator.Generate(8)
|
||||||
|
|||||||
Reference in New Issue
Block a user