cert.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package model
  2. import (
  3. "os"
  4. "github.com/0xJacky/Nginx-UI/internal/helper"
  5. "github.com/0xJacky/Nginx-UI/internal/nginx"
  6. "github.com/go-acme/lego/v4/certcrypto"
  7. "github.com/go-acme/lego/v4/certificate"
  8. "github.com/lib/pq"
  9. "gorm.io/gorm/clause"
  10. )
  11. const (
  12. AutoCertSync = 2
  13. AutoCertEnabled = 1
  14. AutoCertDisabled = -1
  15. CertChallengeMethodHTTP01 = "http01"
  16. CertChallengeMethodDNS01 = "dns01"
  17. )
  18. type CertDomains []string
  19. type CertificateResource struct {
  20. *certificate.Resource
  21. PrivateKey []byte `json:"private_key"`
  22. Certificate []byte `json:"certificate"`
  23. IssuerCertificate []byte `json:"issuerCertificate"`
  24. CSR []byte `json:"csr"`
  25. }
  26. type Cert struct {
  27. Model
  28. Name string `json:"name"`
  29. Domains pq.StringArray `json:"domains" gorm:"type:text[]"`
  30. Filename string `json:"filename"`
  31. SSLCertificatePath string `json:"ssl_certificate_path"`
  32. SSLCertificateKeyPath string `json:"ssl_certificate_key_path"`
  33. AutoCert int `json:"auto_cert"`
  34. ChallengeMethod string `json:"challenge_method"`
  35. DnsCredentialID uint64 `json:"dns_credential_id"`
  36. DnsCredential *DnsCredential `json:"dns_credential,omitempty"`
  37. ACMEUserID uint64 `json:"acme_user_id"`
  38. ACMEUser *AcmeUser `json:"acme_user,omitempty"`
  39. KeyType certcrypto.KeyType `json:"key_type"`
  40. Log string `json:"log"`
  41. Resource *CertificateResource `json:"-" gorm:"serializer:json"`
  42. SyncNodeIds []uint64 `json:"sync_node_ids" gorm:"serializer:json"`
  43. MustStaple bool `json:"must_staple"`
  44. LegoDisableCNAMESupport bool `json:"lego_disable_cname_support"`
  45. RevokeOld bool `json:"revoke_old"`
  46. }
  47. func FirstCert(confName string) (c Cert, err error) {
  48. err = db.Limit(1).Where(&Cert{
  49. Filename: confName,
  50. }).Find(&c).Error
  51. return
  52. }
  53. func FirstOrCreateCert(confName string, keyType certcrypto.KeyType) (c Cert, err error) {
  54. // Filename is used to check whether this site is enabled
  55. err = db.FirstOrCreate(&c, &Cert{Name: confName, Filename: confName, KeyType: keyType}).Error
  56. return
  57. }
  58. func FirstOrInit(confName string, keyType certcrypto.KeyType) (c Cert, err error) {
  59. err = db.FirstOrInit(&c, &Cert{Name: confName, Filename: confName, KeyType: keyType}).Error
  60. return
  61. }
  62. func (c *Cert) Insert() error {
  63. return db.Create(c).Error
  64. }
  65. func GetAutoCertList() (c []*Cert) {
  66. var t []*Cert
  67. if db == nil {
  68. return
  69. }
  70. db.Where("auto_cert", AutoCertEnabled).Find(&t)
  71. // check if this domain is enabled
  72. enabledConfig, err := os.ReadDir(nginx.GetConfPath("sites-enabled"))
  73. if err != nil {
  74. return
  75. }
  76. enabledConfigMap := make(map[string]bool)
  77. for i := range enabledConfig {
  78. enabledConfigMap[enabledConfig[i].Name()] = true
  79. }
  80. for _, v := range t {
  81. if v.ChallengeMethod == CertChallengeMethodDNS01 || enabledConfigMap[v.Filename] == true {
  82. c = append(c, v)
  83. }
  84. }
  85. return
  86. }
  87. func (c *Cert) Updates(n *Cert) error {
  88. return db.Model(c).Clauses(clause.Returning{}).
  89. Where("id", c.ID).Updates(n).Error
  90. }
  91. func (c *Cert) Remove() error {
  92. if c.Filename == "" {
  93. return db.Delete(c).Error
  94. }
  95. return db.Where("filename", c.Filename).Delete(c).Error
  96. }
  97. func (c *Cert) GetKeyType() certcrypto.KeyType {
  98. return helper.GetKeyType(c.KeyType)
  99. }
  100. func (c *CertificateResource) GetResource() certificate.Resource {
  101. return certificate.Resource{
  102. Domain: c.Resource.Domain,
  103. CertURL: c.Resource.CertURL,
  104. CertStableURL: c.Resource.CertStableURL,
  105. PrivateKey: c.PrivateKey,
  106. Certificate: c.Certificate,
  107. IssuerCertificate: c.IssuerCertificate,
  108. CSR: c.CSR,
  109. }
  110. }
  111. // GetCertList returns all certificates
  112. func GetCertList() (c []*Cert) {
  113. if db == nil {
  114. return
  115. }
  116. db.Find(&c)
  117. return
  118. }