Skip to content

Commit

Permalink
修复插件配置将自定义Host识别为非法地址的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Val-istar-Guo committed Aug 13, 2024
1 parent 8585f79 commit d8461f8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/young-roses-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@opendoc/frontend": patch
"@opendoc/backend": patch
---

修复插件配置将自定义 Host 识别为非法地址的问题
14 changes: 10 additions & 4 deletions app/backend/src/modules/plugin/web-socket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ export class WebSocketService {
) {}

async connect(url: string): Promise<[WebSocket, PluginMetadata]> {
if (!isURL(url, { require_host: true, require_protocol: true, protocols: ['ws'] })) {
if (!isURL(url, { require_host: true, require_tld: false, require_protocol: true, protocols: ['ws'] })) {
throw new BadRequestException('Invalid URL')
}

const ttl = this.appConfig.ttl
console.log('🚀 ~ WebSocketService ~ connect ~ ttl:', ttl)
const appName = this.appConfig.appName
const logger = this.logger

Expand Down Expand Up @@ -52,15 +51,22 @@ export class WebSocketService {
ws.off('message', onMessage)
ws.off('error', onError)

reject(new BadRequestException('Invalid Websocket message'))
reject(new BadRequestException('编译器回应了无法识别的数据格式,请检查 OpenDoc 与 Plugin 版本是否兼容'))
}
}

function onError(err: Error) {
logger.error(`Plugin ${url} is breakdown`)
clearTimeout(ttlHandler)
ws.close()
reject(err)

if ('code' in err && err['code'] === 'ENOTFOUND') {
reject(new BadRequestException(`无法连接至 Plugin(${url})`))
} else if ('code' in err && err['code'] === 'ECONNREFUSED') {
reject(new BadRequestException(`Plugin(${url}) 拒绝连接`))
} else {
reject(err)
}
}

const ws = new WebSocket(url, {
Expand Down
2 changes: 1 addition & 1 deletion app/frontend/pages/administration/plugin-management.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const { status, data: plugins, execute: reload } = useAsyncData(async () => {
const urlInput = ref('')
const pluginUrl = computed(() => `ws://${urlInput.value}`)
const isValidUrl = computed(() => isURL(pluginUrl.value, { require_host: true, require_protocol: true, protocols: ['ws'] }))
const isValidUrl = computed(() => isURL(pluginUrl.value, { require_host: true, require_tld: false, require_protocol: false, protocols: ['ws'] }))
const { pending: appending, execute: create } = useAsyncFn(async () => {
if (!pluginUrl.value) return
Expand Down

0 comments on commit d8461f8

Please sign in to comment.