설명 없음
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

server.js 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // BizGaze Connect — backend entry point.
  2. // Thin wiring layer: HTTP request dispatch + WebSocket attach + listeners.
  3. // All logic lives in focused modules:
  4. // repos.js data-access (all SQL)
  5. // bizgaze.js BizGaze identity provider
  6. // lib.js HTTP helpers (json/readBody/parseCookies/now)
  7. // session.js currentUser / audit
  8. // presence.js shared in-memory live state (agents/sessions/shares)
  9. // routes.js HTTP JSON API (/api/*, /sso)
  10. // static.js static files + authenticated downloads (GET fallback)
  11. // signaling.js WebSocket signaling (consent + SDP/ICE relay)
  12. const http = require('http');
  13. const https = require('https');
  14. const fs = require('fs');
  15. const path = require('path');
  16. const { WebSocketServer } = require('ws');
  17. const { PORT, HTTPS_PORT } = require('./config');
  18. const { json } = require('./lib');
  19. const routes = require('./routes');
  20. const { handleGet } = require('./static');
  21. const { onConnection } = require('./signaling');
  22. // ---------- HTTP request dispatch ----------
  23. const server = http.createServer((req, res) => {
  24. const key = `${req.method} ${req.url.split('?')[0]}`;
  25. if (routes[key]) return routes[key](req, res);
  26. if (req.method === 'GET') return handleGet(req, res); // downloads + static
  27. json(res, 404, { error: 'not found' });
  28. });
  29. // ---------- WebSocket signaling ----------
  30. const wss = new WebSocketServer({ server, path: '/ws' });
  31. wss.on('connection', onConnection);
  32. server.listen(PORT, () => {
  33. console.log(`HTTP on http://localhost:${PORT}`);
  34. });
  35. // HTTPS — required so other devices can share their screen (browsers block
  36. // screen capture on non-secure origins). Uses cert.pem/key.pem if present.
  37. let httpsServer = null;
  38. try {
  39. const certPath = path.join(__dirname, 'cert.pem');
  40. const keyPath = path.join(__dirname, 'key.pem');
  41. if (fs.existsSync(certPath) && fs.existsSync(keyPath)) {
  42. httpsServer = https.createServer(
  43. { cert: fs.readFileSync(certPath), key: fs.readFileSync(keyPath) },
  44. (req, res) => server.emit('request', req, res)
  45. );
  46. const wssSecure = new WebSocketServer({ server: httpsServer, path: '/ws' });
  47. wssSecure.on('connection', onConnection);
  48. httpsServer.listen(HTTPS_PORT, () => {
  49. console.log(`HTTPS on https://localhost:${HTTPS_PORT} (use this address from other devices)`);
  50. console.log(` End user shares screen: https://<this-pc-ip>:${HTTPS_PORT}/share`);
  51. console.log(` Technician connects: https://<this-pc-ip>:${HTTPS_PORT}/connect`);
  52. });
  53. } else {
  54. console.log('(No cert.pem/key.pem found — HTTPS disabled. Other devices can view but not share their screen.)');
  55. }
  56. } catch (e) {
  57. console.log('HTTPS failed to start:', e.message);
  58. }
  59. module.exports = { server };