cert.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package model
  2. import (
  3. "github.com/0xJacky/Nginx-UI/internal/helper"
  4. "github.com/0xJacky/Nginx-UI/internal/nginx"
  5. "github.com/go-acme/lego/v4/certcrypto"
  6. "github.com/go-acme/lego/v4/certificate"
  7. "github.com/lib/pq"
  8. "gorm.io/gorm/clause"
  9. "os"
  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 (c *Cert) Insert() error {
  58. return db.Create(c).Error
  59. }
  60. func GetAutoCertList() (c []*Cert) {
  61. var t []*Cert
  62. if db == nil {
  63. return
  64. }
  65. db.Where("auto_cert", AutoCertEnabled).Find(&t)
  66. // check if this domain is enabled
  67. enabledConfig, err := os.ReadDir(nginx.GetConfPath("sites-enabled"))
  68. if err != nil {
  69. return
  70. }
  71. enabledConfigMap := make(map[string]bool)
  72. for i := range enabledConfig {
  73. enabledConfigMap[enabledConfig[i].Name()] = true
  74. }
  75. for _, v := range t {
  76. if v.ChallengeMethod == CertChallengeMethodDNS01 || enabledConfigMap[v.Filename] == true {
  77. c = append(c, v)
  78. }
  79. }
  80. return
  81. }
  82. func (c *Cert) Updates(n *Cert) error {
  83. return db.Model(c).Clauses(clause.Returning{}).
  84. Where("id", c.ID).Updates(n).Error
  85. }
  86. func (c *Cert) Remove() error {
  87. if c.Filename == "" {
  88. return db.Delete(c).Error
  89. }
  90. return db.Where("filename", c.Filename).Delete(c).Error
  91. }
  92. func (c *Cert) GetKeyType() certcrypto.KeyType {
  93. return helper.GetKeyType(c.KeyType)
  94. }
  95. func (c *CertificateResource) GetResource() certificate.Resource {
  96. return certificate.Resource{
  97. Domain: c.Resource.Domain,
  98. CertURL: c.Resource.CertURL,
  99. CertStableURL: c.Resource.CertStableURL,
  100. PrivateKey: c.PrivateKey,
  101. Certificate: c.Certificate,
  102. IssuerCertificate: c.IssuerCertificate,
  103. CSR: c.CSR,
  104. }
  105. }