mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
enhance: auto obtain cert
This commit is contained in:
parent
e260860adc
commit
e9d26ded1c
4 changed files with 70 additions and 74 deletions
|
@ -29,14 +29,10 @@ const columns = [{
|
|||
},
|
||||
search: true
|
||||
}, {
|
||||
title: () => $gettext('Domain'),
|
||||
dataIndex: 'domain',
|
||||
title: () => $gettext('Config Name'),
|
||||
dataIndex: 'filename',
|
||||
sorter: true,
|
||||
pithy: true,
|
||||
edit: {
|
||||
type: input
|
||||
},
|
||||
search: true
|
||||
pithy: true
|
||||
}, {
|
||||
title: () => $gettext('Auto Cert'),
|
||||
dataIndex: 'auto_cert',
|
||||
|
|
|
@ -364,6 +364,7 @@ func AddDomainToAutoCert(c *gin.Context) {
|
|||
}
|
||||
|
||||
err = certModel.Updates(&model.Cert{
|
||||
Name: name,
|
||||
AutoCert: model.AutoCertEnabled,
|
||||
})
|
||||
|
||||
|
|
|
@ -1,93 +1,97 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"github.com/0xJacky/Nginx-UI/server/pkg/nginx"
|
||||
"github.com/lib/pq"
|
||||
"os"
|
||||
"github.com/0xJacky/Nginx-UI/server/pkg/nginx"
|
||||
"github.com/lib/pq"
|
||||
"os"
|
||||
)
|
||||
|
||||
const (
|
||||
AutoCertEnabled = 1
|
||||
AutoCertDisabled = -1
|
||||
AutoCertEnabled = 1
|
||||
AutoCertDisabled = -1
|
||||
)
|
||||
|
||||
type CertDomains []string
|
||||
|
||||
type Cert struct {
|
||||
Model
|
||||
Name string `json:"name"`
|
||||
Domains pq.StringArray `json:"domains" gorm:"type:text[]"`
|
||||
Filename string `json:"filename"`
|
||||
SSLCertificatePath string `json:"ssl_certificate_path"`
|
||||
SSLCertificateKeyPath string `json:"ssl_certificate_key_path"`
|
||||
AutoCert int `json:"auto_cert"`
|
||||
Log string `json:"log"`
|
||||
Model
|
||||
Name string `json:"name"`
|
||||
Domains pq.StringArray `json:"domains" gorm:"type:text[]"`
|
||||
Filename string `json:"filename"`
|
||||
SSLCertificatePath string `json:"ssl_certificate_path"`
|
||||
SSLCertificateKeyPath string `json:"ssl_certificate_key_path"`
|
||||
AutoCert int `json:"auto_cert"`
|
||||
Log string `json:"log"`
|
||||
}
|
||||
|
||||
func FirstCert(confName string) (c Cert, err error) {
|
||||
err = db.First(&c, &Cert{
|
||||
Filename: confName,
|
||||
}).Error
|
||||
err = db.First(&c, &Cert{
|
||||
Filename: confName,
|
||||
}).Error
|
||||
|
||||
return
|
||||
return
|
||||
}
|
||||
|
||||
func FirstOrCreateCert(confName string) (c Cert, err error) {
|
||||
err = db.FirstOrCreate(&c, &Cert{Filename: confName}).Error
|
||||
return
|
||||
err = db.FirstOrCreate(&c, &Cert{Filename: confName}).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Cert) Insert() error {
|
||||
return db.Create(c).Error
|
||||
return db.Create(c).Error
|
||||
}
|
||||
|
||||
func GetAutoCertList() (c []*Cert) {
|
||||
var t []*Cert
|
||||
db.Where("auto_cert", AutoCertEnabled).Find(&t)
|
||||
var t []*Cert
|
||||
db.Where("auto_cert", AutoCertEnabled).Find(&t)
|
||||
|
||||
// check if this domain is enabled
|
||||
enabledConfig, err := os.ReadDir(nginx.GetConfPath("sites-enabled"))
|
||||
// check if this domain is enabled
|
||||
enabledConfig, err := os.ReadDir(nginx.GetConfPath("sites-enabled"))
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
enabledConfigMap := make(map[string]bool)
|
||||
for i := range enabledConfig {
|
||||
enabledConfigMap[enabledConfig[i].Name()] = true
|
||||
}
|
||||
enabledConfigMap := make(map[string]bool)
|
||||
for i := range enabledConfig {
|
||||
enabledConfigMap[enabledConfig[i].Name()] = true
|
||||
}
|
||||
|
||||
for _, v := range t {
|
||||
if enabledConfigMap[v.Filename] == true {
|
||||
c = append(c, v)
|
||||
}
|
||||
}
|
||||
for _, v := range t {
|
||||
if enabledConfigMap[v.Filename] == true {
|
||||
c = append(c, v)
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
return
|
||||
}
|
||||
|
||||
func GetCertList(name, domain string) (c []Cert) {
|
||||
tx := db
|
||||
if name != "" {
|
||||
tx = tx.Where("name LIKE ? or domain LIKE ?", "%"+name+"%", "%"+name+"%")
|
||||
}
|
||||
if domain != "" {
|
||||
tx = tx.Where("domain LIKE ?", "%"+domain+"%")
|
||||
}
|
||||
tx.Find(&c)
|
||||
return
|
||||
tx := db
|
||||
if name != "" {
|
||||
tx = tx.Where("name LIKE ? or domain LIKE ?", "%"+name+"%", "%"+name+"%")
|
||||
}
|
||||
if domain != "" {
|
||||
tx = tx.Where("domain LIKE ?", "%"+domain+"%")
|
||||
}
|
||||
tx.Find(&c)
|
||||
return
|
||||
}
|
||||
|
||||
func FirstCertByID(id int) (c Cert, err error) {
|
||||
err = db.First(&c, id).Error
|
||||
err = db.First(&c, id).Error
|
||||
|
||||
return
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Cert) Updates(n *Cert) error {
|
||||
return db.Model(&Cert{}).Where("id", c.ID).Updates(n).Error
|
||||
return db.Model(&Cert{}).Where("id", c.ID).Updates(n).Error
|
||||
}
|
||||
|
||||
func (c *Cert) Remove() error {
|
||||
return db.Where("filename", c.Filename).Delete(c).Error
|
||||
if c.Filename == "" {
|
||||
return db.Delete(c).Error
|
||||
}
|
||||
|
||||
return db.Where("filename", c.Filename).Delete(c).Error
|
||||
}
|
||||
|
|
|
@ -81,23 +81,18 @@ func AutoObtain() {
|
|||
continue
|
||||
}
|
||||
|
||||
if certModel.SSLCertificatePath == "" {
|
||||
errLog.Exit(confName, errors.New("ssl_certificate_path is empty, "+
|
||||
"try to reopen auto-cert for this config:"+confName))
|
||||
continue
|
||||
if certModel.SSLCertificatePath != "" {
|
||||
cert, err := GetCertInfo(certModel.SSLCertificatePath)
|
||||
if err != nil {
|
||||
errLog.Push("get cert info", err)
|
||||
// Get certificate info error, ignore this domain
|
||||
continue
|
||||
}
|
||||
// every week
|
||||
if time.Now().Sub(cert.NotBefore).Hours()/24 < 7 {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
cert, err := GetCertInfo(certModel.SSLCertificatePath)
|
||||
if err != nil {
|
||||
errLog.Push("get cert info", err)
|
||||
// Get certificate info error, ignore this domain
|
||||
continue
|
||||
}
|
||||
// every week
|
||||
if time.Now().Sub(cert.NotBefore).Hours()/24 < 7 {
|
||||
continue
|
||||
}
|
||||
//
|
||||
// after 1 mo, reissue certificate
|
||||
logChan := make(chan string, 1)
|
||||
errChan := make(chan error, 1)
|
||||
|
@ -108,7 +103,7 @@ func AutoObtain() {
|
|||
go handleIssueCertLogChan(logChan)
|
||||
|
||||
// block, unless errChan closed
|
||||
for err = range errChan {
|
||||
for err := range errChan {
|
||||
errLog.Push("issue cert", err)
|
||||
}
|
||||
// store error log to db
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue