mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-12 02:45:49 +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
|
search: true
|
||||||
}, {
|
}, {
|
||||||
title: () => $gettext('Domain'),
|
title: () => $gettext('Config Name'),
|
||||||
dataIndex: 'domain',
|
dataIndex: 'filename',
|
||||||
sorter: true,
|
sorter: true,
|
||||||
pithy: true,
|
pithy: true
|
||||||
edit: {
|
|
||||||
type: input
|
|
||||||
},
|
|
||||||
search: true
|
|
||||||
}, {
|
}, {
|
||||||
title: () => $gettext('Auto Cert'),
|
title: () => $gettext('Auto Cert'),
|
||||||
dataIndex: 'auto_cert',
|
dataIndex: 'auto_cert',
|
||||||
|
|
|
@ -364,6 +364,7 @@ func AddDomainToAutoCert(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
err = certModel.Updates(&model.Cert{
|
err = certModel.Updates(&model.Cert{
|
||||||
|
Name: name,
|
||||||
AutoCert: model.AutoCertEnabled,
|
AutoCert: model.AutoCertEnabled,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -1,93 +1,97 @@
|
||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/0xJacky/Nginx-UI/server/pkg/nginx"
|
"github.com/0xJacky/Nginx-UI/server/pkg/nginx"
|
||||||
"github.com/lib/pq"
|
"github.com/lib/pq"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
AutoCertEnabled = 1
|
AutoCertEnabled = 1
|
||||||
AutoCertDisabled = -1
|
AutoCertDisabled = -1
|
||||||
)
|
)
|
||||||
|
|
||||||
type CertDomains []string
|
type CertDomains []string
|
||||||
|
|
||||||
type Cert struct {
|
type Cert struct {
|
||||||
Model
|
Model
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Domains pq.StringArray `json:"domains" gorm:"type:text[]"`
|
Domains pq.StringArray `json:"domains" gorm:"type:text[]"`
|
||||||
Filename string `json:"filename"`
|
Filename string `json:"filename"`
|
||||||
SSLCertificatePath string `json:"ssl_certificate_path"`
|
SSLCertificatePath string `json:"ssl_certificate_path"`
|
||||||
SSLCertificateKeyPath string `json:"ssl_certificate_key_path"`
|
SSLCertificateKeyPath string `json:"ssl_certificate_key_path"`
|
||||||
AutoCert int `json:"auto_cert"`
|
AutoCert int `json:"auto_cert"`
|
||||||
Log string `json:"log"`
|
Log string `json:"log"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func FirstCert(confName string) (c Cert, err error) {
|
func FirstCert(confName string) (c Cert, err error) {
|
||||||
err = db.First(&c, &Cert{
|
err = db.First(&c, &Cert{
|
||||||
Filename: confName,
|
Filename: confName,
|
||||||
}).Error
|
}).Error
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func FirstOrCreateCert(confName string) (c Cert, err error) {
|
func FirstOrCreateCert(confName string) (c Cert, err error) {
|
||||||
err = db.FirstOrCreate(&c, &Cert{Filename: confName}).Error
|
err = db.FirstOrCreate(&c, &Cert{Filename: confName}).Error
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cert) Insert() error {
|
func (c *Cert) Insert() error {
|
||||||
return db.Create(c).Error
|
return db.Create(c).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAutoCertList() (c []*Cert) {
|
func GetAutoCertList() (c []*Cert) {
|
||||||
var t []*Cert
|
var t []*Cert
|
||||||
db.Where("auto_cert", AutoCertEnabled).Find(&t)
|
db.Where("auto_cert", AutoCertEnabled).Find(&t)
|
||||||
|
|
||||||
// check if this domain is enabled
|
// check if this domain is enabled
|
||||||
enabledConfig, err := os.ReadDir(nginx.GetConfPath("sites-enabled"))
|
enabledConfig, err := os.ReadDir(nginx.GetConfPath("sites-enabled"))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
enabledConfigMap := make(map[string]bool)
|
enabledConfigMap := make(map[string]bool)
|
||||||
for i := range enabledConfig {
|
for i := range enabledConfig {
|
||||||
enabledConfigMap[enabledConfig[i].Name()] = true
|
enabledConfigMap[enabledConfig[i].Name()] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, v := range t {
|
for _, v := range t {
|
||||||
if enabledConfigMap[v.Filename] == true {
|
if enabledConfigMap[v.Filename] == true {
|
||||||
c = append(c, v)
|
c = append(c, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetCertList(name, domain string) (c []Cert) {
|
func GetCertList(name, domain string) (c []Cert) {
|
||||||
tx := db
|
tx := db
|
||||||
if name != "" {
|
if name != "" {
|
||||||
tx = tx.Where("name LIKE ? or domain LIKE ?", "%"+name+"%", "%"+name+"%")
|
tx = tx.Where("name LIKE ? or domain LIKE ?", "%"+name+"%", "%"+name+"%")
|
||||||
}
|
}
|
||||||
if domain != "" {
|
if domain != "" {
|
||||||
tx = tx.Where("domain LIKE ?", "%"+domain+"%")
|
tx = tx.Where("domain LIKE ?", "%"+domain+"%")
|
||||||
}
|
}
|
||||||
tx.Find(&c)
|
tx.Find(&c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func FirstCertByID(id int) (c Cert, err error) {
|
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 {
|
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 {
|
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
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if certModel.SSLCertificatePath == "" {
|
if certModel.SSLCertificatePath != "" {
|
||||||
errLog.Exit(confName, errors.New("ssl_certificate_path is empty, "+
|
cert, err := GetCertInfo(certModel.SSLCertificatePath)
|
||||||
"try to reopen auto-cert for this config:"+confName))
|
if err != nil {
|
||||||
continue
|
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
|
// after 1 mo, reissue certificate
|
||||||
logChan := make(chan string, 1)
|
logChan := make(chan string, 1)
|
||||||
errChan := make(chan error, 1)
|
errChan := make(chan error, 1)
|
||||||
|
@ -108,7 +103,7 @@ func AutoObtain() {
|
||||||
go handleIssueCertLogChan(logChan)
|
go handleIssueCertLogChan(logChan)
|
||||||
|
|
||||||
// block, unless errChan closed
|
// block, unless errChan closed
|
||||||
for err = range errChan {
|
for err := range errChan {
|
||||||
errLog.Push("issue cert", err)
|
errLog.Push("issue cert", err)
|
||||||
}
|
}
|
||||||
// store error log to db
|
// store error log to db
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue