diff --git a/bin/peerjs.js b/bin/peerjs.js index be8ddec..af54fb7 100755 --- a/bin/peerjs.js +++ b/bin/peerjs.js @@ -4,8 +4,19 @@ const { ExpressPeerServer } = require('peer') const app = express(); const server = app.listen(9000); + +const peerServers = process.env.PEERJS_SERVERS + ? process.env.PEERJS_SERVERS.split(',').map(url => url.trim()) + : []; + +if (peerServers.length > 0) { + app.use('/api/peerjs-servers', (req, res) => { + res.json({ servers: peerServers }); + }); +} + const peerServer = ExpressPeerServer(server, { path: '/filepizza' }) -app.use('/peerjs', peerServer) +app.use('/peerjs', peerServer) \ No newline at end of file diff --git a/docker-compose.cloudflare.yml b/docker-compose.cloudflare.yml index 7352d31..83fe363 100644 --- a/docker-compose.cloudflare.yml +++ b/docker-compose.cloudflare.yml @@ -2,7 +2,7 @@ services: redis: image: redis:latest ports: - - 127.0.0.1:6379:6379 + - 127.0.0.1:${REDIS_PORT:-6379}:6379 networks: - filepizza volumes: @@ -29,12 +29,13 @@ services: - 127.0.0.1:8080:80 environment: - PORT=80 - - REDIS_URL=redis://redis:6379 + - REDIS_URL=redis://redis:${REDIS_PORT:-6379} - COTURN_ENABLED=true - TURN_REALM=https://${HOST_DOMAIN} - TURN_HOST=${HOST_DOMAIN} - API_ORIGINS=${API_ORIGINS} - CLOUDFLARE_TUNNEL=true + - PEERJS_SERVERS=${PEERJS_SERVERS:-} networks: - filepizza depends_on: diff --git a/docker-compose.production.yml b/docker-compose.production.yml index 0a24b03..f353fd4 100644 --- a/docker-compose.production.yml +++ b/docker-compose.production.yml @@ -2,7 +2,7 @@ services: redis: image: redis:latest ports: - - 127.0.0.1:6379:6379 + - 127.0.0.1:${REDIS_PORT:-6379}:6379 networks: - filepizza volumes: @@ -28,11 +28,12 @@ services: - 0.0.0.0:80:80 environment: - PORT=80 - - REDIS_URL=redis://redis:6379 + - REDIS_URL=redis://redis:${REDIS_PORT:-6379} + - PEERJS_SERVERS=${PEERJS_SERVERS:-} - COTURN_ENABLED=true - # - TURN_REALM=https://emaily.re - # - TURN_HOST=https://emaily.re - # - API_ORIGINS=https://emaily.re,https://texlyre.github.io + - TURN_REALM=https://${HOST_DOMAIN} + - TURN_HOST=${HOST_DOMAIN} + - API_ORIGINS=${API_ORIGINS} networks: - filepizza depends_on: @@ -45,4 +46,4 @@ networks: driver: bridge volumes: - redis_data: + redis_data: \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index c04d3e9..f605cfe 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,7 +2,7 @@ services: redis: image: redis:latest ports: - - 6379:6379 + - ${REDIS_PORT:-6379}:6379 networks: - filepizza volumes: @@ -29,8 +29,9 @@ services: - 8080:8080 environment: - PORT=8080 - - REDIS_URL=redis://redis:6379 + - REDIS_URL=redis://redis:${REDIS_PORT:-6379} - API_ORIGINS=http://localhost:8081 + - PEERJS_SERVERS=${PEERJS_SERVERS:-} networks: - filepizza depends_on: diff --git a/envfile b/envfile index 652ae18..f4ae1f4 100644 --- a/envfile +++ b/envfile @@ -6,6 +6,10 @@ NODE_ENV=production # Host Configuration HOST_DOMAIN=filepizza.example.com +# PeerJS URL and Redis PORT +# REDIS_PORT=6379 +# PEERJS_SERVERS=peerjs.mydomain:9000 + # CORS Configuration API_ORIGINS=https://filepizza.example.com,https://myapp.example.com,http://localhost:8081 diff --git a/src/components/WebRTCProvider.tsx b/src/components/WebRTCProvider.tsx index 7a52bf8..c05902d 100644 --- a/src/components/WebRTCProvider.tsx +++ b/src/components/WebRTCProvider.tsx @@ -37,12 +37,34 @@ async function getOrCreateGlobalPeer(): Promise { const { iceServers } = await response.json() console.log('[WebRTCProvider] ICE servers:', iceServers) - globalPeer = new Peer({ + let peerConfig: any = { debug: 3, config: { iceServers, }, - }) + } + + try { + const peerServersResponse = await fetch('/api/peerjs-servers') + if (peerServersResponse.ok) { + const { servers } = await peerServersResponse.json() + if (servers && servers.length > 0) { + const serverUrl = new URL(servers[0]) + peerConfig = { + ...peerConfig, + host: serverUrl.hostname, + port: serverUrl.port ? parseInt(serverUrl.port) : (serverUrl.protocol === 'https:' ? 443 : 80), + path: serverUrl.pathname, + secure: serverUrl.protocol === 'https:', + } + console.log('[WebRTCProvider] Using custom PeerJS server:', peerConfig) + } + } + } catch (error) { + console.log('[WebRTCProvider] No custom PeerJS servers configured, using default') + } + + globalPeer = new Peer(peerConfig) } if (globalPeer.id) {