Skip to content

Commit

Permalink
fix: ignore unable to parse frames completely (#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
td-famedly authored Jun 12, 2024
1 parent bcbe636 commit 8c74825
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
16 changes: 4 additions & 12 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.3"
cryptography:
dependency: "direct main"
description:
name: cryptography
sha256: d146b76d33d94548cf035233fbc2f4338c1242fa119013bead807d033fc4ae05
url: "https://pub.dev"
source: hosted
version: "2.7.0"
dart_style:
dependency: transitive
description:
Expand All @@ -149,10 +141,10 @@ packages:
dependency: "direct main"
description:
name: dart_webrtc
sha256: fe4db21dc389b99e04cb7bf43bc927dba2e42768d4c28211b66a4b5a16e4d516
sha256: d663abfe6ed95afadcb64083e364aa16dbeaadead193bb03047cf6913e57b65e
url: "https://pub.dev"
source: hosted
version: "1.4.5"
version: "1.4.6+hotfix.1"
dbus:
dependency: transitive
description:
Expand Down Expand Up @@ -236,10 +228,10 @@ packages:
dependency: "direct main"
description:
name: flutter_webrtc
sha256: "1c61bc08d14be57ac28e9e540c44b8b1b9ab1b25bbdb66a8c658e61a3211cc5d"
sha256: fd5f115a08dcdc00b988bea3003c956f1b60a78a61d899cbddfb44f5d0e44d4a
url: "https://pub.dev"
source: hosted
version: "0.10.7"
version: "0.10.8"
glob:
dependency: transitive
description:
Expand Down
43 changes: 31 additions & 12 deletions web/e2ee.cryptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,11 @@ class FrameCryptor {
}));
try {
readable.pipeThrough(transformer).pipeTo(writable);
} catch (e) {
logger.warning('e ${e.toString()}');
} catch (e, s) {
logger.warning('kInternalError: e ${e.toString()} s ${s.toString()}');
if (lastError != CryptorError.kInternalError) {
logger.info(
'cryptorState changed from $lastError to kInternalError because ${e.toString()}, ${s.toString()}');
lastError = CryptorError.kInternalError;
postMessage({
'type': 'cryptorState',
Expand Down Expand Up @@ -327,6 +329,7 @@ class FrameCryptor {

if (secretKey == null) {
if (lastError != CryptorError.kMissingKey) {
logger.info('cryptorState changed from $lastError to kMissingKey');
lastError = CryptorError.kMissingKey;
postMessage({
'type': 'cryptorState',
Expand Down Expand Up @@ -377,6 +380,7 @@ class FrameCryptor {
controller.enqueue(frame);

if (lastError != CryptorError.kOk) {
logger.info('cryptorState changed from $lastError to kOk');
lastError = CryptorError.kOk;
postMessage({
'type': 'cryptorState',
Expand All @@ -391,9 +395,11 @@ class FrameCryptor {

logger.finer(
'encrypto kind $kind,codec $codec headerLength: $headerLength, timestamp: ${frame.timestamp}, ssrc: ${metaData.synchronizationSource}, data length: ${buffer.length}, encrypted length: ${finalBuffer.toBytes().length}, iv $iv');
} catch (e) {
logger.warning('encrypt: e ${e.toString()}');
} catch (e, s) {
logger.warning('kEncryptError: e ${e.toString()}, s: ${s.toString()}');
if (lastError != CryptorError.kEncryptError) {
logger.info(
'cryptorState changed from $lastError to kEncryptError because ${e.toString()}, ${s.toString()}');
lastError = CryptorError.kEncryptError;
postMessage({
'type': 'cryptorState',
Expand Down Expand Up @@ -460,14 +466,21 @@ class FrameCryptor {
var headerLength =
kind == 'video' ? getUnencryptedBytes(frame, codec) : 1;
var metaData = frame.getMetadata();
Uint8List frameTrailer, iv;
int ivLength, keyIndex;
try {
frameTrailer = buffer.sublist(buffer.length - 2);
ivLength = frameTrailer[0];
keyIndex = frameTrailer[1];
iv = buffer.sublist(buffer.length - ivLength - 2, buffer.length - 2);

var frameTrailer = buffer.sublist(buffer.length - 2);
var ivLength = frameTrailer[0];
var keyIndex = frameTrailer[1];
var iv = buffer.sublist(buffer.length - ivLength - 2, buffer.length - 2);

initialKeySet = keyHandler.getKeySet(keyIndex);
initialKeyIndex = keyIndex;
initialKeySet = keyHandler.getKeySet(keyIndex);
initialKeyIndex = keyIndex;
} catch (e) {
logger.finest(
'getting frameTrailer or iv failed, ignoring frame completely');
return;
}

/// missingKey flow:
/// tries to decrypt once, fails, tries to ratchet once and decrypt again,
Expand All @@ -477,6 +490,7 @@ class FrameCryptor {
/// to throw missingkeys faster lower your failureTolerance
if (initialKeySet == null || !keyHandler.hasValidKey) {
if (lastError != CryptorError.kMissingKey) {
logger.info('cryptorState changed from $lastError to kMissingKey');
lastError = CryptorError.kMissingKey;
postMessage({
'type': 'cryptorState',
Expand Down Expand Up @@ -526,6 +540,7 @@ class FrameCryptor {
logger.finer(
'ratchetKey: lastError != CryptorError.kKeyRatcheted, reset state to kKeyRatcheted');

logger.info('cryptorState changed from $lastError to kKeyRatcheted');
lastError = CryptorError.kKeyRatcheted;
postMessage({
'type': 'cryptorState',
Expand Down Expand Up @@ -584,6 +599,7 @@ class FrameCryptor {
controller.enqueue(frame);

if (lastError != CryptorError.kOk) {
logger.info('cryptorState changed from $lastError to kOk');
lastError = CryptorError.kOk;
postMessage({
'type': 'cryptorState',
Expand All @@ -598,8 +614,11 @@ class FrameCryptor {

logger.finer(
'decrypto kind $kind,codec $codec headerLength: $headerLength, timestamp: ${frame.timestamp}, ssrc: ${metaData.synchronizationSource}, data length: ${buffer.length}, decrypted length: ${finalBuffer.toBytes().length}, keyindex $keyIndex iv $iv');
} catch (e) {
} catch (e, s) {
logger.warning('kDecryptError ${e.toString()}, s: ${s.toString()}');
if (lastError != CryptorError.kDecryptError) {
logger.info(
'cryptorState changed from $lastError to kDecryptError ${e.toString()}, ${s.toString()}');
lastError = CryptorError.kDecryptError;
postMessage({
'type': 'cryptorState',
Expand Down

0 comments on commit 8c74825

Please sign in to comment.