@warren-bank/hls-proxy 3.5.1 → 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>
@@ -132,7 +133,8 @@ options:
132
133
  --cache-storage <adapter>
133
134
  --cache-storage-fs-dirpath <dirpath>
134
135
  -v <number>
135
- --acl-whitelist <ip_address_list>
136
+ --acl-ip <ip_address_list>
137
+ --acl-pass <password_list>
136
138
  --http-proxy <http[s]://[user:pass@]hostname:port>
137
139
  --tls-cert <filepath>
138
140
  --tls-key <filepath>
@@ -166,6 +168,7 @@ options:
166
168
  * when this option is not specified:
167
169
  * HTTP proxy binds to: `80`
168
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
169
172
  * _--req-headers_ is the filepath to a JSON data _Object_ containing key:value pairs
170
173
  * each _key_ is the name of an HTTP header to send in every outbound request
171
174
  * _--origin_ is the value of the corresponding HTTP request header
@@ -363,8 +366,10 @@ options:
363
366
  * show an enhanced technical trace (useful while debugging unexpected behavior)
364
367
  * `4`:
365
368
  * show the content of .m3u8 files (both before and after URLs are modified)
366
- * _--acl-whitelist_ restricts proxy server access to clients at IP addresses in whitelist
369
+ * _--acl-ip_ restricts proxy server access to clients at IP addresses in whitelist
367
370
  * ex: `"192.168.1.100,192.168.1.101,192.168.1.102"`
371
+ * _--acl-pass_ restricts proxy server access to requests that include a `password` querystring parameter having a value in whitelist
372
+ * ex: `"1111,2222,3333,4444,5555"`
368
373
  * --http-proxy enables all outbound HTTP and HTTPS requests from HLS-Proxy to be tunnelled through an additional external web proxy server
369
374
  * SOCKS proxies are not supported
370
375
  * ex: `http://myusername:mypassword@myproxy.example.com:1234`
