-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
210 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
.DS_Store | ||
.next | ||
node_modules | ||
dist | ||
dist | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
node_modules | ||
dist | ||
tsconfig.tsbuildinfo | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { NextResponse } from 'next/server' | ||
import crypto from 'crypto' | ||
import { setTurnCredentials } from '../../../coturn' | ||
|
||
const turnHost = process.env.TURN_HOST || '127.0.0.1' | ||
|
||
export async function POST(): Promise<NextResponse> { | ||
if (!process.env.COTURN_ENABLED) { | ||
return NextResponse.json({ | ||
iceServers: [ | ||
{ urls: 'stun:stun.l.google.com:19302' } | ||
] | ||
}) | ||
} | ||
|
||
// Generate ephemeral credentials | ||
const username = crypto.randomBytes(8).toString('hex') | ||
const password = crypto.randomBytes(8).toString('hex') | ||
const ttl = 86400 // 24 hours | ||
|
||
// Store credentials in Redis | ||
await setTurnCredentials(username, password, ttl) | ||
|
||
return NextResponse.json({ | ||
iceServers: [ | ||
{ urls: 'stun:stun.l.google.com:19302' }, | ||
{ | ||
urls: [ | ||
`turn:${turnHost}:3478`, | ||
`turns:${turnHost}:5349` | ||
], | ||
username, | ||
credential: password | ||
} | ||
] | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import crypto from 'crypto' | ||
import { getRedisClient } from './redisClient' | ||
|
||
function generateHMACKey(username: string, realm: string, password: string): string { | ||
const str = `${username}:${realm}:${password}` | ||
return crypto.createHash('md5').update(str).digest('hex') | ||
} | ||
|
||
export async function setTurnCredentials( | ||
username: string, | ||
password: string, | ||
ttl: number | ||
): Promise<void> { | ||
if (!process.env.COTURN_ENABLED) { | ||
return | ||
} | ||
|
||
const realm = process.env.TURN_REALM || 'file.pizza' | ||
|
||
if (!realm) { | ||
throw new Error('TURN_REALM environment variable not set') | ||
} | ||
|
||
const redis = getRedisClient() | ||
|
||
const hmacKey = generateHMACKey(username, realm, password) | ||
const key = `turn/realm/${realm}/user/${username}/key` | ||
|
||
await redis.setex(key, ttl, hmacKey) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.