From e77d37bbaaceb15f9956eb810129cd9c094aa117 Mon Sep 17 00:00:00 2001 From: Jacky Date: Tue, 30 Apr 2024 10:26:02 +0800 Subject: [PATCH] feat: set renewal interval of certificates #343 --- app/src/views/preference/BasicSettings.vue | 8 ++++ app/src/views/preference/Preference.vue | 1 + app/src/views/preference/typedef.ts | 1 + internal/cert/auto_cert.go | 5 ++- settings/server.go | 48 +++++++++++----------- 5 files changed, 38 insertions(+), 25 deletions(-) diff --git a/app/src/views/preference/BasicSettings.vue b/app/src/views/preference/BasicSettings.vue index a2da91f7..4a902a3e 100644 --- a/app/src/views/preference/BasicSettings.vue +++ b/app/src/views/preference/BasicSettings.vue @@ -50,6 +50,14 @@ const errors: Record> = inject('errors') as Recor > + + + diff --git a/app/src/views/preference/Preference.vue b/app/src/views/preference/Preference.vue index 4147514f..02a876ed 100644 --- a/app/src/views/preference/Preference.vue +++ b/app/src/views/preference/Preference.vue @@ -21,6 +21,7 @@ const data = ref({ github_proxy: '', ca_dir: '', node_secret: '', + cert_renewal_interval: 7, }, nginx: { access_log_path: '', diff --git a/app/src/views/preference/typedef.ts b/app/src/views/preference/typedef.ts index 29216187..af3d5762 100644 --- a/app/src/views/preference/typedef.ts +++ b/app/src/views/preference/typedef.ts @@ -10,6 +10,7 @@ export interface Settings { github_proxy: string email: string ca_dir: string + cert_renewal_interval: number } nginx: { access_log_path: string diff --git a/internal/cert/auto_cert.go b/internal/cert/auto_cert.go index 6533a7ea..e8777f62 100644 --- a/internal/cert/auto_cert.go +++ b/internal/cert/auto_cert.go @@ -4,6 +4,7 @@ import ( "github.com/0xJacky/Nginx-UI/internal/logger" "github.com/0xJacky/Nginx-UI/internal/notification" "github.com/0xJacky/Nginx-UI/model" + "github.com/0xJacky/Nginx-UI/settings" "github.com/pkg/errors" "runtime" "strings" @@ -59,8 +60,8 @@ func renew(certModel *model.Cert) { notification.Error("Renew Certificate Error", strings.Join(certModel.Domains, ", ")) return } - if time.Now().Sub(cert.NotBefore).Hours()/24 < 7 { - // not between 1 week, ignore this certificate + if int(time.Now().Sub(cert.NotBefore).Hours()/24) < settings.ServerSettings.CertRenewalInterval { + // not after settings.ServerSettings.CertRenewalInterval, ignore return } diff --git a/settings/server.go b/settings/server.go index 2c972445..1ea47203 100644 --- a/settings/server.go +++ b/settings/server.go @@ -5,19 +5,20 @@ import ( ) type Server struct { - HttpHost string `json:"http_host" protected:"true"` - HttpPort string `json:"http_port" protected:"true"` - RunMode string `json:"run_mode" protected:"true"` - JwtSecret string `json:"jwt_secret" protected:"true"` - NodeSecret string `json:"node_secret" protected:"true"` - HTTPChallengePort string `json:"http_challenge_port"` - Email string `json:"email" protected:"true"` - Database string `json:"database" protected:"true"` - StartCmd string `json:"start_cmd" protected:"true"` - CADir string `json:"ca_dir" binding:"omitempty,url"` - Demo bool `json:"demo" protected:"true"` - PageSize int `json:"page_size" protected:"true"` - GithubProxy string `json:"github_proxy" binding:"omitempty,url"` + HttpHost string `json:"http_host" protected:"true"` + HttpPort string `json:"http_port" protected:"true"` + RunMode string `json:"run_mode" protected:"true"` + JwtSecret string `json:"jwt_secret" protected:"true"` + NodeSecret string `json:"node_secret" protected:"true"` + HTTPChallengePort string `json:"http_challenge_port"` + Email string `json:"email" protected:"true"` + Database string `json:"database" protected:"true"` + StartCmd string `json:"start_cmd" protected:"true"` + CADir string `json:"ca_dir" binding:"omitempty,url"` + Demo bool `json:"demo" protected:"true"` + PageSize int `json:"page_size" protected:"true"` + GithubProxy string `json:"github_proxy" binding:"omitempty,url"` + CertRenewalInterval int `json:"cert_renewal_interval" binging:"min=7,max=21"` } func (s *Server) GetCADir() string { @@ -33,14 +34,15 @@ func (s *Server) GetCADir() string { } var ServerSettings = Server{ - HttpHost: "0.0.0.0", - HttpPort: "9000", - RunMode: "debug", - HTTPChallengePort: "9180", - Database: "database", - StartCmd: "login", - Demo: false, - PageSize: 10, - CADir: "", - GithubProxy: "", + HttpHost: "0.0.0.0", + HttpPort: "9000", + RunMode: "debug", + HTTPChallengePort: "9180", + Database: "database", + StartCmd: "login", + Demo: false, + PageSize: 10, + CADir: "", + GithubProxy: "", + CertRenewalInterval: 7, }