@@ -3,13 +3,20 @@ package cert
33import (
44 "github.com/0xJacky/Nginx-UI/internal/helper"
55 "github.com/0xJacky/Nginx-UI/internal/logger"
6+ "github.com/0xJacky/Nginx-UI/internal/nginx"
67 "github.com/0xJacky/Nginx-UI/model"
78 "github.com/0xJacky/Nginx-UI/query"
89 "github.com/go-acme/lego/v4/certcrypto"
10+ "github.com/pkg/errors"
11+ "log"
12+ "os"
13+ "path/filepath"
14+ "strings"
915 "time"
1016)
1117
1218type ConfigPayload struct {
19+ CertID int `json:"cert_id"`
1320 ServerName []string `json:"server_name"`
1421 ChallengeMethod string `json:"challenge_method"`
1522 DNSCredentialID int `json:"dns_credential_id"`
@@ -38,3 +45,46 @@ func (c *ConfigPayload) GetACMEUser() (user *model.AcmeUser, err error) {
3845func (c * ConfigPayload ) GetKeyType () certcrypto.KeyType {
3946 return helper .GetKeyType (c .KeyType )
4047}
48+
49+ func (c * ConfigPayload ) WriteFile (l * log.Logger , errChan chan error ) {
50+ name := strings .Join (c .ServerName , "_" )
51+ saveDir := nginx .GetConfPath ("ssl/" + name + "_" + string (c .KeyType ))
52+ if _ , err := os .Stat (saveDir ); os .IsNotExist (err ) {
53+ err = os .MkdirAll (saveDir , 0755 )
54+ if err != nil {
55+ errChan <- errors .Wrap (err , "mkdir error" )
56+ return
57+ }
58+ }
59+
60+ // Each certificate comes back with the cert bytes, the bytes of the client's
61+ // private key, and a certificate URL. SAVE THESE TO DISK.
62+ l .Println ("[INFO] [Nginx UI] Writing certificate to disk" )
63+ err := os .WriteFile (filepath .Join (saveDir , "fullchain.cer" ),
64+ c .Resource .Certificate , 0644 )
65+
66+ if err != nil {
67+ errChan <- errors .Wrap (err , "write fullchain.cer error" )
68+ return
69+ }
70+
71+ l .Println ("[INFO] [Nginx UI] Writing certificate private key to disk" )
72+ err = os .WriteFile (filepath .Join (saveDir , "private.key" ),
73+ c .Resource .PrivateKey , 0644 )
74+
75+ if err != nil {
76+ errChan <- errors .Wrap (err , "write private.key error" )
77+ return
78+ }
79+
80+ // update database
81+ if c .CertID <= 0 {
82+ return
83+ }
84+
85+ db := model .UseDB ()
86+ db .Where ("id = ?" , c .CertID ).Updates (& model.Cert {
87+ SSLCertificatePath : filepath .Join (saveDir , "fullchain.cer" ),
88+ SSLCertificateKeyPath : filepath .Join (saveDir , "private.key" ),
89+ })
90+ }
0 commit comments