From 093975d265b8b6c07b62dd638c6dbc26018d7d02 Mon Sep 17 00:00:00 2001 From: Pedro Sanders Date: Thu, 21 Dec 2023 22:42:56 -0500 Subject: [PATCH] chore: implement custom ICE server configuration support --- .env.example | 2 ++ README.md | 2 ++ docs/docs/self-hosting/deploy-with-docker.md | 4 ++++ mods/apiserver/src/context.ts | 11 +++++++++-- mods/apiserver/src/envs.ts | 3 +++ mods/apiserver/src/tokens/createAnonymousToken.ts | 1 + mods/apiserver/src/tokens/createToken.ts | 3 ++- mods/common/src/types.ts | 1 + mods/frontoffice/src/containers/ChatContainer.tsx | 4 +++- mods/widget/src/GoodtokUA.tsx | 5 ++++- 10 files changed, 31 insertions(+), 5 deletions(-) diff --git a/.env.example b/.env.example index 4cd01b7..2e76a19 100644 --- a/.env.example +++ b/.env.example @@ -12,6 +12,8 @@ OWNER_PASSWORD=changeme # PeerJs Server config SIGNALING_HOST=localhost SIGNALING_PORT=9000 +# Ucomment to enable ICE servers +# ICE_SERVERS_CONFIG='[{"urls": "stun:stun.l.google.com:19302"}, {"urls": "turn:turn.bistri.com:80", "username": "homeo", "credential": "homeo"}]' # SMTP config SMTP_HOST=localhost diff --git a/README.md b/README.md index 825ee95..b143227 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,8 @@ OWNER_PASSWORD=changeme # PeerJs Server config SIGNALING_HOST=localhost SIGNALING_PORT=9000 +# Ucomment to use custom ICE servers +# ICE_SERVERS_CONFIG='[{"url": "stun:stun.l.google.com:19302"}, {"url": "turn:turn.bistri.com:80", "username": "homeo", "credential": "homeo"}]' # SMTP config SMTP_HOST=localhost diff --git a/docs/docs/self-hosting/deploy-with-docker.md b/docs/docs/self-hosting/deploy-with-docker.md index 7997a75..dc77d70 100644 --- a/docs/docs/self-hosting/deploy-with-docker.md +++ b/docs/docs/self-hosting/deploy-with-docker.md @@ -32,6 +32,8 @@ OWNER_PASSWORD=changeme # PeerJs Server config SIGNALING_HOST=localhost SIGNALING_PORT=9000 +# Ucomment to use custom ICE servers +# ICE_SERVERS_CONFIG='[{"url": "stun:stun.l.google.com:19302"}, {"url": "turn:us-turn4.xirsys.com:80?transport=udp", "username": "xirsys", "credential": "xirsys"}]' # SMTP config SMTP_HOST=localhost @@ -57,6 +59,8 @@ Few important things to note: - The `SIGNALLING_PORT` must be the port where the signaling server will be accessible. For example, if you are running the application locally, you can use `9000`. If you are running the application on a server, you can use, for example, `443` - You must point your SMTP variables to a valid SMTP server - Goodtok will use the `OWNER_EMAIL` and `OWNER_PASSWORD` variables to create the first user. The server will create a new owner if the email does not exist. Goodtok will update the password if the email exists. You can use any email address and password for this purpose. +- The `CLOAK_ENCRYPTION_KEY` must be a valid encryption key. You can use [Cloack](https://cloack.47ng.com) to generate a new key. +- Uncomment the `ICE_SERVERS_CONFIG` variable to enable ICE servers. You can use [Xirsys](https://xirsys.com) to get a free account. Finally, run the following command to start the application: diff --git a/mods/apiserver/src/context.ts b/mods/apiserver/src/context.ts index 703471b..9ee7054 100644 --- a/mods/apiserver/src/context.ts +++ b/mods/apiserver/src/context.ts @@ -25,7 +25,8 @@ import { JWT_SIGN_OPTIONS, JWT_SECURITY_SALT, SIGNALING_HOST, - SIGNALING_PORT + SIGNALING_PORT, + ICE_SERVERS_CONFIG } from "./envs"; import jwt from "jsonwebtoken"; @@ -53,7 +54,8 @@ export async function createContext(opts: ContextOptions) { jwtSecuritySalt: JWT_SECURITY_SALT, jwtSignOptions: JWT_SIGN_OPTIONS, signalingHost: SIGNALING_HOST, - signalingPort: SIGNALING_PORT + signalingPort: SIGNALING_PORT, + iceServersConfig: ICE_SERVERS_CONFIG } }; } @@ -68,5 +70,10 @@ export type Context = { jwtSignOptions: jwt.SignOptions; signalingHost: string; signalingPort: string; + iceServersConfig?: { + urls: string | string[]; + username?: string; + credential?: string; + }[]; }; }; diff --git a/mods/apiserver/src/envs.ts b/mods/apiserver/src/envs.ts index 14b1b0e..588bc3e 100644 --- a/mods/apiserver/src/envs.ts +++ b/mods/apiserver/src/envs.ts @@ -51,6 +51,9 @@ export const JWT_SIGN_OPTIONS = e.JWT_SIGN_OPTIONS // PeerJS server configurations export const SIGNALING_HOST = e.SIGNALING_HOST; export const SIGNALING_PORT = e.SIGNALING_PORT ?? "443"; +export const ICE_SERVERS_CONFIG = e.ICE_SERVERS_CONFIG + ? JSON.parse(e.ICE_SERVERS_CONFIG) + : [{ urls: "stun:stun.l.google.com:19302" }]; // SMTP configurations export const SMTP_HOST = e.SMTP_HOST; diff --git a/mods/apiserver/src/tokens/createAnonymousToken.ts b/mods/apiserver/src/tokens/createAnonymousToken.ts index 22d732c..29529c4 100644 --- a/mods/apiserver/src/tokens/createAnonymousToken.ts +++ b/mods/apiserver/src/tokens/createAnonymousToken.ts @@ -41,6 +41,7 @@ export async function createAnonymousToken( ref: ref, signalingHost: ctx.config.signalingHost, signalingPort: ctx.config.signalingPort, + iceServers: ctx.config.iceServersConfig, // Use the same ref as the customerId (only for annonymous users) customerId: ref, workspaceId, diff --git a/mods/apiserver/src/tokens/createToken.ts b/mods/apiserver/src/tokens/createToken.ts index d3eb77c..26eb1a1 100644 --- a/mods/apiserver/src/tokens/createToken.ts +++ b/mods/apiserver/src/tokens/createToken.ts @@ -39,7 +39,8 @@ export async function createToken( customerId: input.customerId, workspaceId: workspaceId, signalingHost: ctx.config.signalingHost, - signalingPort: ctx.config.signalingPort + signalingPort: ctx.config.signalingPort, + iceServers: ctx.config.iceServersConfig }; return jwt.sign( diff --git a/mods/common/src/types.ts b/mods/common/src/types.ts index a114249..c9be947 100644 --- a/mods/common/src/types.ts +++ b/mods/common/src/types.ts @@ -19,6 +19,7 @@ export type ConnectionObject = { signalingHost: string; signalingPort: number; + iceServers: { urls: string | string[]; username?: string; credential?: string }[]; customerId: string; workspaceId: string; calendarUrl: string; diff --git a/mods/frontoffice/src/containers/ChatContainer.tsx b/mods/frontoffice/src/containers/ChatContainer.tsx index c4d4e75..e93b441 100644 --- a/mods/frontoffice/src/containers/ChatContainer.tsx +++ b/mods/frontoffice/src/containers/ChatContainer.tsx @@ -77,7 +77,9 @@ function ChatContainer() { const peer = new Peer(undefined!, { host: connectionObject.signalingHost, port: connectionObject.signalingPort, - debug: 3 + config: { + iceServers: connectionObject.iceServers + } }); setConnectionObject(connectionObject); diff --git a/mods/widget/src/GoodtokUA.tsx b/mods/widget/src/GoodtokUA.tsx index 7f75b91..7221973 100644 --- a/mods/widget/src/GoodtokUA.tsx +++ b/mods/widget/src/GoodtokUA.tsx @@ -72,7 +72,10 @@ const GoodtokUA = () => { const peer = new Peer(connectionObject.customerId, { host: connectionObject.signalingHost, - port: connectionObject.signalingPort + port: connectionObject.signalingPort, + config: { + iceServers: connectionObject.iceServers + } }); setPeer(peer);