]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/assets/player/p2p-media-loader/segment-validator.ts
9add3518459d0a5831584f69fabee2c654424c50
[github/Chocobozzz/PeerTube.git] / client / src / assets / player / p2p-media-loader / segment-validator.ts
1 import { wait } from '@root-helpers/utils'
2 import { Segment } from 'p2p-media-loader-core'
3 import { basename } from 'path'
4
5 type SegmentsJSON = { [filename: string]: string | { [byterange: string]: string } }
6
7 const maxRetries = 3
8
9 function segmentValidatorFactory (segmentsSha256Url: string) {
10 let segmentsJSON = fetchSha256Segments(segmentsSha256Url)
11 const regex = /bytes=(\d+)-(\d+)/
12
13 return async function segmentValidator (segment: Segment, retry = 1) {
14 const filename = basename(segment.url)
15
16 const segmentValue = (await segmentsJSON)[filename]
17
18 if (!segmentValue && retry > maxRetries) {
19 throw new Error(`Unknown segment name ${filename} in segment validator`)
20 }
21
22 if (!segmentValue) {
23 await wait(1000)
24
25 console.log('Refetching sha segments for %s.', filename)
26
27 segmentsJSON = fetchSha256Segments(segmentsSha256Url)
28 await segmentValidator(segment, retry + 1)
29
30 return
31 }
32
33 let hashShouldBe: string
34 let range = ''
35
36 if (typeof segmentValue === 'string') {
37 hashShouldBe = segmentValue
38 } else {
39 const captured = regex.exec(segment.range)
40 range = captured[1] + '-' + captured[2]
41
42 hashShouldBe = segmentValue[range]
43 }
44
45 if (hashShouldBe === undefined) {
46 throw new Error(`Unknown segment name ${filename}/${range} in segment validator`)
47 }
48
49 const calculatedSha = await sha256Hex(segment.data)
50 if (calculatedSha !== hashShouldBe) {
51 throw new Error(
52 `Hashes does not correspond for segment ${filename}/${range}` +
53 `(expected: ${hashShouldBe} instead of ${calculatedSha})`
54 )
55 }
56 }
57 }
58
59 // ---------------------------------------------------------------------------
60
61 export {
62 segmentValidatorFactory
63 }
64
65 // ---------------------------------------------------------------------------
66
67 function fetchSha256Segments (url: string) {
68 return fetch(url)
69 .then(res => res.json() as Promise<SegmentsJSON>)
70 .catch(err => {
71 console.error('Cannot get sha256 segments', err)
72 return {}
73 })
74 }
75
76 async function sha256Hex (data?: ArrayBuffer) {
77 if (!data) return undefined
78
79 if (window.crypto.subtle) {
80 return window.crypto.subtle.digest('SHA-256', data)
81 .then(data => bufferToHex(data))
82 }
83
84 // Fallback for non HTTPS context
85 const shaModule = await import('sha.js')
86 return new shaModule.sha256().update(Buffer.from(data)).digest('hex')
87 }
88
89 // Thanks: https://stackoverflow.com/a/53307879
90 function bufferToHex (buffer?: ArrayBuffer) {
91 if (!buffer) return ''
92
93 let s = ''
94 const h = '0123456789abcdef'
95 const o = new Uint8Array(buffer)
96
97 o.forEach((v: any) => s += h[ v >> 4 ] + h[ v & 15 ])
98
99 return s
100 }