|
17 | 17 | 'use strict';
|
18 | 18 | var logger = require('../logger').logger;
|
19 | 19 | var log = logger.getLogger('MediaBridge');
|
| 20 | +const cipher = require('../cipher'); |
20 | 21 | var addon = require('../quicCascading/build/Release/quicCascading.node');
|
21 | 22 | const QuicTransportStreamPipeline =
|
22 | 23 | require('./quicTransportStreamPipeline');
|
@@ -432,6 +433,7 @@ module.exports = function (rpcClient, selfRpcId, parentRpcId, clusterWorkerIP) {
|
432 | 433 | var info = {
|
433 | 434 | type: 'cluster',
|
434 | 435 | room: data.room,
|
| 436 | + token: data.token, |
435 | 437 | cluster: data.selfCluster
|
436 | 438 | }
|
437 | 439 | quicStream.send(JSON.stringify(info));
|
@@ -543,113 +545,133 @@ module.exports = function (rpcClient, selfRpcId, parentRpcId, clusterWorkerIP) {
|
543 | 545 |
|
544 | 546 | //work as quic server to wait for another OWT cluster to establish quic connection
|
545 | 547 | const start = function () {
|
546 |
| - server = new addon.QuicTransportServer(port, cf, kf); |
547 |
| - |
548 |
| - server.start(); |
549 |
| - server.onNewSession((session) => { |
550 |
| - session.connected = true; |
551 |
| - var dest; |
552 |
| - var sessionId = session.getId(); |
553 |
| - if (!clusters[sessionId]) { |
554 |
| - clusters[sessionId] = {}; |
555 |
| - } |
556 |
| - clusters[sessionId].quicsession = session; |
557 |
| - clusters[sessionId].id = sessionId; |
558 |
| - |
559 |
| - log.info("Server get new session:", sessionId); |
560 |
| - session.onNewStream((quicStream) => { |
561 |
| - var streamId = quicStream.getId(); |
562 |
| - log.info("Server get new stream id:", streamId); |
563 |
| - if (clusters[dest]) { |
564 |
| - if (!clusters[dest].streams) { |
565 |
| - clusters[dest].streams = {}; |
566 |
| - } |
| 548 | + const keystore = path.resolve(path.dirname(global.config.bridge.keystorePath), cipher.kstore); |
| 549 | + cipher.unlock(cipher.k, keystore, (error, password) => { |
| 550 | + if (error) { |
| 551 | + log.error('Failed to read certificate and key.'); |
| 552 | + return; |
| 553 | + } |
| 554 | + log.info('path is '+path.resolve(global.config.bridge.keystorePath)); |
567 | 555 |
|
568 |
| - if (!clusters[dest].streams[streamId]) { |
569 |
| - clusters[dest].streams[streamId] = {}; |
570 |
| - } |
571 |
| - clusters[dest].streams[streamId].quicstream = quicStream; |
572 |
| - } |
573 | 556 |
|
574 |
| - quicStream.onStreamData((msg) => { |
575 |
| - var info = JSON.parse(msg); |
576 |
| - log.info("Server get stream data:", info, " in stream:", streamId, " and session:", sessionId); |
577 |
| - if (info.type === 'cluster') { |
578 |
| - dest = info.cluster + '-' + info.room; |
579 |
| - session.dest = dest; |
580 |
| - if (!clusters[dest]) { |
581 |
| - clusters[dest] = {}; |
582 |
| - } |
583 |
| - clusters[dest].quicsession = session; |
584 |
| - clusters[dest].signalStream = quicStream; |
585 |
| - rpcReq.getController(clusterName, info.room) |
586 |
| - .then(function(controller) { |
587 |
| - clusters[dest].controller = controller; |
588 |
| - }); |
589 |
| - } else if (info.type === 'track') { |
590 |
| - log.info("Server stream id:", streamId, " get track msg and track info:", info, " in stream:", streamId, " and session:", sessionId); |
591 |
| - var conn = createStreamPipeline(info.id, 'out', info.options); |
592 |
| - conn.quicStream(quicStream); |
593 |
| - if (!conn) { |
594 |
| - return; |
| 557 | + server = new addon.QuicTransportServer(port, path.resolve(global.config.bridge.keystorePath),password); |
| 558 | + |
| 559 | + server.start(); |
| 560 | + server.onNewSession((session) => { |
| 561 | + session.connected = true; |
| 562 | + var dest; |
| 563 | + var sessionId = session.getId(); |
| 564 | + if (!clusters[sessionId]) { |
| 565 | + clusters[sessionId] = {}; |
| 566 | + } |
| 567 | + clusters[sessionId].quicsession = session; |
| 568 | + clusters[sessionId].id = sessionId; |
| 569 | + |
| 570 | + log.info("Server get new session:", sessionId); |
| 571 | + session.onNewStream((quicStream) => { |
| 572 | + var streamId = quicStream.getId(); |
| 573 | + log.info("Server get new stream id:", streamId); |
| 574 | + if (clusters[dest]) { |
| 575 | + if (!clusters[dest].streams) { |
| 576 | + clusters[dest].streams = {}; |
| 577 | + } |
| 578 | + |
| 579 | + if (!clusters[dest].streams[streamId]) { |
| 580 | + clusters[dest].streams[streamId] = {}; |
| 581 | + } |
| 582 | + clusters[dest].streams[streamId].quicstream = quicStream; |
595 | 583 | }
|
596 |
| - quicStream.trackKind = info.kind; |
597 |
| - var connid = 'quic-' + info.id; |
598 |
| - router.addLocalDestination(info.id, 'mediabridge', conn); |
599 |
| - |
600 |
| - } else if (info.type === 'subscribe') { |
601 |
| - log.info("Server stream id:", streamId, " get subscribe msg with subscribe info:", info, " and session:", sessionId); |
602 |
| - info.options.locality = {agent: parentRpcId, node: selfRpcId}; |
603 |
| - var connectionId = info.options.connectionId; |
604 |
| - var str = connectionId.split('-'); |
605 |
| - connectionids[str[2]] = connectionId; |
606 |
| - log.info("get connection id:", connectionId, " split str", str[2], " connectionids:", connectionids); |
607 |
| - if (clusters[dest].controller) { |
608 |
| - rpcReq.subscribe(clusters[dest].controller, 'admin', connectionId, info.options); |
609 |
| - } else { |
610 |
| - rpcReq.getController(clusterName, info.options.room) |
611 |
| - .then(function(controller) { |
612 |
| - clusters[dest].controller = controller; |
613 |
| - log.info("Subscribe to controller:", controller, "connection id:", connectionId, " with info:", info.options.media.tracks); |
614 | 584 |
|
615 |
| - return rpcReq.subscribe(controller, 'admin', connectionId, info.options); |
616 |
| - }) |
617 |
| - .then(function(result) { |
618 |
| - log.info("subscribe result is:", result); |
| 585 | + quicStream.onStreamData((msg) => { |
| 586 | + var info = JSON.parse(msg); |
| 587 | + log.info("Server get stream data:", info, " in stream:", streamId, " and session:", sessionId); |
| 588 | + if (info.type === 'cluster') { |
619 | 589 |
|
| 590 | + rpcReq.getController(clusterName, info.room) |
| 591 | + .then(function(controller) { |
| 592 | + dest = info.cluster + '-' + info.room; |
| 593 | + session.dest = dest; |
| 594 | + if (!clusters[dest]) { |
| 595 | + clusters[dest] = {}; |
| 596 | + } |
| 597 | + clusters[dest].controller = controller; |
| 598 | + return rpcReq.getToken(controller); |
620 | 599 | })
|
621 |
| - .catch((e) => { |
622 |
| - log.info("subscribe failed with error:", e); |
| 600 | + .then(function(token) { |
| 601 | + if (info.token !== token) { |
| 602 | + //Quic client token validation failed |
| 603 | + delete clusters[dest]; |
| 604 | + session.close(); |
| 605 | + } else { |
| 606 | + clusters[dest].quicsession = session; |
| 607 | + clusters[dest].signalStream = quicStream; |
| 608 | + } |
623 | 609 | });
|
| 610 | + } else if (info.type === 'track') { |
| 611 | + log.info("Server stream id:", streamId, " get track msg and track info:", info, " in stream:", streamId, " and session:", sessionId); |
| 612 | + var conn = createStreamPipeline(info.id, 'out', info.options); |
| 613 | + conn.quicStream(quicStream); |
| 614 | + if (!conn) { |
| 615 | + return; |
| 616 | + } |
| 617 | + quicStream.trackKind = info.kind; |
| 618 | + var connid = 'quic-' + info.id; |
| 619 | + router.addLocalDestination(info.id, 'mediabridge', conn); |
| 620 | + |
| 621 | + } else if (info.type === 'subscribe') { |
| 622 | + log.info("Server stream id:", streamId, " get subscribe msg with subscribe info:", info, " and session:", sessionId); |
| 623 | + info.options.locality = {agent: parentRpcId, node: selfRpcId}; |
| 624 | + var connectionId = info.options.connectionId; |
| 625 | + var str = connectionId.split('-'); |
| 626 | + connectionids[str[2]] = connectionId; |
| 627 | + log.info("get connection id:", connectionId, " split str", str[2], " connectionids:", connectionids); |
| 628 | + if (clusters[dest].controller) { |
| 629 | + rpcReq.subscribe(clusters[dest].controller, 'admin', connectionId, info.options); |
| 630 | + } else { |
| 631 | + rpcReq.getController(clusterName, info.options.room) |
| 632 | + .then(function(controller) { |
| 633 | + clusters[dest].controller = controller; |
| 634 | + log.info("Subscribe to controller:", controller, "connection id:", connectionId, " with info:", info.options.media.tracks); |
| 635 | + |
| 636 | + return rpcReq.subscribe(controller, 'admin', connectionId, info.options); |
| 637 | + }) |
| 638 | + .then(function(result) { |
| 639 | + log.info("subscribe result is:", result); |
| 640 | + |
| 641 | + }) |
| 642 | + .catch((e) => { |
| 643 | + log.info("subscribe failed with error:", e); |
| 644 | + }); |
| 645 | + } |
| 646 | + } else if (info.type === 'unsubscribe') { |
| 647 | + //handle unsusbcribe request |
| 648 | + } |
| 649 | + }); |
| 650 | + var data = { |
| 651 | + type: 'ready' |
624 | 652 | }
|
625 |
| - } else if (info.type === 'unsubscribe') { |
626 |
| - //handle unsusbcribe request |
627 |
| - } |
| 653 | + quicStream.send(JSON.stringify(data)); |
| 654 | + }) |
| 655 | + |
| 656 | + session.onClosedStream((closedStreamId) => { |
| 657 | + log.info("server stream:", closedStreamId, " is closed"); |
| 658 | + if (clusters[session.dest] && clusters[session.dest].streams[closedStreamId] && clusters[session.dest].streams[closedStreamId].connid) { |
| 659 | + rpcReq.unsubscribe(clusters[session.dest].controller, 'admin', clusters[session.dest].streams[closedStreamId].connid); |
| 660 | + delete clusters[session.dest].streams[closedStreamId] |
| 661 | + } |
| 662 | + }) |
628 | 663 | });
|
629 |
| - var data = { |
630 |
| - type: 'ready' |
631 |
| - } |
632 |
| - quicStream.send(JSON.stringify(data)); |
633 |
| - }) |
634 |
| - |
635 |
| - session.onClosedStream((closedStreamId) => { |
636 |
| - log.info("server stream:", closedStreamId, " is closed"); |
637 |
| - if (clusters[session.dest] && clusters[session.dest].streams[closedStreamId] && clusters[session.dest].streams[closedStreamId].connid) { |
638 |
| - rpcReq.unsubscribe(clusters[session.dest].controller, 'admin', clusters[session.dest].streams[closedStreamId].connid); |
639 |
| - delete clusters[session.dest].streams[closedStreamId] |
640 |
| - } |
641 |
| - }) |
642 |
| - }); |
643 | 664 |
|
644 |
| - server.onClosedSession((sessionId) => { |
645 |
| - log.info("Session:", sessionId, " in server is closed"); |
| 665 | + server.onClosedSession((sessionId) => { |
| 666 | + log.info("Session:", sessionId, " in server is closed"); |
646 | 667 |
|
647 |
| - for (var item in clusters[sessionId].streams) { |
648 |
| - rpcReq.unsubscribe(clusters[sessionId].controller, 'admin', clusters[sessionId].streams[item].connid); |
649 |
| - } |
650 |
| - delete clusters[sessionId]; |
| 668 | + for (var item in clusters[sessionId].streams) { |
| 669 | + rpcReq.unsubscribe(clusters[sessionId].controller, 'admin', clusters[sessionId].streams[item].connid); |
| 670 | + } |
| 671 | + delete clusters[sessionId]; |
651 | 672 |
|
652 |
| - }) |
| 673 | + }) |
| 674 | + }); |
653 | 675 | }
|
654 | 676 |
|
655 | 677 | start();
|
|
0 commit comments