Skip to content

Commit

Permalink
feat: 支持签发自签名证书
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Oct 26, 2024
1 parent 9179543 commit aed3599
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 4 deletions.
1 change: 1 addition & 0 deletions internal/biz/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type CertRepo interface {
Delete(id uint) error
ObtainAuto(id uint) (*acme.Certificate, error)
ObtainManual(id uint) (*acme.Certificate, error)
ObtainSelfSigned(id uint) error
Renew(id uint) (*acme.Certificate, error)
ManualDNS(id uint) ([]acme.DNSRecord, error)
Deploy(ID, WebsiteID uint) error
Expand Down
24 changes: 24 additions & 0 deletions internal/data/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,30 @@ func (r *certRepo) ObtainManual(id uint) (*acme.Certificate, error) {
return &ssl, nil
}

func (r *certRepo) ObtainSelfSigned(id uint) error {
cert, err := r.Get(id)
if err != nil {
return err
}

crt, key, err := pkgcert.GenerateSelfSigned(cert.Domains)
if err != nil {
return err
}

cert.Cert = string(crt)
cert.Key = string(key)
if err = app.Orm.Save(cert).Error; err != nil {
return err
}

if cert.Website != nil {
return r.Deploy(cert.ID, cert.WebsiteID)
}

return nil
}

func (r *certRepo) Renew(id uint) (*acme.Certificate, error) {
cert, err := r.Get(id)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/route/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func Http(r chi.Router) {
r.Delete("/{id}", cert.Delete)
r.Post("/{id}/obtainAuto", cert.ObtainAuto)
r.Post("/{id}/obtainManual", cert.ObtainManual)
r.Post("/{id}/obtainSelfSigned", cert.ObtainSelfSigned)
r.Post("/{id}/renew", cert.Renew)
r.Post("/{id}/manualDNS", cert.ManualDNS)
r.Post("/{id}/deploy", cert.Deploy)
Expand Down
15 changes: 15 additions & 0 deletions internal/service/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,21 @@ func (s *CertService) ObtainManual(w http.ResponseWriter, r *http.Request) {
Success(w, nil)
}

func (s *CertService) ObtainSelfSigned(w http.ResponseWriter, r *http.Request) {
req, err := Bind[request.ID](r)
if err != nil {
Error(w, http.StatusUnprocessableEntity, "%v", err)
return
}

if err = s.certRepo.ObtainSelfSigned(req.ID); err != nil {
Error(w, http.StatusInternalServerError, "%v", err)
return
}

Success(w, nil)
}

func (s *CertService) Renew(w http.ResponseWriter, r *http.Request) {
req, err := Bind[request.ID](r)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions web/src/api/panel/cert/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export default {
// 证书手动签发
obtainManual: (id: number): Promise<AxiosResponse<any>> =>
request.post(`/cert/cert/${id}/obtainManual`, { id }),
// 证书自签名签发
obtainSelfSigned: (id: number): Promise<AxiosResponse<any>> =>
request.post(`/cert/cert/${id}/obtainSelfSigned`, { id }),
// 续签
renew: (id: number): Promise<AxiosResponse<any>> =>
request.post(`/cert/cert/${id}/renew`, { id }),
Expand Down
2 changes: 1 addition & 1 deletion web/src/views/cert/CertView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ const columns: any = [
resizable: true,
render(row: any) {
return [
row.type != 'upload' && row.account_id != 0 && row.cert == '' && row.key == ''
row.type != 'upload' && row.cert == '' && row.key == ''
? h(
NButton,
{
Expand Down
19 changes: 16 additions & 3 deletions web/src/views/cert/ObtainModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ const model = ref({
})
const options = [
{ label: '自动验证', value: 'auto' },
{ label: '手动 DNS 验证', value: 'manual' }
{ label: '自动', value: 'auto' },
{ label: '手动', value: 'manual' },
{ label: '自签名', value: 'self-signed' }
]
const handleSubmit = async () => {
Expand All @@ -33,7 +34,7 @@ const handleSubmit = async () => {
window.$bus.emit('cert:refresh-cert')
window.$bus.emit('cert:refresh-async')
})
} else {
} else if (model.value.type == 'manual') {
const { data } = await cert.manualDNS(id.value)
messageReactive.destroy()
window.$message.info('请先前往域名处设置 DNS 解析,再继续签发')
Expand Down Expand Up @@ -78,6 +79,18 @@ const handleSubmit = async () => {
})
}
})
} else {
await cert
.obtainSelfSigned(id.value)
.then(() => {
window.$message.success('签发成功')
show.value = false
})
.finally(() => {
messageReactive?.destroy()
window.$bus.emit('cert:refresh-cert')
window.$bus.emit('cert:refresh-async')
})
}
}
</script>
Expand Down

0 comments on commit aed3599

Please sign in to comment.