Acme dont always reregister

This commit is contained in:
Pablu23
2025-10-15 18:52:45 +02:00
parent ea8f84f0d7
commit d4d7d3e067
2 changed files with 29 additions and 8 deletions

View File

@@ -20,6 +20,7 @@ import (
"github.com/go-acme/lego/v4/lego"
"github.com/go-acme/lego/v4/registration"
domainrouter "github.com/pablu23/domain-router"
"github.com/rs/zerolog/log"
)
type Acme struct {
@@ -32,6 +33,7 @@ type Acme struct {
}
type CertDomainStorage struct {
IsUserRegistered bool
Domains map[string]time.Time
}
@@ -107,6 +109,7 @@ func SetupAcme(config *domainrouter.Config) (*Acme, error) {
renewTicker: time.NewTicker(d),
}
isUserRegistered := false
_, err = os.Stat("data.json")
if err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, err
@@ -121,6 +124,7 @@ func SetupAcme(config *domainrouter.Config) (*Acme, error) {
if err != nil {
return nil, err
}
isUserRegistered = data.IsUserRegistered
mustRenew := false
for _, domain := range domains {
@@ -140,6 +144,24 @@ func SetupAcme(config *domainrouter.Config) (*Acme, error) {
}
}
if !isUserRegistered {
log.Debug().Str("user", user.Email).Msg("Registering new User")
reg, err := client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
if err != nil {
return nil, err
}
user.Registration = reg
} else {
log.Debug().Str("user", user.Email).Msg("Resolving registration by Key")
reg, err := client.Registration.ResolveAccountByKey()
if err != nil {
return nil, err
}
user.Registration = reg
}
request := certificate.ObtainRequest{
Domains: domains,
Bundle: true,
@@ -166,8 +188,10 @@ func SetupAcme(config *domainrouter.Config) (*Acme, error) {
dataDomains[domain] = now
}
// User registration is hella scuffed
data := CertDomainStorage{
Domains: dataDomains,
IsUserRegistered: true,
}
file, err := os.Create("data.json")
@@ -184,17 +208,11 @@ func SetupAcme(config *domainrouter.Config) (*Acme, error) {
}
func (a *Acme) RenewAcme() error {
reg, err := a.client.Registration.Register(registration.RegisterOptions{TermsOfServiceAgreed: true})
if err != nil {
return err
}
a.user.Registration = reg
request := certificate.ObtainRequest{
Domains: a.domains,
Bundle: true,
}
certificates, err := a.client.Certificate.Obtain(request)
if err != nil {
return err

View File

@@ -68,7 +68,10 @@ func main() {
defer cancel()
server.Shutdown(ctx)
log.Info().Msg("Http Server stopped")
log.Info().Msg("Stopping pipeline")
pipeline.Stop(ctx)
log.Info().Msg("Pipeline stopped")
}()
if config.Server.Ssl.Enabled {