Skip to content

Commit

Permalink
feat: 优化自动签发证书
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Oct 25, 2024
1 parent d56b41b commit 46ae37a
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 17 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,4 @@ require (
modernc.org/sqlite v1.32.0 // indirect
)

replace github.com/mholt/acmez/v2 => github.com/TheTNB/acmez/v2 v2.0.0-20241012163130-5833d84639e0
replace github.com/mholt/acmez/v2 => github.com/TheTNB/acmez/v2 v2.0.0-20241025203320-cc718c4c870b
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/Microsoft/go-winio v0.4.14 h1:+hMXMk01us9KgxGb7ftKQt2Xpf5hH/yky+TDA+qxleU=
github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=
github.com/TheTNB/acmez/v2 v2.0.0-20241012163130-5833d84639e0 h1:o66ZhpjXVs7oi3VyiZvlpp9StFFe4KTeklzB/T4s59Q=
github.com/TheTNB/acmez/v2 v2.0.0-20241012163130-5833d84639e0/go.mod h1:pQ1ysaDeGrIMvJ9dfJMk5kJNkn7L2sb3UhyrX6Q91cw=
github.com/TheTNB/acmez/v2 v2.0.0-20241025203320-cc718c4c870b h1:1iVxCglpEJ/kq48YcujkWq8SeOacMuMmuekMmGyOSPA=
github.com/TheTNB/acmez/v2 v2.0.0-20241025203320-cc718c4c870b/go.mod h1:pQ1ysaDeGrIMvJ9dfJMk5kJNkn7L2sb3UhyrX6Q91cw=
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/beevik/ntp v1.4.3 h1:PlbTvE5NNy4QHmA4Mg57n7mcFTmr1W1j3gcK7L1lqho=
Expand Down
1 change: 1 addition & 0 deletions internal/biz/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Cert struct {
type CertRepo interface {
List(page, limit uint) ([]*Cert, int64, error)
Get(id uint) (*Cert, error)
GetByWebsite(WebsiteID uint) (*Cert, error)
Create(req *request.CertCreate) (*Cert, error)
Update(req *request.CertUpdate) error
Delete(id uint) error
Expand Down
6 changes: 6 additions & 0 deletions internal/data/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ func (r *certRepo) Get(id uint) (*biz.Cert, error) {
return cert, err
}

func (r *certRepo) GetByWebsite(WebsiteID uint) (*biz.Cert, error) {
cert := new(biz.Cert)
err := app.Orm.Model(&biz.Cert{}).Preload("Website").Preload("Account").Preload("DNS").Where("website_id = ?", WebsiteID).First(cert).Error
return cert, err
}

func (r *certRepo) Create(req *request.CertCreate) (*biz.Cert, error) {
cert := &biz.Cert{
AccountID: req.AccountID,
Expand Down
7 changes: 6 additions & 1 deletion internal/data/cert_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ func (r certAccountRepo) GetDefault(userID uint) (*biz.CertAccount, error) {
return nil, err
}

account := new(biz.CertAccount)
if err = app.Orm.Model(&biz.CertAccount{}).Where("ca = ?", "googlecn").Where("email = ?", user.Email).First(account).Error; err == nil {
return account, nil
}

req := &request.CertAccountCreate{
CA: acme.CAGoogleCN,
CA: "googlecn",
Email: user.Email,
KeyType: string(acme.KeyEC256),
}
Expand Down
19 changes: 11 additions & 8 deletions internal/data/website.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,15 +695,18 @@ func (r *websiteRepo) ObtainCert(ctx context.Context, id uint) error {
}

cRepo := NewCertRepo()
newCert, err := cRepo.Create(&request.CertCreate{
Type: string(acme.KeyEC256),
Domains: website.Domains,
AutoRenew: true,
AccountID: account.ID,
WebsiteID: website.ID,
})
newCert, err := cRepo.GetByWebsite(website.ID)
if err != nil {
return err
newCert, err = cRepo.Create(&request.CertCreate{
Type: string(acme.KeyEC256),
Domains: website.Domains,
AutoRenew: true,
AccountID: account.ID,
WebsiteID: website.ID,
})
if err != nil {
return err
}
}

_, err = cRepo.ObtainAuto(newCert.ID)
Expand Down
2 changes: 1 addition & 1 deletion internal/http/request/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type FileCreate struct {

type FileSave struct {
Path string `form:"path" json:"path" validate:"required"`
Content string `form:"content" json:"content" validate:"required"`
Content string `form:"content" json:"content"`
}

type FileMove struct {
Expand Down
3 changes: 2 additions & 1 deletion web/src/views/cert/CertView.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script setup lang="ts">
import Editor from '@guolao/vue-monaco-editor'
import type { MessageReactive } from 'naive-ui'
import { NButton, NDataTable, NFlex, NPopconfirm, NSpace, NSwitch, NTable, NTag } from 'naive-ui'
import cert from '@/api/panel/cert'
Expand All @@ -15,7 +16,7 @@ const props = defineProps({
const { algorithms, websites, accounts, dns, caProviders } = toRefs(props)
let messageReactive: any
let messageReactive: MessageReactive | null = null
const updateCertModel = ref<any>({
domains: [],
Expand Down
17 changes: 14 additions & 3 deletions web/src/views/website/EditView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ defineOptions({
})
import Editor from '@guolao/vue-monaco-editor'
import type { MessageReactive } from 'naive-ui'
import { NButton } from 'naive-ui'
import dashboard from '@/api/panel/dashboard'
import website from '@/api/panel/website'
import type { WebsiteListen, WebsiteSetting } from '@/views/website/types'
let messageReactive: MessageReactive | null = null
const current = ref('listen')
const route = useRoute()
const { id } = route.params
Expand Down Expand Up @@ -97,10 +100,18 @@ const handleReset = async () => {
}
const handleObtainCert = async () => {
await website.obtainCert(Number(id)).then(() => {
getWebsiteSetting()
window.$message.success('成功,请开启 HTTPS 并保存')
messageReactive = window.$message.loading('请稍后...', {
duration: 0
})
await website
.obtainCert(Number(id))
.then(() => {
getWebsiteSetting()
window.$message.success('签发成功')
})
.finally(() => {
messageReactive?.destroy()
})
}
const clearLog = async () => {
Expand Down

0 comments on commit 46ae37a

Please sign in to comment.