diff --git a/cli/.env.example b/cli/.env.example index 9045e78..b29c920 100644 --- a/cli/.env.example +++ b/cli/.env.example @@ -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" diff --git a/cli/lib/peer.ts b/cli/lib/peer.ts index 7c3e25b..76bfbd2 100644 --- a/cli/lib/peer.ts +++ b/cli/lib/peer.ts @@ -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!, + }); diff --git a/shared/lib/peer.ts b/shared/lib/peer.ts index 15617c0..426e191 100644 --- a/shared/lib/peer.ts +++ b/shared/lib/peer.ts @@ -1,5 +1,6 @@ import Peer from 'peerjs'; import { generateId } from './util'; +import { IceServerCredentials } from '../types/peer'; const isServer = typeof window === 'undefined'; @@ -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) => { diff --git a/shared/types/peer.ts b/shared/types/peer.ts new file mode 100644 index 0000000..dfd581b --- /dev/null +++ b/shared/types/peer.ts @@ -0,0 +1,8 @@ +export type IceServerCredentials = { + username?: string; + credential: string; +}; + +export type IceServerConfigurationItem = { urls: string } & IceServerCredentials; + +export type IceServerConfiguration = IceServerConfigurationItem[]; diff --git a/web/.env.example b/web/.env.example index 599d663..d06c0be 100644 --- a/web/.env.example +++ b/web/.env.example @@ -26,3 +26,6 @@ IPINFO_IO_TOKEN=1234567890 STRIPE_SECRET_KEY="" STRIPE_WEBHOOK_SECRET="" NEXT_PUBLIC_STRIPE_LIFETIME_LICENSE_LINK="" + +NEXT_PUBLIC_TURN_USERNAME="username" +NEXT_PUBLIC_TURN_PWD="password" diff --git a/web/lib/peer.ts b/web/lib/peer.ts index f7a4410..bfb23b7 100644 --- a/web/lib/peer.ts +++ b/web/lib/peer.ts @@ -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!, + });