mirror of https://github.com/kern/filepizza
Add coturn support
parent
1c1cc16e60
commit
9e7d78c755
@ -1,4 +1,5 @@
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
.next
|
.next
|
||||||
node_modules
|
node_modules
|
||||||
dist
|
dist
|
||||||
|
.env
|
||||||
@ -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
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
}
|
||||||
@ -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)
|
||||||
|
}
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
import Redis from 'ioredis'
|
||||||
|
|
||||||
|
export { Redis }
|
||||||
|
|
||||||
|
let redisClient: Redis.Redis | null = null
|
||||||
|
|
||||||
|
export function getRedisClient(): Redis.Redis {
|
||||||
|
if (!redisClient) {
|
||||||
|
redisClient = new Redis(process.env.REDIS_URL)
|
||||||
|
}
|
||||||
|
return redisClient
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue