forked from nikkiii/node-mumble-ping
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
92 lines (81 loc) · 2.73 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const dgram = require('dgram')
/**
* Response of the Mumble Ping.
* @typedef {Object} MumblePingResponse
* @property {string} version - Version of the mumble server.
* @property {number} users - Number of users currently online.
* @property {number} maxUsers - Maximum number of users that can connect simultaneously.
* @property {number} bandwidth - Maximum bandwidth in b/s per user.
*/
function _getPingBody () {
const requestBody = Buffer.alloc(12)
requestBody.writeUInt32BE(0, 0)
requestBody.writeUInt32BE(1234, 4)
requestBody.writeUInt32BE(5678, 8)
return requestBody
}
/**
* Ping the given mumble server and query version, number of users and bandwidth
* @param {string} host Host to connect to.
* @param {number} port Port to connect to. Mumble's default port is 64738
* @param {number} requestTimeout Number of milliseconds to wait before rejecting with a timeout error
* @returns {Promise<MumblePingResponse>} Promise that resolves with the response of the ping if successful.
*/
function pingMumble (host, port = 64738, requestTimeout = 5000) {
const client = dgram.createSocket('udp4')
return new Promise(function (resolve, reject) {
const timeout = setTimeout(() => {
reject(new Error('Timeout'))
client.close()
}, requestTimeout)
client.on('error', (e) => {
clearTimeout(timeout)
client.close()
reject(e)
})
client.on('message', (message) => {
clearTimeout(timeout)
const major = message.readUInt16BE(0)
const minor = message.readUInt8(2)
const patch = message.readUInt8(3)
const version = [major, minor, patch]
client.close()
resolve({
version: version.join('.'),
users: message.readUInt32BE(12),
maxUsers: message.readUInt32BE(16),
bandwidth: message.readUInt32BE(20)
})
})
const requestBody = _getPingBody()
client.send(requestBody, 0, requestBody.length, port, host)
})
}
/**
* Callback for MumblePing
* @callback MumblePingCallback
* @param {Error} error
* @param {MumblePingResponse} response
*/
/**
* Ping the given mumble server and query version, number of users and bandwidth
* @param {string} host Host to connect to.
* @param {number} port Port to connect to. Mumble's default port is 64738
* @param {MumblePingCallback} callback
*/
function MumblePing (host, port, callback) {
const DEFAULT_MUMBLE_PORT = 64738
let callbackFn = callback
let actualPort = port
if (!callback && typeof port === 'function') {
callbackFn = port
actualPort = DEFAULT_MUMBLE_PORT
}
pingMumble(host, actualPort).then(response => {
callbackFn(null, response)
}).catch(error => {
callbackFn(error, null)
})
}
exports.MumblePing = MumblePing
exports.pingMumble = pingMumble