Skip to content

Commit

Permalink
fix: fix website domain bind error;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed May 15, 2022
1 parent 96449b8 commit 441970b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
19 changes: 15 additions & 4 deletions packages/app-console/src/views/storage/websites.vue
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
<el-button @click="editFormVisible = false">
取消
</el-button>
<el-button type="primary" @click="handleBindDomain">
<el-button type="primary" @click="handleBindDomain" :loading="loading">
确定
</el-button>
</div>
Expand Down Expand Up @@ -228,15 +228,26 @@ export default {
this.$refs.editForm.validate(async valid => {
if (!valid) return
const REGEX_DOMAIN = /^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/
if(REGEX_DOMAIN.test(this.editForm.domain) === false) {
showError('域名格式不正确')
return
}
const params = {
website_id: this.editForm._id,
domain: this.editForm.domain
}
this.loading = true
const ret = await websiteAPI.bindDomain(params)
.finally(() => this.loading = false)
if (ret.code) {
showError(ret.error)
return
if (ret.code === 'DOMAIN_NOT_RESOLVEABLE') {
return showError('解析失败,请先对该域名做 CNAME 解析')
}
if(ret.code === 'DOMAIN_RESOLVED_ERROR') {
return showError('解析错误,请使用正确的 CNAME 解析值')
}
this.$notify({
Expand Down
1 change: 1 addition & 0 deletions packages/system-server/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const BUCKET_QUOTA_MIN = 1 * GB

/** regex const */
export const REGEX_BUCKET_NAME = /^[a-z0-9]{1,16}$/
export const REGEX_DOMAIN = /^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/

/** collection name of cloud functions published to app db */
export const CN_PUBLISHED_FUNCTIONS = '__published__functions'
Expand Down
21 changes: 12 additions & 9 deletions packages/system-server/src/handler/website/domain-bind.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { ObjectId } from "mongodb"
import * as dns from "node:dns"
import { CN_WEBSITE_HOSTING } from "../../constants"
import { CN_WEBSITE_HOSTING, REGEX_DOMAIN } from "../../constants"
import { Request, Response } from "express"
import { CONST_DICTS } from "../../constants"
import { checkPermission } from "../../support/permission"
import { IApplicationData } from "../../support/application"
import { DatabaseAgent } from "../../db"
import { logger } from "../../support/logger"
// import { handleCheckDomain } from "./domain-check"

/**
Expand All @@ -15,8 +16,9 @@ export async function handleBindDomain(req: Request, res: Response) {
const db = DatabaseAgent.db
const app: IApplicationData = req["parsed-app"]
const uid = req["auth"]?.uid
const { domain, website_id } = req.body

const website_id = req.body?.website_id
const domain = req.body?.domain || ''

// check login
if (!uid) {
res.status(401).send()
Expand All @@ -38,18 +40,19 @@ export async function handleBindDomain(req: Request, res: Response) {
}

// check domain
if (!domain) {
if (REGEX_DOMAIN.test(domain) === false) {
return res.status(422).send("invalid domain")
}

// check domain is available
const dnsPromise = dns.promises
const result = await dnsPromise.resolveCname(domain as string).catch(() => {})
const resolver = new dns.promises.Resolver({ timeout: 3000, tries: 2 })
const result = await resolver.resolveCname(domain as string).catch(() => { })
if (!result) {
return res.send({code: 'DOMAIN_NOT_RESOLVEABLE', error: 'domain is not resolveable'})
return res.send({ code: 'DOMAIN_NOT_RESOLVEABLE', error: 'domain is not resolveable' })
}
if ((result || []).includes(website.cname)) {
return res.send({code: 'DOMAIN_RESOLVED_ERROR', error: 'error resolved result got'})

if (false === (result || []).includes(website.cname)) {
return res.send({ code: 'DOMAIN_RESOLVED_ERROR', error: 'error resolved result got' })
}

// check if domain is already binded
Expand Down
4 changes: 2 additions & 2 deletions packages/system-server/src/handler/website/website-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export async function handleCreateWebsite(req: Request, res: Response) {

// generate cname url for bucket and appid
const endpoint = new URL(Config.MINIO_CONFIG.endpoint.external)
const { host } = endpoint
const cname = `${app.appid}-${bucket}.${host}`
const { hostname } = endpoint
const cname = `${app.appid}-${bucket}.${hostname}`

// build website hosting data
const doc = {
Expand Down

0 comments on commit 441970b

Please sign in to comment.