diff options
Diffstat (limited to 'client/src/assets/player/shared/p2p-media-loader')
-rw-r--r-- | client/src/assets/player/shared/p2p-media-loader/segment-validator.ts | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/client/src/assets/player/shared/p2p-media-loader/segment-validator.ts b/client/src/assets/player/shared/p2p-media-loader/segment-validator.ts index 18cb6750f..a7ee91950 100644 --- a/client/src/assets/player/shared/p2p-media-loader/segment-validator.ts +++ b/client/src/assets/player/shared/p2p-media-loader/segment-validator.ts | |||
@@ -2,13 +2,22 @@ import { basename } from 'path' | |||
2 | import { Segment } from '@peertube/p2p-media-loader-core' | 2 | import { Segment } from '@peertube/p2p-media-loader-core' |
3 | import { logger } from '@root-helpers/logger' | 3 | import { logger } from '@root-helpers/logger' |
4 | import { wait } from '@root-helpers/utils' | 4 | import { wait } from '@root-helpers/utils' |
5 | import { isSameOrigin } from '../common' | ||
5 | 6 | ||
6 | type SegmentsJSON = { [filename: string]: string | { [byterange: string]: string } } | 7 | type SegmentsJSON = { [filename: string]: string | { [byterange: string]: string } } |
7 | 8 | ||
8 | const maxRetries = 3 | 9 | const maxRetries = 3 |
9 | 10 | ||
10 | function segmentValidatorFactory (segmentsSha256Url: string, isLive: boolean) { | 11 | function segmentValidatorFactory (options: { |
11 | let segmentsJSON = fetchSha256Segments(segmentsSha256Url) | 12 | serverUrl: string |
13 | segmentsSha256Url: string | ||
14 | isLive: boolean | ||
15 | authorizationHeader: () => string | ||
16 | requiresAuth: boolean | ||
17 | }) { | ||
18 | const { serverUrl, segmentsSha256Url, isLive, authorizationHeader, requiresAuth } = options | ||
19 | |||
20 | let segmentsJSON = fetchSha256Segments({ serverUrl, segmentsSha256Url, authorizationHeader, requiresAuth }) | ||
12 | const regex = /bytes=(\d+)-(\d+)/ | 21 | const regex = /bytes=(\d+)-(\d+)/ |
13 | 22 | ||
14 | return async function segmentValidator (segment: Segment, _method: string, _peerId: string, retry = 1) { | 23 | return async function segmentValidator (segment: Segment, _method: string, _peerId: string, retry = 1) { |
@@ -28,7 +37,7 @@ function segmentValidatorFactory (segmentsSha256Url: string, isLive: boolean) { | |||
28 | 37 | ||
29 | await wait(1000) | 38 | await wait(1000) |
30 | 39 | ||
31 | segmentsJSON = fetchSha256Segments(segmentsSha256Url) | 40 | segmentsJSON = fetchSha256Segments({ serverUrl, segmentsSha256Url, authorizationHeader, requiresAuth }) |
32 | await segmentValidator(segment, _method, _peerId, retry + 1) | 41 | await segmentValidator(segment, _method, _peerId, retry + 1) |
33 | 42 | ||
34 | return | 43 | return |
@@ -68,8 +77,19 @@ export { | |||
68 | 77 | ||
69 | // --------------------------------------------------------------------------- | 78 | // --------------------------------------------------------------------------- |
70 | 79 | ||
71 | function fetchSha256Segments (url: string) { | 80 | function fetchSha256Segments (options: { |
72 | return fetch(url) | 81 | serverUrl: string |
82 | segmentsSha256Url: string | ||
83 | authorizationHeader: () => string | ||
84 | requiresAuth: boolean | ||
85 | }) { | ||
86 | const { serverUrl, segmentsSha256Url, requiresAuth, authorizationHeader } = options | ||
87 | |||
88 | const headers = requiresAuth && isSameOrigin(serverUrl, segmentsSha256Url) | ||
89 | ? { Authorization: authorizationHeader() } | ||
90 | : {} | ||
91 | |||
92 | return fetch(segmentsSha256Url, { headers }) | ||
73 | .then(res => res.json() as Promise<SegmentsJSON>) | 93 | .then(res => res.json() as Promise<SegmentsJSON>) |
74 | .catch(err => { | 94 | .catch(err => { |
75 | logger.error('Cannot get sha256 segments', err) | 95 | logger.error('Cannot get sha256 segments', err) |