This commit is contained in:
1 parent
4cce5754d3
commit
0f777d3e68
8 files changed
+333
-213
No files matched your search
@@ -0,0 +1,47 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"scm/utils"
|
||||
)
|
||||
|
||||
func CertKeyDownloadZip(w http.ResponseWriter, r *http.Request) {
|
||||
query := r.URL.Query()
|
||||
domain := query.Get("domain")
|
||||
if domain == "" {
|
||||
http.Error(w, "domain parameter is required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Пути к файлам сертификата и ключа
|
||||
certPath := filepath.Join(os.Getenv("CERTS_PATH"), domain, domain+".crt")
|
||||
keyPath := filepath.Join(os.Getenv("CERTS_PATH"), domain, domain+".key")
|
||||
|
||||
// Проверяем существование файлов
|
||||
if _, err := os.Stat(certPath); os.IsNotExist(err) {
|
||||
http.Error(w, "Certificate file not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
if _, err := os.Stat(keyPath); os.IsNotExist(err) {
|
||||
http.Error(w, "Key file not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
// Создаем ZIP архив
|
||||
zipBuffer, err := utils.CreateDomainZip(certPath, keyPath, domain)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Error creating zip: %v", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Устанавливаем заголовки для скачивания
|
||||
w.Header().Set("Content-Type", "application/zip")
|
||||
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s-cert-key.zip\"", domain))
|
||||
w.Header().Set("Content-Length", fmt.Sprintf("%d", zipBuffer.Len()))
|
||||
|
||||
// Отправляем ZIP архив
|
||||
w.Write(zipBuffer.Bytes())
|
||||
}
|
||||
+193
-174
@@ -62,13 +62,15 @@
|
||||
</button>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<button
|
||||
<a
|
||||
type="button"
|
||||
class="btn btn-primary"
|
||||
id="downloadrootcert"
|
||||
href="/api/certs/download/?domain=root"
|
||||
download="root.zip"
|
||||
>
|
||||
Download Root Certificate
|
||||
</button>
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
@@ -91,71 +93,78 @@
|
||||
></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="domain"
|
||||
class="form-control"
|
||||
placeholder="example.domain"
|
||||
/>
|
||||
<label for="domain">Domain</label>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="commonname"
|
||||
class="form-control"
|
||||
placeholder="commonname"
|
||||
/>
|
||||
<label for="commonname">Common Name</label>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="organizationname"
|
||||
class="form-control"
|
||||
placeholder="organizationname"
|
||||
/>
|
||||
<label for="organizationname"
|
||||
>Organization Name</label
|
||||
>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="organizationunit"
|
||||
class="form-control"
|
||||
placeholder="organizationunit"
|
||||
/>
|
||||
<label for="organizationunit"
|
||||
>Organization Unit</label
|
||||
>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="password"
|
||||
id="password"
|
||||
class="form-control"
|
||||
placeholder="example.domain"
|
||||
/>
|
||||
<label for="password">RootCA password</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button
|
||||
type="button"
|
||||
data-bs-dismiss="modal"
|
||||
class="btn btn-secondary"
|
||||
>
|
||||
Close
|
||||
</button>
|
||||
<button
|
||||
id="createcert"
|
||||
type="button"
|
||||
class="btn btn-primary"
|
||||
>
|
||||
Create
|
||||
</button>
|
||||
<form id="create_certificate_form">
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="domain"
|
||||
class="form-control"
|
||||
placeholder="example.domain"
|
||||
required
|
||||
/>
|
||||
<label for="domain">Domain</label>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="commonname"
|
||||
class="form-control"
|
||||
placeholder="commonname"
|
||||
required
|
||||
/>
|
||||
<label for="commonname">Common Name</label>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="organizationname"
|
||||
class="form-control"
|
||||
placeholder="organizationname"
|
||||
required
|
||||
/>
|
||||
<label for="organizationname"
|
||||
>Organization Name</label
|
||||
>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="organizationunit"
|
||||
class="form-control"
|
||||
placeholder="organizationunit"
|
||||
required
|
||||
/>
|
||||
<label for="organizationunit"
|
||||
>Organization Unit</label
|
||||
>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="password"
|
||||
id="password"
|
||||
class="form-control"
|
||||
placeholder="example.domain"
|
||||
required
|
||||
/>
|
||||
<label for="password">RootCA password</label>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button
|
||||
type="button"
|
||||
data-bs-dismiss="modal"
|
||||
class="btn btn-secondary"
|
||||
>
|
||||
Close
|
||||
</button>
|
||||
<button
|
||||
id="createcert"
|
||||
type="submit"
|
||||
class="btn btn-primary"
|
||||
>
|
||||
Create
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -315,114 +324,124 @@
|
||||
</h5>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="rootcommonname"
|
||||
class="form-control"
|
||||
placeholder="example.domain"
|
||||
/>
|
||||
<label for="rootcommonname">Common Name</label>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="rootorganizationname"
|
||||
class="form-control"
|
||||
placeholder="organizationname"
|
||||
/>
|
||||
<label for="rootorganizationname"
|
||||
>Organization Name</label
|
||||
<form id="createRootCertificateForm">
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="rootcommonname"
|
||||
class="form-control"
|
||||
placeholder="example.domain"
|
||||
required
|
||||
/>
|
||||
<label for="rootcommonname">Common Name</label>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="rootorganizationname"
|
||||
class="form-control"
|
||||
placeholder="organizationname"
|
||||
required
|
||||
/>
|
||||
<label for="rootorganizationname"
|
||||
>Organization Name</label
|
||||
>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="rootorganizationalunit"
|
||||
class="form-control"
|
||||
placeholder="organizationalunit"
|
||||
required
|
||||
/>
|
||||
<label for="rootorganizationalunit"
|
||||
>Organizational Unit</label
|
||||
>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="rootCountry"
|
||||
class="form-control"
|
||||
placeholder="Country"
|
||||
required
|
||||
/>
|
||||
<label for="rootCountry">Country</label>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="rootLocality"
|
||||
class="form-control"
|
||||
placeholder="Asia/Novosibirsk"
|
||||
required
|
||||
/>
|
||||
<label for="rootLocality">Locality</label>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="rootProvince"
|
||||
class="form-control"
|
||||
placeholder="Tomsk Oblast"
|
||||
required
|
||||
/>
|
||||
<label for="rootProvince">Province</label>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="rootStreetAddress"
|
||||
class="form-control"
|
||||
placeholder="Tomsk Oblast, Russia"
|
||||
required
|
||||
/>
|
||||
<label for="rootStreetAddress"
|
||||
>Street address</label
|
||||
>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="rootPostalCode"
|
||||
class="form-control"
|
||||
placeholder="646037"
|
||||
required
|
||||
/>
|
||||
<label for="rootPostalCode">Postal code</label>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="number"
|
||||
id="rootValidityYears"
|
||||
class="form-control"
|
||||
placeholder="10"
|
||||
required
|
||||
/>
|
||||
<label for="rootValidityYears"
|
||||
>Validity years</label
|
||||
>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="password"
|
||||
id="rootkeypassword"
|
||||
class="form-control"
|
||||
placeholder="Password"
|
||||
required
|
||||
/>
|
||||
<label for="rootkeypassword">Password</label>
|
||||
</div>
|
||||
<button
|
||||
id="createRootCrt"
|
||||
type="submit"
|
||||
class="btn btn-primary"
|
||||
style="width: 100%"
|
||||
>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="rootorganizationalunit"
|
||||
class="form-control"
|
||||
placeholder="organizationalunit"
|
||||
/>
|
||||
<label for="rootorganizationalunit"
|
||||
>Organizational Unit</label
|
||||
>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="rootCountry"
|
||||
class="form-control"
|
||||
placeholder="Country"
|
||||
/>
|
||||
<label for="rootCountry">Country</label>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="rootLocality"
|
||||
class="form-control"
|
||||
placeholder="Asia/Novosibirsk"
|
||||
/>
|
||||
<label for="rootLocality">Locality</label>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="rootProvince"
|
||||
class="form-control"
|
||||
placeholder="Tomsk Oblast"
|
||||
/>
|
||||
<label for="rootProvince">Province</label>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="rootStreetAddress"
|
||||
class="form-control"
|
||||
placeholder="Tomsk Oblast, Russia"
|
||||
/>
|
||||
<label for="rootStreetAddress"
|
||||
>Street address</label
|
||||
>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="text"
|
||||
id="rootPostalCode"
|
||||
class="form-control"
|
||||
placeholder="646037"
|
||||
/>
|
||||
<label for="rootPostalCode">Postal code</label>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="number"
|
||||
id="rootValidityYears"
|
||||
class="form-control"
|
||||
placeholder="10"
|
||||
/>
|
||||
<label for="rootValidityYears"
|
||||
>Validity years</label
|
||||
>
|
||||
</div>
|
||||
<div class="form-floating mb-3">
|
||||
<input
|
||||
type="password"
|
||||
id="rootkeypassword"
|
||||
class="form-control"
|
||||
placeholder="Password"
|
||||
/>
|
||||
<label for="rootkeypassword">Password</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button
|
||||
id="createRootCrt"
|
||||
type="button"
|
||||
class="btn btn-primary"
|
||||
style="width: 100%"
|
||||
>
|
||||
Create
|
||||
</button>
|
||||
Create
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -34,9 +34,9 @@ const renderCards = () => {
|
||||
>
|
||||
Cert
|
||||
</button>
|
||||
<button class="btn btn-primary" style="width: 100%">
|
||||
<a class="btn btn-primary" style="width: 100%" href="${window.location.origin + "/api/certs/download/?domain=" + cert.certinfo.DNSNames[0]}" download="${cert.certinfo.DNSNames[0]}.zip">
|
||||
Download
|
||||
</button>
|
||||
</a>
|
||||
<button class="btn btn-danger delcert" id="${cert.id}" style="width: 100%">
|
||||
Delete
|
||||
</button>
|
||||
|
||||
@@ -3,42 +3,45 @@ const commonname = document.querySelector("#commonname");
|
||||
const organizationname = document.querySelector("#organizationname");
|
||||
const password = document.querySelector("#password");
|
||||
const organizationunit = document.querySelector("#organizationunit");
|
||||
const createcert = document.querySelector("#createcert");
|
||||
const create_certificate_form = document.querySelector(
|
||||
"#create_certificate_form",
|
||||
);
|
||||
|
||||
createcert.addEventListener("click", () => {
|
||||
const domainValue = domain.value;
|
||||
const commonnameValue = commonname.value;
|
||||
const organizationnameValue = organizationname.value;
|
||||
const passwordValue = password.value;
|
||||
const organizationunitValue = organizationunit.value;
|
||||
create_certificate_form.addEventListener("submit", (event) => {
|
||||
event.preventDefault();
|
||||
const domainValue = domain.value;
|
||||
const commonnameValue = commonname.value;
|
||||
const organizationnameValue = organizationname.value;
|
||||
const passwordValue = password.value;
|
||||
const organizationunitValue = organizationunit.value;
|
||||
|
||||
fetch(window.location.origin + "/api/certs/create", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
dns: [domainValue],
|
||||
commonname: commonnameValue,
|
||||
organizationname: [organizationnameValue],
|
||||
organizationunit: [organizationunitValue],
|
||||
password: passwordValue,
|
||||
}),
|
||||
fetch(window.location.origin + "/api/certs/create", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
dns: [domainValue],
|
||||
commonname: commonnameValue,
|
||||
organizationname: [organizationnameValue],
|
||||
organizationunit: [organizationunitValue],
|
||||
password: passwordValue,
|
||||
}),
|
||||
})
|
||||
.then((data) => data.json())
|
||||
.then((jsondata) => {
|
||||
if (jsondata.status === "success") {
|
||||
getCerts();
|
||||
var createmodal = bootstrap.Modal.getOrCreateInstance(
|
||||
document.getElementById("create_certificate"),
|
||||
);
|
||||
createmodal.hide();
|
||||
alert("Certificate created successfully");
|
||||
} else {
|
||||
alert("Failed to create certificate:", jsondata.error);
|
||||
}
|
||||
})
|
||||
.then((data) => data.json())
|
||||
.then((jsondata) => {
|
||||
if (jsondata.status === "success") {
|
||||
getCerts();
|
||||
var createmodal = bootstrap.Modal.getOrCreateInstance(
|
||||
document.getElementById("create_certificate"),
|
||||
);
|
||||
createmodal.hide();
|
||||
alert("Certificate created successfully");
|
||||
} else {
|
||||
alert("Failed to create certificate:", jsondata.error);
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
alert("An error occurred:", error);
|
||||
});
|
||||
.catch((error) => {
|
||||
alert("An error occurred:", error);
|
||||
});
|
||||
});
|
||||
@@ -1,7 +1,9 @@
|
||||
const createRootCertificateModal = bootstrap.Modal.getOrCreateInstance(
|
||||
document.querySelector("#createRootCertificate"),
|
||||
);
|
||||
const createRootCrtButton = document.querySelector("#createRootCrt");
|
||||
const createRootCertificateForm = document.querySelector(
|
||||
"#createRootCertificateForm",
|
||||
);
|
||||
|
||||
const rootcommonnameinput = document.querySelector("#rootcommonname");
|
||||
const rootorganizationinput = document.querySelector("#rootorganizationname");
|
||||
@@ -29,7 +31,8 @@ const getRootCrt = () => {
|
||||
|
||||
getRootCrt();
|
||||
|
||||
createRootCrtButton.addEventListener("click", () => {
|
||||
createRootCertificateForm.addEventListener("submit", (event) => {
|
||||
event.preventDefault();
|
||||
const newRootCertificate = {
|
||||
CommonName: rootcommonnameinput.value,
|
||||
Organization: [rootorganizationinput.value],
|
||||
|
||||
@@ -9,6 +9,7 @@ func Configure() {
|
||||
http.Handle("/", api.SpaHandler(http.FS(api.Frontend)))
|
||||
http.HandleFunc("/api/certs", api.Certs)
|
||||
http.HandleFunc("/api/certs/root", api.RootCert)
|
||||
http.HandleFunc("/api/certs/download/", api.CertKeyDownloadZip)
|
||||
http.HandleFunc("/api/certs/create", api.CreateCert)
|
||||
http.HandleFunc("/api/certs/delete", api.DeleteCert)
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"os"
|
||||
)
|
||||
|
||||
func AddFileToZipWithName(zipWriter *zip.Writer, filePath, fileName string) error {
|
||||
data, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
f, err := zipWriter.Create(fileName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = f.Write(data)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
)
|
||||
|
||||
func CreateDomainZip(certPath, keyPath, domain string) (*bytes.Buffer, error) {
|
||||
buf := new(bytes.Buffer)
|
||||
zipWriter := zip.NewWriter(buf)
|
||||
defer zipWriter.Close()
|
||||
|
||||
// Добавляем сертификат
|
||||
err := AddFileToZipWithName(zipWriter, certPath, domain+".crt")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Добавляем ключ
|
||||
err = AddFileToZipWithName(zipWriter, keyPath, domain+".key")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return buf, nil
|
||||
}
|
||||
Reference in new issue
Block a user