@@ -0,0 +1,25 @@
1
+ const expressjs = require('./expressjs_utils')
2
+ const {URL} = require('./url')
3
+
4
+ const get_encoded_qs_password = function(req) {
5
+ const req_url = new URL( expressjs.get_full_req_url(req) )
6
+
7
+ return req_url.searchParams.get('password') || ''
8
+ }
9
+
10
+ const is_allowed = function(params, req) {
11
+ const {acl_pass} = params
12
+
13
+ if (acl_pass && Array.isArray(acl_pass) && acl_pass.length) {
14
+ const password = decodeURIComponent( get_encoded_qs_password(req) )
15
+
16
+ return (acl_pass.indexOf(password) >= 0)
17
+ }
18
+
19
+ return true
20
+ }
21
+
22
+ module.exports = {
23
+ get_encoded_qs_password,
24
+ is_allowed
25
+ }
@@ -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"],
@@ -44,7 +45,8 @@ const middleware = require('../proxy')({
44
45
  cache_storage: argv_vals["--cache-storage"],
45
46
  cache_storage_fs_dirpath: argv_vals["--cache-storage-fs-dirpath"],
46
47
  debug_level: argv_vals["-v"],
47
- acl_whitelist: argv_vals["--acl-whitelist"],
48
+ acl_ip: argv_vals["--acl-ip"],
49
+ acl_pass: argv_vals["--acl-pass"],
48
50
  http_proxy: argv_vals["--http-proxy"],
49
51
  manifest_extension: argv_vals["--manifest-extension"],
50
52
  segment_extension: argv_vals["--segment-extension"]
@@ -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>
@@ -27,7 +28,8 @@ options:
27
28
  --cache-storage <adapter>
28
29
  --cache-storage-fs-dirpath <dirpath>
29
30
  -v <number>
30
- --acl-whitelist <ip_address_list>
31
+ --acl-ip <ip_address_list>
32
+ --acl-pass <password_list>
31
33
  --http-proxy <http[s]://[user:pass@]hostname:port>
32
34
  --tls-cert <filepath>
33
35
  --tls-key <filepath>
@@ -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": {},
@@ -33,7 +36,8 @@ const argv_flags = {
33
36
  "--cache-storage-fs-dirpath": {file: "path-exists"},
34
37
 
35
38
  "-v": {num: "int"},
36
- "--acl-whitelist": {},
39
+ "--acl-ip": {},
40
+ "--acl-pass": {},
37
41
  "--http-proxy": {},
38
42
 
39
43
  "--tls-cert": {file: "path-exists"},
@@ -46,6 +50,7 @@ const argv_flags = {
46
50
 
47
51
  const argv_flag_aliases = {
48
52
  "--help": ["-h"],
53
+ "--acl-ip": ["--acl-whitelist"],
49
54
  "--http-proxy": ["--https-proxy", "--proxy"]
50
55
  }
51
56
 
@@ -72,7 +77,7 @@ if (argv_vals["--version"]) {
72
77
  }
73
78
 
74
79
  if (argv_vals["--origin"] || argv_vals["--referer"] || argv_vals["--useragent"] || (Array.isArray(argv_vals["--header"]) && argv_vals["--header"].length)) {
75
- argv_vals["--req-headers"] = argv_vals["--req-headers"] || {}
80
+ argv_vals["--req-headers"] = normalize_req_headers( argv_vals["--req-headers"] || {} )
76
81
 
77
82
  if (argv_vals["--origin"]) {
78
83
  argv_vals["--req-headers"]["origin"] = argv_vals["--origin"]
@@ -149,13 +154,8 @@ if (argv_vals["--req-secure-honor-server-cipher-order"] || argv_vals["--req-secu
149
154
  }
150
155
  }
151
156
 
152
- if (argv_vals["--req-options"] && argv_vals["--req-options"]["headers"]) {
153
- const lc_headers = {}
154
- for (let key in argv_vals["--req-options"]["headers"]) {
155
- lc_headers[ key.toLowerCase() ] = argv_vals["--req-options"]["headers"][key]
156
- }
157
- argv_vals["--req-options"]["headers"] = lc_headers
158
- }
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"] )
159
159
 
160
160
  if (typeof argv_vals["--max-segments"] !== 'number')
161
161
  argv_vals["--max-segments"] = 20
@@ -169,6 +169,14 @@ if (typeof argv_vals["--cache-key"] !== 'number')
169
169
  if (typeof argv_vals["-v"] !== 'number')
170
170
  argv_vals["-v"] = 0
171
171
 
172
+ if (argv_vals["--acl-ip"]) {
173
+ argv_vals["--acl-ip"] = argv_vals["--acl-ip"].trim().toLowerCase().split(/\s*,\s*/g)
174
+ }
175
+
176
+ if (argv_vals["--acl-pass"]) {
177
+ argv_vals["--acl-pass"] = argv_vals["--acl-pass"].trim().split(/\s*,\s*/g)
178
+ }
179
+
172
180
  if (argv_vals["--http-proxy"]) {
173
181
  const proxy_options = {
174
182
  keepAlive: true,
@@ -112,7 +112,7 @@ const parse_HHMMSS_to_seconds = function(str) {
112
112
  // prefetch_urls: [],
113
113
  // modified_m3u8: ''
114
114
  // }
115
- const parse_manifest = function(m3u8_content, m3u8_url, referer_url, hooks, cache_segments, debug, vod_start_at_ms, redirected_base_url, should_prefetch_url, manifest_extension, segment_extension) {
115
+ const parse_manifest = function(m3u8_content, m3u8_url, referer_url, hooks, cache_segments, debug, vod_start_at_ms, redirected_base_url, should_prefetch_url, manifest_extension, segment_extension, qs_password) {
116
116
  const m3u8_lines = m3u8_content.split(regexs.m3u8_line_separator)
117
117
  m3u8_content = null
118
118
 
@@ -125,7 +125,7 @@ const parse_manifest = function(m3u8_content, m3u8_url, referer_url, hooks, cach
125
125
  redirect_embedded_url(embedded_url, hooks, m3u8_url, debug)
126
126
  if (validate_embedded_url(embedded_url)) {
127
127
  finalize_embedded_url(embedded_url, vod_start_at_ms, debug)
128
- encode_embedded_url(embedded_url, hooks, redirected_base_url, debug, manifest_extension, segment_extension)
128
+ encode_embedded_url(embedded_url, hooks, redirected_base_url, debug, manifest_extension, segment_extension, qs_password)
129
129
  get_prefetch_url(embedded_url, should_prefetch_url, prefetch_urls)
130
130
  modify_m3u8_line(embedded_url, m3u8_lines)
131
131
  }
@@ -332,7 +332,7 @@ const finalize_embedded_url = function(embedded_url, vod_start_at_ms, debug) {
332
332
  }
333
333
  }
334
334
 
335
- const encode_embedded_url = function(embedded_url, hooks, redirected_base_url, debug, manifest_extension, segment_extension) {
335
+ const encode_embedded_url = function(embedded_url, hooks, redirected_base_url, debug, manifest_extension, segment_extension, qs_password) {
336
336
  if (embedded_url.unencoded_url) {
337
337
  let file_extension = embedded_url.url_type
338
338
  if (file_extension) {
@@ -344,6 +344,9 @@ const encode_embedded_url = function(embedded_url, hooks, redirected_base_url, d
344
344
 
345
345
  embedded_url.encoded_url = `${redirected_base_url}/${ utils.base64_encode(embedded_url.unencoded_url) }.${file_extension || 'other'}`
346
346
 
347
+ if (qs_password)
348
+ embedded_url.encoded_url += `?password=${qs_password}`
349
+
347
350
  debug(3, 'redirecting (proxied):', embedded_url.encoded_url)
348
351
 
349
352
  if (hooks && (hooks instanceof Object) && hooks.redirect_final && (typeof hooks.redirect_final === 'function')) {
@@ -379,7 +382,7 @@ const modify_m3u8_line = function(embedded_url, m3u8_lines) {
379
382
  }
380
383
  }
381
384
 
382
- const modify_m3u8_content = function(params, segment_cache, m3u8_content, m3u8_url, referer_url, redirected_base_url) {
385
+ const modify_m3u8_content = function(params, segment_cache, m3u8_content, m3u8_url, referer_url, inbound_req_headers, redirected_base_url, qs_password) {
383
386
  const {hooks, cache_segments, max_segments, debug_level, manifest_extension, segment_extension} = params
384
387
 
385
388
  const {has_cache, get_time_since_last_access, is_expired, prefetch_segment} = segment_cache
@@ -421,13 +424,13 @@ const modify_m3u8_content = function(params, segment_cache, m3u8_content, m3u8_u
421
424
  const matching_url = urls[0]
422
425
  urls[0] = undefined
423
426
 
424
- 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)
425
428
  }
426
429
 
427
430
  promise.then(() => {
428
431
  urls.forEach((matching_url, index) => {
429
432
  if (matching_url) {
430
- 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)
431
434
 
432
435
  urls[index] = undefined
433
436
  }
@@ -437,7 +440,7 @@ const modify_m3u8_content = function(params, segment_cache, m3u8_content, m3u8_u
437
440
  : null
438
441
 
439
442
  {
440
- const parsed_manifest = parse_manifest(m3u8_content, m3u8_url, referer_url, hooks, cache_segments, debug, vod_start_at_ms, redirected_base_url, should_prefetch_url, manifest_extension, segment_extension)
443
+ const parsed_manifest = parse_manifest(m3u8_content, m3u8_url, referer_url, hooks, cache_segments, debug, vod_start_at_ms, redirected_base_url, should_prefetch_url, manifest_extension, segment_extension, qs_password)
441
444
  is_vod = !!parsed_manifest.meta_data.is_vod // default: false => hls live stream
442
445
  seg_duration_ms = parsed_manifest.meta_data.seg_duration_ms || 10000 // default: 10 seconds in ms
443
446
  prefetch_urls = parsed_manifest.prefetch_urls
@@ -1,16 +1,18 @@
1
- const request = require('@warren-bank/node-request').request
2
- const cookies = require('./cookies')
3
- const parser = require('./manifest_parser')
4
- const timers = require('./timers')
5
- const utils = require('./utils')
1
+ const request = require('@warren-bank/node-request').request
2
+ const acl_pass = require('./acl_pass')
3
+ const cookies = require('./cookies')
4
+ const parser = require('./manifest_parser')
5
+ const timers = require('./timers')
6
+ const utils = require('./utils')
6
7
 
7
8
  const get_middleware = function(params) {
8
9
  const {cache_segments} = params
9
- let {acl_whitelist} = params
10
+ let {acl_ip} = params
10
11
 
11
12
  const segment_cache = require('./segment_cache')(params)
12
13
  const {get_segment, add_listener} = segment_cache
13
14
 
15
+ const is_acl_pass_allowed = acl_pass.is_allowed.bind(null, params)
14
16
  const debug = utils.debug.bind(null, params)
15
17
  const parse_req_url = utils.parse_req_url.bind(null, params)
16
18
  const get_request_options = utils.get_request_options.bind(null, params)
@@ -19,16 +21,14 @@ const get_middleware = function(params) {
19
21
  const middleware = {}
20
22
 
21
23
  // Access Control
22
- if (acl_whitelist) {
23
- acl_whitelist = acl_whitelist.trim().toLowerCase().split(/\s*,\s*/g)
24
-
24
+ if (acl_ip && Array.isArray(acl_ip) && acl_ip.length) {
25
25
  middleware.connection = (socket) => {
26
26
  if (socket && socket.remoteAddress) {
27
- let remoteIP = socket.remoteAddress.toLowerCase().replace(/^::?ffff:/, '')
27
+ const remote_ip = socket.remoteAddress.toLowerCase().replace(/^::?ffff:/, '')
28
28
 
29
- if (acl_whitelist.indexOf(remoteIP) === -1) {
29
+ if (acl_ip.indexOf(remote_ip) === -1) {
30
30
  socket.destroy()
31
- debug(2, socket.remoteFamily, 'connection blocked by ACL whitelist:', remoteIP)
31
+ debug(2, socket.remoteFamily, 'connection blocked by ACL IP whitelist:', remote_ip)
32
32
  }
33
33
  }
34
34
  }
@@ -36,6 +36,13 @@ const get_middleware = function(params) {
36
36
 
37
37
  // Create an HTTP tunneling proxy
38
38
  middleware.request = async (req, res) => {
39
+ if (!is_acl_pass_allowed(req)) {
40
+ res.writeHead(401)
41
+ res.end()
42
+ debug(2, 'request blocked by ACL password whitelist:', req.url)
43
+ return
44
+ }
45
+
39
46
  debug(3, 'proxying (raw):', req.url)
40
47
 
41
48
  utils.add_CORS_headers(res)
@@ -48,7 +55,8 @@ const get_middleware = function(params) {
48
55
  return
49
56
  }
50
57
 
51
- const is_m3u8 = (url_type === 'm3u8')
58
+ const qs_password = acl_pass.get_encoded_qs_password(req)
59
+ const is_m3u8 = (url_type === 'm3u8')
52
60
 
53
61
  const send_cache_segment = function(segment, type) {
54
62
  if (!type)
@@ -71,7 +79,7 @@ const get_middleware = function(params) {
71
79
  }
72
80
  }
73
81
 
74
- const options = get_request_options(url, is_m3u8, referer_url)
82
+ const options = get_request_options(url, is_m3u8, referer_url, req.headers)
75
83
  debug(1, 'proxying:', url)
76
84
  debug(3, 'm3u8:', (is_m3u8 ? 'true' : 'false'))
77
85
 
@@ -99,7 +107,7 @@ const get_middleware = function(params) {
99
107
  : url
100
108
 
101
109
  res.writeHead(200, { "content-type": "application/x-mpegURL" })
102
- res.end( modify_m3u8_content(response.toString().trim(), m3u8_url, referer_url, redirected_base_url) )
110
+ res.end( modify_m3u8_content(response.toString().trim(), m3u8_url, referer_url, req.headers, redirected_base_url, qs_password) )
103
111
  }
104
112
  })
105
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.1",
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"