Skip to content

Commit

Permalink
Fix servers edition (fixes #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMicky-FR committed Dec 29, 2023
1 parent 4bbdb8e commit 76ed871
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 38 deletions.
16 changes: 6 additions & 10 deletions frontend/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,14 @@ export function fetchRecentStats() {
return client.get<RecentServersStats>('/servers/stats/recent')
}

export function saveServers(
token: string,
servers: ServerDescription[],
): Promise<Record<string, string>> {
return client.post('/servers/update', JSON.stringify({ token, servers }))
export function saveServers(token: string, servers: ServerDescription[]) {
const params = { token, servers }

return client.post<Record<string, string>>('/servers/update', params)
}

export function uploadServerIcons(
token: string,
icons: Record<string, string>,
): Promise<Record<string, string>> {
return client.post('/servers/icons', JSON.stringify({ token, icons }))
export function saveServerIcons(token: string, icons: Record<string, string>) {
return client.post<Record<string, string>>('/servers/icons', { token, icons })
}

export function encodeFileAsBase64(file: File): Promise<string> {
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/ServersEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ServerDescription } from '@/api'
import { onMounted, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import { fetchServers, saveServers, uploadServerIcons } from '@/api'
import { fetchServers, saveServers, saveServerIcons } from '@/api'
import BLoader from '@/components/BLoader.vue'
import EditorServer from '@/components/EditorServer.vue'
import ThemeButton from '@/components/ThemeButton.vue'
Expand Down Expand Up @@ -53,7 +53,7 @@ async function save() {
try {
if (Object.keys(pendingIcons).length) {
await uploadServerIcons(token.value, pendingIcons)
await saveServerIcons(token.value, pendingIcons)
Object.keys(pendingIcons).forEach((key) => delete pendingIcons[key])
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Minecraft servers tracker built on Cloudflare Workers with Vue.js.",
"scripts": {
"start": "wrangler dev",
"publish": "wrangler publish",
"deploy": "wrangler deploy",
"format": "prettier --write '*.{json,js}' 'src/**/*.ts'",
"lint": "eslint --max-warnings=0 src && prettier --check '*.{json,js}' 'src/**/*.ts'"
},
Expand Down
26 changes: 17 additions & 9 deletions src/protocol/dns.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
export async function resolveSrv(name: string) {
const response = await fetch(
`https://cloudflare-dns.com/dns-query?name=${name}&type=srv&ct=application/dns-json`,
{
headers: {
Accept: 'application/dns-json',
},
const params = new URLSearchParams({
name,
type: 'src',
ct: 'application/dns-json',
})

const res = await fetch(`https://cloudflare-dns.com/dns-query?${params}`, {
headers: {
Accept: 'application/dns-json',
},
)
})

if (!res.ok) {
console.error(`Invalid DNS response for ${name}: ${res.status}`)
return undefined
}

const json = await response.text()
const data = JSON.parse(json)
const data = JSON.parse(await res.text())

if (!data.Answer?.length) {
console.error(`No DNS records for ${name}}.`)
return undefined
}

Expand Down
18 changes: 7 additions & 11 deletions src/protocol/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ async function rawStatus(hostname: string, port: number) {
await writer.write(createHandshakePacket(hostname, port))
await writer.write(createRequestPacket())

const response = new SocketReader(socket.readable.getReader())
const packetLength = await response.readVarInt()
const packetType = await response.readVarInt()
const reader = new SocketReader(socket.readable.getReader())
const packetLength = await reader.readVarInt()
const packetType = await reader.readVarInt()

await response.ensureByteLength(packetLength)
await reader.ensureByteLength(packetLength)

if (packetType !== 0x00) {
throw new Error(`Unexpected packet type: ${packetType}`)
}

const json = await response.readStringVarInt()
const json = await reader.readStringVarInt()

await writer.close()
await socket.close()
Expand All @@ -67,7 +67,7 @@ function createRequestPacket() {
}

class SocketWriter {
private encoder = new TextEncoder()
private readonly encoder = new TextEncoder()
private data = new Uint8Array(0)

write(byteLength: number, callback: (view: DataView) => void) {
Expand All @@ -79,11 +79,7 @@ class SocketWriter {
}

writeBytes(data: Uint8Array) {
const buffer = new Uint8Array(this.data.length + data.length)
buffer.set(this.data, 0)
buffer.set(data, this.data.length)

this.data = buffer
this.data = concatBuffers(this.data, data)

return this
}
Expand Down
11 changes: 6 additions & 5 deletions wrangler.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@ PING_ATTEMPTS = 2
# Errors will be sent as a Discord webhook to this URL
# WEBHOOK_URL = ""

# By default stats in the indivudal servers charts are deleted after 2 hours (120 min)
# By default, stats in the indivudal servers charts are deleted after 2 hours (120 min)
RECENT_CHARTS_DELETE_AFTER_MINUTES = 120

# By default the global server chart will have data
# with a 15 min interval
# By default, the global server chart will have data
# with a 15-min interval
GLOBAL_CHART_PING_INTERVAL = 15
# By default stats in the global server chart are deleted after 6 months (180 days)
# By default, stats in the global server chart are deleted after 6 months (180 days)
GLOBAL_CHART_DELETE_AFTER_DAYS = 180

# You can define a token to edit the servers from the /edit page
# The token should be at least 10 characters
SERVERS_EDIT_TOKEN = ""

# You can define here the URL used to get the servers protocol for Minecraft: Java Edition
# If not defined, the workers will directly get the status from the server
# If not defined, the workers will directly get the status from the server.
# Directly getting the status from the server may consume more CPU.
# STATUS_URL = "https://api.mcstatus.io/v2/status/java/{address}?query=false"

# URL used to get the servers status for Minecraft: Bedrock Edition
Expand Down

0 comments on commit 76ed871

Please sign in to comment.