Skip to content

Commit

Permalink
feat(Voice): check encryption libraries before playing stream
Browse files Browse the repository at this point in the history
  • Loading branch information
skick1234 committed Oct 27, 2024
1 parent ceb74fd commit a7441f7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/core/DisTubeHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export class DisTubeHandler extends DisTubeBase {
headers: {
"user-agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " +
"Chrome/125.0.0.0 Safari/537.36",
"Chrome/129.0.0.0 Safari/537.3",
},
});

Expand Down
7 changes: 6 additions & 1 deletion src/core/DisTubeVoice.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Constants } from "discord.js";
import { TypedEmitter } from "tiny-typed-emitter";
import { DisTubeError, isSupportedVoiceChannel } from "..";
import { checkEncryptionLibraries, DisTubeError, isSupportedVoiceChannel } from "..";
import {
AudioPlayerStatus,
VoiceConnectionDisconnectReason,
Expand Down Expand Up @@ -114,6 +114,7 @@ export class DisTubeVoice extends TypedEmitter<DisTubeVoiceEvents> {
return joinVoiceChannel({
channelId: channel.id,
guildId: this.id,
// @ts-ignore
adapterCreator: channel.guild.voiceAdapterCreator,
group: channel.client.user?.id,
});
Expand Down Expand Up @@ -161,6 +162,10 @@ export class DisTubeVoice extends TypedEmitter<DisTubeVoiceEvents> {
* @param dtStream - DisTubeStream
*/
play(dtStream: DisTubeStream) {
if (!checkEncryptionLibraries()) {
dtStream.kill();
throw new DisTubeError("ENCRYPTION_LIBRARIES_MISSING");
}
this.emittedError = false;
dtStream.on("error", (error: NodeJS.ErrnoException) => {
if (this.emittedError || error.code === "ERR_STREAM_PREMATURE_CLOSE") return;
Expand Down
2 changes: 2 additions & 0 deletions src/struct/DisTubeError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const ERROR_MESSAGES = {

FFMPEG_EXITED: (code: number) => `ffmpeg exited with code ${code}`,
FFMPEG_NOT_INSTALLED: (path: string) => `ffmpeg is not installed at '${path}' path`,
ENCRYPTION_LIBRARIES_MISSING:
"Cannot play audio as no valid encryption package is installed. Please install sodium-native, sodium, libsodium-wrappers, or tweetnacl.",

NO_QUEUE: "There is no playing queue in this guild",
QUEUE_EXIST: "This guild has a Queue already",
Expand Down
10 changes: 10 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,13 @@ export function isNsfwChannel(channel?: GuildTextBasedChannel): boolean {

export type Falsy = undefined | null | false | 0 | "";
export const isTruthy = <T>(x: T | Falsy): x is T => Boolean(x);

export const checkEncryptionLibraries = () => {
for (const lib of ["sodium-native", "sodium", "libsodium-wrappers", "tweetnacl"]) {
try {
require(lib);
return true;
} catch {}
}
return false;
};

0 comments on commit a7441f7

Please sign in to comment.