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