Browse Source

auto update script executed

Toby Chui 2 years ago
parent
commit
67650545ac
5 changed files with 54 additions and 2 deletions
  1. 1 0
      api.go
  2. 13 0
      cert.go
  3. 22 1
      mod/tlscert/tlscert.go
  4. BIN
      sys.db
  5. 18 1
      web/components/cert.html

+ 1 - 0
api.go

@@ -24,4 +24,5 @@ func initAPIs() {
 	http.HandleFunc("/cert/upload", handleCertUpload)
 	http.HandleFunc("/cert/list", handleListCertificate)
 	http.HandleFunc("/cert/checkDefault", handleDefaultCertCheck)
+	http.HandleFunc("/cert/delete", handleCertRemove)
 }

+ 13 - 0
cert.go

@@ -174,3 +174,16 @@ func handleCertUpload(w http.ResponseWriter, r *http.Request) {
 	// send response
 	fmt.Fprintln(w, "File upload successful!")
 }
+
+//Handle cert remove
+func handleCertRemove(w http.ResponseWriter, r *http.Request) {
+	domain, err := utils.PostPara(r, "domain")
+	if err != nil {
+		sendErrorResponse(w, "invalid domain given")
+		return
+	}
+	err = tlsCertManager.RemoveCert(domain)
+	if err != nil {
+		sendErrorResponse(w, err.Error())
+	}
+}

+ 22 - 1
mod/tlscert/tlscert.go

@@ -110,8 +110,29 @@ func (m *Manager) DefaultCertExistsSep() (bool, bool) {
 	return utils.FileExists(filepath.Join(m.CertStore, "default.crt")), utils.FileExists(filepath.Join(m.CertStore, "default.key"))
 }
 
+//Delete the cert if exists
+func (m *Manager) RemoveCert(domain string) error {
+	pubKey := filepath.Join(m.CertStore, domain+".crt")
+	priKey := filepath.Join(m.CertStore, domain+".key")
+	if utils.FileExists(pubKey) {
+		err := os.Remove(pubKey)
+		if err != nil {
+			return err
+		}
+	}
+
+	if utils.FileExists(priKey) {
+		err := os.Remove(priKey)
+		if err != nil {
+			return err
+		}
+	}
+
+	return nil
+}
+
 //Check if the given file is a valid TLS file
-func isValidTLSFile(file io.Reader) bool {
+func IsValidTLSFile(file io.Reader) bool {
 	// Read the contents of the uploaded file
 	contents, err := io.ReadAll(file)
 	if err != nil {

BIN
sys.db


+ 18 - 1
web/components/cert.html

@@ -68,6 +68,23 @@
     var uploadPendingPublicKey = undefined;
     var uploadPendingPrivateKey = undefined;
 
+    //Delete the certificate by its domain
+    function deleteCertificate(domain){
+        $.ajax({
+            url: "/cert/delete",
+            method: "POST",
+            data: {domain: domain},
+            success: function(data){
+                if (data.error != undefined){
+                    alert(data.error);
+                }else{
+                    initManagedDomainCertificateList();
+                }
+            }
+        })
+    }
+
+    //List the stored certificates
     function initManagedDomainCertificateList(){
         $("#certifiedDomainList").html("");
         $.get("/cert/list?date=true", function(data){
@@ -78,7 +95,7 @@
                     $("#certifiedDomainList").append(`<tr>
                     <td>${entry.Domain}</td>
                     <td>${entry.LastModifiedDate}</td>
-                    <td><button title="Delete key-pair" class="ui mini basic red icon button"><i class="ui red trash icon"></i></button></td>
+                    <td><button title="Delete key-pair" class="ui mini basic red icon button" onclick="deleteCertificate('${entry.Domain}');"><i class="ui red trash icon"></i></button></td>
                     </tr>`);
                 })
             }