Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/api-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export { XHRUploadClient } from './platform/xhr-upload-client'
export { clearNodeAuthState, nodeAuthState, setNodeAuthState } from './state/node-auth'
export * from './types'
export { withJWTRetry } from './utils/jwt-retry'
export { getNodeWebSocketUrl } from './utils/node-url'
export {
type ParsedSseEvent,
type ParsedSseItem,
Expand Down
8 changes: 7 additions & 1 deletion packages/api-client/src/utils/node-url.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
const NODE_FS_PATH_REGEX = /\/modrinth\/v\d+\/fs\/?$/
const HTTP_SCHEME_REGEX = /^https?:\/\//i
const WS_SCHEME_REGEX = /^wss?:\/\//i
const HTTP_SECURE_SCHEME_REGEX = /^https:\/\//i
const HTTP_INSECURE_SCHEME_REGEX = /^http:\/\//i

export function getNodeBaseUrl(url: string): string {
const baseUrl = url.replace(NODE_FS_PATH_REGEX, '')
return HTTP_SCHEME_REGEX.test(baseUrl) ? baseUrl : `https://${baseUrl}`
}

export function getNodeWebSocketUrl(url: string): string {
return WS_SCHEME_REGEX.test(url) ? url : `wss://${url}`
if (WS_SCHEME_REGEX.test(url)) return url
if (HTTP_SECURE_SCHEME_REGEX.test(url)) return url.replace(HTTP_SECURE_SCHEME_REGEX, 'wss://')
if (HTTP_INSECURE_SCHEME_REGEX.test(url)) return url.replace(HTTP_INSECURE_SCHEME_REGEX, 'ws://')

return `wss://${url}`
}
14 changes: 10 additions & 4 deletions packages/ui/src/layouts/wrapped/hosting/manage/root.vue
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@

<script setup lang="ts">
import type { Archon, Labrinth } from '@modrinth/api-client'
import { ModrinthApiError } from '@modrinth/api-client'
import { getNodeWebSocketUrl, ModrinthApiError } from '@modrinth/api-client'
import {
BoxesIcon,
CheckIcon,
Expand Down Expand Up @@ -1299,9 +1299,15 @@ async function testNodeReachability(): Promise<boolean> {
const nodeInstance = serverData.value?.node?.instance
if (!nodeInstance) return false

const wsUrl = `wss://${nodeInstance}/pingtest`

try {
const auth = await client.archon.servers_v0.getWebSocketAuth(props.serverId)
const authUrl = getNodeWebSocketUrl(auth.url)
const protocol = authUrl.toLowerCase().startsWith('ws://') ? 'ws' : 'wss'
const wsUrl = getNodeWebSocketUrl(`${nodeInstance}/pingtest`).replace(
/^wss?:\/\//i,
`${protocol}://`,
)

return await new Promise((resolve) => {
const socket = new WebSocket(wsUrl)
const timeout = setTimeout(() => {
Expand All @@ -1326,7 +1332,7 @@ async function testNodeReachability(): Promise<boolean> {
}
})
} catch (error) {
console.error(`Failed to ping node ${wsUrl}:`, error)
console.error(`Failed to ping node ${nodeInstance}:`, error)
return false
}
}
Expand Down
Loading