Alan Yeung 1 year ago
parent
commit
f93f3c0c83
2 changed files with 15 additions and 7 deletions
  1. 2 0
      acme.go
  2. 13 7
      mod/acme/acme.go

+ 2 - 0
acme.go

@@ -39,6 +39,7 @@ func isPortInUse(port int) bool {
 	return false // Port is not in use
 }
 
+// init the new ACME instance
 func initACME() *acme.ACMEHandler {
 	log.Println("Start initializing ACME")
 	rand.Seed(time.Now().UnixNano())
@@ -53,6 +54,7 @@ func initACME() *acme.ACMEHandler {
 	return acme.NewACME("[email protected]", "https://acme-staging-v02.api.letsencrypt.org/directory", strconv.Itoa(port))
 }
 
+// create the special routing rule for ACME
 func acmeRegisterSpecialRoutingRule() {
 	log.Println("Assigned temporary port:" + acmeHandler.Getport())
 

+ 13 - 7
mod/acme/acme.go

@@ -67,20 +67,23 @@ func NewACME(email string, acmeServer string, port string) *ACMEHandler {
 func (a *ACMEHandler) ObtainCert(domains []string, certificateName string) (bool, error) {
 	log.Println("Obtaining certificate...")
 
+	// generate private key
 	privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
 	if err != nil {
 		log.Println(err)
 		return false, err
 	}
 
-	log.Println(a.acmeServer)
+	// create a admin user for our new generation
 	adminUser := ACMEUser{
 		Email: a.email,
 		key:   privateKey,
 	}
 
+	// create config
 	config := lego.NewConfig(&adminUser)
 
+	// setup who is the issuer and the key type
 	config.CADirURL = a.acmeServer
 	config.Certificate.KeyType = certcrypto.RSA2048
 
@@ -90,6 +93,7 @@ func (a *ACMEHandler) ObtainCert(domains []string, certificateName string) (bool
 		return false, err
 	}
 
+	// setup how to receive challenge
 	err = client.Challenge.SetHTTP01Provider(http01.NewProviderServer("", a.port))
 	if err != nil {
 		log.Println(err)
@@ -104,6 +108,7 @@ func (a *ACMEHandler) ObtainCert(domains []string, certificateName string) (bool
 	}
 	adminUser.Registration = reg
 
+	// obtain the certificate
 	request := certificate.ObtainRequest{
 		Domains: domains,
 		Bundle:  true,
@@ -115,7 +120,7 @@ func (a *ACMEHandler) ObtainCert(domains []string, certificateName string) (bool
 	}
 
 	// Each certificate comes back with the cert bytes, the bytes of the client's
-	// private key, and a certificate URL. SAVE THESE TO DISK.
+	// private key, and a certificate URL.
 	err = ioutil.WriteFile("./certs/"+certificateName+".crt", certificates.Certificate, 0777)
 	if err != nil {
 		log.Println(err)
@@ -131,7 +136,11 @@ func (a *ACMEHandler) ObtainCert(domains []string, certificateName string) (bool
 }
 
 // CheckCertificate returns a list of domains that are in expired certificates.
+// It will return all domains that is in expired certificates
+// *** if there is a vaild certificate contains the domain and there is a expired certificate contains the same domain
+// it will said expired as well!
 func (a *ACMEHandler) CheckCertificate() []string {
+	// read from dir
 	filenames, err := os.ReadDir("./certs/")
 
 	expiredCerts := []string{}
@@ -155,10 +164,9 @@ func (a *ACMEHandler) CheckCertificate() []string {
 				cert, err := x509.ParseCertificate(block.Bytes)
 				if err == nil {
 					elapsed := time.Since(cert.NotAfter)
-					// approxMonths := -int(elapsed.Hours() / (24 * 30.44))
-					// approxDays := -int(elapsed.Hours()/24) % 30
 					if elapsed > 0 {
-						// log.Println("Certificate", certFilepath, " expired")
+						// if it is expired then add it in
+						// make sure it's uniqueless
 						for _, dnsName := range cert.DNSNames {
 							if !contains(expiredCerts, dnsName) {
 								expiredCerts = append(expiredCerts, dnsName)
@@ -167,8 +175,6 @@ func (a *ACMEHandler) CheckCertificate() []string {
 						if !contains(expiredCerts, cert.Subject.CommonName) {
 							expiredCerts = append(expiredCerts, cert.Subject.CommonName)
 						}
-					} else {
-						// log.Println("Certificate", certFilepath, " will still be valid for the next ", approxMonths, "m", approxDays, "d")
 					}
 				}
 			}