@warren-bank/hls-proxy 3.5.2 → 3.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -113,6 +113,7 @@ options:
113
113
  --tls
114
114
  --host <host>
115
115
  --port <number>
116
+ --copy-req-headers
116
117
  --req-headers <filepath>
117
118
  --origin <header>
118
119
  --referer <header>
@@ -167,6 +168,7 @@ options:
167
168
  * when this option is not specified:
168
169
  * HTTP proxy binds to: `80`
169
170
  * HTTPS proxy binds to: `443`
171
+ * _--copy-req-headers_ is a flag to enable the duplication of all HTTP request headers sent to the proxy &rarr; to the request made by the proxy to the video server
170
172
  * _--req-headers_ is the filepath to a JSON data _Object_ containing key:value pairs
171
173
  * each _key_ is the name of an HTTP header to send in every outbound request
172
174
  * _--origin_ is the value of the corresponding HTTP request header
@@ -34,6 +34,7 @@ const server = (use_tls)
34
34
  const middleware = require('../proxy')({
35
35
  is_secure: use_tls,
36
36
  host: normalize_host(argv_vals["--host"], argv_vals["--port"]),
37
+ copy_req_headers: argv_vals["--copy-req-headers"],
37
38
  req_headers: argv_vals["--req-headers"],
38
39
  req_options: argv_vals["--req-options"],
39
40
  hooks: argv_vals["--hooks"],
@@ -8,6 +8,7 @@ options:
8
8
  --tls
9
9
  --host <host>
10
10
  --port <number>
11
+ --copy-req-headers
11
12
  --req-headers <filepath>
12
13
  --origin <header>
13
14
  --referer <header>
@@ -2,6 +2,8 @@ const process_argv = require('@warren-bank/node-process-argv')
2
2
 
3
3
  const {HttpProxyAgent, HttpsProxyAgent} = require('hpagent')
4
4
 
5
+ const {normalize_req_headers} = require('../../utils')
6
+
5
7
  const argv_flags = {
6
8
  "--help": {bool: true},
7
9
  "--version": {bool: true},
@@ -10,6 +12,7 @@ const argv_flags = {
10
12
  "--host": {},
11
13
  "--port": {num: "int"},
12
14
 
15
+ "--copy-req-headers": {bool: true},
13
16
  "--req-headers": {file: "json"},
14
17
  "--origin": {},
15
18
  "--referer": {},
@@ -74,7 +77,7 @@ if (argv_vals["--version"]) {
74
77
  }
75
78
 
76
79
  if (argv_vals["--origin"] || argv_vals["--referer"] || argv_vals["--useragent"] || (Array.isArray(argv_vals["--header"]) && argv_vals["--header"].length)) {
77
- argv_vals["--req-headers"] = argv_vals["--req-headers"] || {}
80
+ argv_vals["--req-headers"] = normalize_req_headers( argv_vals["--req-headers"] || {} )
78
81
 
79
82
  if (argv_vals["--origin"]) {
80
83
  argv_vals["--req-headers"]["origin"] = argv_vals["--origin"]
@@ -151,13 +154,8 @@ if (argv_vals["--req-secure-honor-server-cipher-order"] || argv_vals["--req-secu
151
154
  }
152
155
  }
153
156
 
154
- if (argv_vals["--req-options"] && argv_vals["--req-options"]["headers"]) {
155
- const lc_headers = {}
156
- for (let key in argv_vals["--req-options"]["headers"]) {
157
- lc_headers[ key.toLowerCase() ] = argv_vals["--req-options"]["headers"][key]
158
- }
159
- argv_vals["--req-options"]["headers"] = lc_headers
160
- }
157
+ if (argv_vals["--req-options"] && argv_vals["--req-options"]["headers"])
158
+ argv_vals["--req-options"]["headers"] = normalize_req_headers( argv_vals["--req-options"]["headers"] )
161
159
 
162
160
  if (typeof argv_vals["--max-segments"] !== 'number')
163
161
  argv_vals["--max-segments"] = 20
@@ -382,7 +382,7 @@ const modify_m3u8_line = function(embedded_url, m3u8_lines) {
382
382
  }
383
383
  }
384
384
 
385
- const modify_m3u8_content = function(params, segment_cache, m3u8_content, m3u8_url, referer_url, redirected_base_url, qs_password) {
385
+ const modify_m3u8_content = function(params, segment_cache, m3u8_content, m3u8_url, referer_url, inbound_req_headers, redirected_base_url, qs_password) {
386
386
  const {hooks, cache_segments, max_segments, debug_level, manifest_extension, segment_extension} = params
387
387
 
388
388
  const {has_cache, get_time_since_last_access, is_expired, prefetch_segment} = segment_cache
@@ -424,13 +424,13 @@ const modify_m3u8_content = function(params, segment_cache, m3u8_content, m3u8_u
424
424
  const matching_url = urls[0]
425
425
  urls[0] = undefined
426
426
 
427
- promise = prefetch_segment(m3u8_url, matching_url, referer_url, dont_touch_access)
427
+ promise = prefetch_segment(m3u8_url, matching_url, referer_url, inbound_req_headers, dont_touch_access)
428
428
  }
429
429
 
430
430
  promise.then(() => {
431
431
  urls.forEach((matching_url, index) => {
432
432
  if (matching_url) {
433
- prefetch_segment(m3u8_url, matching_url, referer_url, dont_touch_access)
433
+ prefetch_segment(m3u8_url, matching_url, referer_url, inbound_req_headers, dont_touch_access)
434
434
 
435
435
  urls[index] = undefined
436
436
  }
@@ -79,7 +79,7 @@ const get_middleware = function(params) {
79
79
  }
80
80
  }
81
81
 
82
- const options = get_request_options(url, is_m3u8, referer_url)
82
+ const options = get_request_options(url, is_m3u8, referer_url, req.headers)
83
83
  debug(1, 'proxying:', url)
84
84
  debug(3, 'm3u8:', (is_m3u8 ? 'true' : 'false'))
85
85
 
@@ -107,7 +107,7 @@ const get_middleware = function(params) {
107
107
  : url
108
108
 
109
109
  res.writeHead(200, { "content-type": "application/x-mpegURL" })
110
- res.end( modify_m3u8_content(response.toString().trim(), m3u8_url, referer_url, redirected_base_url, qs_password) )
110
+ res.end( modify_m3u8_content(response.toString().trim(), m3u8_url, referer_url, req.headers, redirected_base_url, qs_password) )
111
111
  }
112
112
  })
113
113
  .catch((e) => {
@@ -161,7 +161,7 @@ module.exports = function(params) {
161
161
  }
162
162
  }
163
163
 
164
- const prefetch_segment = function(m3u8_url, url, referer_url, dont_touch_access) {
164
+ const prefetch_segment = function(m3u8_url, url, referer_url, inbound_req_headers, dont_touch_access) {
165
165
  let promise = Promise.resolve()
166
166
 
167
167
  if (cache[m3u8_url] === undefined) {
@@ -184,7 +184,7 @@ module.exports = function(params) {
184
184
  index = ts.length
185
185
  ts[index] = {key: get_privatekey_from_url(url), has: false, cb: [], type: null, state: {}}
186
186
 
187
- let options = get_request_options(url, /* is_m3u8= */ false, referer_url)
187
+ let options = get_request_options(url, /* is_m3u8= */ false, referer_url, inbound_req_headers)
188
188
  promise = request(options, '', {binary: true, stream: false, cookieJar: cookies.getCookieJar()})
189
189
  .then(({redirects, response}) => {
190
190
  debug(1, `prefetch (complete, ${response.length} bytes):`, debug_url)
@@ -11,7 +11,7 @@ const initialize_timers = function(params) {
11
11
  const get_request_options = utils.get_request_options.bind(null, params)
12
12
 
13
13
  const request_wrapper = function(url, POST_data, user_config) {
14
- const options = get_request_options(url, /* is_m3u8= */ false, /* referer_url= */ null)
14
+ const options = get_request_options(url, /* is_m3u8= */ false, /* referer_url= */ null, /* inbound_req_headers= */ null)
15
15
  const config = Object.assign(
16
16
  {},
17
17
  (user_config || {}),
@@ -120,8 +120,22 @@ const debug = function() {
120
120
  }
121
121
  }
122
122
 
123
- const get_request_options = function(params, url, is_m3u8, referer_url) {
124
- const {req_headers, req_options, hooks, http_proxy} = params
123
+ const normalize_req_headers = function(req_headers) {
124
+ const normalized = {}
125
+
126
+ for (let name in req_headers) {
127
+ normalized[ name.toLowerCase() ] = req_headers[name]
128
+ }
129
+
130
+ return normalized
131
+ }
132
+
133
+ const get_request_options = function(params, url, is_m3u8, referer_url, inbound_req_headers) {
134
+ const {copy_req_headers, req_headers, req_options, hooks, http_proxy} = params
135
+
136
+ const copied_req_headers = (copy_req_headers && inbound_req_headers && (inbound_req_headers instanceof Object))
137
+ ? normalize_req_headers(inbound_req_headers)
138
+ : null
125
139
 
126
140
  const additional_req_options = (hooks && (hooks instanceof Object) && hooks.add_request_options && (typeof hooks.add_request_options === 'function'))
127
141
  ? hooks.add_request_options(url, is_m3u8)
@@ -131,7 +145,7 @@ const get_request_options = function(params, url, is_m3u8, referer_url) {
131
145
  ? hooks.add_request_headers(url, is_m3u8)
132
146
  : null
133
147
 
134
- if (!req_options && !http_proxy && !additional_req_options && !req_headers && !additional_req_headers && !referer_url) return url
148
+ if (!req_options && !http_proxy && !additional_req_options && !copied_req_headers && !req_headers && !additional_req_headers && !referer_url) return url
135
149
 
136
150
  const request_options = Object.assign(
137
151
  {},
@@ -142,6 +156,7 @@ const get_request_options = function(params, url, is_m3u8, referer_url) {
142
156
 
143
157
  request_options.headers = Object.assign(
144
158
  {},
159
+ (copied_req_headers || {}),
145
160
  (( req_options && req_options.headers) ? req_options.headers : {}),
146
161
  ((additional_req_options && additional_req_options.headers) ? additional_req_options.headers : {}),
147
162
  (req_headers || {}),
@@ -186,6 +201,7 @@ module.exports = {
186
201
  get_content_type,
187
202
  add_CORS_headers,
188
203
  debug,
204
+ normalize_req_headers,
189
205
  get_request_options,
190
206
  should_prefetch_url
191
207
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@warren-bank/hls-proxy",
3
3
  "description": "Node.js server to proxy HLS video streams",
4
- "version": "3.5.2",
4
+ "version": "3.5.3",
5
5
  "scripts": {
6
6
  "start": "node hls-proxy/bin/hlsd.js",
7
7
  "sudo": "sudo node hls-proxy/bin/hlsd.js"