Acme dont always reregister
This commit is contained in:
34
acme/acme.go
34
acme/acme.go
@@ -20,6 +20,7 @@ import (
|
|||||||
"github.com/go-acme/lego/v4/lego"
|
"github.com/go-acme/lego/v4/lego"
|
||||||
"github.com/go-acme/lego/v4/registration"
|
"github.com/go-acme/lego/v4/registration"
|
||||||
domainrouter "github.com/pablu23/domain-router"
|
domainrouter "github.com/pablu23/domain-router"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Acme struct {
|
type Acme struct {
|
||||||
@@ -32,7 +33,8 @@ type Acme struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type CertDomainStorage struct {
|
type CertDomainStorage struct {
|
||||||
Domains map[string]time.Time
|
IsUserRegistered bool
|
||||||
|
Domains map[string]time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetupAcme(config *domainrouter.Config) (*Acme, error) {
|
func SetupAcme(config *domainrouter.Config) (*Acme, error) {
|
||||||
@@ -107,6 +109,7 @@ func SetupAcme(config *domainrouter.Config) (*Acme, error) {
|
|||||||
renewTicker: time.NewTicker(d),
|
renewTicker: time.NewTicker(d),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isUserRegistered := false
|
||||||
_, err = os.Stat("data.json")
|
_, err = os.Stat("data.json")
|
||||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -121,6 +124,7 @@ func SetupAcme(config *domainrouter.Config) (*Acme, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
isUserRegistered = data.IsUserRegistered
|
||||||
|
|
||||||
mustRenew := false
|
mustRenew := false
|
||||||
for _, domain := range domains {
|
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{
|
request := certificate.ObtainRequest{
|
||||||
Domains: domains,
|
Domains: domains,
|
||||||
Bundle: true,
|
Bundle: true,
|
||||||
@@ -166,8 +188,10 @@ func SetupAcme(config *domainrouter.Config) (*Acme, error) {
|
|||||||
dataDomains[domain] = now
|
dataDomains[domain] = now
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// User registration is hella scuffed
|
||||||
data := CertDomainStorage{
|
data := CertDomainStorage{
|
||||||
Domains: dataDomains,
|
Domains: dataDomains,
|
||||||
|
IsUserRegistered: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := os.Create("data.json")
|
file, err := os.Create("data.json")
|
||||||
@@ -184,17 +208,11 @@ func SetupAcme(config *domainrouter.Config) (*Acme, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *Acme) RenewAcme() 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{
|
request := certificate.ObtainRequest{
|
||||||
Domains: a.domains,
|
Domains: a.domains,
|
||||||
Bundle: true,
|
Bundle: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
certificates, err := a.client.Certificate.Obtain(request)
|
certificates, err := a.client.Certificate.Obtain(request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -68,7 +68,10 @@ func main() {
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
server.Shutdown(ctx)
|
server.Shutdown(ctx)
|
||||||
|
log.Info().Msg("Http Server stopped")
|
||||||
|
log.Info().Msg("Stopping pipeline")
|
||||||
pipeline.Stop(ctx)
|
pipeline.Stop(ctx)
|
||||||
|
log.Info().Msg("Pipeline stopped")
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if config.Server.Ssl.Enabled {
|
if config.Server.Ssl.Enabled {
|
||||||
|
|||||||
Reference in New Issue
Block a user