From 6d61da4e32487bdb1f1a84a7b2eddf2df0ddf1ca Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 10 Nov 2020 10:36:50 +0100 Subject: HLS player fallback for non https context --- client/package.json | 2 ++ .../assets/player/p2p-media-loader/segment-validator.ts | 15 +++++++++++---- client/yarn.lock | 7 +++++++ 3 files changed, 20 insertions(+), 4 deletions(-) (limited to 'client') diff --git a/client/package.json b/client/package.json index 152f8445f..5147ca86c 100644 --- a/client/package.json +++ b/client/package.json @@ -63,6 +63,7 @@ "@types/markdown-it": "^10.0.1", "@types/node": "^14.0.14", "@types/sanitize-html": "1.23.3", + "@types/sha.js": "^2.4.0", "@types/socket.io-client": "^1.4.32", "@types/video.js": "^7.3.8", "@types/webtorrent": "^0.107.0", @@ -110,6 +111,7 @@ "sass-lint": "^1.13.1", "sass-loader": "8.0.2", "sass-resources-loader": "^2.0.0", + "sha.js": "^2.4.11", "socket.io-client": "^2.2.0", "stream-browserify": "^3.0.0", "stream-http": "^3.0.0", diff --git a/client/src/assets/player/p2p-media-loader/segment-validator.ts b/client/src/assets/player/p2p-media-loader/segment-validator.ts index 0614f73d2..fa6e6df1d 100644 --- a/client/src/assets/player/p2p-media-loader/segment-validator.ts +++ b/client/src/assets/player/p2p-media-loader/segment-validator.ts @@ -41,7 +41,7 @@ function segmentValidatorFactory (segmentsSha256Url: string) { throw new Error(`Unknown segment name ${filename}/${range} in segment validator`) } - const calculatedSha = bufferToEx(await sha256(segment.data)) + const calculatedSha = await sha256Hex(segment.data) if (calculatedSha !== hashShouldBe) { throw new Error( `Hashes does not correspond for segment ${filename}/${range}` + @@ -68,14 +68,21 @@ function fetchSha256Segments (url: string) { }) } -function sha256 (data?: ArrayBuffer) { +async function sha256Hex (data?: ArrayBuffer) { if (!data) return undefined - return window.crypto.subtle.digest('SHA-256', data) + if (window.crypto.subtle) { + return window.crypto.subtle.digest('SHA-256', data) + .then(data => bufferToHex(data)) + } + + // Fallback for non HTTPS context + const shaModule = await import('sha.js') + return new shaModule.sha256().update(Buffer.from(data)).digest('hex') } // Thanks: https://stackoverflow.com/a/53307879 -function bufferToEx (buffer?: ArrayBuffer) { +function bufferToHex (buffer?: ArrayBuffer) { if (!buffer) return '' let s = '' diff --git a/client/yarn.lock b/client/yarn.lock index d3603a4a9..0efcabe8c 100644 --- a/client/yarn.lock +++ b/client/yarn.lock @@ -1495,6 +1495,13 @@ resolved "https://registry.yarnpkg.com/@types/selenium-webdriver/-/selenium-webdriver-3.0.17.tgz#50bea0c3c2acc31c959c5b1e747798b3b3d06d4b" integrity sha512-tGomyEuzSC1H28y2zlW6XPCaDaXFaD6soTdb4GNdmte2qfHtrKqhy0ZFs4r/1hpazCfEZqeTSRLvSasmEx89uw== +"@types/sha.js@^2.4.0": + version "2.4.0" + resolved "https://registry.yarnpkg.com/@types/sha.js/-/sha.js-2.4.0.tgz#bce682ef860b40f419d024fa08600c3b8d24bb01" + integrity sha512-amxKgPy6WJTKuw8mpUwjX2BSxuBtBmZfRwIUDIuPJKNwGN8CWDli8JTg5ONTWOtcTkHIstvT7oAhhYXqEjStHQ== + dependencies: + "@types/node" "*" + "@types/simple-peer@*": version "9.6.0" resolved "https://registry.yarnpkg.com/@types/simple-peer/-/simple-peer-9.6.0.tgz#b5828d835b7f42dde27db584ba127e7a9f9072f4" -- cgit v1.2.3