diff options
| author | Julian Hurst <julian.hurst@digdash.com> | 2025-01-23 11:04:43 +0100 |
|---|---|---|
| committer | Julian Hurst <julian.hurst@digdash.com> | 2025-01-23 11:04:43 +0100 |
| commit | 70cf829b8258cec241dceeb9a02abee626935828 (patch) | |
| tree | 84cfc962a548f6c697bc5b97ae31d12c7ef82e22 | |
| parent | 6037a7e762a2d5015700b96b583a9ecbde9be263 (diff) | |
| download | box-70cf829b8258cec241dceeb9a02abee626935828.tar.gz | |
Support token, host and port flags
| -rw-r--r-- | main.go | 24 | ||||
| -rw-r--r-- | templates/index.html | 22 |
2 files changed, 37 insertions, 9 deletions
@@ -10,21 +10,24 @@ import ( "os" "path" "path/filepath" + "flag" ) //go:embed templates var tmplFS embed.FS + type BoxHandler struct { dataPath string + token string } -func serve(w http.ResponseWriter, views ...string) { +func serve(w http.ResponseWriter, token string, views ...string) { t, err := template.New("index.html").ParseFS(tmplFS, views...) if err != nil { log.Fatal(err) } - if err := t.Execute(w, nil); err != nil { + if err := t.Execute(w, token); err != nil { log.Fatal(err) } } @@ -32,13 +35,20 @@ func serve(w http.ResponseWriter, views ...string) { func (handler BoxHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: - serve(w, "templates/index.html") + serve(w, handler.token, "templates/index.html") return case http.MethodPost: + token := r.Header.Get("X-Upload-Token") + if token != handler.token { + log.Println("unauthorized") + w.WriteHeader(http.StatusUnauthorized) + return + } filename := filepath.Join(handler.dataPath, path.Base(r.URL.Path)) log.Printf("boxing %s\n", filename) f, err := os.Create(filename) if err != nil { + log.Println(err) fmt.Fprint(w, err.Error()) w.WriteHeader(http.StatusInternalServerError) return @@ -52,12 +62,18 @@ func (handler BoxHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } func main() { + host := flag.String("n", "", "The hostname to listen on") + port := flag.Int("p", 8080, "The port to listen on") + token := flag.String("t", "", "The token to use to protect uploads") + flag.Parse() boxHandler := BoxHandler { "data", + *token, } err := os.MkdirAll(boxHandler.dataPath, 0750) if err != nil { log.Fatal(err) } - log.Fatal(http.ListenAndServe(":8080", boxHandler)) + log.Printf("Listening on %s:%d", *host, *port) + log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", *host, *port), boxHandler)) } diff --git a/templates/index.html b/templates/index.html index 220c8c9..97f5eca 100644 --- a/templates/index.html +++ b/templates/index.html @@ -5,6 +5,7 @@ <title>box</title> <script> function upload() { + token = document.getElementById("token").value f = document.getElementById("file").files[0]; const xhr = new XMLHttpRequest(); xhr.upload.addEventListener("progress", (e) => { @@ -13,20 +14,31 @@ perc = Math.floor(ratio * 100); p.innerHTML = perc + "%"; }); + xhr.onreadystatechange = () => { + if (xhr.readyState === 4) { + if (xhr.status != 200) { + if (xhr.status != 0) { + document.getElementById("progress").innerHTML = "Error: " + xhr.status + " " + xhr.statusText; + } else { + document.getElementById("progress").innerHTML = "Error: unknown"; + } + } + } + } xhr.open("POST", "/" + f.name, true); + xhr.setRequestHeader("X-Upload-Token", token) xhr.send(f) - /*const response = await fetch("/" + name, { - method: "POST", - body: f, - });*/ } </script> </head> <body> <h1>Box</h1> <pre>Server for uploading files. Use the form here or send a POST request to /[filename] with the content of the file in the body.</pre> + {{ if ne . "" }} + <input type="text" id="token"/> + {{end}} <input type="file" id="file"/><br/><br/> - <button type="button" onclick="upload()">Upload</button><br/> + <button type="button" onclick="upload()">Upload</button> <span id="progress"></span> </body> </html> |
