@zaisma/game-server 1.47.0 → 1.49.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bench/glass.js +685 -89
- package/bin/local-play-bench.js +1 -1
- package/bin/local-play.js +2 -2
- package/package.json +1 -1
- package/sdk/zaisma.d.ts +88 -1
- package/sdk/zaisma.js +510 -3
- package/src/chat.d.ts +11 -0
- package/src/chat.js +11 -0
- package/src/doors.d.ts +1 -1
- package/src/index.d.ts +3 -2
- package/src/index.js +3 -2
- package/src/internal.d.ts +1 -0
- package/src/internal.js +1 -0
- package/src/pass.d.ts +15 -0
- package/src/pass.js +16 -0
- package/src/room-calls.d.ts +9 -0
- package/src/room-calls.js +21 -1
- package/src/room.d.ts +99 -1
- package/src/room.js +93 -8
- package/src/voice-ticket.d.ts +56 -0
- package/src/voice-ticket.js +107 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { VOICE_ROOM_LIMITS } from "./chat.js";
|
|
2
|
+
import { fromBase64Url } from "./pass.js";
|
|
3
|
+
/** A channel's name: 1 to 40 of a-z, 0-9, -, _ and :. */
|
|
4
|
+
export const VOICE_CHANNEL_NAME_PATTERN = /^[a-z0-9_:-]{1,40}$/;
|
|
5
|
+
export class VoiceTicketError extends Error {
|
|
6
|
+
code;
|
|
7
|
+
constructor(code) {
|
|
8
|
+
super(`Voice ticket refused: ${code}`);
|
|
9
|
+
this.name = "VoiceTicketError";
|
|
10
|
+
this.code = code;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
const encoder = new TextEncoder();
|
|
14
|
+
const toBase64Url = (bytes) => {
|
|
15
|
+
let binary = "";
|
|
16
|
+
for (const byte of bytes)
|
|
17
|
+
binary += String.fromCharCode(byte);
|
|
18
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
19
|
+
};
|
|
20
|
+
const macKey = (ticketSecret) => crypto.subtle.importKey("raw", encoder.encode(ticketSecret), { name: "HMAC", hash: "SHA-256" }, false, ["sign", "verify"]);
|
|
21
|
+
/**
|
|
22
|
+
* Sign a voice ticket for a player whose pass your server checked (verifyPass), for a channel your server names, like
|
|
23
|
+
* "match-12" or "team-red-12". Hand it to the player's game, which joins with room.voice.join({ ticket }), and send a new
|
|
24
|
+
* one when their team or range changes (room.voice.updateTicket). It lasts 5 minutes: enough to join or update.
|
|
25
|
+
*/
|
|
26
|
+
export async function voiceTicket(playerPass, channelName, ticketSecret, voiceTicketOptions = {}) {
|
|
27
|
+
if (!playerPass || typeof playerPass.sub !== "string" || typeof playerPass.aud !== "string")
|
|
28
|
+
throw new TypeError("voiceTicket takes a pass's claims, as verifyPass answered them.");
|
|
29
|
+
if (!VOICE_CHANNEL_NAME_PATTERN.test(channelName))
|
|
30
|
+
throw new TypeError("voiceTicket's channelName is 1 to 40 of a-z, 0-9, -, _ and :.");
|
|
31
|
+
if (typeof ticketSecret !== "string" || ticketSecret.length < 32)
|
|
32
|
+
throw new TypeError("voiceTicket needs the game's ticket secret (create_ticket_secret).");
|
|
33
|
+
const groupName = voiceTicketOptions.voiceGroupName ?? null;
|
|
34
|
+
if (groupName !== null && (typeof groupName !== "string" || !groupName || groupName.length > VOICE_ROOM_LIMITS.maximumVoiceGroupNameCharacterCount))
|
|
35
|
+
throw new TypeError(`voiceGroupName is 1 to ${VOICE_ROOM_LIMITS.maximumVoiceGroupNameCharacterCount} characters, or null.`);
|
|
36
|
+
const heardSpeakerCount = voiceTicketOptions.heardSpeakerCount ?? VOICE_ROOM_LIMITS.defaultHeardSpeakerCount;
|
|
37
|
+
if (!Number.isInteger(heardSpeakerCount) || heardSpeakerCount < 1 || heardSpeakerCount > VOICE_ROOM_LIMITS.maximumHeardSpeakerCount)
|
|
38
|
+
throw new TypeError(`heardSpeakerCount is a whole number from 1 to ${VOICE_ROOM_LIMITS.maximumHeardSpeakerCount}.`);
|
|
39
|
+
const heard = voiceTicketOptions.heardSpeakerPlayerIds ?? null;
|
|
40
|
+
if (heard !== null && (!Array.isArray(heard) || heard.length > 100 || heard.some((playerId) => typeof playerId !== "string")))
|
|
41
|
+
throw new TypeError("heardSpeakerPlayerIds is up to 100 player IDs, or null.");
|
|
42
|
+
const claims = {
|
|
43
|
+
ticket_kind: "voice",
|
|
44
|
+
aud: playerPass.aud,
|
|
45
|
+
sub: playerPass.sub,
|
|
46
|
+
channel_name: channelName,
|
|
47
|
+
voice_group_name: groupName,
|
|
48
|
+
heard_speaker_ids: heard ? [...new Set(heard)] : null,
|
|
49
|
+
heard_speaker_count: heardSpeakerCount,
|
|
50
|
+
exp: Math.floor(Date.now() / 1000) + VOICE_ROOM_LIMITS.voiceTicketDurationSec,
|
|
51
|
+
};
|
|
52
|
+
const body = toBase64Url(encoder.encode(JSON.stringify(claims)));
|
|
53
|
+
const signature = new Uint8Array(await crypto.subtle.sign("HMAC", await macKey(ticketSecret), encoder.encode(body)));
|
|
54
|
+
return `${body}.${toBase64Url(signature)}`;
|
|
55
|
+
}
|
|
56
|
+
/** The channel a ticket names, unchecked: for the SDK to know where to connect. Null for anything else. */
|
|
57
|
+
export function voiceTicketChannelName(ticket) {
|
|
58
|
+
if (typeof ticket !== "string" || ticket.length > 8000)
|
|
59
|
+
return null;
|
|
60
|
+
try {
|
|
61
|
+
const claims = JSON.parse(new TextDecoder().decode(fromBase64Url(ticket.split(".")[0])));
|
|
62
|
+
return typeof claims.channel_name === "string" && VOICE_CHANNEL_NAME_PATTERN.test(claims.channel_name) ? claims.channel_name : null;
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/** Check a voice ticket (Zaisma's voice channels do): its signature with the game's ticket secret, then what it's for. */
|
|
69
|
+
export async function verifyVoiceTicket(ticket, ticketSecret, expectations) {
|
|
70
|
+
if (typeof ticket !== "string" || ticket.length > 8000)
|
|
71
|
+
throw new VoiceTicketError("malformed");
|
|
72
|
+
const [body, signature, extra] = ticket.split(".");
|
|
73
|
+
if (!body || !signature || extra !== undefined)
|
|
74
|
+
throw new VoiceTicketError("malformed");
|
|
75
|
+
let isSigned;
|
|
76
|
+
try {
|
|
77
|
+
isSigned = await crypto.subtle.verify("HMAC", await macKey(ticketSecret), fromBase64Url(signature), encoder.encode(body));
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
throw new VoiceTicketError("malformed");
|
|
81
|
+
}
|
|
82
|
+
if (!isSigned)
|
|
83
|
+
throw new VoiceTicketError("bad_signature");
|
|
84
|
+
let claims;
|
|
85
|
+
try {
|
|
86
|
+
claims = JSON.parse(new TextDecoder().decode(fromBase64Url(body)));
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
throw new VoiceTicketError("malformed");
|
|
90
|
+
}
|
|
91
|
+
if (!claims || typeof claims !== "object" || claims.ticket_kind !== "voice")
|
|
92
|
+
throw new VoiceTicketError("wrong_kind");
|
|
93
|
+
const isShaped = (claims.voice_group_name === null || (typeof claims.voice_group_name === "string" && claims.voice_group_name.length >= 1 && claims.voice_group_name.length <= VOICE_ROOM_LIMITS.maximumVoiceGroupNameCharacterCount)) &&
|
|
94
|
+
(claims.heard_speaker_ids === null || (Array.isArray(claims.heard_speaker_ids) && claims.heard_speaker_ids.length <= 100 && claims.heard_speaker_ids.every((playerId) => typeof playerId === "string"))) &&
|
|
95
|
+
Number.isInteger(claims.heard_speaker_count) && claims.heard_speaker_count >= 1 && claims.heard_speaker_count <= VOICE_ROOM_LIMITS.maximumHeardSpeakerCount;
|
|
96
|
+
if (!isShaped)
|
|
97
|
+
throw new VoiceTicketError("malformed");
|
|
98
|
+
if (typeof claims.exp !== "number" || claims.exp <= (expectations.nowSec ?? Math.floor(Date.now() / 1000)))
|
|
99
|
+
throw new VoiceTicketError("expired");
|
|
100
|
+
if (claims.aud !== expectations.gameSlug)
|
|
101
|
+
throw new VoiceTicketError("wrong_game");
|
|
102
|
+
if (claims.sub !== expectations.playerId)
|
|
103
|
+
throw new VoiceTicketError("wrong_player");
|
|
104
|
+
if (claims.channel_name !== expectations.channelName)
|
|
105
|
+
throw new VoiceTicketError("wrong_channel");
|
|
106
|
+
return claims;
|
|
107
|
+
}
|