Skip to content

Commit

Permalink
Get certificate info from local file
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJacky committed Aug 11, 2022
1 parent c747390 commit e28e46d
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 166 deletions.
4 changes: 2 additions & 2 deletions frontend/src/api/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class Domain extends Curd {
return http.get('template')
}

cert_info(domain: string) {
return http.get('cert/' + domain + '/info')
cert_info(path: string) {
return http.get('cert_info?ssl_certificate_path=' + path)
}

add_auto_cert(domain: string) {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/version.json
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}
7 changes: 6 additions & 1 deletion frontend/src/views/domain/cert/Cert.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ const name = computed(() => {
return props.directivesMap['server_name'][0].params.trim()
})
const ssl_certificate_path = computed(() => {
return props.directivesMap['ssl_certificate']?.[0].params.trim() ?? null
})
const enabled = computed({
get() {
return props.enabled
Expand All @@ -36,7 +41,7 @@ const enabled = computed({

<template>
<div>
<cert-info ref="info" :domain="name" v-if="name"/>
<cert-info ref="info" :ssl_certificate_path="ssl_certificate_path" v-if="ssl_certificate_path"/>
<issue-cert
:current_server_directives="props.current_server_directives"
:directives-map="props.directivesMap"
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/views/domain/cert/CertInfo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import dayjs from 'dayjs'
import {reactive, ref} from 'vue'
import domain from '@/api/domain'
const props = defineProps(['domain'])
const props = defineProps(['ssl_certificate_path'])
const ok = ref(false)
const cert = reactive({issuer_name: '', subject_name: '', not_after: '', not_before: ''})
get()
function get() {
domain.cert_info(props.domain).then((r: any) => {
domain.cert_info(props.ssl_certificate_path).then((r: any) => {
Object.assign(cert, r)
ok.value = true
}).catch(() => {
Expand Down
2 changes: 1 addition & 1 deletion frontend/version.json
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}
270 changes: 145 additions & 125 deletions server/api/cert.go
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
}
}
}
1 change: 1 addition & 0 deletions server/api/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ func DeleteDomain(c *gin.Context) {

func AddDomainToAutoCert(c *gin.Context) {
domain := c.Param("domain")

cert, err := model.FirstOrCreateCert(domain)
if err != nil {
ErrHandler(c, err)
Expand Down
9 changes: 7 additions & 2 deletions server/model/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (

type Cert struct {
Model
Domain string `json:"domain"`
Domain string `json:"domain"`
SSLCertificatePath string `json:"ssl_certificate_path"`
}

func FirstCert(domain string) (c Cert, err error) {
Expand All @@ -27,8 +28,8 @@ func FirstOrCreateCert(domain string) (c Cert, err error) {
func GetAutoCertList() (c []Cert) {
var t []Cert
db.Find(&t)
// check if this domain is enabled

// check if this domain is enabled
enabledConfig, err := os.ReadDir(filepath.Join(nginx.GetNginxConfPath("sites-enabled")))

if err != nil {
Expand All @@ -49,6 +50,10 @@ func GetAutoCertList() (c []Cert) {
return
}

func (c *Cert) Updates(n *Cert) error {
return db.Model(c).Updates(n).Error
}

func (c *Cert) Remove() error {
return db.Where("domain", c.Domain).Delete(c).Error
}
Loading

0 comments on commit e28e46d

Please sign in to comment.