The easy way to transfer files over the internet from your shell session, like copy and paste
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
1.8 KiB

package main
import (
7 years ago
"fmt"
"io/ioutil"
"net/http"
"strings"
"io"
"bytes"
)
var clipboard []byte
var filename string
func mainHandler(response http.ResponseWriter, request *http.Request) {
if request.Method == "GET" {
requestPath := strings.TrimPrefix(request.URL.Path, "/")
if len(requestPath) > 0 {
response.Header().Add("content-type", "text/plain")
fmt.Fprintf(response, `
#!/bin/bash
function wrapper {
filename="%s"
if [ ! -f "$filename" ]; then
echo "Error: $filename is not a file."
7 years ago
exit 1
fi
curl -s -X POST -H "X-File-Name: $filename" -H "Content-Type: application/octet-stream" --data-binary "@$filename" https://%s/
}
wrapper
`, requestPath, request.Host)
} else if filename != "" && len(clipboard) > 0 {
response.Header().Set("Content-Disposition", fmt.Sprintf("inline; filename=%s", filename))
response.Header().Add("Content-Type", "application/octet-stream")
io.Copy(response, bytes.NewBuffer(clipboard))
7 years ago
clipboard = make([]byte, 0)
filename = ""
} else {
7 years ago
response.Header().Add("content-type", "text/plain")
response.WriteHeader(500)
fmt.Fprint(response, "404 nothing in webclip.\n")
7 years ago
}
}
if request.Method == "POST" {
bodyBytes, err := ioutil.ReadAll(request.Body)
7 years ago
if err != nil {
response.Header().Add("content-type", "text/plain")
response.WriteHeader(500)
fmt.Fprint(response, "500 internal server error: could not read body.\n")
7 years ago
} else {
filename = request.Header.Get("X-File-Name")
clipboard = bodyBytes
response.WriteHeader(200)
fmt.Fprintf(response, "200 ok I got \"%s\" with %d bytes.\n\n", filename, len(clipboard))
}
}
}
func main() {
7 years ago
http.HandleFunc("/", mainHandler)
http.ListenAndServe(":8080", nil)
}