|
@@ -2,13 +2,16 @@ package api
|
|
|
|
|
|
import (
|
|
|
"github.com/0xJacky/Nginx-UI/server/model"
|
|
|
+ "github.com/0xJacky/Nginx-UI/server/pkg/cert"
|
|
|
"github.com/0xJacky/Nginx-UI/server/pkg/config_list"
|
|
|
- nginx2 "github.com/0xJacky/Nginx-UI/server/pkg/nginx"
|
|
|
+ "github.com/0xJacky/Nginx-UI/server/pkg/nginx"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
+ "log"
|
|
|
"net/http"
|
|
|
"os"
|
|
|
"path/filepath"
|
|
|
"strings"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
func GetDomains(c *gin.Context) {
|
|
@@ -21,14 +24,14 @@ func GetDomains(c *gin.Context) {
|
|
|
"modify": "time",
|
|
|
}
|
|
|
|
|
|
- configFiles, err := os.ReadDir(nginx2.GetNginxConfPath("sites-available"))
|
|
|
+ configFiles, err := os.ReadDir(nginx.GetNginxConfPath("sites-available"))
|
|
|
|
|
|
if err != nil {
|
|
|
ErrHandler(c, err)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- enabledConfig, err := os.ReadDir(filepath.Join(nginx2.GetNginxConfPath("sites-enabled")))
|
|
|
+ enabledConfig, err := os.ReadDir(filepath.Join(nginx.GetNginxConfPath("sites-enabled")))
|
|
|
|
|
|
if err != nil {
|
|
|
ErrHandler(c, err)
|
|
@@ -62,22 +65,53 @@ func GetDomains(c *gin.Context) {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+type CertificateInfo struct {
|
|
|
+ SubjectName string `json:"subject_name"`
|
|
|
+ IssuerName string `json:"issuer_name"`
|
|
|
+ NotAfter time.Time `json:"not_after"`
|
|
|
+ NotBefore time.Time `json:"not_before"`
|
|
|
+}
|
|
|
+
|
|
|
func GetDomain(c *gin.Context) {
|
|
|
name := c.Param("name")
|
|
|
- path := filepath.Join(nginx2.GetNginxConfPath("sites-available"), name)
|
|
|
+ path := filepath.Join(nginx.GetNginxConfPath("sites-available"), name)
|
|
|
|
|
|
enabled := true
|
|
|
- if _, err := os.Stat(filepath.Join(nginx2.GetNginxConfPath("sites-enabled"), name)); os.IsNotExist(err) {
|
|
|
+ if _, err := os.Stat(filepath.Join(nginx.GetNginxConfPath("sites-enabled"), name)); os.IsNotExist(err) {
|
|
|
enabled = false
|
|
|
}
|
|
|
|
|
|
- config, err := nginx2.ParseNgxConfig(path)
|
|
|
+ config, err := nginx.ParseNgxConfig(path)
|
|
|
|
|
|
if err != nil {
|
|
|
ErrHandler(c, err)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+ certInfoMap := make(map[int]CertificateInfo)
|
|
|
+ for serverIdx, server := range config.Servers {
|
|
|
+ for _, directive := range server.Directives {
|
|
|
+ if directive.Directive == "ssl_certificate" {
|
|
|
+
|
|
|
+ pubKey, err := cert.GetCertInfo(directive.Params)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ log.Println("Failed to get certificate information", err)
|
|
|
+ break
|
|
|
+ }
|
|
|
+
|
|
|
+ certInfoMap[serverIdx] = CertificateInfo{
|
|
|
+ SubjectName: pubKey.Subject.CommonName,
|
|
|
+ IssuerName: pubKey.Issuer.CommonName,
|
|
|
+ NotAfter: pubKey.NotAfter,
|
|
|
+ NotBefore: pubKey.NotBefore,
|
|
|
+ }
|
|
|
+
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
_, err = model.FirstCert(name)
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
@@ -86,6 +120,7 @@ func GetDomain(c *gin.Context) {
|
|
|
"config": config.BuildConfig(),
|
|
|
"tokenized": config,
|
|
|
"auto_cert": err == nil,
|
|
|
+ "cert_info": certInfoMap,
|
|
|
})
|
|
|
|
|
|
}
|
|
@@ -95,7 +130,7 @@ func EditDomain(c *gin.Context) {
|
|
|
name := c.Param("name")
|
|
|
request := make(gin.H)
|
|
|
err = c.BindJSON(&request)
|
|
|
- path := filepath.Join(nginx2.GetNginxConfPath("sites-available"), name)
|
|
|
+ path := filepath.Join(nginx.GetNginxConfPath("sites-available"), name)
|
|
|
|
|
|
err = os.WriteFile(path, []byte(request["content"].(string)), 0644)
|
|
|
if err != nil {
|
|
@@ -103,10 +138,10 @@ func EditDomain(c *gin.Context) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- enabledConfigFilePath := filepath.Join(nginx2.GetNginxConfPath("sites-enabled"), name)
|
|
|
+ enabledConfigFilePath := filepath.Join(nginx.GetNginxConfPath("sites-enabled"), name)
|
|
|
if _, err = os.Stat(enabledConfigFilePath); err == nil {
|
|
|
// Test nginx configuration
|
|
|
- err = nginx2.TestNginxConf()
|
|
|
+ err = nginx.TestNginxConf()
|
|
|
if err != nil {
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
|
"message": err.Error(),
|
|
@@ -114,7 +149,7 @@ func EditDomain(c *gin.Context) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- output := nginx2.ReloadNginx()
|
|
|
+ output := nginx.ReloadNginx()
|
|
|
|
|
|
if output != "" && strings.Contains(output, "error") {
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
@@ -128,8 +163,8 @@ func EditDomain(c *gin.Context) {
|
|
|
}
|
|
|
|
|
|
func EnableDomain(c *gin.Context) {
|
|
|
- configFilePath := filepath.Join(nginx2.GetNginxConfPath("sites-available"), c.Param("name"))
|
|
|
- enabledConfigFilePath := filepath.Join(nginx2.GetNginxConfPath("sites-enabled"), c.Param("name"))
|
|
|
+ configFilePath := filepath.Join(nginx.GetNginxConfPath("sites-available"), c.Param("name"))
|
|
|
+ enabledConfigFilePath := filepath.Join(nginx.GetNginxConfPath("sites-enabled"), c.Param("name"))
|
|
|
|
|
|
_, err := os.Stat(configFilePath)
|
|
|
|
|
@@ -148,7 +183,7 @@ func EnableDomain(c *gin.Context) {
|
|
|
}
|
|
|
|
|
|
// Test nginx config, if not pass then rollback.
|
|
|
- err = nginx2.TestNginxConf()
|
|
|
+ err = nginx.TestNginxConf()
|
|
|
if err != nil {
|
|
|
_ = os.Remove(enabledConfigFilePath)
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
@@ -157,7 +192,7 @@ func EnableDomain(c *gin.Context) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- output := nginx2.ReloadNginx()
|
|
|
+ output := nginx.ReloadNginx()
|
|
|
|
|
|
if output != "" && strings.Contains(output, "error") {
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
@@ -172,7 +207,7 @@ func EnableDomain(c *gin.Context) {
|
|
|
}
|
|
|
|
|
|
func DisableDomain(c *gin.Context) {
|
|
|
- enabledConfigFilePath := filepath.Join(nginx2.GetNginxConfPath("sites-enabled"), c.Param("name"))
|
|
|
+ enabledConfigFilePath := filepath.Join(nginx.GetNginxConfPath("sites-enabled"), c.Param("name"))
|
|
|
|
|
|
_, err := os.Stat(enabledConfigFilePath)
|
|
|
|
|
@@ -196,7 +231,7 @@ func DisableDomain(c *gin.Context) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- output := nginx2.ReloadNginx()
|
|
|
+ output := nginx.ReloadNginx()
|
|
|
|
|
|
if output != "" {
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
@@ -213,8 +248,8 @@ func DisableDomain(c *gin.Context) {
|
|
|
func DeleteDomain(c *gin.Context) {
|
|
|
var err error
|
|
|
name := c.Param("name")
|
|
|
- availablePath := filepath.Join(nginx2.GetNginxConfPath("sites-available"), name)
|
|
|
- enabledPath := filepath.Join(nginx2.GetNginxConfPath("sites-enabled"), name)
|
|
|
+ availablePath := filepath.Join(nginx.GetNginxConfPath("sites-available"), name)
|
|
|
+ enabledPath := filepath.Join(nginx.GetNginxConfPath("sites-enabled"), name)
|
|
|
|
|
|
if _, err = os.Stat(availablePath); os.IsNotExist(err) {
|
|
|
c.JSON(http.StatusNotFound, gin.H{
|