cert.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. }
  46. func FirstCert(confName string) (c Cert, err error) {
  47. err = db.Limit(1).Where(&Cert{
  48. Filename: confName,
  49. }).Find(&c).Error
  50. return
  51. }
  52. func FirstOrCreateCert(confName string, keyType certcrypto.KeyType) (c Cert, err error) {
  53. // Filename is used to check whether this site is enabled
  54. err = db.FirstOrCreate(&c, &Cert{Name: confName, Filename: confName, KeyType: keyType}).Error
  55. return
  56. }
  57. func FirstOrInit(confName string, keyType certcrypto.KeyType) (c Cert, err error) {
  58. err = db.FirstOrInit(&c, &Cert{Name: confName, Filename: confName, KeyType: keyType}).Error
  59. return
  60. }
  61. func (c *Cert) Insert() error {
  62. return db.Create(c).Error
  63. }
  64. func GetAutoCertList() (c []*Cert) {
  65. var t []*Cert
  66. if db == nil {
  67. return
  68. }
  69. db.Where("auto_cert", AutoCertEnabled).Find(&t)
  70. // check if this domain is enabled
  71. enabledConfig, err := os.ReadDir(nginx.GetConfPath("sites-enabled"))
  72. if err != nil {
  73. return
  74. }
  75. enabledConfigMap := make(map[string]bool)
  76. for i := range enabledConfig {
  77. enabledConfigMap[enabledConfig[i].Name()] = true
  78. }
  79. for _, v := range t {
  80. if v.ChallengeMethod == CertChallengeMethodDNS01 || enabledConfigMap[v.Filename] == true {
  81. c = append(c, v)
  82. }
  83. }
  84. return
  85. }
  86. func (c *Cert) Updates(n *Cert) error {
  87. return db.Model(c).Clauses(clause.Returning{}).
  88. Where("id", c.ID).Updates(n).Error
  89. }
  90. func (c *Cert) Remove() error {
  91. if c.Filename == "" {
  92. return db.Delete(c).Error
  93. }
  94. return db.Where("filename", c.Filename).Delete(c).Error
  95. }
  96. func (c *Cert) GetKeyType() certcrypto.KeyType {
  97. return helper.GetKeyType(c.KeyType)
  98. }
  99. func (c *CertificateResource) GetResource() certificate.Resource {
  100. return certificate.Resource{
  101. Domain: c.Resource.Domain,
  102. CertURL: c.Resource.CertURL,
  103. CertStableURL: c.Resource.CertStableURL,
  104. PrivateKey: c.PrivateKey,
  105. Certificate: c.Certificate,
  106. IssuerCertificate: c.IssuerCertificate,
  107. CSR: c.CSR,
  108. }
  109. }
  110. // GetCertList returns all certificates
  111. func GetCertList() (c []*Cert) {
  112. if db == nil {
  113. return
  114. }
  115. db.Find(&c)
  116. return
  117. }