1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- package main
- import (
- "fmt"
- "io"
- "net/http"
- "os"
- "path/filepath"
- "imuslab.com/arozos/ReverseProxy/mod/utils"
- )
- func handleCertUpload(w http.ResponseWriter, r *http.Request) {
- // check if request method is POST
- if r.Method != "POST" {
- http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
- return
- }
- // get the key type
- keytype, err := utils.GetPara(r, "ktype")
- overWriteFilename := ""
- if err != nil {
- http.Error(w, "Not defined key type (pub / pri)", http.StatusBadRequest)
- return
- }
- // get the domain
- domain, err := utils.GetPara(r, "domain")
- if err != nil {
- //Assume localhost
- domain = "localhost"
- }
- if keytype == "pub" {
- overWriteFilename = domain + ".crt"
- } else if keytype == "pri" {
- overWriteFilename = domain + ".key"
- } else {
- http.Error(w, "Not supported keytype: "+keytype, http.StatusBadRequest)
- return
- }
- // parse multipart form data
- err = r.ParseMultipartForm(10 << 20) // 10 MB
- if err != nil {
- http.Error(w, "Failed to parse form data", http.StatusBadRequest)
- return
- }
- // get file from form data
- file, _, err := r.FormFile("file")
- if err != nil {
- http.Error(w, "Failed to get file", http.StatusBadRequest)
- return
- }
- defer file.Close()
- // create file in upload directory
- os.MkdirAll("./certs", 0775)
- f, err := os.Create(filepath.Join("./certs", overWriteFilename))
- if err != nil {
- http.Error(w, "Failed to create file", http.StatusInternalServerError)
- return
- }
- defer f.Close()
- // copy file contents to destination file
- _, err = io.Copy(f, file)
- if err != nil {
- http.Error(w, "Failed to save file", http.StatusInternalServerError)
- return
- }
- // send response
- fmt.Fprintln(w, "File upload successful!")
- }
|