diff --git a/src/acme.go b/src/acme.go index 8778384..a04a257 100644 --- a/src/acme.go +++ b/src/acme.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "fmt" "io" "log" @@ -114,3 +115,23 @@ func AcmeCheckAndHandleRenewCertificate(w http.ResponseWriter, r *http.Request) } } } + +// HandleACMEPreferredCA return the user preferred / default CA for new subdomain auto creation +func HandleACMEPreferredCA(w http.ResponseWriter, r *http.Request) { + ca, err := utils.PostPara(r, "set") + if err != nil { + //Return the current ca to user + prefCA := "Let's Encrypt" + sysdb.Read("acmepref", "prefca", &prefCA) + js, _ := json.Marshal(prefCA) + utils.SendJSONResponse(w, string(js)) + } else { + //Check if the CA is supported + acme.IsSupportedCA(ca) + //Set the new config + sysdb.Write("acmepref", "prefca", ca) + log.Println("Updating prefered ACME CA to " + ca) + utils.SendOK(w) + } + +} diff --git a/src/api.go b/src/api.go index be55b9a..5b52342 100644 --- a/src/api.go +++ b/src/api.go @@ -162,6 +162,7 @@ func initAPIs() { authRouter.HandleFunc("/api/acme/listExpiredDomains", acmeHandler.HandleGetExpiredDomains) authRouter.HandleFunc("/api/acme/obtainCert", AcmeCheckAndHandleRenewCertificate) authRouter.HandleFunc("/api/acme/autoRenew/enable", acmeAutoRenewer.HandleAutoRenewEnable) + authRouter.HandleFunc("/api/acme/autoRenew/ca", HandleACMEPreferredCA) authRouter.HandleFunc("/api/acme/autoRenew/email", acmeAutoRenewer.HandleACMEEmail) authRouter.HandleFunc("/api/acme/autoRenew/setDomains", acmeAutoRenewer.HandleSetAutoRenewDomains) authRouter.HandleFunc("/api/acme/autoRenew/listDomains", acmeAutoRenewer.HandleLoadAutoRenewDomains) diff --git a/src/mod/acme/acme.go b/src/mod/acme/acme.go index 01e0783..efbd325 100644 --- a/src/mod/acme/acme.go +++ b/src/mod/acme/acme.go @@ -361,8 +361,8 @@ func IsPortInUse(port int) bool { } +// Load cert information from json file func loadCertInfoJSON(filename string) (*CertificateInfoJSON, error) { - certInfoBytes, err := os.ReadFile(filename) if err != nil { return nil, err diff --git a/src/mod/acme/ca.go b/src/mod/acme/ca.go index 87f9fc4..b673170 100644 --- a/src/mod/acme/ca.go +++ b/src/mod/acme/ca.go @@ -49,3 +49,8 @@ func loadCAApiServerFromName(caName string) (string, error) { return val, nil } + +func IsSupportedCA(caName string) bool { + _, err := loadCAApiServerFromName(caName) + return err == nil +} diff --git a/src/start.go b/src/start.go index a0a1b3a..4dd4aa8 100644 --- a/src/start.go +++ b/src/start.go @@ -204,6 +204,8 @@ func startupSequence() { Obtaining certificates from ACME Server */ + //Create a table just to store acme related preferences + sysdb.NewTable("acmepref") acmeHandler = initACME() acmeAutoRenewer, err = acme.NewAutoRenewer("./conf/acme_conf.json", "./conf/certs/", int64(*acmeAutoRenewInterval), acmeHandler) if err != nil { diff --git a/src/web/components/cert.html b/src/web/components/cert.html index 77cea78..1f2ab4c 100644 --- a/src/web/components/cert.html +++ b/src/web/components/cert.html @@ -65,17 +65,20 @@
- - - - - - - - - - -
DomainLast UpdateExpire AtRemove
+
+ + + + + + + + + + +
DomainLast UpdateExpire AtRemove
+
+
@@ -90,6 +93,7 @@

The default CA to use when create a new subdomain proxy endpoint with TLS certificate

+
+
+ + +

-
Certificate Renew / Generation (ACME) Settings
+
+

+ +
+ Disabled +
Auto-Renewer Status
+
+

+

This tool provide you a graphical interface to setup auto certificate renew on your (sub)domains. You can also manually generate a certificate if one of your domain do not have certificate.

@@ -134,9 +150,60 @@ } + function initAcmeStatus(){ + //Initialize the current default CA options + $.get("/api/acme/autoRenew/email", function(data){ + $("#prefACMEEmail").val(data); + }); + + $.get("/api/acme/autoRenew/ca", function(data){ + $("#defaultCA").dropdown("set value", data); + }); + + $.get("/api/acme/autoRenew/enable", function(data){ + setACMEEnableStates(data); + }) + } + //Set the status of the acme enable icon + function setACMEEnableStates(enabled){ + $("#acmeAutoRenewerStatus").text(enabled?"Enabled":"Disabled"); + $("#acmeAutoRenewer").find("i").attr("class", enabled?"green circle icon":"red circle icon"); + } + initAcmeStatus(); + function saveDefaultCA(){ - //TODO: Add an endpoint to handle default CA set and load - alert("WIP"); + let newDefaultEmail = $("#prefACMEEmail").val().trim(); + let newDefaultCA = $("#defaultCA").dropdown("get value"); + + if (newDefaultEmail == ""){ + msgbox("Invalid acme email given", false); + return; + } + + $.ajax({ + url: "/api/acme/autoRenew/email", + method: "POST", + data: {"set": newDefaultEmail}, + success: function(data){ + if (data.error != undefined){ + msgbox(data.error, false); + } + } + }); + + $.ajax({ + url: "/api/acme/autoRenew/ca", + data: {"set": newDefaultCA}, + method: "POST", + success: function(data){ + if (data.error != undefined){ + msgbox(data.error, false); + } + } + }); + + msgbox("Settings updated"); + } //List the stored certificates diff --git a/src/web/components/rules.html b/src/web/components/rules.html index 065dc39..194ea7e 100644 --- a/src/web/components/rules.html +++ b/src/web/components/rules.html @@ -184,10 +184,15 @@ if (type == "subd" && $("#tls").checkbox("is checked")){ confirmBox("Request new SSL Cert for this subdomain?", function(choice){ if (choice == true){ + //Load the prefer CA from TLS page + let defaultCA = $("#defaultCA").dropdown("get value"); + if (defaultCA.trim() == ""){ + defaultCA = "Let's Encrypt"; + } //Get a new cert using ACME - msgbox("Requesting certificate via Let's Encrypt..."); + msgbox("Requesting certificate via " + defaultCA +"..."); console.log("Trying to get a new certificate via ACME"); - obtainCertificate(rootname); + obtainCertificate(rootname, defaultCA.trim()); }else{ msgbox("Proxy Endpoint Added"); } diff --git a/src/web/components/webserv.html b/src/web/components/webserv.html index d321823..a0e201f 100644 --- a/src/web/components/webserv.html +++ b/src/web/components/webserv.html @@ -9,7 +9,7 @@
Running -
Listening on :8081
+
Listen port :8081
diff --git a/src/web/snippet/acme.html b/src/web/snippet/acme.html index c358eae..20165b6 100644 --- a/src/web/snippet/acme.html +++ b/src/web/snippet/acme.html @@ -218,6 +218,11 @@ $("#enableToggleSucc").stop().finish().fadeIn("fast").delay(3000).fadeOut("fast"); } }); + + if (parent && parent.setACMEEnableStates){ + parent.setACMEEnableStates(enabled); + } + } //Render the domains table that exists in this zoraxy host