Prettier
This commit is contained in:
+10
-2
@@ -11,7 +11,15 @@ export class Server {
|
||||
public server: http.Server;
|
||||
public production: boolean;
|
||||
|
||||
constructor({ port, server, production }: { port: number; server?: http.Server; production?: boolean }) {
|
||||
constructor({
|
||||
port,
|
||||
server,
|
||||
production,
|
||||
}: {
|
||||
port: number;
|
||||
server?: http.Server;
|
||||
production?: boolean;
|
||||
}) {
|
||||
this.port = port;
|
||||
this.production = production || false;
|
||||
|
||||
@@ -53,4 +61,4 @@ export class Server {
|
||||
closeDatabase();
|
||||
this.server.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,4 +6,4 @@ export async function onClose(this: WebSocket, code: number, reason: string) {
|
||||
|
||||
if (this.session_id) await Session.delete({ session_id: this.session_id });
|
||||
this.removeAllListeners();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,11 @@ try {
|
||||
// TODO: specify rate limit in config
|
||||
// TODO: check msg max size
|
||||
|
||||
export async function Connection(this: WS.Server, socket: WebSocket, request: IncomingMessage) {
|
||||
export async function Connection(
|
||||
this: WS.Server,
|
||||
socket: WebSocket,
|
||||
request: IncomingMessage,
|
||||
) {
|
||||
try {
|
||||
socket.on("close", onClose.bind(socket));
|
||||
socket.on("message", onMessage.bind(socket));
|
||||
@@ -29,7 +33,7 @@ export async function Connection(this: WS.Server, socket: WebSocket, request: In
|
||||
"open",
|
||||
"ping",
|
||||
"pong",
|
||||
"unexpected-response"
|
||||
"unexpected-response",
|
||||
].forEach((x) => {
|
||||
socket.on(x, (y) => console.log("[WebRTC]", x, y));
|
||||
});
|
||||
@@ -39,7 +43,8 @@ export async function Connection(this: WS.Server, socket: WebSocket, request: In
|
||||
|
||||
socket.encoding = "json";
|
||||
socket.version = Number(searchParams.get("v")) || 5;
|
||||
if (socket.version < 3) return socket.close(CLOSECODES.Unknown_error, "invalid version");
|
||||
if (socket.version < 3)
|
||||
return socket.close(CLOSECODES.Unknown_error, "invalid version");
|
||||
|
||||
setHeartbeat(socket);
|
||||
|
||||
@@ -50,11 +55,11 @@ export async function Connection(this: WS.Server, socket: WebSocket, request: In
|
||||
await Send(socket, {
|
||||
op: VoiceOPCodes.HELLO,
|
||||
d: {
|
||||
heartbeat_interval: 1000 * 30
|
||||
}
|
||||
heartbeat_interval: 1000 * 30,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("[WebRTC]", error);
|
||||
return socket.close(CLOSECODES.Unknown_error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,13 +7,14 @@ const PayloadSchema = {
|
||||
op: Number,
|
||||
$d: new Tuple(Object, Number), // or number for heartbeat sequence
|
||||
$s: Number,
|
||||
$t: String
|
||||
$t: String,
|
||||
};
|
||||
|
||||
export async function onMessage(this: WebSocket, buffer: Buffer) {
|
||||
try {
|
||||
var data: Payload = JSON.parse(buffer.toString());
|
||||
if (data.op !== VoiceOPCodes.IDENTIFY && !this.user_id) return this.close(CLOSECODES.Not_authenticated);
|
||||
if (data.op !== VoiceOPCodes.IDENTIFY && !this.user_id)
|
||||
return this.close(CLOSECODES.Not_authenticated);
|
||||
|
||||
// @ts-ignore
|
||||
const OPCodeHandler = OPCodeHandlers[data.op];
|
||||
@@ -25,7 +26,11 @@ export async function onMessage(this: WebSocket, buffer: Buffer) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (![VoiceOPCodes.HEARTBEAT, VoiceOPCodes.SPEAKING].includes(data.op as VoiceOPCodes)) {
|
||||
if (
|
||||
![VoiceOPCodes.HEARTBEAT, VoiceOPCodes.SPEAKING].includes(
|
||||
data.op as VoiceOPCodes,
|
||||
)
|
||||
) {
|
||||
// @ts-ignore
|
||||
console.log("[WebRTC] Opcode " + VoiceOPCodes[data.op]);
|
||||
}
|
||||
@@ -35,4 +40,4 @@ export async function onMessage(this: WebSocket, buffer: Buffer) {
|
||||
console.error("[WebRTC] error", error);
|
||||
// if (!this.CLOSED && this.CLOSING) return this.close(CloseCodes.Unknown_error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,2 +1,2 @@
|
||||
export * from "./Server";
|
||||
export * from "./util/index";
|
||||
export * from "./util/index";
|
||||
|
||||
@@ -2,5 +2,8 @@ import { Payload, Send, WebSocket } from "@fosscord/gateway";
|
||||
import { VoiceOPCodes } from "../util";
|
||||
|
||||
export async function onBackendVersion(this: WebSocket, data: Payload) {
|
||||
await Send(this, { op: VoiceOPCodes.VOICE_BACKEND_VERSION, d: { voice: "0.8.43", rtc_worker: "0.3.26" } });
|
||||
}
|
||||
await Send(this, {
|
||||
op: VoiceOPCodes.VOICE_BACKEND_VERSION,
|
||||
d: { voice: "0.8.43", rtc_worker: "0.3.26" },
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
import { CLOSECODES, Payload, Send, setHeartbeat, WebSocket } from "@fosscord/gateway";
|
||||
import {
|
||||
CLOSECODES,
|
||||
Payload,
|
||||
Send,
|
||||
setHeartbeat,
|
||||
WebSocket,
|
||||
} from "@fosscord/gateway";
|
||||
import { VoiceOPCodes } from "../util";
|
||||
|
||||
export async function onHeartbeat(this: WebSocket, data: Payload) {
|
||||
@@ -6,4 +12,4 @@ export async function onHeartbeat(this: WebSocket, data: Payload) {
|
||||
if (isNaN(data.d)) return this.close(CLOSECODES.Decode_error);
|
||||
|
||||
await Send(this, { op: VoiceOPCodes.HEARTBEAT_ACK, d: data.d });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,33 +1,46 @@
|
||||
import { CLOSECODES, Payload, Send, WebSocket } from "@fosscord/gateway";
|
||||
import { validateSchema, VoiceIdentifySchema, VoiceState } from "@fosscord/util";
|
||||
import {
|
||||
validateSchema,
|
||||
VoiceIdentifySchema,
|
||||
VoiceState,
|
||||
} from "@fosscord/util";
|
||||
import { endpoint, getClients, VoiceOPCodes, PublicIP } from "@fosscord/webrtc";
|
||||
import SemanticSDP from "semantic-sdp";
|
||||
const defaultSDP = require("./sdp.json");
|
||||
|
||||
export async function onIdentify(this: WebSocket, data: Payload) {
|
||||
clearTimeout(this.readyTimeout);
|
||||
const { server_id, user_id, session_id, token, streams, video } = validateSchema("VoiceIdentifySchema", data.d) as VoiceIdentifySchema;
|
||||
const { server_id, user_id, session_id, token, streams, video } =
|
||||
validateSchema("VoiceIdentifySchema", data.d) as VoiceIdentifySchema;
|
||||
|
||||
const voiceState = await VoiceState.findOne({ where: { guild_id: server_id, user_id, token, session_id } });
|
||||
const voiceState = await VoiceState.findOne({
|
||||
where: { guild_id: server_id, user_id, token, session_id },
|
||||
});
|
||||
if (!voiceState) return this.close(CLOSECODES.Authentication_failed);
|
||||
|
||||
this.user_id = user_id;
|
||||
this.session_id = session_id;
|
||||
const sdp = SemanticSDP.SDPInfo.expand(defaultSDP);
|
||||
sdp.setDTLS(SemanticSDP.DTLSInfo.expand({ setup: "actpass", hash: "sha-256", fingerprint: endpoint.getDTLSFingerprint() }));
|
||||
sdp.setDTLS(
|
||||
SemanticSDP.DTLSInfo.expand({
|
||||
setup: "actpass",
|
||||
hash: "sha-256",
|
||||
fingerprint: endpoint.getDTLSFingerprint(),
|
||||
}),
|
||||
);
|
||||
|
||||
this.client = {
|
||||
websocket: this,
|
||||
out: {
|
||||
tracks: new Map()
|
||||
tracks: new Map(),
|
||||
},
|
||||
in: {
|
||||
audio_ssrc: 0,
|
||||
video_ssrc: 0,
|
||||
rtx_ssrc: 0
|
||||
rtx_ssrc: 0,
|
||||
},
|
||||
sdp,
|
||||
channel_id: voiceState.channel_id
|
||||
channel_id: voiceState.channel_id,
|
||||
};
|
||||
|
||||
const clients = getClients(voiceState.channel_id)!;
|
||||
@@ -51,10 +64,10 @@ export async function onIdentify(this: WebSocket, data: Payload) {
|
||||
"xsalsa20_poly1305_lite_rtpsize",
|
||||
"xsalsa20_poly1305_lite",
|
||||
"xsalsa20_poly1305_suffix",
|
||||
"xsalsa20_poly1305"
|
||||
"xsalsa20_poly1305",
|
||||
],
|
||||
ip: PublicIP,
|
||||
experiments: []
|
||||
}
|
||||
experiments: [],
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,10 @@ import SemanticSDP, { MediaInfo, SDPInfo } from "semantic-sdp";
|
||||
export async function onSelectProtocol(this: WebSocket, payload: Payload) {
|
||||
if (!this.client) return;
|
||||
|
||||
const data = validateSchema("SelectProtocolSchema", payload.d) as SelectProtocolSchema;
|
||||
const data = validateSchema(
|
||||
"SelectProtocolSchema",
|
||||
payload.d,
|
||||
) as SelectProtocolSchema;
|
||||
|
||||
const offer = SemanticSDP.SDPInfo.parse("m=audio\n" + data.sdp!);
|
||||
this.client.sdp!.setICE(offer.getICE());
|
||||
@@ -25,14 +28,14 @@ export async function onSelectProtocol(this: WebSocket, payload: Payload) {
|
||||
const candidate = candidates[0];
|
||||
|
||||
const answer =
|
||||
`m=audio ${port} ICE/SDP`
|
||||
+ `a=fingerprint:${fingerprint}`
|
||||
+ `c=IN IP4 ${PublicIP}`
|
||||
+ `a=rtcp:${port}`
|
||||
+ `a=ice-ufrag:${ice.getUfrag()}`
|
||||
+ `a=ice-pwd:${ice.getPwd()}`
|
||||
+ `a=fingerprint:${fingerprint}`
|
||||
+ `a=candidate:1 1 ${candidate.getTransport()} ${candidate.getFoundation()} ${candidate.getAddress()} ${candidate.getPort()} typ host`;
|
||||
`m=audio ${port} ICE/SDP` +
|
||||
`a=fingerprint:${fingerprint}` +
|
||||
`c=IN IP4 ${PublicIP}` +
|
||||
`a=rtcp:${port}` +
|
||||
`a=ice-ufrag:${ice.getUfrag()}` +
|
||||
`a=ice-pwd:${ice.getPwd()}` +
|
||||
`a=fingerprint:${fingerprint}` +
|
||||
`a=candidate:1 1 ${candidate.getTransport()} ${candidate.getFoundation()} ${candidate.getAddress()} ${candidate.getPort()} typ host`;
|
||||
|
||||
await Send(this, {
|
||||
op: VoiceOPCodes.SELECT_PROTOCOL_ACK,
|
||||
@@ -40,7 +43,7 @@ export async function onSelectProtocol(this: WebSocket, payload: Payload) {
|
||||
video_codec: "H264",
|
||||
sdp: answer,
|
||||
media_session_id: this.session_id,
|
||||
audio_codec: "opus"
|
||||
}
|
||||
audio_codec: "opus",
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,8 +15,8 @@ export async function onSpeaking(this: WebSocket, data: Payload) {
|
||||
d: {
|
||||
user_id: client.websocket.user_id,
|
||||
speaking: data.d.speaking,
|
||||
ssrc: ssrc?.audio_ssrc || 0
|
||||
}
|
||||
ssrc: ssrc?.audio_ssrc || 0,
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
+27
-12
@@ -21,8 +21,8 @@ export async function onVideo(this: WebSocket, payload: Payload) {
|
||||
SemanticSDP.StreamInfo.expand({
|
||||
id,
|
||||
// @ts-ignore
|
||||
tracks: []
|
||||
})
|
||||
tracks: [],
|
||||
}),
|
||||
);
|
||||
this.client.in.stream = stream;
|
||||
|
||||
@@ -46,8 +46,8 @@ export async function onVideo(this: WebSocket, payload: Payload) {
|
||||
SemanticSDP.StreamInfo.expand({
|
||||
id: "out" + this.user_id,
|
||||
// @ts-ignore
|
||||
tracks: []
|
||||
})
|
||||
tracks: [],
|
||||
}),
|
||||
);
|
||||
this.client.out.stream = out;
|
||||
|
||||
@@ -64,20 +64,35 @@ export async function onVideo(this: WebSocket, payload: Payload) {
|
||||
}
|
||||
|
||||
if (d.audio_ssrc) {
|
||||
handleSSRC.call(this, "audio", { media: d.audio_ssrc, rtx: d.audio_ssrc + 1 });
|
||||
handleSSRC.call(this, "audio", {
|
||||
media: d.audio_ssrc,
|
||||
rtx: d.audio_ssrc + 1,
|
||||
});
|
||||
}
|
||||
if (d.video_ssrc && d.rtx_ssrc) {
|
||||
handleSSRC.call(this, "video", { media: d.video_ssrc, rtx: d.rtx_ssrc });
|
||||
handleSSRC.call(this, "video", {
|
||||
media: d.video_ssrc,
|
||||
rtx: d.rtx_ssrc,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function attachTrack(this: WebSocket, track: IncomingStreamTrack, user_id: string) {
|
||||
function attachTrack(
|
||||
this: WebSocket,
|
||||
track: IncomingStreamTrack,
|
||||
user_id: string,
|
||||
) {
|
||||
if (!this.client) return;
|
||||
const outTrack = this.client.transport!.createOutgoingStreamTrack(track.getMedia());
|
||||
const outTrack = this.client.transport!.createOutgoingStreamTrack(
|
||||
track.getMedia(),
|
||||
);
|
||||
outTrack.attachTo(track);
|
||||
this.client.out.stream!.addTrack(outTrack);
|
||||
var ssrcs = this.client.out.tracks.get(user_id)!;
|
||||
if (!ssrcs) ssrcs = this.client.out.tracks.set(user_id, { audio_ssrc: 0, rtx_ssrc: 0, video_ssrc: 0 }).get(user_id)!;
|
||||
if (!ssrcs)
|
||||
ssrcs = this.client.out.tracks
|
||||
.set(user_id, { audio_ssrc: 0, rtx_ssrc: 0, video_ssrc: 0 })
|
||||
.get(user_id)!;
|
||||
|
||||
if (track.getMedia() === "audio") {
|
||||
ssrcs.audio_ssrc = outTrack.getSSRCs().media!;
|
||||
@@ -90,8 +105,8 @@ function attachTrack(this: WebSocket, track: IncomingStreamTrack, user_id: strin
|
||||
op: VoiceOPCodes.VIDEO,
|
||||
d: {
|
||||
user_id: user_id,
|
||||
...ssrcs
|
||||
} as VoiceVideoSchema
|
||||
...ssrcs,
|
||||
} as VoiceVideoSchema,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -115,4 +130,4 @@ function handleSSRC(this: WebSocket, type: "audio" | "video", ssrcs: SSRCs) {
|
||||
attachTrack.call(this, track, client.websocket.user_id);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,5 +15,5 @@ export default {
|
||||
[VoiceOPCodes.VOICE_BACKEND_VERSION]: onBackendVersion,
|
||||
[VoiceOPCodes.VIDEO]: onVideo,
|
||||
[VoiceOPCodes.SPEAKING]: onSpeaking,
|
||||
[VoiceOPCodes.SELECT_PROTOCOL]: onSelectProtocol
|
||||
};
|
||||
[VoiceOPCodes.SELECT_PROTOCOL]: onSelectProtocol,
|
||||
};
|
||||
|
||||
@@ -417,4 +417,4 @@
|
||||
}
|
||||
],
|
||||
"candidates": []
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -8,6 +8,6 @@ config();
|
||||
const port = Number(process.env.PORT) || 3004;
|
||||
|
||||
const server = new Server({
|
||||
port
|
||||
port,
|
||||
});
|
||||
server.start();
|
||||
server.start();
|
||||
|
||||
@@ -3,7 +3,7 @@ export enum VoiceStatus {
|
||||
CONNECTING = 1,
|
||||
AUTHENTICATING = 2,
|
||||
RECONNECTING = 3,
|
||||
DISCONNECTED = 4
|
||||
DISCONNECTED = 4,
|
||||
}
|
||||
|
||||
export enum VoiceOPCodes {
|
||||
@@ -22,5 +22,5 @@ export enum VoiceOPCodes {
|
||||
SESSION_UPDATE = 14,
|
||||
MEDIA_SINK_WANTS = 15,
|
||||
VOICE_BACKEND_VERSION = 16,
|
||||
CHANNEL_OPTIONS_UPDATE = 17
|
||||
}
|
||||
CHANNEL_OPTIONS_UPDATE = 17,
|
||||
}
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import { WebSocket } from "@fosscord/gateway";
|
||||
import MediaServer, { IncomingStream, OutgoingStream, Transport } from "medooze-media-server";
|
||||
import MediaServer, {
|
||||
IncomingStream,
|
||||
OutgoingStream,
|
||||
Transport,
|
||||
} from "medooze-media-server";
|
||||
import SemanticSDP from "semantic-sdp";
|
||||
MediaServer.enableLog(true);
|
||||
|
||||
@@ -13,7 +17,11 @@ try {
|
||||
|
||||
MediaServer.setPortRange(min, max);
|
||||
} catch (error) {
|
||||
console.error("Invalid env var: WEBRTC_PORT_RANGE", process.env.WEBRTC_PORT_RANGE, error);
|
||||
console.error(
|
||||
"Invalid env var: WEBRTC_PORT_RANGE",
|
||||
process.env.WEBRTC_PORT_RANGE,
|
||||
error,
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
@@ -48,4 +56,4 @@ export interface Client {
|
||||
export function getClients(channel_id: string) {
|
||||
if (!channels.has(channel_id)) channels.set(channel_id, new Set());
|
||||
return channels.get(channel_id)!;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
export * from "./Constants";
|
||||
export * from "./MediaServer";
|
||||
export * from "./MediaServer";
|
||||
|
||||
Reference in New Issue
Block a user