diff --git a/TODO b/TODO index d50c361..442904d 100644 --- a/TODO +++ b/TODO @@ -1,7 +1,6 @@ maint: clean up source code maint: document more +maint: make the colors more cross platform feat: improve flags feat: replace uuid dep with custom id generator feat: multiple files -feat: share link -maint: make the colors more cross platform diff --git a/cmd/lash/main.go b/cmd/lash/main.go index 990f0d4..4d5571c 100644 --- a/cmd/lash/main.go +++ b/cmd/lash/main.go @@ -7,10 +7,12 @@ import ( "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.") func main() { @@ -24,6 +26,7 @@ func main() { srv := app.New() // TODO(vxc): Make this more portable + 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://0.0.0.0:%d", *port) diff --git a/internal/app/routes.go b/internal/app/routes.go index 41858d8..ca75ca7 100644 --- a/internal/app/routes.go +++ b/internal/app/routes.go @@ -32,6 +32,7 @@ func New() http.Handler { mux.HandleFunc("/", share.Handler) mux.HandleFunc("/api/receive-token", file.APIHandler) + mux.HandleFunc("/"+lash.ShareLinkToken, file.FileHandler) return mux } diff --git a/internal/handlers/file.go b/internal/handlers/file.go index 07ce2c8..53d802a 100644 --- a/internal/handlers/file.go +++ b/internal/handlers/file.go @@ -40,3 +40,13 @@ func (h FileData) APIHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Write(h.Contents) } + +func (h FileData) FileHandler(w http.ResponseWriter, r *http.Request) { + // Send the file over as a stream of bytes + w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", h.FileName)) + 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) +} diff --git a/internal/shareLink/shareLink.go b/internal/shareLink/shareLink.go new file mode 100644 index 0000000..2e04af6 --- /dev/null +++ b/internal/shareLink/shareLink.go @@ -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() +} diff --git a/lash.go b/lash.go index 9b763bc..70741a8 100644 --- a/lash.go +++ b/lash.go @@ -14,3 +14,5 @@ var Version string // TODO(vxc): Replace this with custom token generator var Token string = uuid.New().String() + +var ShareLinkToken string = uuid.New().String()