Skip to content

Commit

Permalink
Support srv records.
Browse files Browse the repository at this point in the history
  • Loading branch information
dscalzi committed Mar 18, 2023
1 parent 01dbd4c commit 2c037d8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
21 changes: 19 additions & 2 deletions lib/mojang/net/ServerStatusAPI.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* eslint-disable no-control-regex */
import { SrvRecord } from 'dns'
import { resolveSrv } from 'dns/promises'
import { connect } from 'net'
import { LoggerUtil } from '../../util/LoggerUtil'
import { ServerBoundPacket, ClientBoundPacket, ProtocolUtils } from './Protocol'
Expand Down Expand Up @@ -82,9 +84,24 @@ function unifyStatusResponse(resp: ServerStatus): ServerStatus {
return resp
}

export function getServerStatus(protocol: number, hostname: string, port = 25565): Promise<ServerStatus> {
async function checkSrv(hostname: string): Promise<SrvRecord | null> {
try {
const records = await resolveSrv(`_minecraft._tcp.${hostname}`)
return records.length > 0 ? records[0] : null
} catch(err) {
return null
}
}

export async function getServerStatus(protocol: number, hostname: string, port = 25565): Promise<ServerStatus> {

const srvRecord = await checkSrv(hostname)
if(srvRecord != null) {
hostname = srvRecord.name
port = srvRecord.port
}

return new Promise((resolve, reject) => {
return await new Promise((resolve, reject) => {

const socket = connect(port, hostname, () => {
socket.write(getHandshakePacket(protocol, hostname, port))
Expand Down
3 changes: 2 additions & 1 deletion test/mojang/net/ServerStatusAPITest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ function verifyResult(res: ServerStatus): void {
const serversToCheck = [
'play.hypixel.net',
'mc.westeroscraft.com',
'us.mineplex.com'
'us.mineplex.com',
// 'stoneblock.colaian.tech' // SRV - commented out b/c may be shut down.
]

describe('[Server Status API] Server Status', () => {
Expand Down

0 comments on commit 2c037d8

Please sign in to comment.