-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get certificate info from local file
- Loading branch information
Showing
11 changed files
with
197 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"version":"1.5.0","build_id":40,"total_build":110} | ||
{"version":"1.5.0","build_id":41,"total_build":111} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"version":"1.5.0","build_id":40,"total_build":110} | ||
{"version":"1.5.0","build_id":41,"total_build":111} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,135 +1,155 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/0xJacky/Nginx-UI/server/pkg/cert" | ||
"github.com/0xJacky/Nginx-UI/server/pkg/nginx" | ||
"github.com/gin-gonic/gin" | ||
"github.com/gorilla/websocket" | ||
"log" | ||
"net/http" | ||
"os" | ||
"github.com/0xJacky/Nginx-UI/server/model" | ||
"github.com/0xJacky/Nginx-UI/server/pkg/cert" | ||
"github.com/0xJacky/Nginx-UI/server/pkg/nginx" | ||
"github.com/gin-gonic/gin" | ||
"github.com/gorilla/websocket" | ||
"log" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
func CertInfo(c *gin.Context) { | ||
domain := c.Param("domain") | ||
|
||
key, err := cert.GetCertInfo(domain) | ||
|
||
if err != nil { | ||
c.JSON(http.StatusOK, gin.H{ | ||
"message": "Failed to get cert information", | ||
"error": err, | ||
}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"subject_name": key.Subject.CommonName, | ||
"issuer_name": key.Issuer.CommonName, | ||
"not_after": key.NotAfter, | ||
"not_before": key.NotBefore, | ||
}) | ||
path := c.Query("ssl_certificate_path") | ||
|
||
log.Println(path) | ||
|
||
key, err := cert.GetCertInfo(path) | ||
|
||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{ | ||
"message": "Failed to get certificate information", | ||
"error": err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{ | ||
"subject_name": key.Subject.CommonName, | ||
"issuer_name": key.Issuer.CommonName, | ||
"not_after": key.NotAfter, | ||
"not_before": key.NotBefore, | ||
}) | ||
} | ||
|
||
func IssueCert(c *gin.Context) { | ||
domain := c.Param("domain") | ||
var upGrader = websocket.Upgrader{ | ||
CheckOrigin: func(r *http.Request) bool { | ||
return true | ||
}, | ||
} | ||
|
||
// upgrade http to websocket | ||
ws, err := upGrader.Upgrade(c.Writer, c.Request, nil) | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
defer func(ws *websocket.Conn) { | ||
err := ws.Close() | ||
if err != nil { | ||
log.Println("defer websocket close err", err) | ||
} | ||
}(ws) | ||
|
||
// read | ||
mt, message, err := ws.ReadMessage() | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
if mt == websocket.TextMessage && string(message) == "go" { | ||
|
||
err = cert.IssueCert(domain) | ||
|
||
if err != nil { | ||
|
||
log.Println(err) | ||
|
||
err = ws.WriteJSON(gin.H{ | ||
"status": "error", | ||
"message": err.Error(), | ||
}) | ||
|
||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
return | ||
} | ||
|
||
sslCertificatePath := nginx.GetNginxConfPath("ssl/" + domain + "/fullchain.cer") | ||
_, err = os.Stat(sslCertificatePath) | ||
|
||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
log.Println("[found]", "fullchain.cer") | ||
|
||
err = ws.WriteJSON(gin.H{ | ||
"status": "success", | ||
"message": "[found] fullchain.cer", | ||
}) | ||
|
||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
sslCertificateKeyPath := nginx.GetNginxConfPath("ssl/" + domain + "/" + domain + ".key") | ||
_, err = os.Stat(sslCertificateKeyPath) | ||
|
||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
log.Println("[found]", "cert key") | ||
err = ws.WriteJSON(gin.H{ | ||
"status": "success", | ||
"message": "[found] Certificate Key", | ||
}) | ||
|
||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
err = ws.WriteJSON(gin.H{ | ||
"status": "success", | ||
"message": "Issued certificate successfully", | ||
"ssl_certificate": sslCertificatePath, | ||
"ssl_certificate_key": sslCertificateKeyPath, | ||
}) | ||
|
||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
} | ||
domain := c.Param("domain") | ||
|
||
var upGrader = websocket.Upgrader{ | ||
CheckOrigin: func(r *http.Request) bool { | ||
return true | ||
}, | ||
} | ||
|
||
// upgrade http to websocket | ||
ws, err := upGrader.Upgrade(c.Writer, c.Request, nil) | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
defer func(ws *websocket.Conn) { | ||
err := ws.Close() | ||
if err != nil { | ||
log.Println("defer websocket close err", err) | ||
} | ||
}(ws) | ||
|
||
// read | ||
mt, message, err := ws.ReadMessage() | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
if mt == websocket.TextMessage && string(message) == "go" { | ||
|
||
err = cert.IssueCert(domain) | ||
|
||
if err != nil { | ||
|
||
log.Println(err) | ||
|
||
err = ws.WriteJSON(gin.H{ | ||
"status": "error", | ||
"message": err.Error(), | ||
}) | ||
|
||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
return | ||
} | ||
|
||
sslCertificatePath := nginx.GetNginxConfPath("ssl/" + domain + "/fullchain.cer") | ||
_, err = os.Stat(sslCertificatePath) | ||
|
||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
log.Println("[found]", "fullchain.cer") | ||
|
||
err = ws.WriteJSON(gin.H{ | ||
"status": "success", | ||
"message": "[found] fullchain.cer", | ||
}) | ||
|
||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
sslCertificateKeyPath := nginx.GetNginxConfPath("ssl/" + domain + "/" + domain + ".key") | ||
_, err = os.Stat(sslCertificateKeyPath) | ||
|
||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
log.Println("[found]", "cert key") | ||
err = ws.WriteJSON(gin.H{ | ||
"status": "success", | ||
"message": "[found] Certificate Key", | ||
}) | ||
|
||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
certModel, err := model.FirstCert(domain) | ||
|
||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
err = certModel.Updates(&model.Cert{ | ||
SSLCertificatePath: sslCertificatePath, | ||
}) | ||
|
||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
|
||
err = ws.WriteJSON(gin.H{ | ||
"status": "success", | ||
"message": "Issued certificate successfully", | ||
"ssl_certificate": sslCertificatePath, | ||
"ssl_certificate_key": sslCertificateKeyPath, | ||
}) | ||
|
||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.