2023-02-26 10:02:30 +00:00
|
|
|
package certs
|
|
|
|
|
|
|
|
import (
|
2023-02-26 13:06:15 +00:00
|
|
|
"encoding/json"
|
2023-02-26 10:02:30 +00:00
|
|
|
"log"
|
2023-02-26 13:06:15 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2023-02-26 13:35:07 +00:00
|
|
|
"time"
|
2023-02-26 10:02:30 +00:00
|
|
|
|
2023-02-26 13:35:07 +00:00
|
|
|
"github.com/go-acme/lego/v4/certcrypto"
|
2023-02-26 10:02:30 +00:00
|
|
|
"github.com/go-acme/lego/v4/certificate"
|
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/lego"
|
|
|
|
"local-ip.sh/xip"
|
|
|
|
)
|
|
|
|
|
|
|
|
type certsClient struct {
|
2023-02-26 13:06:15 +00:00
|
|
|
legoClient *lego.Client
|
|
|
|
lastCertificate *certificate.Resource
|
2023-02-26 10:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *certsClient) RequestCertificate() {
|
2023-02-26 13:06:15 +00:00
|
|
|
log.Println("requesting a certificate")
|
|
|
|
if c.lastCertificate != nil {
|
2023-02-26 13:35:07 +00:00
|
|
|
certificates, err := certcrypto.ParsePEMBundle(c.lastCertificate.Certificate)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
x509Cert := certificates[0]
|
|
|
|
timeLeft := x509Cert.NotAfter.Sub(time.Now().UTC())
|
|
|
|
if timeLeft > time.Hour*24*30 {
|
|
|
|
log.Printf("%d days left before expiration, will not renew", int(timeLeft.Hours()/24))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.renewCertificate()
|
2023-02-26 13:06:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-26 10:02:30 +00:00
|
|
|
certificates, err := c.legoClient.Certificate.Obtain(certificate.ObtainRequest{
|
|
|
|
Domains: []string{"*.local-ip.sh"},
|
|
|
|
Bundle: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-02-26 13:06:15 +00:00
|
|
|
c.lastCertificate = certificates
|
|
|
|
|
|
|
|
persistFiles(certificates)
|
|
|
|
log.Printf("%#v\n", certificates)
|
|
|
|
}
|
|
|
|
|
2023-02-26 13:35:07 +00:00
|
|
|
func (c *certsClient) renewCertificate() {
|
2023-02-26 13:06:15 +00:00
|
|
|
log.Println("renewing currently existing certificate")
|
|
|
|
certificates, err := c.legoClient.Certificate.Renew(*c.lastCertificate, true, false, "")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.lastCertificate = certificates
|
|
|
|
|
|
|
|
persistFiles(certificates)
|
|
|
|
log.Printf("%#v\n", certificates)
|
|
|
|
}
|
|
|
|
|
|
|
|
func persistFiles(certificates *certificate.Resource) {
|
|
|
|
os.WriteFile("/certs/server.pem", certificates.Certificate, 0o644)
|
|
|
|
os.WriteFile("/certs/server.key", certificates.PrivateKey, 0o644)
|
|
|
|
jsonBytes, err := json.MarshalIndent(certificates, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
os.WriteFile("/certs/output.json", jsonBytes, 0o644)
|
2023-02-26 10:02:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewCertsClient(xip *xip.Xip, user *Account) *certsClient {
|
|
|
|
config := lego.NewConfig(user)
|
|
|
|
config.CADirURL = caDirUrl
|
|
|
|
legoClient, err := lego.NewClient(config)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
provider := newProviderLocalIp(xip)
|
2023-02-26 13:06:15 +00:00
|
|
|
legoClient.Challenge.SetDNS01Provider(provider, dns01.AddRecursiveNameservers([]string{"1.1.1.1:53", "8.8.8.8:53"}), dns01.DisableCompletePropagationRequirement())
|
|
|
|
|
|
|
|
lastCertificate := getLastCertificate(legoClient)
|
2023-02-26 10:02:30 +00:00
|
|
|
|
|
|
|
return &certsClient{
|
|
|
|
legoClient,
|
2023-02-26 13:06:15 +00:00
|
|
|
lastCertificate,
|
2023-02-26 10:02:30 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-26 13:06:15 +00:00
|
|
|
|
|
|
|
func getLastCertificate(legoClient *lego.Client) *certificate.Resource {
|
|
|
|
jsonBytes, err := os.ReadFile("/certs/output.json")
|
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "no such file or directory") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
log.Println(err)
|
|
|
|
log.Println("falling back to getting a brand new cert")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-12 21:25:39 +00:00
|
|
|
lastCertificate := &certificate.Resource{}
|
2023-02-26 13:06:15 +00:00
|
|
|
err = json.Unmarshal(jsonBytes, lastCertificate)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
log.Println("falling back to getting a brand new cert")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
lastCertificate, err = legoClient.Certificate.Get(lastCertificate.CertURL, true)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
log.Println("falling back to getting a brand new cert")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastCertificate
|
|
|
|
}
|