Improve e2ee setup (#111)
This commit is contained in:
parent
a267b9410b
commit
9ca05b1c3c
3 changed files with 48 additions and 45 deletions
|
@ -1,16 +0,0 @@
|
||||||
// just for demonstration purposes, extremely insecure
|
|
||||||
|
|
||||||
import { BaseKeyProvider, createKeyMaterialFromString } from 'livekit-client';
|
|
||||||
|
|
||||||
export class DummyKeyProvider extends BaseKeyProvider {
|
|
||||||
readonly participantKeys = new Map([
|
|
||||||
['dev1', 'dev1key'],
|
|
||||||
['dev2', 'dev2key'],
|
|
||||||
]);
|
|
||||||
|
|
||||||
async setKey(participantId: string) {
|
|
||||||
// @ts-ignore
|
|
||||||
const cryptoKey = await createKeyMaterialFromString(this.participantKeys.get(participantId));
|
|
||||||
this.onSetEncryptionKey(cryptoKey, participantId);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,5 +1,12 @@
|
||||||
import { formatChatMessageLinks, LiveKitRoom, VideoConference } from '@livekit/components-react';
|
import { formatChatMessageLinks, LiveKitRoom, VideoConference } from '@livekit/components-react';
|
||||||
import { ExternalE2EEKeyProvider, LogLevel, Room, RoomOptions, VideoPresets } from 'livekit-client';
|
import {
|
||||||
|
ExternalE2EEKeyProvider,
|
||||||
|
LogLevel,
|
||||||
|
RoomConnectOptions,
|
||||||
|
Room,
|
||||||
|
RoomOptions,
|
||||||
|
VideoPresets,
|
||||||
|
} from 'livekit-client';
|
||||||
import { useRouter } from 'next/router';
|
import { useRouter } from 'next/router';
|
||||||
import { DebugMode } from '../../lib/Debug';
|
import { DebugMode } from '../../lib/Debug';
|
||||||
import { decodePassphrase } from '../../lib/client-utils';
|
import { decodePassphrase } from '../../lib/client-utils';
|
||||||
|
@ -10,14 +17,12 @@ export default function CustomRoomConnection() {
|
||||||
const { liveKitUrl, token } = router.query;
|
const { liveKitUrl, token } = router.query;
|
||||||
|
|
||||||
const hash = typeof window !== 'undefined' && window.location.hash;
|
const hash = typeof window !== 'undefined' && window.location.hash;
|
||||||
const keyProvider = new ExternalE2EEKeyProvider();
|
|
||||||
if (hash) {
|
|
||||||
keyProvider.setKey(decodePassphrase(hash.substring(1)));
|
|
||||||
}
|
|
||||||
|
|
||||||
const worker =
|
const worker =
|
||||||
typeof window !== 'undefined' &&
|
typeof window !== 'undefined' &&
|
||||||
new Worker(new URL('livekit-client/e2ee-worker', import.meta.url));
|
new Worker(new URL('livekit-client/e2ee-worker', import.meta.url));
|
||||||
|
const keyProvider = new ExternalE2EEKeyProvider();
|
||||||
|
|
||||||
|
const e2eeEnabled = !!(hash && worker);
|
||||||
|
|
||||||
const roomOptions = useMemo((): RoomOptions => {
|
const roomOptions = useMemo((): RoomOptions => {
|
||||||
return {
|
return {
|
||||||
|
@ -26,17 +31,26 @@ export default function CustomRoomConnection() {
|
||||||
},
|
},
|
||||||
adaptiveStream: { pixelDensity: 'screen' },
|
adaptiveStream: { pixelDensity: 'screen' },
|
||||||
dynacast: true,
|
dynacast: true,
|
||||||
e2ee:
|
e2ee: e2eeEnabled
|
||||||
hash && worker
|
? {
|
||||||
? {
|
keyProvider,
|
||||||
keyProvider,
|
worker,
|
||||||
worker,
|
}
|
||||||
}
|
: undefined,
|
||||||
: undefined,
|
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const room = useMemo(() => new Room(roomOptions), []);
|
const room = useMemo(() => new Room(roomOptions), []);
|
||||||
|
if (e2eeEnabled) {
|
||||||
|
keyProvider.setKey(decodePassphrase(hash.substring(1)));
|
||||||
|
room.setE2EEEnabled(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
const connectOptions = useMemo((): RoomConnectOptions => {
|
||||||
|
return {
|
||||||
|
autoSubscribe: false,
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
if (typeof liveKitUrl !== 'string') {
|
if (typeof liveKitUrl !== 'string') {
|
||||||
return <h2>Missing LiveKit URL</h2>;
|
return <h2>Missing LiveKit URL</h2>;
|
||||||
|
@ -48,7 +62,14 @@ export default function CustomRoomConnection() {
|
||||||
return (
|
return (
|
||||||
<main data-lk-theme="default">
|
<main data-lk-theme="default">
|
||||||
{liveKitUrl && (
|
{liveKitUrl && (
|
||||||
<LiveKitRoom room={room} token={token} serverUrl={liveKitUrl} audio={true} video={true}>
|
<LiveKitRoom
|
||||||
|
room={room}
|
||||||
|
token={token}
|
||||||
|
connectOptions={connectOptions}
|
||||||
|
serverUrl={liveKitUrl}
|
||||||
|
audio={true}
|
||||||
|
video={true}
|
||||||
|
>
|
||||||
<VideoConference chatMessageFormatter={formatChatMessageLinks} />
|
<VideoConference chatMessageFormatter={formatChatMessageLinks} />
|
||||||
<DebugMode logLevel={LogLevel.info} />
|
<DebugMode logLevel={LogLevel.info} />
|
||||||
</LiveKitRoom>
|
</LiveKitRoom>
|
||||||
|
|
|
@ -21,7 +21,6 @@ import { useRouter } from 'next/router';
|
||||||
import { useMemo, useState } from 'react';
|
import { useMemo, useState } from 'react';
|
||||||
import { DebugMode } from '../../lib/Debug';
|
import { DebugMode } from '../../lib/Debug';
|
||||||
import { decodePassphrase, useServerUrl } from '../../lib/client-utils';
|
import { decodePassphrase, useServerUrl } from '../../lib/client-utils';
|
||||||
import { DummyKeyProvider } from '../../lib/DummyKeyProvider';
|
|
||||||
|
|
||||||
const Home: NextPage = () => {
|
const Home: NextPage = () => {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
@ -87,15 +86,13 @@ const ActiveRoom = ({ roomName, userChoices, onLeave }: ActiveRoomProps) => {
|
||||||
const liveKitUrl = useServerUrl(region as string | undefined);
|
const liveKitUrl = useServerUrl(region as string | undefined);
|
||||||
|
|
||||||
const hash = typeof window !== 'undefined' && window.location.hash;
|
const hash = typeof window !== 'undefined' && window.location.hash;
|
||||||
const keyProvider = new ExternalE2EEKeyProvider();
|
|
||||||
if (hash) {
|
|
||||||
keyProvider.setKey(decodePassphrase(hash.substring(1)));
|
|
||||||
}
|
|
||||||
|
|
||||||
const worker =
|
const worker =
|
||||||
typeof window !== 'undefined' &&
|
typeof window !== 'undefined' &&
|
||||||
new Worker(new URL('livekit-client/e2ee-worker', import.meta.url));
|
new Worker(new URL('livekit-client/e2ee-worker', import.meta.url));
|
||||||
|
|
||||||
|
const e2eeEnabled = !!(hash && worker);
|
||||||
|
const keyProvider = new ExternalE2EEKeyProvider();
|
||||||
|
|
||||||
const roomOptions = useMemo((): RoomOptions => {
|
const roomOptions = useMemo((): RoomOptions => {
|
||||||
return {
|
return {
|
||||||
videoCaptureDefaults: {
|
videoCaptureDefaults: {
|
||||||
|
@ -113,20 +110,21 @@ const ActiveRoom = ({ roomName, userChoices, onLeave }: ActiveRoomProps) => {
|
||||||
},
|
},
|
||||||
adaptiveStream: { pixelDensity: 'screen' },
|
adaptiveStream: { pixelDensity: 'screen' },
|
||||||
dynacast: true,
|
dynacast: true,
|
||||||
e2ee:
|
e2ee: e2eeEnabled
|
||||||
hash && worker
|
? {
|
||||||
? {
|
keyProvider,
|
||||||
keyProvider,
|
worker,
|
||||||
worker,
|
}
|
||||||
}
|
: undefined,
|
||||||
: undefined,
|
|
||||||
};
|
};
|
||||||
}, [userChoices, hq]);
|
}, [userChoices, hq]);
|
||||||
|
|
||||||
const room = useMemo(() => new Room(roomOptions), []);
|
const room = useMemo(() => new Room(roomOptions), []);
|
||||||
|
|
||||||
room.setE2EEEnabled(true);
|
if (e2eeEnabled) {
|
||||||
|
keyProvider.setKey(decodePassphrase(hash.substring(1)));
|
||||||
|
room.setE2EEEnabled(true);
|
||||||
|
}
|
||||||
const connectOptions = useMemo((): RoomConnectOptions => {
|
const connectOptions = useMemo((): RoomConnectOptions => {
|
||||||
return {
|
return {
|
||||||
autoSubscribe: false,
|
autoSubscribe: false,
|
||||||
|
|
Loading…
Reference in a new issue