Acme dont always reregister
This commit is contained in:
30
acme/acme.go
30
acme/acme.go
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user