You can use it from command line with curl
like this
c:> curl -sSL -F [email protected] http://localhost:4500/sign -o project1_signed.exe --fail || echo failed
You can use it from command line with curl
like this
c:> curl -sSL -F [email protected] http://localhost:4500/sign -o project1_signed.exe --fail || echo failed
package main | |
import ( | |
"fmt" | |
"io" | |
"log" | |
"net/http" | |
"os" | |
"os/exec" | |
"path/filepath" | |
"strconv" | |
"strings" | |
"time" | |
) | |
func uploadHandler(w http.ResponseWriter, r *http.Request) { | |
if r.Method != "POST" { | |
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) | |
return | |
} | |
// limit upload up to 1024 MB | |
if err := r.ParseMultipartForm(1024 << 20); err != nil { | |
http.Error(w, err.Error(), http.StatusBadRequest) | |
return | |
} | |
exFile, err := os.Executable() | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusBadRequest) | |
return | |
} | |
exPath := filepath.Dir(exFile) | |
files := r.MultipartForm.File["file"] | |
for _, fileHeader := range files { | |
file, err := fileHeader.Open() | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
err = os.MkdirAll(filepath.Join(exPath, "uploads"), os.ModePerm) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
uploadFile := filepath.Base(fileHeader.Filename) | |
uploadPath := filepath.Join(exPath, "uploads", fmt.Sprintf("%d-%s", time.Now().UnixNano(), uploadFile)) | |
f, err := os.Create(uploadPath) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusBadRequest) | |
return | |
} | |
defer f.Close() | |
_, err = io.Copy(f, file) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
f.Close() | |
batFile, err := filepath.Abs(filepath.Join(exPath, "s.bat")) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
out, err := exec.Command("cmd.exe", "/c", batFile, uploadPath).Output() | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
outstr := string(out) | |
if !strings.Contains(outstr, "Succeed") { | |
http.Error(w, outstr, http.StatusInternalServerError) | |
return | |
} | |
w.Header().Set("Content-Disposition", "attachment; filename="+strconv.Quote(uploadFile)) | |
w.Header().Set("Content-Type", "application/octet-stream") | |
http.ServeFile(w, r, uploadPath) | |
os.Remove(uploadPath) | |
} | |
} | |
func main() { | |
mux := http.NewServeMux() | |
mux.HandleFunc("/sign", uploadHandler) | |
if err := http.ListenAndServe(":4500", mux); err != nil { | |
log.Fatal(err) | |
} | |
} |