From 5cf2ff1b11a37af0c853c1b88a62053d8b5f17a6 Mon Sep 17 00:00:00 2001 From: Joao Mario Lago Date: Fri, 26 Apr 2024 11:31:22 -0300 Subject: [PATCH] Add incoming session filter based on protocols * Add one more filter based on protocols in incoming sessions --- src/composables/webRTC.ts | 31 +++++++++++++++++++++++++++++-- src/libs/webrtc/session.ts | 21 +++++++++++++++++++-- src/stores/video.ts | 2 +- 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/composables/webRTC.ts b/src/composables/webRTC.ts index 040366524..fdb5bc781 100644 --- a/src/composables/webRTC.ts +++ b/src/composables/webRTC.ts @@ -44,6 +44,7 @@ export class WebRTCManager { private session: Session | undefined private rtcConfiguration: RTCConfiguration private selectedICEIPs: string[] = [] + private selectedICEProtocols: string[] = [] private hasEnded = false private signaller: Signaller @@ -81,11 +82,17 @@ export class WebRTCManager { /** * * @param { Ref } selectedStream - Stream to receive stream from - * @param { Ref } selectedICEIPs + * @param { Ref } selectedICEIPs - ICE IPs allowed to be used in the connection + * @param { Ref } selectedICEProtocols - ICE protocols allowed to be used in the connection * @returns { startStreamReturn } */ - public startStream(selectedStream: Ref, selectedICEIPs: Ref): startStreamReturn { + public startStream( + selectedStream: Ref, + selectedICEIPs: Ref, + selectedICEProtocols: Ref + ): startStreamReturn { this.selectedICEIPs = selectedICEIPs.value + this.selectedICEProtocols = selectedICEProtocols.value watch(selectedStream, (newStream, oldStream) => { if (newStream?.id === oldStream?.id) { @@ -122,6 +129,25 @@ export class WebRTCManager { } }) + watch(selectedICEProtocols, (newProtocols, oldProtocols) => { + if (newProtocols === oldProtocols) { + return + } + + const msg = `Selected Protocols changed from "${oldProtocols}" to "${newProtocols}".` + console.debug('[WebRTC] ' + msg) + + this.selectedICEProtocols = newProtocols + + if (this.streamName !== undefined) { + this.stopSession(msg) + } + + if (this.streamName !== undefined) { + this.startSession() + } + }) + return { mediaStream: this.mediaStream, connected: this.connected, @@ -321,6 +347,7 @@ export class WebRTCManager { this.signaller, this.rtcConfiguration, this.selectedICEIPs, + this.selectedICEProtocols, (event: RTCTrackEvent): void => this.onTrackAdded(event), (): void => this.onPeerConnected(), (availableICEIPs: string[]) => (this.availableICEIPs.value = availableICEIPs), diff --git a/src/libs/webrtc/session.ts b/src/libs/webrtc/session.ts index f81f0bd30..b5b530af7 100644 --- a/src/libs/webrtc/session.ts +++ b/src/libs/webrtc/session.ts @@ -21,6 +21,7 @@ export class Session { private peerConnection: RTCPeerConnection private availableICEIPs: string[] private selectedICEIPs: string[] + private selectedICEProtocols: string[] public rtcConfiguration: RTCConfiguration public onTrackAdded?: OnTrackAddedCallback public onPeerConnected?: OnPeerConnectedCallback @@ -36,6 +37,7 @@ export class Session { * @param {Signaller} signaller - The Signaller instance for this Session to use * @param {RTCConfiguration} rtcConfiguration - Configuration for the RTC connection, such as Turn and Stun servers * @param {string[]} selectedICEIPs - A whitelist for ICE IP addresses, ignored if empty + * @param {string[]} selectedICEProtocols - A whitelist for protocols allowed, ignored if empty * @param {OnTrackAddedCallback} onTrackAdded - An optional callback for when a track is added to this session * @param {OnPeerConnectedCallback} onPeerConnected - An optional callback for when the peer is connected * @param {onNewIceRemoteAddressCallback} onNewIceRemoteAddress - An optional callback for when a new ICE candidate IP addres is available @@ -49,6 +51,7 @@ export class Session { signaller: Signaller, rtcConfiguration: RTCConfiguration, selectedICEIPs: string[] = [], + selectedICEProtocols: string[] = [], onTrackAdded?: OnTrackAddedCallback, onPeerConnected?: OnPeerConnectedCallback, onNewIceRemoteAddress?: onNewIceRemoteAddressCallback, @@ -69,6 +72,7 @@ export class Session { this.ended = false this.availableICEIPs = [] this.selectedICEIPs = selectedICEIPs + this.selectedICEProtocols = selectedICEProtocols this.peerConnection = this.createRTCPeerConnection(rtcConfiguration) @@ -205,8 +209,21 @@ export class Session { !this.selectedICEIPs.isEmpty() && !this.selectedICEIPs.some((address) => candidate.candidate!.includes(address)) ) { - this.onStatusChange?.(`Ignoring ICE candidate ${candidate.candidate}`) - console.debug(`[WebRTC] [Session] ICE candidate ignored: ${JSON.stringify(candidate, null, 4)}`) + this.onStatusChange?.(`Ignoring ICE candidate ${candidate.candidate} by IP filter`) + console.debug(`[WebRTC] [Session] ICE candidate ignored by IP filter: ${JSON.stringify(candidate, null, 4)}`) + return + } + + if ( + candidate.candidate && + Array.isArray(this.selectedICEIPs) && + !this.selectedICEProtocols.isEmpty() && + !this.selectedICEProtocols.some((protocol) => candidate.candidate!.toLowerCase().includes(protocol)) + ) { + this.onStatusChange?.(`Ignoring ICE candidate ${candidate.candidate} by Protocol filter`) + console.debug( + `[WebRTC] [Session] ICE candidate ignored by Protocol filter: ${JSON.stringify(candidate, null, 4)}` + ) return } diff --git a/src/stores/video.ts b/src/stores/video.ts index e835af9cc..8c8dd243b 100644 --- a/src/stores/video.ts +++ b/src/stores/video.ts @@ -77,7 +77,7 @@ export const useVideoStore = defineStore('video', () => { const activateStream = (streamName: string): void => { const stream = ref() const webRtcManager = new WebRTCManager(webRTCSignallingURI.val, rtcConfiguration) - const { mediaStream, connected } = webRtcManager.startStream(stream, allowedIceIps) + const { mediaStream, connected } = webRtcManager.startStream(stream, allowedIceIps, allowedIceProtocols) activeStreams.value[streamName] = { // @ts-ignore: This is actually not reactive stream: stream,