Skip to content

Instantly share code, notes, and snippets.

@CapacitorSet
Created February 19, 2017 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CapacitorSet/ef51a3d84c52633782178fea3c072b48 to your computer and use it in GitHub Desktop.
Save CapacitorSet/ef51a3d84c52633782178fea3c072b48 to your computer and use it in GitHub Desktop.
// A one-time server. It serves a file with the format "uuid.7z.dat" and deletes it after 5 minutes since the first access.
/*
Copyright 2017 CapacitorSet
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"net/http"
"os/exec"
"regexp"
)
func handler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
regex := regexp.MustCompile("/[0-9A-Fa-f-]+\\.7z\\.dat")
if !regex.Match([]byte(r.URL.Path)) {
http.Error(w, "Invalid path", 400)
return
}
path := "/tmp" + r.URL.Path
http.ServeFile(w, r, path)
exec.Command("bash", "-c", "echo 'rm "+path+"' | at now + 5 minutes").Run()
}
func main() {
http.HandleFunc("/", handler)
err := http.ListenAndServe(":9000", nil)
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment