Skip to content

Commit

Permalink
Merge pull request #55 from dallen4/custom-turn
Browse files Browse the repository at this point in the history
Update TURN Server Configuration
  • Loading branch information
dallen4 authored Nov 23, 2023
2 parents 5ca9d3b + b7d7eeb commit df584d7
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 3 deletions.
3 changes: 3 additions & 0 deletions cli/.env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
DEADDROP_API_URL=https://api-base.com
PEER_SERVER_URL=https://peer-server.com/endpoint

TURN_USERNAME="username"
TURN_PWD="password"
5 changes: 4 additions & 1 deletion cli/lib/peer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { createPeer } from '@shared/lib/peer';

export const initPeer = () =>
createPeer(process.env.PEER_SERVER_URL!);
createPeer(process.env.PEER_SERVER_URL!, {
username: process.env.TURN_USERNAME!,
credential: process.env.TURN_PWD!,
});
30 changes: 29 additions & 1 deletion shared/lib/peer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Peer from 'peerjs';
import { generateId } from './util';
import { IceServerCredentials } from '../types/peer';

const isServer = typeof window === 'undefined';

Expand All @@ -18,15 +19,42 @@ export const removeOnUnloadListener = () => {
}
};

export function createPeer(url: string) {
export function createPeer(
url: string,
{ username, credential }: IceServerCredentials,
) {
const id = generateId();
const server = new URL(url);

const iceConfig = {
iceServers: [
{
urls: 'stun:stun.relay.metered.ca:80',
},
{
urls: 'turn:a.relay.metered.ca:80',
username,
credential,
},
{
urls: 'turn:a.relay.metered.ca:443',
username,
credential,
},
{
urls: 'turn:a.relay.metered.ca:443?transport=tcp',
username,
credential,
},
],
};

const peer = new Peer(id, {
host: server.host,
path: server.pathname,
secure: true,
port: 443,
config: iceConfig,
});

peer.on('call', (call) => {
Expand Down
8 changes: 8 additions & 0 deletions shared/types/peer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export type IceServerCredentials = {
username?: string;
credential: string;
};

export type IceServerConfigurationItem = { urls: string } & IceServerCredentials;

export type IceServerConfiguration = IceServerConfigurationItem[];
3 changes: 3 additions & 0 deletions web/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ IPINFO_IO_TOKEN=1234567890
STRIPE_SECRET_KEY="<secret_key>"
STRIPE_WEBHOOK_SECRET="<webhook_secret>"
NEXT_PUBLIC_STRIPE_LIFETIME_LICENSE_LINK="<payment_link>"

NEXT_PUBLIC_TURN_USERNAME="username"
NEXT_PUBLIC_TURN_PWD="password"
5 changes: 4 additions & 1 deletion web/lib/peer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { createPeer } from '@shared/lib/peer';

export const initPeer = () =>
createPeer(process.env.NEXT_PUBLIC_PEER_SERVER_URL!);
createPeer(process.env.NEXT_PUBLIC_PEER_SERVER_URL!, {
username: process.env.NEXT_PUBLIC_TURN_USERNAME!,
credential: process.env.NEXT_PUBLIC_TURN_PWD!,
});

0 comments on commit df584d7

Please sign in to comment.